1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package resourceactuation_test
16
17 import (
18 "strconv"
19 "testing"
20
21 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/resourceactuation"
22 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
23
24 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
25 )
26
27 func TestShouldSkip(t *testing.T) {
28 testcases := []struct {
29 name string
30 generation int64
31 observedGeneration int64
32 reconcileInterval int32
33 status string
34 reason string
35 wantSkip bool
36 }{
37 {
38 name: "Should skip the resource actuation",
39 generation: 1,
40 observedGeneration: 1,
41 reconcileInterval: 0,
42 status: "True",
43 reason: k8s.UpToDate,
44 wantSkip: true,
45 },
46 {
47 name: "Generation and ObservedGeneration don't match",
48 generation: 2,
49 observedGeneration: 1,
50 reconcileInterval: 0,
51 status: "True",
52 reason: k8s.UpToDate,
53 wantSkip: false,
54 },
55 {
56 name: "Reconcile interval is not set to 0",
57 generation: 1,
58 observedGeneration: 1,
59 reconcileInterval: 600,
60 status: "True",
61 reason: k8s.UpToDate,
62 wantSkip: false,
63 },
64 {
65 name: "Resource status is not true",
66 generation: 1,
67 observedGeneration: 1,
68 reconcileInterval: 0,
69 status: "False",
70 reason: k8s.UpToDate,
71 wantSkip: false,
72 },
73 {
74 name: "Resource reason is not UpToDate",
75 generation: 1,
76 observedGeneration: 1,
77 reconcileInterval: 0,
78 status: "True",
79 reason: k8s.Updating,
80 wantSkip: false,
81 },
82 }
83
84 for _, tc := range testcases {
85 t.Run(tc.name, func(t *testing.T) {
86 var u unstructured.Unstructured
87 u.SetGeneration(tc.generation)
88 if err := unstructured.SetNestedField(u.Object, tc.observedGeneration, "status", "observedGeneration"); err != nil {
89 t.Errorf("Unable to set status.observedGeneration: %v", err)
90 }
91 condition := map[string]interface{}{
92 "status": tc.status,
93 "reason": tc.reason,
94 }
95 if err := unstructured.SetNestedSlice(u.Object, []interface{}{condition}, "status", "conditions"); err != nil {
96 t.Errorf("Unable to set status.conditions: %v", err)
97 }
98 u.SetAnnotations(map[string]string{k8s.ReconcileIntervalInSecondsAnnotation: strconv.Itoa(int(tc.reconcileInterval))})
99 skip, _ := resourceactuation.ShouldSkip(&u)
100 if skip != tc.wantSkip {
101 t.Errorf("resourceactuation.ShouldSkip returns %t, want %t", skip, tc.wantSkip)
102 }
103 })
104 }
105 }
106
View as plain text