-
Notifications
You must be signed in to change notification settings - Fork 14
/
servers_test.go
329 lines (269 loc) · 15 KB
/
servers_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package glesys
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func TestServerDetailsIsLocked(t *testing.T) {
serverDetails := ServerDetails{}
assert.Equal(t, false, serverDetails.IsLocked, "should not be locked")
serverDetails.IsLocked = true
assert.Equal(t, true, serverDetails.IsLocked, "should be locked")
}
func TestServerDetailsIsRunning(t *testing.T) {
serverDetails := ServerDetails{}
assert.Equal(t, false, serverDetails.IsRunning, "should not be running")
serverDetails.IsRunning = true
assert.Equal(t, true, serverDetails.IsRunning, "should be running")
}
func TestCreateServerParamsWithDefaults(t *testing.T) {
params := CreateServerParams{}.WithDefaults()
assert.Equal(t, 100, params.Bandwidth, "Bandwidth has correct default value")
assert.Equal(t, 2, params.CPU, "CPU has correct default value")
assert.Equal(t, "Falkenberg", params.DataCenter, "DataCenter has correct default value")
assert.Equal(t, "any", params.IPv4, "IPv4 has correct default value")
assert.Equal(t, "any", params.IPv6, "IPv6 has correct default value")
assert.Equal(t, 2048, params.Memory, "Memory has correct default value")
assert.Equal(t, "KVM", params.Platform, "Platform has correct default value")
assert.Equal(t, 50, params.Storage, "Storage has correct default value")
assert.Equal(t, "Debian 11 (Bullseye)", params.Template, "Template has correct default value")
assert.NotEmpty(t, params.Hostname, "Hostname has a default value")
}
func TestCreateServerParamsCustomWithDefaults(t *testing.T) {
params := CreateServerParams{
DataCenter: "Stockholm",
Memory: 4096,
}.WithDefaults()
assert.Equal(t, 100, params.Bandwidth, "Bandwidth has correct default value")
assert.Equal(t, 2, params.CPU, "CPU has correct default value")
assert.Equal(t, "Stockholm", params.DataCenter, "DataCenter has correct custom value")
assert.Equal(t, "any", params.IPv4, "IPv4 has correct default value")
assert.Equal(t, "any", params.IPv6, "IPv6 has correct default value")
assert.Equal(t, 4096, params.Memory, "Memory has correct custom value")
assert.Equal(t, "KVM", params.Platform, "Platform has correct default value")
assert.Equal(t, 50, params.Storage, "Storage has correct default value")
assert.Equal(t, "Debian 11 (Bullseye)", params.Template, "Template has correct default value")
}
func TestCreateServerParamsWithUsers(t *testing.T) {
params := CreateServerParams{
Bandwidth: 100,
CPU: 2,
DataCenter: "Falkenberg",
IPv4: "any",
IPv6: "any",
Memory: 2048,
Storage: 20,
Platform: "KVM",
Template: "ubuntu-18-04",
Hostname: "kvmXXXXXXX",
}.WithUser("glesys", []string{"ssh-rsa"}, "password")
users := []User{{"glesys",
[]string{"ssh-rsa"},
"password",
}}
assert.Equal(t, 100, params.Bandwidth, "Bandwidth has correct default value")
assert.Equal(t, 2, params.CPU, "CPU has correct default value")
assert.Equal(t, "Falkenberg", params.DataCenter, "DataCenter has correct default value")
assert.Equal(t, "any", params.IPv4, "IPv4 has correct default value")
assert.Equal(t, "any", params.IPv6, "IPv6 has correct default value")
assert.Equal(t, 2048, params.Memory, "Memory has correct default value")
assert.Equal(t, "KVM", params.Platform, "Platform has correct default value")
assert.Equal(t, 20, params.Storage, "Storage has correct default value")
assert.Equal(t, "ubuntu-18-04", params.Template, "Template has correct default value")
assert.Equal(t, users, params.Users, "Users has correct default value")
assert.NotEmpty(t, params.Hostname, "Hostname has a default value")
}
func TestServersCreate(t *testing.T) {
c := &mockClient{body: `{ "response": { "server": { "serverid": "kvm12345" } } }`}
s := ServerService{client: c}
server, _ := s.Create(context.Background(), CreateServerParams{})
assert.Equal(t, "POST", c.lastMethod, "method used is correct")
assert.Equal(t, "server/create", c.lastPath, "path used is correct")
assert.Equal(t, "kvm12345", server.ID, "server ID is correct")
}
func TestServersConsole(t *testing.T) {
c := &mockClient{body: `{"response": {"console": {
"host": "None", "password": "", "protocol": "",
"url": "https://console.example.com/view/abc123456-ff00-aabb-ccdd-xyz987654321"}}} `}
s := ServerService{client: c}
console, _ := s.Console(context.Background(), "kvm123456")
assert.Equal(t, "POST", c.lastMethod, "method used is correct")
assert.Equal(t, "server/console", c.lastPath, "path used is correct")
assert.Equal(t, "https://console.example.com/view/abc123456-ff00-aabb-ccdd-xyz987654321", console.URL, "server console url is correct")
}
func TestServersPreviewCloudConfig(t *testing.T) {
c := &mockClient{body: `{"response":{
"cloudconfig":{
"preview": "#cloud-config\nusers:\n -\n name: bob\n shell: /bin/bash\n lock_passwd: false\n sudo: 'ALL=(ALL) PASSWD:ALL'\n passwd: $6$ecb46c3c2a73263f$wYkIrbHQzZ0zZvsb7PxdhIbskjOA4Ti5NnDe7EBBP.1SDAfborckfDcuYsqDmdgbGMFJgBzQMjXgJ4qHbLV5s.\n ssh_authorized_keys: ['ssh-ed25519 AAAAKEY bob@bob-machine']\nssh_pwauth: false\nchpasswd:\n expire: false\n",
"context": {"params": { "foo": "bar", "balloon": 99 },
"users": [{"username": "bob", "password": "hunter333", "sshKeys": ["ssh-ed25519 AAAAKEY bob@bob-machine"]}]
}
}}}`}
s := ServerService{client: c}
cloudConfigParams := map[string]any{"foo": "bar", "balloon": 99}
users := []User{}
users = append(users, User{
Username: "bob",
Password: "hunter333",
PublicKeys: []string{"ssh-ed25519 AAAAKEY bob@bob-machine"},
})
params := PreviewCloudConfigParams{
CloudConfig: "## template: glesys\n#cloud-config\n{{>users}}\n",
CloudConfigParams: cloudConfigParams,
Users: users,
}
preview, _ := s.PreviewCloudConfig(context.Background(), params)
assert.Equal(t, "POST", c.lastMethod, "method used is correct")
assert.Equal(t, "server/previewcloudconfig", c.lastPath, "path used is correct")
assert.Equal(t, "bob", preview.Context.Users[0].Username, "Preview contains user")
assert.Equal(t, float64(99), preview.Context.Params["balloon"], "Preview contains parameter")
}
func TestServersDestroy(t *testing.T) {
c := &mockClient{}
s := ServerService{client: c}
s.Destroy(context.Background(), "kvm123456", DestroyServerParams{})
assert.Equal(t, "POST", c.lastMethod, "method used is correct")
assert.Equal(t, "server/destroy", c.lastPath, "path used is correct")
}
func TestServersDetails(t *testing.T) {
c := &mockClient{body: `{ "response": { "server": { "hostname": "my-server-123",
"bandwidth": 100,
"description": "MyServer",
"templatename": "Debian 11 64-bit",
"backup": {"enabled": "yes", "schedules":
[{"frequency": "daily", "numberofimagestokeep": 1}]}
} } }`}
s := ServerService{client: c}
server, _ := s.Details(context.Background(), "kvm123456")
assert.Equal(t, "GET", c.lastMethod, "method used is correct")
assert.Equal(t, "server/details/serverid/kvm123456/includestate/yes", c.lastPath, "path used is correct")
assert.Equal(t, "my-server-123", server.Hostname, "server Hostname is correct")
assert.Equal(t, 100, server.Bandwidth, "server bandwidth is correct")
assert.Equal(t, "MyServer", server.Description, "server Description is correct")
assert.Equal(t, "Debian 11 64-bit", server.Template, "server Template is correct")
assert.Equal(t, "daily", server.Backup.Schedules[0].Frequency, "Backup schedule is daily")
assert.Equal(t, 1, server.Backup.Schedules[0].Numberofimagestokeep, "Backup images to keep is correct")
}
func TestServersEdit(t *testing.T) {
c := &mockClient{}
s := ServerService{client: c}
s.Edit(context.Background(), "kvm123456", EditServerParams{})
assert.Equal(t, "POST", c.lastMethod, "method used is correct")
assert.Equal(t, "server/edit", c.lastPath, "path used is correct")
}
func TestServersList(t *testing.T) {
c := &mockClient{body: `{ "response": { "servers": [{ "serverid": "kvm12345" }] } }`}
s := ServerService{client: c}
servers, _ := s.List(context.Background())
assert.Equal(t, "GET", c.lastMethod, "method used is correct")
assert.Equal(t, "server/list", c.lastPath, "path used is correct")
assert.Equal(t, "kvm12345", (*servers)[0].ID, "one server was returned")
}
func TestServersNetworkAdapters(t *testing.T) {
c := &mockClient{body: `{ "response": { "networkadapters": [{
"networkadapterid": "9ac61694-eb4d-4011-9d10-c395ba5f7269",
"bandwidth": 100,
"name": "My Network Adapter",
"adaptertype": "VMXNET 3",
"state": "ready",
"serverid": "wps123456",
"networkid": "internet-fbg"
},
{"networkadapterid": "9ac61694-eb4d-4011-9d10-c395ba5f7420",
"bandwidth": 500,
"name": "Network Adapter 2",
"adaptertype": "VMXNET 3",
"state": "ready",
"serverid": "wps123456",
"networkid": "vl123456"
}] } }`}
s := ServerService{client: c}
networkAdapters, _ := s.NetworkAdapters(context.Background(), "9ac61694-eb4d-4011-9d10-c395ba5f7269")
assert.Equal(t, "POST", c.lastMethod, "method used is correct")
assert.Equal(t, "server/networkadapters", c.lastPath, "path used is correct")
assert.Equal(t, "VMXNET 3", (*networkAdapters)[0].AdapterType, "AdapterType is correct")
assert.Equal(t, 100, (*networkAdapters)[0].Bandwidth, "Bandwidth is correct")
assert.Equal(t, "9ac61694-eb4d-4011-9d10-c395ba5f7269", (*networkAdapters)[0].ID, "ID is correct")
assert.Equal(t, "My Network Adapter", (*networkAdapters)[0].Name, "Name is correct")
assert.Equal(t, "internet-fbg", (*networkAdapters)[0].NetworkID, "NetworkID is correct")
assert.Equal(t, "wps123456", (*networkAdapters)[0].ServerID, "ServerID is correct")
assert.Equal(t, "ready", (*networkAdapters)[0].State, "State is correct")
assert.Equal(t, "VMXNET 3", (*networkAdapters)[1].AdapterType, "AdapterType is correct")
assert.Equal(t, 500, (*networkAdapters)[1].Bandwidth, "Bandwidth is correct")
assert.Equal(t, "9ac61694-eb4d-4011-9d10-c395ba5f7420", (*networkAdapters)[1].ID, "ID is correct")
assert.Equal(t, "Network Adapter 2", (*networkAdapters)[1].Name, "Bandwidth is correct")
assert.Equal(t, "vl123456", (*networkAdapters)[1].NetworkID, "NetworkID is correct")
assert.Equal(t, "wps123456", (*networkAdapters)[1].ServerID, "ServerID is correct")
assert.Equal(t, "ready", (*networkAdapters)[1].State, "State is correct")
}
func TestServersStart(t *testing.T) {
c := &mockClient{}
s := ServerService{client: c}
s.Start(context.Background(), "kvm123456")
assert.Equal(t, "POST", c.lastMethod, "method used is correct")
assert.Equal(t, "server/start", c.lastPath, "path used is correct")
}
func TestServersStop(t *testing.T) {
c := &mockClient{}
s := ServerService{client: c}
s.Stop(context.Background(), "kvm123456", StopServerParams{})
assert.Equal(t, "POST", c.lastMethod, "method used is correct")
assert.Equal(t, "server/stop", c.lastPath, "path used is correct")
}
func TestServersListISO(t *testing.T) {
c := &mockClient{body: `{"response":{ "isofiles": ["OpenBSD/7.4/amd64/cd74.iso", "OpenBSD/7.4/amd64/install74.iso"] }}`}
s := ServerService{client: c}
isos, _ := s.ListISOs(context.Background(), "wps123456")
assert.Equal(t, "POST", c.lastMethod, "method used is correct")
assert.Equal(t, "server/listiso", c.lastPath, "path used is correct")
assert.Equal(t, "OpenBSD/7.4/amd64/cd74.iso", (*isos)[0], "iso is correct")
}
func TestServersMountISO(t *testing.T) {
c := &mockClient{body: `{ "response": { "server": { "hostname": "my-server-123",
"bandwidth": 100,
"description": "MyServer",
"templatename": "None",
"isofile": "OpenBSD/7.4/amd64/cd74.iso"
} } }`}
s := ServerService{client: c}
detail, _ := s.MountISO(context.Background(), "wps123456", "OpenBSD/7.4/amd64/cd74.iso")
assert.Equal(t, "POST", c.lastMethod, "method used is correct")
assert.Equal(t, "server/mountiso", c.lastPath, "path used is correct")
assert.Equal(t, "OpenBSD/7.4/amd64/cd74.iso", detail.ISOFile, "iso is correct")
}
func TestServersTemplates(t *testing.T) {
c := &mockClient{body: `{"response":{ "templates": { "KVM": [{"id": "ac7c05f1-4cb6-4330-a0a2-d1f2e6244b21",
"name": "AlmaLinux 8", "minimumdisksize": 5, "minimummemorysize": 512, "operatingsystem": "linux", "platform": "KVM",
"instancecost": {"amount": 0, "currency": "SEK", "timeperiod": "month"},
"licensecost": {"amount": 0, "currency": "SEK", "timeperiod": "month"}, "bootstrapmethod": "CLOUD_INIT"},
{"id": "2563b4d0-ea80-4aef-8f77-f9b9e479a008", "name": "AlmaLinux 9", "minimumdisksize": 5, "minimummemorysize": 512,
"operatingsystem": "linux", "platform": "KVM", "instancecost": {"amount": 0, "currency": "SEK", "timeperiod": "month"},
"licensecost": {"amount": 0, "currency": "SEK", "timeperiod": "month"}, "bootstrapmethod": "CLOUD_INIT"}],
"VMware": [{"id": "420fe17c-bc03-4b2c-a741-7a790e5f21ad", "name": "Alma Linux 8", "minimumdisksize": 5, "minimummemorysize": 512,
"operatingsystem": "linux", "platform": "VMware", "instancecost": {"amount": 0, "currency": "SEK", "timeperiod": "month"},
"licensecost": {"amount": 0, "currency": "SEK", "timeperiod": "month"}, "bootstrapmethod": "DEPLOY_SCRIPT"},
{"id": "dbbca8a7-1e26-4b76-8bb4-8d56dae59039", "name": "Alma Linux 9", "minimumdisksize": 5, "minimummemorysize": 512,
"operatingsystem": "linux", "platform": "VMware", "instancecost": {"amount": 0, "currency": "SEK", "timeperiod": "month"},
"licensecost": {"amount": 0, "currency": "SEK", "timeperiod": "month"}, "bootstrapmethod": "CLOUD_INIT"},
{"id": "d924551c-0a0d-43ba-abcd-aoeuqwer1234", "name": "Windows Server 2022 Standard LTSC", "minimumdisksize": 30,
"minimummemorysize": 1024, "operatingsystem": "windows", "platform": "VMware", "instancecost":
{"amount": 999.10, "currency": "SEK", "timeperiod": "month"}, "licensecost": {"amount": 123.4, "currency": "SEK",
"timeperiod": "month"}, "bootstrapmethod": "DEPLOY_SCRIPT"}]}}}`}
s := ServerService{client: c}
templates, _ := s.Templates(context.Background())
assert.Equal(t, "POST", c.lastMethod, "method used is correct")
assert.Equal(t, "server/templates", c.lastPath, "path used is correct")
assert.Equal(t, 512, templates.KVM[0].MinMemSize, "template minmemsize is correct")
assert.Equal(t, "CLOUD_INIT", templates.KVM[0].BootstrapMethod, "template bootstrapmethod is correct")
assert.Equal(t, "Alma Linux 8", templates.VMware[0].Name, "template name is correct")
assert.Equal(t, "DEPLOY_SCRIPT", templates.VMware[0].BootstrapMethod, "template bootstrapmethod is correct")
assert.Equal(t, 0.0, templates.VMware[0].LicenseCost.Amount, "template licensecost amount is correct")
assert.Equal(t, "month", templates.VMware[0].LicenseCost.Timeperiod, "template licensecost timeperiod is correct")
assert.Equal(t, 999.1, templates.VMware[2].InstanceCost.Amount, "template instancecost amount is correct")
assert.Equal(t, 123.4, templates.VMware[2].LicenseCost.Amount, "template licensecost amount is correct")
assert.Equal(t, "month", templates.VMware[2].LicenseCost.Timeperiod, "template licensecost timeperiod is correct")
}
func TestGenerateHostnameReturnsAHostnameInTheCorrectFormat(t *testing.T) {
hostname := generateHostname()
assert.Regexp(t, "^\\w+-\\w+-\\d{3}$", hostname, "Hostname is dasherized and contains two words followed by a number")
}