-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils_test.go
135 lines (113 loc) · 4.23 KB
/
utils_test.go
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
130
131
132
133
134
135
package fpoc
import (
"encoding/json"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestIsCloudInitFinished(t *testing.T) {
testCases := []struct {
name string
file string
readLen int
expected bool
}{
{"token-not-fond-1", "testdata/console_out.txt", 4096, false},
{"finished-1", "testdata/console_out.txt", 102400, true},
{"token-not-fond-2", "testdata/console_ubuntu2204.txt", 4096, false},
{"finished-2", "testdata/console_ubuntu2204.txt", 102400, true},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
buf, err := os.ReadFile(tc.file)
require.NoError(t, err)
var log string
if len(buf) >= tc.readLen {
log = string(buf[0:tc.readLen])
} else {
log = string(buf)
}
obtained := IsCloudInitFinished(log)
assert.Equal(t, tc.expected, obtained)
})
}
}
func TestIsIgnitionFinished(t *testing.T) {
testCases := []struct {
name string
file string
readLen int
expected bool
}{
{"token-not-fond-1", "testdata/console_flatcar.txt", 4096, false},
{"finished-1", "testdata/console_flatcar.txt", 102400, true},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
buf, err := os.ReadFile(tc.file)
require.NoError(t, err)
var log string
if len(buf) >= tc.readLen {
log = string(buf[0:tc.readLen])
} else {
log = string(buf)
}
obtained := IsIgnitionFinished(log)
assert.Equal(t, tc.expected, obtained)
})
}
}
func TestExtCreateOpts(t *testing.T) {
assert := assert.New(t)
cfgJSON := `
{
"name": "gitlab-runner-%d",
"description": "podman instance",
"imageRef": "f2403879-6fbe-49a0-b71f-54b70039f32a",
"flavorRef": "5",
"key_name": "gitlab-autoscaler",
"networks": [{"uuid": "c487d046-80ad-4da0-8b98-4a48ad3c257a"}],
"security_groups": ["allow_gitlab_runner"],
"scheduler_hints": {"group": "a5b557be-b7f0-4cb3-8f7c-6b5092f29c2c"},
"tags": ["podman", "CI"],
"user_data": "#!cloud-config\npackage_update: true\npackage_upgrade: true\n",
"metadata": {"foo": "bar"}
}
`
expected := `{"server":{"description":"podman instance","flavorRef":"5","imageRef":"f2403879-6fbe-49a0-b71f-54b70039f32a","key_name":"gitlab-autoscaler","metadata":{"foo":"bar"},"name":"gitlab-runner-%d","networks":[{"uuid":"c487d046-80ad-4da0-8b98-4a48ad3c257a"}],"security_groups":[{"name":"allow_gitlab_runner"}],"tags":["podman","CI"],"user_data":"IyFjbG91ZC1jb25maWcKcGFja2FnZV91cGRhdGU6IHRydWUKcGFja2FnZV91cGdyYWRlOiB0cnVlCg=="}}`
cfg := new(ExtCreateOpts)
err := json.Unmarshal([]byte(cfgJSON), cfg)
assert.NoError(err)
assert.Equal("a5b557be-b7f0-4cb3-8f7c-6b5092f29c2c", cfg.SchedulerHints.Group)
omap, err := cfg.ToServerCreateMap()
assert.NoError(err)
assert.NotNil(omap)
req, err := json.Marshal(omap)
assert.NoError(err)
assert.Equal(expected, string(req))
//t.Log(omap)
//t.Log(string(req))
}
func TestInsertSSHKeyIgn(t *testing.T) {
testCases := []struct {
name string
userData string
expected string
}{
{"empty", "", `{"ignition":{"config":{"replace":{"verification":{}}},"proxy":{},"security":{"tls":{}},"timeouts":{},"version":"3.4.0"},"kernelArguments":{},"passwd":{"users":[{"name":"test","sshAuthorizedKeys":["testkey"]}]},"storage":{},"systemd":{}}`},
{"diff-user", `{"ignition":{"version":"3.3.0"},"passwd":{"users":[{"name":"test2"}]}}`, `{"ignition":{"config":{"replace":{"verification":{}}},"proxy":{},"security":{"tls":{}},"timeouts":{},"version":"3.4.0"},"kernelArguments":{},"passwd":{"users":[{"name":"test2"},{"name":"test","sshAuthorizedKeys":["testkey"]}]},"storage":{},"systemd":{}}`},
{"same-user", `{"ignition":{"version":"3.2.0"},"passwd":{"users":[{"name":"test","sshAuthorizedKeys":["testkey1"]}]}}`, `{"ignition":{"config":{"replace":{"verification":{}}},"proxy":{},"security":{"tls":{}},"timeouts":{},"version":"3.4.0"},"kernelArguments":{},"passwd":{"users":[{"name":"test","sshAuthorizedKeys":["testkey1","testkey"]}]},"storage":{},"systemd":{}}`},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert := assert.New(t)
spec := &ExtCreateOpts{
UserData: tc.userData,
}
err := InsertSSHKeyIgn(spec, "test", "testkey")
assert.NoError(err)
assert.Equal(tc.expected, spec.UserData)
})
}
}