1 package dsdssandboxes
2
3 import (
4 "slices"
5 "testing"
6
7 "google.golang.org/api/compute/v1"
8 "gotest.tools/v3/assert"
9 )
10
11 var (
12 testVMs = []string{"s1", "s2", "s3"}
13
14 mockInstanceList = compute.InstanceList{
15 Items: []*compute.Instance{
16 {
17 Name: "s1",
18 LabelFingerprint: "",
19 Labels: map[string]string{},
20 },
21 {
22 Name: "s2",
23 LabelFingerprint: "",
24 Labels: map[string]string{
25 "test": "example",
26 },
27 },
28 {
29 Name: "s3",
30 LabelFingerprint: "",
31 Labels: preexistingSchedule.ToLabelMap(),
32 },
33 },
34 }
35
36 expectedLabels = map[string]map[string]string{
37 "s1": testSchedule.ToLabelMap(),
38 "s2": testSchedule.ToLabelMap(),
39 "s3": testSchedule.ToLabelMap(),
40 }
41
42 testSchedule = Schedule{
43 WeekendStart: intPointer(8),
44 WeekendStop: intPointer(20),
45 WeekdayStart: intPointer(6),
46 WeekdayStop: intPointer(22),
47 Timezone: "utc",
48 }
49
50 preexistingSchedule = Schedule{
51 WeekendStart: intPointer(11),
52 WeekendStop: intPointer(15),
53 WeekdayStart: intPointer(2),
54 WeekdayStop: intPointer(4),
55 Timezone: "eastern_standard_time",
56 }
57 )
58
59 func TestAddVMScheduleLabels(t *testing.T) {
60 labels := map[string]map[string]string{}
61 progressHookCalls := []string{}
62 expectedLabels["s2"]["test"] = "example"
63
64 setLabelMock := func(instance string, request *compute.InstancesSetLabelsRequest) error {
65 labels[instance] = request.Labels
66 return nil
67 }
68
69 listVMMock := func() (*compute.InstanceList, error) {
70 return &mockInstanceList, nil
71 }
72
73 progressHook := func(instance string) {
74 progressHookCalls = append(progressHookCalls, instance)
75 }
76
77 labeller := ProjectLabeller{
78 Name: "testProject",
79 setLabels: setLabelMock,
80 listVMs: listVMMock,
81 progressHook: progressHook,
82 }
83
84 err := labeller.AddVMScheduleLabels(testSchedule)
85
86 assert.NilError(t, err)
87 assert.Equal(t, len(testVMs), len(labels))
88 assert.Equal(t, len(testVMs), len(progressHookCalls))
89
90 for _, instanceName := range testVMs {
91 assert.Equal(t, true, slices.Contains(progressHookCalls, instanceName))
92 assert.DeepEqual(t, expectedLabels[instanceName], labels[instanceName])
93 }
94 }
95
96 func intPointer(value int) *int {
97 return &value
98 }
99
View as plain text