@@ -4,113 +4,118 @@ import (
4
4
"context"
5
5
"testing"
6
6
7
+ "github.com/stretchr/testify/assert"
7
8
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8
9
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
9
10
"k8s.io/apimachinery/pkg/runtime"
10
11
"k8s.io/apimachinery/pkg/runtime/schema"
11
12
"k8s.io/apimachinery/pkg/runtime/serializer/yaml"
13
+ "k8s.io/apimachinery/pkg/util/sets"
12
14
"k8s.io/client-go/dynamic"
13
15
dynamicfake "k8s.io/client-go/dynamic/fake"
14
16
)
15
17
16
- func createMockConfigMachine (t * testing. T , c dynamic.Interface , data string ) {
18
+ func createMockConfigMachine (ctx context. Context , c dynamic.Interface , data string ) error {
17
19
decUnstructured1 := yaml .NewDecodingSerializer (unstructured .UnstructuredJSONScheme )
18
20
testMachineConfig := & unstructured.Unstructured {}
19
21
_ , _ , err := decUnstructured1 .Decode ([]byte (data ), nil , testMachineConfig )
20
22
if err != nil {
21
- t . Fatal ( "unable to decode MachineConfig YAML" , err )
23
+ return err
22
24
}
23
25
24
- _ , _ = c .
26
+ _ , err = c .
25
27
Resource (machineConfigGroupVersionResource ).
26
- Create (context .Background (), testMachineConfig , metav1.CreateOptions {})
27
- }
28
-
29
- func Test_MachineConfigs (t * testing.T ) {
30
- // Initialize the fake dynamic client.
31
- machineConfigClient := dynamicfake .NewSimpleDynamicClientWithCustomListKinds (runtime .NewScheme (), map [schema.GroupVersionResource ]string {
32
- machineConfigGroupVersionResource : "MachineConfigsList" ,
33
- })
34
-
35
- records , errs := gatherMachineConfigs (context .Background (), machineConfigClient )
36
- if len (errs ) > 0 {
37
- t .Fatalf ("unexpected errors: %#v" , errs )
38
- }
39
- // 0 records because there is no MachineConfigs yet.
40
- if len (records ) != 0 {
41
- t .Fatalf ("unexpected number or records in the first run: %d" , len (records ))
28
+ Create (ctx , testMachineConfig , metav1.CreateOptions {})
29
+ if err != nil {
30
+ return err
42
31
}
32
+ return nil
33
+ }
43
34
44
- // Create first MachineConfig resource.
45
- machineConfigYAML1 := `apiVersion: machineconfiguration.openshift.io/v1
35
+ func TestGatherMachineConfigs (t * testing.T ) {
36
+ tests := []struct {
37
+ name string
38
+ machineConfigYAMLs []string
39
+ inUseMachineConfigs sets.Set [string ]
40
+ expectedNumberOfRecords int
41
+ }{
42
+ {
43
+ name : "no machine configs exists" ,
44
+ machineConfigYAMLs : []string {},
45
+ inUseMachineConfigs : sets.Set [string ]{},
46
+ expectedNumberOfRecords : 0 ,
47
+ },
48
+ {
49
+ name : "one machine config which is in use" ,
50
+ machineConfigYAMLs : []string {
51
+ `apiVersion: machineconfiguration.openshift.io/v1
52
+ kind: MachineConfig
53
+ metadata:
54
+ name: 75-worker-sap-data-intelligence` },
55
+ inUseMachineConfigs : sets.Set [string ]{"75-worker-sap-data-intelligence" : {}},
56
+ expectedNumberOfRecords : 1 ,
57
+ },
58
+ {
59
+ name : "two machine configs but only one in use" ,
60
+ expectedNumberOfRecords : 1 ,
61
+ machineConfigYAMLs : []string {
62
+ `apiVersion: machineconfiguration.openshift.io/v1
46
63
kind: MachineConfig
47
64
metadata:
48
- name: 75-worker-sap-data-intelligence
49
- `
50
-
51
- createMockConfigMachine (t , machineConfigClient , machineConfigYAML1 )
52
- records , errs = gatherMachineConfigs (context .Background (), machineConfigClient )
53
- if len (errs ) > 0 {
54
- t .Fatalf ("unexpected errors: %#v" , errs )
55
- }
56
- // 1 record because there is now 1 MachineConfig resource.
57
- if len (records ) != 1 {
58
- t .Fatalf ("unexpected number or records in the second run: %d" , len (records ))
59
- }
60
-
61
- // Create second MachineConfig resource.
62
- machineConfigYAML2 := `apiVersion: machineconfiguration.openshift.io/v1
65
+ name: test-not-in-use` ,
66
+ `apiVersion: machineconfiguration.openshift.io/v1
63
67
kind: MachineConfig
64
68
metadata:
65
- name: 75-master-sap-data-intelligence
66
- `
67
-
68
- createMockConfigMachine (t , machineConfigClient , machineConfigYAML2 )
69
- records , errs = gatherMachineConfigs (context .Background (), machineConfigClient )
70
- if len (errs ) > 0 {
71
- t .Fatalf ("unexpected errors: %#v" , errs )
72
- }
73
- // 2 record because there are now 2 MachineConfig resource.
74
- if len (records ) != 2 {
75
- t .Fatalf ("unexpected number or records in the third run: %d" , len (records ))
76
- }
77
-
78
- // Create third MachineConfig resource.
79
- machineConfigYAML3 := `apiVersion: machineconfiguration.openshift.io/v1
69
+ name: in-use` ,
70
+ },
71
+ inUseMachineConfigs : sets.Set [string ]{"in-use" : {}},
72
+ },
73
+ {
74
+ name : "no machine config in use" ,
75
+ expectedNumberOfRecords : 0 ,
76
+ machineConfigYAMLs : []string {
77
+ `apiVersion: machineconfiguration.openshift.io/v1
80
78
kind: MachineConfig
81
79
metadata:
82
- name: 99-sdi-generated-containerruntime
83
- ownerReferences:
84
- - kind: ContainerRuntimeConfig
85
- name: sdi-pids-limit
86
- `
87
-
88
- createMockConfigMachine (t , machineConfigClient , machineConfigYAML3 )
89
- records , errs = gatherMachineConfigs (context .Background (), machineConfigClient )
90
- if len (errs ) > 0 {
91
- t .Fatalf ("unexpected errors: %#v" , errs )
92
- }
93
- // 3 record because there are now 3 MachineConfig resource.
94
- if len (records ) != 3 {
95
- t .Fatalf ("unexpected number or records in the fourth run: %d" , len (records ))
96
- }
97
-
98
- // Create fourth MachineConfig resource.
99
- machineConfigYAML4 := `apiVersion: machineconfiguration.openshift.io/v1
80
+ name: test-not-in-use-1` ,
81
+ `apiVersion: machineconfiguration.openshift.io/v1
100
82
kind: MachineConfig
101
83
metadata:
102
- name: 00-example-machine-config
103
- labels:
104
- workload: sap-data-intelligence
105
- `
106
-
107
- createMockConfigMachine (t , machineConfigClient , machineConfigYAML4 )
108
- records , errs = gatherMachineConfigs (context .Background (), machineConfigClient )
109
- if len (errs ) > 0 {
110
- t .Fatalf ("unexpected errors: %#v" , errs )
84
+ name: test-not-in-use-2` ,
85
+ },
86
+ inUseMachineConfigs : sets.Set [string ]{"in-use" : {}, "in-use-2" : {}},
87
+ },
88
+ {
89
+ name : "two machine configs in use" ,
90
+ expectedNumberOfRecords : 2 ,
91
+ machineConfigYAMLs : []string {
92
+ `apiVersion: machineconfiguration.openshift.io/v1
93
+ kind: MachineConfig
94
+ metadata:
95
+ name: in-use-1` ,
96
+ `apiVersion: machineconfiguration.openshift.io/v1
97
+ kind: MachineConfig
98
+ metadata:
99
+ name: in-use-2` ,
100
+ },
101
+ inUseMachineConfigs : sets.Set [string ]{"in-use-1" : {}, "in-use-2" : {}},
102
+ },
111
103
}
112
- // 4 record because there are now 4 MachineConfig resource.
113
- if len (records ) != 4 {
114
- t .Fatalf ("unexpected number or records in the fifth run: %d" , len (records ))
104
+
105
+ for _ , tt := range tests {
106
+ t .Run (tt .name , func (t * testing.T ) {
107
+ ctx := context .Background ()
108
+ // Initialize the fake dynamic client.
109
+ machineConfigClient := dynamicfake .NewSimpleDynamicClientWithCustomListKinds (runtime .NewScheme (), map [schema.GroupVersionResource ]string {
110
+ machineConfigGroupVersionResource : "MachineConfigsList" ,
111
+ })
112
+ for _ , mcYAML := range tt .machineConfigYAMLs {
113
+ err := createMockConfigMachine (ctx , machineConfigClient , mcYAML )
114
+ assert .NoError (t , err )
115
+ }
116
+ records , errs := gatherMachineConfigs (ctx , machineConfigClient , tt .inUseMachineConfigs )
117
+ assert .Len (t , errs , 0 )
118
+ assert .Len (t , records , tt .expectedNumberOfRecords )
119
+ })
115
120
}
116
121
}
0 commit comments