-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient_test.go
More file actions
129 lines (109 loc) · 2.94 KB
/
client_test.go
File metadata and controls
129 lines (109 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package sshkeymanager
import (
"bytes"
"fmt"
"log"
"net"
"os"
"os/exec"
"testing"
"time"
"github.com/rs-pro/sshkeymanager/passwd"
"github.com/rs-pro/sshkeymanager/testserver"
"github.com/stretchr/testify/assert"
)
const host = "localhost"
const port = "2222"
func ExecCommand(user, command string) string {
cmd := exec.Command("ssh", "-o StrictHostKeyChecking=no", "-itestdata/id_rsa", "-p "+port, user+"@"+host, command)
var buf *bytes.Buffer
cmd.Stdout = buf
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Println("error in exec command", command, err)
}
return buf.String()
}
func TestMain(m *testing.M) {
// call flag.Parse() here if TestMain uses flags
log.Println("test server: starting")
server := testserver.Start()
log.Println("test server: waiting to be up")
WaitForSshServer(host, port)
log.Println("test server: started")
result := m.Run()
log.Println("test server: stopping")
server.Stop()
os.Exit(result)
}
func WaitForSshServer(host, port string) {
attempt := 0
for {
timeout := time.Second
conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, port), timeout)
if err != nil {
fmt.Println("Connecting error:", err)
}
if conn != nil {
conn.Close()
fmt.Println("Opened", net.JoinHostPort(host, port))
// Give it one more second to finish starting up
time.Sleep(1 * time.Second)
return
} else {
log.Println("wait for ssh on", host, port, "attempt number:", attempt)
attempt += 1
if attempt > 30 {
panic("failed to start ssh docker image")
}
time.Sleep(1 * time.Second)
}
}
}
func TestSudo(t *testing.T) {
client, err := NewClient(host, port, "test", MakeConfig([]string{"./testdata/id_rsa"}))
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
assert.Equal(t, client.useSudo, true)
users, err := client.GetUsers()
assert.NoError(t, err)
assert.Len(t, users, 23) // value from a clean Debian bookworm image
}
func GetClient(t *testing.T) *Client {
client, err := NewClient(host, port, "root", MakeConfig([]string{"./testdata/id_rsa"}))
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
return client
}
func TestGetGroups(t *testing.T) {
client := GetClient(t)
users, err := client.GetUsers()
assert.NoError(t, err)
assert.Len(t, users, 23) // value from a clean Debian bookworm image
}
func TestGetUsers(t *testing.T) {
client := GetClient(t)
users, err := client.GetUsers()
assert.NoError(t, err)
assert.Len(t, users, 23) // value from a clean Debian bookworm image
}
func TestAddUser(t *testing.T) {
client := GetClient(t)
u, err := client.AddUser(passwd.User{
Name: "user",
GID: "1000",
Home: "/data/user",
Shell: "/bin/bash",
}, false)
assert.NoError(t, err)
assert.Equal(t, "user", u.Name)
assert.Equal(t, "1000", u.GID)
assert.Equal(t, "/data/user", u.Home)
assert.Equal(t, "/bin/bash", u.Shell)
users, err := client.GetUsers()
assert.NoError(t, err)
assert.Len(t, users, 24) // 23 base + 1 added
}