Repository navigation
Expand file tree
/
Copy pathinstance_group_test.go
More file actions
1310 lines (1159 loc) · 44.5 KB
/
Copy pathinstance_group_test.go
File metadata and controls
1310 lines (1159 loc) · 44.5 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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"context"
"crypto/ed25519"
"crypto/rand"
"encoding/pem"
"errors"
"net/http"
"slices"
"strings"
"sync"
"testing"
"time"
upcloud "github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/client"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
"github.com/hashicorp/go-hclog"
"gitlab.com/gitlab-org/fleeting/fleeting/provider"
"golang.org/x/crypto/ssh"
)
// ─── mock ────────────────────────────────────────────────────────────────────
// mockSvc is a test double for upcloudSvc.
// Each field is a function so individual tests can customise behaviour.
// Calls are serialized, so the functions may update test state without locking.
type mockSvc struct {
mu sync.Mutex
getAccount func(context.Context) (*upcloud.Account, error)
getStorageDetails func(context.Context, *request.GetStorageDetailsRequest) (*upcloud.StorageDetails, error)
getServersWithFilters func(context.Context, *request.GetServersWithFiltersRequest) (*upcloud.Servers, error)
createServer func(context.Context, *request.CreateServerRequest) (*upcloud.ServerDetails, error)
stopServer func(context.Context, *request.StopServerRequest) (*upcloud.ServerDetails, error)
waitForServerState func(context.Context, *request.WaitForServerStateRequest) (*upcloud.ServerDetails, error)
deleteServerAndStorages func(context.Context, *request.DeleteServerAndStoragesRequest) error
getServerDetails func(context.Context, *request.GetServerDetailsRequest) (*upcloud.ServerDetails, error)
}
func (m *mockSvc) GetAccount(ctx context.Context) (*upcloud.Account, error) {
m.mu.Lock()
defer m.mu.Unlock()
return m.getAccount(ctx)
}
func (m *mockSvc) GetStorageDetails(ctx context.Context, r *request.GetStorageDetailsRequest) (*upcloud.StorageDetails, error) {
m.mu.Lock()
defer m.mu.Unlock()
return m.getStorageDetails(ctx, r)
}
func (m *mockSvc) GetServersWithFilters(ctx context.Context, r *request.GetServersWithFiltersRequest) (*upcloud.Servers, error) {
m.mu.Lock()
defer m.mu.Unlock()
return m.getServersWithFilters(ctx, r)
}
func (m *mockSvc) CreateServer(ctx context.Context, r *request.CreateServerRequest) (*upcloud.ServerDetails, error) {
m.mu.Lock()
defer m.mu.Unlock()
return m.createServer(ctx, r)
}
func (m *mockSvc) StopServer(ctx context.Context, r *request.StopServerRequest) (*upcloud.ServerDetails, error) {
m.mu.Lock()
defer m.mu.Unlock()
return m.stopServer(ctx, r)
}
func (m *mockSvc) WaitForServerState(ctx context.Context, r *request.WaitForServerStateRequest) (*upcloud.ServerDetails, error) {
m.mu.Lock()
defer m.mu.Unlock()
return m.waitForServerState(ctx, r)
}
func (m *mockSvc) DeleteServerAndStorages(ctx context.Context, r *request.DeleteServerAndStoragesRequest) error {
m.mu.Lock()
defer m.mu.Unlock()
return m.deleteServerAndStorages(ctx, r)
}
func (m *mockSvc) GetServerDetails(ctx context.Context, r *request.GetServerDetailsRequest) (*upcloud.ServerDetails, error) {
m.mu.Lock()
defer m.mu.Unlock()
return m.getServerDetails(ctx, r)
}
// newMockSvc returns a mock where every method panics unless overridden.
func newMockSvc() *mockSvc {
panic := func(name string) { panic("unexpected call to mockSvc." + name) }
return &mockSvc{
getAccount: func(context.Context) (*upcloud.Account, error) { panic("GetAccount"); return nil, nil },
getStorageDetails: func(context.Context, *request.GetStorageDetailsRequest) (*upcloud.StorageDetails, error) {
panic("GetStorageDetails")
return nil, nil
},
getServersWithFilters: func(context.Context, *request.GetServersWithFiltersRequest) (*upcloud.Servers, error) {
panic("GetServersWithFilters")
return nil, nil
},
createServer: func(context.Context, *request.CreateServerRequest) (*upcloud.ServerDetails, error) {
panic("CreateServer")
return nil, nil
},
stopServer: func(context.Context, *request.StopServerRequest) (*upcloud.ServerDetails, error) {
panic("StopServer")
return nil, nil
},
waitForServerState: func(context.Context, *request.WaitForServerStateRequest) (*upcloud.ServerDetails, error) {
panic("WaitForServerState")
return nil, nil
},
deleteServerAndStorages: func(context.Context, *request.DeleteServerAndStoragesRequest) error {
panic("DeleteServerAndStorages")
return nil
},
getServerDetails: func(context.Context, *request.GetServerDetailsRequest) (*upcloud.ServerDetails, error) {
panic("GetServerDetails")
return nil, nil
},
}
}
// baseGroup returns a minimal valid, initialized InstanceGroup with a pre-set mock service.
func baseGroup(svc *mockSvc) *InstanceGroup {
g := &InstanceGroup{
Token: "test-token",
Zone: "fi-hel1",
Template: "template-uuid",
Name: "test-group",
Plan: defaultPlan,
svc: svc,
log: hclog.NewNullLogger(),
}
g.initState()
return g
}
// notFound is the error the UpCloud SDK returns for a missing resource.
var notFound = &upcloud.Problem{Status: http.StatusNotFound, Title: "SERVER_NOT_FOUND"}
// detailsInState returns a GetServerDetails mock reporting the given state.
func detailsInState(state string) func(context.Context, *request.GetServerDetailsRequest) (*upcloud.ServerDetails, error) {
return func(context.Context, *request.GetServerDetailsRequest) (*upcloud.ServerDetails, error) {
return &upcloud.ServerDetails{Server: upcloud.Server{State: state}}, nil
}
}
// listServers returns a GetServersWithFilters mock listing the given servers.
func listServers(servers ...upcloud.Server) func(context.Context, *request.GetServersWithFiltersRequest) (*upcloud.Servers, error) {
return func(context.Context, *request.GetServersWithFiltersRequest) (*upcloud.Servers, error) {
return &upcloud.Servers{Servers: servers}, nil
}
}
// ─── validate ────────────────────────────────────────────────────────────────
func TestValidate(t *testing.T) {
tests := []struct {
name string
g InstanceGroup
wantErr bool
wantPlan string
wantPrefix string
wantTitlePrefix string
wantMaxSize int
}{
{
name: "token auth - all required fields",
g: InstanceGroup{Token: "tok", Zone: "z", Template: "t", Name: "n"},
wantPlan: defaultPlan,
wantPrefix: defaultNamePrefix,
wantTitlePrefix: defaultTitlePrefix,
wantMaxSize: defaultMaxSize,
},
{
name: "username+password auth",
g: InstanceGroup{Username: "u", Password: "p", Zone: "z", Template: "t", Name: "n"},
},
{
name: "no auth at all",
g: InstanceGroup{Zone: "z", Template: "t", Name: "n"},
wantErr: true,
},
{
name: "username without password",
g: InstanceGroup{Username: "u", Zone: "z", Template: "t", Name: "n"},
wantErr: true,
},
{
name: "password without username",
g: InstanceGroup{Password: "p", Zone: "z", Template: "t", Name: "n"},
wantErr: true,
},
{
name: "missing zone",
g: InstanceGroup{Token: "tok", Template: "t", Name: "n"},
wantErr: true,
},
{
name: "missing template",
g: InstanceGroup{Token: "tok", Zone: "z", Name: "n"},
wantErr: true,
},
{
name: "missing name",
g: InstanceGroup{Token: "tok", Zone: "z", Template: "t"},
wantErr: true,
},
{
name: "use_private_network without network",
g: InstanceGroup{Token: "tok", Zone: "z", Template: "t", Name: "n", UsePrivateNetwork: true},
wantErr: true,
},
{
name: "use_private_network with network",
g: InstanceGroup{Token: "tok", Zone: "z", Template: "t", Name: "n", UsePrivateNetwork: true, Network: "net-uuid"},
},
{
name: "valid storage tier",
g: InstanceGroup{Token: "tok", Zone: "z", Template: "t", Name: "n", StorageTier: "maxiops"},
},
{
name: "invalid storage tier",
g: InstanceGroup{Token: "tok", Zone: "z", Template: "t", Name: "n", StorageTier: "MaxIOPS"},
wantErr: true,
},
{
name: "negative storage size",
g: InstanceGroup{Token: "tok", Zone: "z", Template: "t", Name: "n", StorageSize: -1},
wantErr: true,
},
{
name: "negative max size",
g: InstanceGroup{Token: "tok", Zone: "z", Template: "t", Name: "n", MaxSize: -1},
wantErr: true,
},
{
name: "explicit plan preserved",
g: InstanceGroup{Token: "tok", Zone: "z", Template: "t", Name: "n", Plan: "2xCPU-4GB"},
wantPlan: "2xCPU-4GB",
wantMaxSize: defaultMaxSize,
},
{
name: "explicit name prefix preserved",
g: InstanceGroup{Token: "tok", Zone: "z", Template: "t", Name: "n", NamePrefix: "ci"},
wantPlan: defaultPlan,
wantPrefix: "ci",
wantMaxSize: defaultMaxSize,
},
{
name: "explicit title prefix preserved",
g: InstanceGroup{Token: "tok", Zone: "z", Template: "t", Name: "n", TitlePrefix: "ci-runner"},
wantPlan: defaultPlan,
wantTitlePrefix: "ci-runner",
wantMaxSize: defaultMaxSize,
},
{
name: "overlong title prefix truncated to fit 255-char title",
g: InstanceGroup{Token: "tok", Zone: "z", Template: "t", Name: "n", TitlePrefix: strings.Repeat("t", 200)},
wantPlan: defaultPlan,
wantTitlePrefix: strings.Repeat("t", maxTitlePrefixLen),
wantMaxSize: defaultMaxSize,
},
{
name: "explicit max size preserved",
g: InstanceGroup{Token: "tok", Zone: "z", Template: "t", Name: "n", MaxSize: 5},
wantPlan: defaultPlan,
wantMaxSize: 5,
},
}
// Index instead of copying: InstanceGroup holds locks.
for i := range tests {
tc := &tests[i]
t.Run(tc.name, func(t *testing.T) {
err := tc.g.validate()
if (err != nil) != tc.wantErr {
t.Fatalf("validate() error = %v, wantErr = %v", err, tc.wantErr)
}
if tc.wantErr {
return
}
if tc.wantPlan != "" && tc.g.Plan != tc.wantPlan {
t.Errorf("Plan = %q, want %q", tc.g.Plan, tc.wantPlan)
}
if tc.wantPrefix != "" && tc.g.NamePrefix != tc.wantPrefix {
t.Errorf("NamePrefix = %q, want %q", tc.g.NamePrefix, tc.wantPrefix)
}
if tc.wantTitlePrefix != "" && tc.g.TitlePrefix != tc.wantTitlePrefix {
t.Errorf("TitlePrefix = %q, want %q", tc.g.TitlePrefix, tc.wantTitlePrefix)
}
if tc.wantMaxSize != 0 && tc.g.MaxSize != tc.wantMaxSize {
t.Errorf("MaxSize = %d, want %d", tc.g.MaxSize, tc.wantMaxSize)
}
})
}
}
// ─── mapServerState ───────────────────────────────────────────────────────────
func TestMapServerState(t *testing.T) {
tests := []struct {
state string
want provider.State
}{
{upcloud.ServerStateStarted, provider.StateRunning},
// Stopped/errored servers still exist and bill for storage: they stay
// tracked (StateDeleting) while Update deletes them, instead of being
// dropped (StateDeleted) and leaked.
{upcloud.ServerStateStopped, provider.StateDeleting},
{upcloud.ServerStateError, provider.StateDeleting},
{"maintenance", provider.StateCreating},
{"new", provider.StateCreating},
{"", provider.StateCreating},
}
for _, tc := range tests {
t.Run(tc.state, func(t *testing.T) {
got := mapServerState(tc.state)
if got != tc.want {
t.Errorf("mapServerState(%q) = %v, want %v", tc.state, got, tc.want)
}
})
}
}
// ─── randomSuffix ─────────────────────────────────────────────────────────────
func TestRandomSuffix(t *testing.T) {
const allowed = "abcdefghijklmnopqrstuvwxyz0123456789"
for _, n := range []int{0, 1, 8, 16} {
s := randomSuffix(n)
if len(s) != n {
t.Errorf("randomSuffix(%d): len = %d, want %d", n, len(s), n)
}
for _, c := range s {
if !strings.ContainsRune(allowed, c) {
t.Errorf("randomSuffix(%d): unexpected character %q in %q", n, c, s)
}
}
}
// Two calls should almost always differ (birthday problem: 36^8 possibilities).
if a, b := randomSuffix(8), randomSuffix(8); a == b {
t.Logf("randomSuffix returned identical values twice (%q) — unlikely but not impossible", a)
}
}
// ─── sanitizeNamePrefix ─────────────────────────────────────────────────────────
func TestSanitizeNamePrefix(t *testing.T) {
cases := []struct {
in, want string
}{
{"fleeting", "fleeting"},
{"CI", "ci"},
{"GitLab", "gitlab"},
{"My_Runner", "my-runner"},
{"My_Runner!", "my-runner"},
{"--ci--", "ci"},
{"a..b", "a-b"},
{" spaced ", "spaced"},
{"___", ""},
{"", ""},
// 60 chars → truncated to 54 so "<prefix>-<8 char suffix>" fits a 63-char label
{strings.Repeat("a", 60), strings.Repeat("a", 54)},
// truncation must not leave a trailing hyphen
{strings.Repeat("a", 53) + "--bb", strings.Repeat("a", 53)},
}
for _, c := range cases {
if got := sanitizeNamePrefix(c.in); got != c.want {
t.Errorf("sanitizeNamePrefix(%q) = %q, want %q", c.in, got, c.want)
}
}
}
// ─── Update ───────────────────────────────────────────────────────────────────
func TestUpdate(t *testing.T) {
var filters []request.QueryFilter
mock := newMockSvc()
mock.getServersWithFilters = func(_ context.Context, r *request.GetServersWithFiltersRequest) (*upcloud.Servers, error) {
filters = r.Filters
return &upcloud.Servers{
Servers: []upcloud.Server{
{UUID: "uuid-1", State: upcloud.ServerStateStarted},
{UUID: "uuid-2", State: upcloud.ServerStateStopped},
{UUID: "uuid-3", State: "maintenance"},
},
}, nil
}
mock.deleteServerAndStorages = func(context.Context, *request.DeleteServerAndStoragesRequest) error { return nil }
g := baseGroup(mock)
seen := map[string]provider.State{}
err := g.Update(context.Background(), func(id string, state provider.State) {
seen[id] = state
})
g.cleanups.Wait()
if err != nil {
t.Fatalf("Update() unexpected error: %v", err)
}
want := map[string]provider.State{
"uuid-1": provider.StateRunning,
"uuid-2": provider.StateDeleting,
"uuid-3": provider.StateCreating,
}
if len(seen) != len(want) {
t.Fatalf("Update() reported %v, want %v", seen, want)
}
for id, state := range want {
if seen[id] != state {
t.Errorf("%s state = %v, want %v", id, seen[id], state)
}
}
// The group label filter is what scopes the plugin to its own servers;
// a wrong key or value would leak (or steal) servers.
wantFilter := request.FilterLabel{Label: upcloud.Label{Key: groupLabelKey, Value: "test-group"}}
if len(filters) != 1 || filters[0] != wantFilter {
t.Errorf("GetServersWithFilters filters = %#v, want [%#v]", filters, wantFilter)
}
}
func TestUpdate_APIError(t *testing.T) {
mock := newMockSvc()
mock.getServersWithFilters = func(_ context.Context, _ *request.GetServersWithFiltersRequest) (*upcloud.Servers, error) {
return nil, errors.New("api error")
}
g := baseGroup(mock)
if err := g.Update(context.Background(), func(string, provider.State) {}); err == nil {
t.Fatal("Update() expected error, got nil")
}
}
func TestUpdate_DeletesStoppedAndErroredServers(t *testing.T) {
var deleted []string
mock := newMockSvc()
mock.getServersWithFilters = listServers(
upcloud.Server{UUID: "uuid-running", State: upcloud.ServerStateStarted},
upcloud.Server{UUID: "uuid-stopped", State: upcloud.ServerStateStopped},
upcloud.Server{UUID: "uuid-error", State: upcloud.ServerStateError},
)
mock.deleteServerAndStorages = func(_ context.Context, r *request.DeleteServerAndStoragesRequest) error {
deleted = append(deleted, r.UUID)
return nil
}
g := baseGroup(mock)
if err := g.Update(context.Background(), func(string, provider.State) {}); err != nil {
t.Fatalf("Update() unexpected error: %v", err)
}
g.cleanups.Wait()
slices.Sort(deleted)
if want := []string{"uuid-error", "uuid-stopped"}; !slices.Equal(deleted, want) {
t.Errorf("deleted = %v, want %v", deleted, want)
}
}
func TestUpdate_ThrottlesDeleteRetries(t *testing.T) {
attempts := 0
mock := newMockSvc()
mock.getServersWithFilters = listServers(upcloud.Server{UUID: "uuid-stopped", State: upcloud.ServerStateStopped})
mock.deleteServerAndStorages = func(context.Context, *request.DeleteServerAndStoragesRequest) error {
attempts++
return errors.New("delete failed")
}
g := baseGroup(mock)
update := func() {
t.Helper()
if err := g.Update(context.Background(), func(string, provider.State) {}); err != nil {
t.Fatalf("Update() unexpected error: %v", err)
}
g.cleanups.Wait()
}
update()
update() // within deleteRetryInterval: no new attempt
if attempts != 1 {
t.Fatalf("delete attempts = %d, want 1 (retries throttled)", attempts)
}
// Once the interval has passed, the failed delete is retried.
g.mu.Lock()
g.deleteAttempts["uuid-stopped"] = time.Now().Add(-deleteRetryInterval)
g.mu.Unlock()
update()
if attempts != 2 {
t.Errorf("delete attempts = %d, want 2 (retried after interval)", attempts)
}
}
func TestUpdate_ForgetsServersThatAreGone(t *testing.T) {
mock := newMockSvc()
mock.getServersWithFilters = listServers(upcloud.Server{UUID: "uuid-1", State: upcloud.ServerStateStarted})
g := baseGroup(mock)
g.deleteAttempts["uuid-gone"] = time.Now()
g.stopping["uuid-gone"] = struct{}{}
if err := g.Update(context.Background(), func(string, provider.State) {}); err != nil {
t.Fatalf("Update() unexpected error: %v", err)
}
if len(g.deleteAttempts) != 0 || len(g.stopping) != 0 {
t.Errorf("bookkeeping not pruned: deleteAttempts=%v stopping=%v", g.deleteAttempts, g.stopping)
}
}
// ─── Increase ─────────────────────────────────────────────────────────────────
func TestIncrease_AllSucceed(t *testing.T) {
var created []string
mock := newMockSvc()
mock.createServer = func(_ context.Context, r *request.CreateServerRequest) (*upcloud.ServerDetails, error) {
created = append(created, r.Hostname)
return &upcloud.ServerDetails{}, nil
}
g := baseGroup(mock)
n, err := g.Increase(context.Background(), 3)
if err != nil {
t.Fatalf("Increase() unexpected error: %v", err)
}
if n != 3 {
t.Errorf("Increase() = %d, want 3", n)
}
if len(created) != 3 {
t.Errorf("CreateServer called %d times, want 3", len(created))
}
}
func TestIncrease_CreateRequest(t *testing.T) {
var req *request.CreateServerRequest
mock := newMockSvc()
mock.createServer = func(_ context.Context, r *request.CreateServerRequest) (*upcloud.ServerDetails, error) {
req = r
return &upcloud.ServerDetails{}, nil
}
g := baseGroup(mock)
g.NamePrefix = "ci"
g.TitlePrefix = "CI Runner"
g.StorageSize = 20
g.StorageTier = upcloud.StorageTierMaxIOPS
g.publicKey = "ssh-ed25519 AAAA test\n"
g.settings.Username = "root"
if _, err := g.Increase(context.Background(), 1); err != nil {
t.Fatalf("Increase() unexpected error: %v", err)
}
if !strings.HasPrefix(req.Hostname, "ci-") || len(req.Hostname) != len("ci-")+8 {
t.Errorf("Hostname = %q, want ci-<8 chars>", req.Hostname)
}
if want := "CI Runner - " + req.Hostname; req.Title != want {
t.Errorf("Title = %q, want %q", req.Title, want)
}
if req.Zone != "fi-hel1" || req.Plan != defaultPlan {
t.Errorf("Zone/Plan = %q/%q, want fi-hel1/%s", req.Zone, req.Plan, defaultPlan)
}
if req.Metadata != upcloud.True {
t.Error("Metadata service not enabled; cloud-init can't inject the SSH key")
}
// Update finds the group's servers by this label.
if req.Labels == nil || len(*req.Labels) != 1 || (*req.Labels)[0] != (upcloud.Label{Key: groupLabelKey, Value: "test-group"}) {
t.Errorf("Labels = %v, want [%s=test-group]", req.Labels, groupLabelKey)
}
if len(req.StorageDevices) != 1 {
t.Fatalf("StorageDevices = %v, want one clone of the template", req.StorageDevices)
}
disk := req.StorageDevices[0]
if disk.Action != request.CreateServerStorageDeviceActionClone || disk.Storage != "template-uuid" || disk.Size != 20 || disk.Tier != upcloud.StorageTierMaxIOPS {
t.Errorf("storage device = %+v, want clone of template-uuid, 20 GB, maxiops", disk)
}
if req.LoginUser == nil || req.LoginUser.Username != "root" || !slices.Equal([]string(req.LoginUser.SSHKeys), []string{g.publicKey}) {
t.Errorf("LoginUser = %+v, want root with the connector key", req.LoginUser)
}
ifaces := req.Networking.Interfaces
if len(ifaces) != 1 || ifaces[0].Type != upcloud.NetworkTypePublic {
t.Errorf("interfaces = %+v, want a single public interface", ifaces)
}
}
func TestIncrease_AttachesPrivateNetwork(t *testing.T) {
var req *request.CreateServerRequest
mock := newMockSvc()
mock.createServer = func(_ context.Context, r *request.CreateServerRequest) (*upcloud.ServerDetails, error) {
req = r
return &upcloud.ServerDetails{}, nil
}
g := baseGroup(mock)
g.Network = "net-uuid"
g.UsePrivateNetwork = true
if _, err := g.Increase(context.Background(), 1); err != nil {
t.Fatalf("Increase() unexpected error: %v", err)
}
ifaces := req.Networking.Interfaces
if len(ifaces) != 2 {
t.Fatalf("interfaces = %+v, want public + private", ifaces)
}
// UpCloud rejects private interfaces without a network UUID.
if ifaces[1].Type != upcloud.NetworkTypePrivate || ifaces[1].Network != "net-uuid" {
t.Errorf("private interface = %+v, want type private on net-uuid", ifaces[1])
}
}
func TestIncrease_NoSSHKey(t *testing.T) {
var req *request.CreateServerRequest
mock := newMockSvc()
mock.createServer = func(_ context.Context, r *request.CreateServerRequest) (*upcloud.ServerDetails, error) {
req = r
return &upcloud.ServerDetails{}, nil
}
g := baseGroup(mock)
if _, err := g.Increase(context.Background(), 1); err != nil {
t.Fatalf("Increase() unexpected error: %v", err)
}
if req.LoginUser != nil {
t.Errorf("LoginUser = %+v, want nil without a connector key", req.LoginUser)
}
}
func TestIncrease_PartialFailure(t *testing.T) {
calls := 0
mock := newMockSvc()
mock.createServer = func(_ context.Context, _ *request.CreateServerRequest) (*upcloud.ServerDetails, error) {
calls++
if calls%2 == 0 {
return nil, errors.New("quota exceeded")
}
return &upcloud.ServerDetails{}, nil
}
g := baseGroup(mock)
n, err := g.Increase(context.Background(), 4)
// Failures are aggregated into the returned error so the taskscaler can back off.
if err == nil {
t.Fatal("Increase() expected error for partial failure, got nil")
}
if n != 2 {
t.Errorf("Increase() = %d, want 2 (half succeed)", n)
}
}
func TestIncrease_AllFail(t *testing.T) {
mock := newMockSvc()
mock.createServer = func(_ context.Context, _ *request.CreateServerRequest) (*upcloud.ServerDetails, error) {
return nil, errors.New("HOSTNAME_INVALID")
}
g := baseGroup(mock)
n, err := g.Increase(context.Background(), 2)
if err == nil {
t.Fatal("Increase() expected error when all creates fail, got nil")
}
if !strings.Contains(err.Error(), "HOSTNAME_INVALID") {
t.Errorf("Increase() error = %v, want it to wrap the API error", err)
}
if n != 0 {
t.Errorf("Increase() = %d, want 0", n)
}
}
func TestIncrease_CancelledContext(t *testing.T) {
// createServer intentionally left as a panic: no creates may start once
// the caller's context is cancelled.
mock := newMockSvc()
ctx, cancel := context.WithCancel(context.Background())
cancel()
g := baseGroup(mock)
n, err := g.Increase(ctx, 3)
if err == nil {
t.Fatal("Increase() expected error for cancelled context, got nil")
}
if n != 0 {
t.Errorf("Increase() = %d, want 0", n)
}
}
func TestIncrease_Zero(t *testing.T) {
g := baseGroup(newMockSvc())
n, err := g.Increase(context.Background(), 0)
if err != nil || n != 0 {
t.Errorf("Increase(0) = (%d, %v), want (0, nil)", n, err)
}
}
func TestIncrease_SetsUserData(t *testing.T) {
var got string
mock := newMockSvc()
mock.createServer = func(_ context.Context, r *request.CreateServerRequest) (*upcloud.ServerDetails, error) {
got = r.UserData
return &upcloud.ServerDetails{}, nil
}
g := baseGroup(mock)
g.UserData = "https://example.com/init.sh"
if _, err := g.Increase(context.Background(), 1); err != nil {
t.Fatalf("Increase() unexpected error: %v", err)
}
if got != g.UserData {
t.Errorf("CreateServer UserData = %q, want %q", got, g.UserData)
}
}
// ─── Decrease ─────────────────────────────────────────────────────────────────
func TestDecrease_RunningServersAreStoppedNotWaitedOn(t *testing.T) {
var stopped []string
mock := newMockSvc()
mock.getServerDetails = detailsInState(upcloud.ServerStateStarted)
mock.stopServer = func(_ context.Context, r *request.StopServerRequest) (*upcloud.ServerDetails, error) {
if r.StopType != request.ServerStopTypeHard {
t.Errorf("StopType = %q, want hard", r.StopType)
}
stopped = append(stopped, r.UUID)
return &upcloud.ServerDetails{}, nil
}
// waitForServerState and deleteServerAndStorages intentionally left as
// panics: Decrease must not block on the stop; Update deletes the server
// once it is stopped.
g := baseGroup(mock)
instances := []string{"uuid-1", "uuid-2", "uuid-3"}
succeeded, err := g.Decrease(context.Background(), instances)
if err != nil {
t.Fatalf("Decrease() unexpected error: %v", err)
}
slices.Sort(succeeded)
slices.Sort(stopped)
if !slices.Equal(succeeded, instances) || !slices.Equal(stopped, instances) {
t.Errorf("succeeded = %v, stopped = %v, want both %v", succeeded, stopped, instances)
}
if len(g.stopping) != 3 {
t.Errorf("stopping = %v, want all 3 tracked for Shutdown", g.stopping)
}
}
func TestDecrease_PartialFailure(t *testing.T) {
mock := newMockSvc()
mock.getServerDetails = detailsInState(upcloud.ServerStateStarted)
mock.stopServer = func(_ context.Context, r *request.StopServerRequest) (*upcloud.ServerDetails, error) {
if r.UUID == "uuid-bad" {
return nil, errors.New("stop failed")
}
return &upcloud.ServerDetails{}, nil
}
g := baseGroup(mock)
succeeded, err := g.Decrease(context.Background(), []string{"uuid-ok", "uuid-bad"})
if err == nil {
t.Fatal("Decrease() expected error for partial failure, got nil")
}
if len(succeeded) != 1 || succeeded[0] != "uuid-ok" {
t.Errorf("Decrease() succeeded = %v, want [uuid-ok]", succeeded)
}
}
func TestDecrease_StoppedOrErroredDeletedDirectly(t *testing.T) {
for _, state := range []string{upcloud.ServerStateStopped, upcloud.ServerStateError} {
t.Run(state, func(t *testing.T) {
deleted := false
mock := newMockSvc()
mock.getServerDetails = detailsInState(state)
// stopServer intentionally left as a panic: a stop attempt would fail.
mock.deleteServerAndStorages = func(context.Context, *request.DeleteServerAndStoragesRequest) error {
deleted = true
return nil
}
g := baseGroup(mock)
succeeded, err := g.Decrease(context.Background(), []string{"uuid-1"})
if err != nil {
t.Fatalf("Decrease() unexpected error: %v", err)
}
if !deleted {
t.Error("Decrease() did not delete the server")
}
if len(succeeded) != 1 {
t.Errorf("Decrease() succeeded = %v, want [uuid-1]", succeeded)
}
})
}
}
func TestDecrease_DeleteFailure(t *testing.T) {
mock := newMockSvc()
mock.getServerDetails = detailsInState(upcloud.ServerStateStopped)
mock.deleteServerAndStorages = func(context.Context, *request.DeleteServerAndStoragesRequest) error {
return errors.New("delete failed")
}
g := baseGroup(mock)
succeeded, err := g.Decrease(context.Background(), []string{"uuid-1"})
if err == nil || len(succeeded) != 0 {
t.Errorf("Decrease() = (%v, %v), want ([], error)", succeeded, err)
}
}
func TestDecrease_AlreadyGoneCountsAsRemoved(t *testing.T) {
t.Run("details 404", func(t *testing.T) {
mock := newMockSvc()
mock.getServerDetails = func(context.Context, *request.GetServerDetailsRequest) (*upcloud.ServerDetails, error) {
return nil, notFound
}
g := baseGroup(mock)
succeeded, err := g.Decrease(context.Background(), []string{"uuid-1"})
if err != nil || len(succeeded) != 1 {
t.Errorf("Decrease() = (%v, %v), want ([uuid-1], nil)", succeeded, err)
}
})
t.Run("delete 404", func(t *testing.T) {
mock := newMockSvc()
mock.getServerDetails = detailsInState(upcloud.ServerStateStopped)
mock.deleteServerAndStorages = func(context.Context, *request.DeleteServerAndStoragesRequest) error {
return notFound
}
g := baseGroup(mock)
succeeded, err := g.Decrease(context.Background(), []string{"uuid-1"})
if err != nil || len(succeeded) != 1 {
t.Errorf("Decrease() = (%v, %v), want ([uuid-1], nil)", succeeded, err)
}
})
}
func TestDecrease_DetailsError(t *testing.T) {
mock := newMockSvc()
mock.getServerDetails = func(context.Context, *request.GetServerDetailsRequest) (*upcloud.ServerDetails, error) {
return nil, errors.New("api error")
}
g := baseGroup(mock)
succeeded, err := g.Decrease(context.Background(), []string{"uuid-1"})
if err == nil || len(succeeded) != 0 {
t.Errorf("Decrease() = (%v, %v), want ([], error)", succeeded, err)
}
}
func TestDecrease_Empty(t *testing.T) {
g := baseGroup(newMockSvc())
succeeded, err := g.Decrease(context.Background(), nil)
if err != nil || len(succeeded) != 0 {
t.Errorf("Decrease(nil) = (%v, %v), want ([], nil)", succeeded, err)
}
}
// ─── Shutdown ─────────────────────────────────────────────────────────────────
func TestShutdown_FinishesStoppedServers(t *testing.T) {
var deleted []string
var hadDeadline bool
mock := newMockSvc()
mock.getServerDetails = func(_ context.Context, r *request.GetServerDetailsRequest) (*upcloud.ServerDetails, error) {
switch r.UUID {
case "uuid-stopping":
return &upcloud.ServerDetails{Server: upcloud.Server{State: "maintenance"}}, nil
case "uuid-stopped":
return &upcloud.ServerDetails{Server: upcloud.Server{State: upcloud.ServerStateStopped}}, nil
default:
return nil, notFound
}
}
mock.waitForServerState = func(ctx context.Context, r *request.WaitForServerStateRequest) (*upcloud.ServerDetails, error) {
if r.UUID != "uuid-stopping" {
t.Errorf("WaitForServerState called for %s, want only uuid-stopping", r.UUID)
}
_, hadDeadline = ctx.Deadline()
return &upcloud.ServerDetails{}, nil
}
mock.deleteServerAndStorages = func(_ context.Context, r *request.DeleteServerAndStoragesRequest) error {
deleted = append(deleted, r.UUID)
return nil
}
g := baseGroup(mock)
for _, uuid := range []string{"uuid-stopping", "uuid-stopped", "uuid-gone"} {
g.stopping[uuid] = struct{}{}
}
if err := g.Shutdown(context.Background()); err != nil {
t.Fatalf("Shutdown() unexpected error: %v", err)
}
slices.Sort(deleted)
if want := []string{"uuid-stopped", "uuid-stopping"}; !slices.Equal(deleted, want) {
t.Errorf("deleted = %v, want %v", deleted, want)
}
if !hadDeadline {
t.Error("WaitForServerState ctx has no deadline; an unresponsive server would hang Shutdown forever")
}
if g.ctx.Err() == nil {
t.Error("Shutdown() did not cancel the background context")
}
}
func TestShutdown_ReportsFailures(t *testing.T) {
mock := newMockSvc()
mock.getServerDetails = detailsInState(upcloud.ServerStateStopped)
mock.deleteServerAndStorages = func(context.Context, *request.DeleteServerAndStoragesRequest) error {
return errors.New("delete failed")
}
g := baseGroup(mock)
g.stopping["uuid-1"] = struct{}{}
if err := g.Shutdown(context.Background()); err == nil {
t.Error("Shutdown() expected error when a delete fails, got nil")
}
}
func TestShutdown_StopWaitFailure(t *testing.T) {
mock := newMockSvc()
mock.getServerDetails = detailsInState("maintenance")
mock.waitForServerState = func(context.Context, *request.WaitForServerStateRequest) (*upcloud.ServerDetails, error) {
return nil, context.DeadlineExceeded
}
// deleteServerAndStorages intentionally left as a panic: a server that
// never stopped must not be deleted.
g := baseGroup(mock)
g.stopping["uuid-1"] = struct{}{}
if err := g.Shutdown(context.Background()); !errors.Is(err, context.DeadlineExceeded) {
t.Errorf("Shutdown() error = %v, want it to wrap context.DeadlineExceeded", err)
}
}
func TestShutdown_WaitsForBackgroundDeletions(t *testing.T) {
release := make(chan struct{})
deleted := false
mock := newMockSvc()
mock.getServersWithFilters = listServers(upcloud.Server{UUID: "uuid-stopped", State: upcloud.ServerStateStopped})
mock.deleteServerAndStorages = func(context.Context, *request.DeleteServerAndStoragesRequest) error {
<-release
deleted = true
return nil
}
g := baseGroup(mock)
if err := g.Update(context.Background(), func(string, provider.State) {}); err != nil {
t.Fatalf("Update() unexpected error: %v", err)
}
done := make(chan error)
go func() { done <- g.Shutdown(context.Background()) }()
select {
case err := <-done:
t.Fatalf("Shutdown() returned (%v) while a background deletion was still running", err)
case <-time.After(50 * time.Millisecond):
}
close(release)
if err := <-done; err != nil {
t.Fatalf("Shutdown() unexpected error: %v", err)
}
if !deleted {
t.Error("background deletion did not complete")
}
}
func TestShutdown_ContextCancelled(t *testing.T) {
g := baseGroup(newMockSvc())
g.cleanups.Add(1) // a background deletion that never finishes
defer g.cleanups.Done()
ctx, cancel := context.WithCancel(context.Background())
cancel()
if err := g.Shutdown(ctx); !errors.Is(err, context.Canceled) {
t.Errorf("Shutdown() error = %v, want context.Canceled", err)
}
}
func TestShutdown_BeforeInit(t *testing.T) {
g := &InstanceGroup{}
if err := g.Shutdown(context.Background()); err != nil {
t.Errorf("Shutdown() on an uninitialized group = %v, want nil", err)
}
}
// ─── ConnectInfo ──────────────────────────────────────────────────────────────
func makeDetails(publicIP, privateIP string) *upcloud.ServerDetails {
d := &upcloud.ServerDetails{}