@@ -36,84 +36,137 @@ import (
36
36
// PopulateTimeout is the timeout on populating the cache for the first time.
37
37
const PopulateTimeout = 10 * time .Second
38
38
39
- func TestVariableVKsDiscoveryAndResolution ( t * testing. T ) {
40
-
41
- // Initialise options.
42
- opts := options . NewOptions ()
43
- cmd := options . InitCommand
44
- opts . AddFlags ( cmd )
45
- klog . InfoS ( "options" , "options" , opts )
39
+ type resourceManager struct {
40
+ crConfigFile * os. File
41
+ initCrdFile * os. File
42
+ initCrFile * os. File
43
+ newCrdFile * os. File
44
+ newCrFile * os. File
45
+ }
46
46
47
- // Create testdata.
47
+ func ( rm * resourceManager ) createConfigAndResourceFiles ( t * testing. T ) {
48
48
crConfigFile , err := os .CreateTemp ("" , "cr-config.yaml" )
49
49
if err != nil {
50
50
t .Fatal (err )
51
51
}
52
+ rm .crConfigFile = crConfigFile
53
+
52
54
initCrdFile , err := os .CreateTemp ("" , "crd.yaml" )
53
55
if err != nil {
54
56
t .Fatal (err )
55
57
}
58
+ rm .initCrdFile = initCrdFile
59
+
56
60
initCrFile , err := os .CreateTemp ("" , "cr.yaml" )
57
61
if err != nil {
58
62
t .Fatal (err )
59
63
}
64
+ rm .initCrFile = initCrFile
65
+
60
66
newCrdFile , err := os .CreateTemp ("" , "new-crd.yaml" )
61
67
if err != nil {
62
68
t .Fatal (err )
63
69
}
70
+ rm .newCrdFile = newCrdFile
71
+
64
72
newCrFile , err := os .CreateTemp ("" , "new-cr.yaml" )
65
73
if err != nil {
66
74
t .Fatal (err )
67
75
}
76
+ rm .newCrFile = newCrFile
68
77
klog .InfoS ("testdata" , "crConfigFile" , crConfigFile .Name (), "initCrdFile" , initCrdFile .Name (), "initCrFile" , initCrFile .Name (), "newCrdFile" , newCrdFile .Name (), "newCrFile" , newCrFile .Name ())
78
+ }
79
+
80
+ func (rm * resourceManager ) removeResourceFiles (t * testing.T ) {
81
+ err := os .Remove (rm .crConfigFile .Name ())
82
+ if err != nil {
83
+ t .Fatalf ("failed to remove CR config: %v" , err )
84
+ }
85
+ err = os .Remove (rm .initCrdFile .Name ())
86
+ if err != nil {
87
+ t .Fatalf ("failed to remove initial CRD manifest: %v" , err )
88
+ }
89
+ err = os .Remove (rm .initCrFile .Name ())
90
+ if err != nil {
91
+ t .Fatalf ("failed to remove initial CR manifest: %v" , err )
92
+ }
93
+ err = os .Remove (rm .newCrdFile .Name ())
94
+ if err != nil {
95
+ t .Fatalf ("failed to remove new CRD manifest: %v" , err )
96
+ }
97
+ err = os .Remove (rm .newCrFile .Name ())
98
+ if err != nil {
99
+ t .Fatalf ("failed to remove new CR manifest: %v" , err )
100
+ }
101
+ klog .InfoS ("deleted artefacts" , "crConfigFile" , rm .crConfigFile .Name (), "initCrdFile" , rm .initCrdFile .Name (), "initCrFile" , rm .initCrFile .Name (), "newCrdFile" , rm .newCrdFile .Name (), "newCrFile" , rm .newCrFile .Name ())
102
+ }
103
+
104
+ func (rm * resourceManager ) writeConfigFile (t * testing.T ) {
105
+ crConfig := getCRConfig ()
106
+ configFile := rm .crConfigFile .Name ()
107
+ err := os .WriteFile (configFile , []byte (crConfig ), 0600 /* rw------- */ )
108
+ if err != nil {
109
+ t .Fatalf ("cannot write to config file: %v" , err )
110
+ }
111
+ klog .InfoS ("populated cr config file" , "crConfigFile" , configFile )
112
+ }
113
+
114
+ func (rm * resourceManager ) writeResourceFiles (t * testing.T ) {
115
+ initCr := getCR ()
116
+ initCrd := getCRD ()
117
+
118
+ newCr := getNewCR ()
119
+ newCrd := getNewCRD ()
120
+ err := os .WriteFile (rm .initCrdFile .Name (), []byte (initCrd ), 0600 /* rw------- */ )
121
+ if err != nil {
122
+ t .Fatalf ("cannot write to initial crd file: %v" , err )
123
+ }
124
+ err = os .WriteFile (rm .initCrFile .Name (), []byte (initCr ), 0600 /* rw------- */ )
125
+ if err != nil {
126
+ t .Fatalf ("cannot write to initial cr file: %v" , err )
127
+ }
128
+ err = os .WriteFile (rm .newCrdFile .Name (), []byte (newCrd ), 0600 /* rw------- */ )
129
+ if err != nil {
130
+ t .Fatalf ("cannot write to new crd file: %v" , err )
131
+ }
132
+ err = os .WriteFile (rm .newCrFile .Name (), []byte (newCr ), 0600 /* rw------- */ )
133
+ if err != nil {
134
+ t .Fatalf ("cannot write to new cr file: %v" , err )
135
+ }
136
+ klog .InfoS ("created initial and new CR and CRD manifests" )
137
+ }
138
+
139
+ func TestVariableVKsDiscoveryAndResolution (t * testing.T ) {
140
+ rm := & resourceManager {}
141
+ // Create testdata.
142
+ rm .createConfigAndResourceFiles (t )
143
+
144
+ // Initialise options.
145
+ opts := options .NewOptions ()
146
+ cmd := options .InitCommand
147
+ opts .AddFlags (cmd )
148
+ klog .InfoS ("options" , "options" , opts )
69
149
70
150
// Delete artefacts.
71
- defer func () {
72
- err := os .Remove (crConfigFile .Name ())
73
- if err != nil {
74
- t .Fatalf ("failed to remove CR config: %v" , err )
75
- }
76
- err = os .Remove (initCrdFile .Name ())
77
- if err != nil {
78
- t .Fatalf ("failed to remove initial CRD manifest: %v" , err )
79
- }
80
- err = os .Remove (initCrFile .Name ())
81
- if err != nil {
82
- t .Fatalf ("failed to remove initial CR manifest: %v" , err )
83
- }
84
- err = os .Remove (newCrdFile .Name ())
85
- if err != nil {
86
- t .Fatalf ("failed to remove new CRD manifest: %v" , err )
87
- }
88
- err = os .Remove (newCrFile .Name ())
89
- if err != nil {
90
- t .Fatalf ("failed to remove new CR manifest: %v" , err )
91
- }
92
- klog .InfoS ("deleted artefacts" , "crConfigFile" , crConfigFile .Name (), "initCrdFile" , initCrdFile .Name (), "initCrFile" , initCrFile .Name (), "newCrdFile" , newCrdFile .Name (), "newCrFile" , newCrFile .Name ())
93
- }()
151
+ defer rm .removeResourceFiles (t )
94
152
95
153
// Populate options, and parse them.
96
- opts .CustomResourceConfigFile = crConfigFile .Name ()
154
+ opts .CustomResourceConfigFile = rm . crConfigFile .Name ()
97
155
opts .Kubeconfig = os .Getenv ("HOME" ) + "/.kube/config"
98
156
if err := opts .Parse (); err != nil {
99
157
t .Fatalf ("failed to parse options: %v" , err )
100
158
}
101
159
klog .InfoS ("parsed options" , "options" , opts )
102
160
103
161
// Write to the config file.
104
- crConfig := getCRConfig ()
105
- err = os .WriteFile (opts .CustomResourceConfigFile , []byte (crConfig ), 0600 /* rw------- */ )
106
- if err != nil {
107
- t .Fatalf ("cannot write to config file: %v" , err )
108
- }
109
- klog .InfoS ("populated cr config file" , "crConfigFile" , opts .CustomResourceConfigFile )
162
+ rm .writeConfigFile (t )
110
163
111
164
// Make the process asynchronous.
112
165
go internal .RunKubeStateMetricsWrapper (opts )
113
166
klog .InfoS ("started KSM" )
114
167
115
168
// Wait for port 8080 to come up.
116
- err = wait .PollUntilContextTimeout (context .TODO (), 1 * time .Second , 20 * time .Second , true , func (_ context.Context ) (bool , error ) {
169
+ err : = wait .PollUntilContextTimeout (context .TODO (), 1 * time .Second , 20 * time .Second , true , func (_ context.Context ) (bool , error ) {
117
170
conn , err := net .Dial ("tcp" , "localhost:8080" )
118
171
if err != nil {
119
172
return false , nil
@@ -130,35 +183,14 @@ func TestVariableVKsDiscoveryAndResolution(t *testing.T) {
130
183
klog .InfoS ("port 8080 up" )
131
184
132
185
// Create CRD and CR files.
133
- initCr := getCR ()
134
- initCrd := getCRD ()
135
-
136
- newCr := getNewCR ()
137
- newCrd := getNewCRD ()
138
- err = os .WriteFile (initCrdFile .Name (), []byte (initCrd ), 0600 /* rw------- */ )
139
- if err != nil {
140
- t .Fatalf ("cannot write to initial crd file: %v" , err )
141
- }
142
- err = os .WriteFile (initCrFile .Name (), []byte (initCr ), 0600 /* rw------- */ )
143
- if err != nil {
144
- t .Fatalf ("cannot write to initial cr file: %v" , err )
145
- }
146
- err = os .WriteFile (newCrdFile .Name (), []byte (newCrd ), 0600 /* rw------- */ )
147
- if err != nil {
148
- t .Fatalf ("cannot write to new crd file: %v" , err )
149
- }
150
- err = os .WriteFile (newCrFile .Name (), []byte (newCr ), 0600 /* rw------- */ )
151
- if err != nil {
152
- t .Fatalf ("cannot write to new cr file: %v" , err )
153
- }
154
- klog .InfoS ("created initial and new CR and CRD manifests" )
186
+ rm .writeResourceFiles (t )
155
187
156
188
// Apply initial CRD and CR to the cluster.
157
- err = exec .Command ("kubectl" , "apply" , "-f" , initCrdFile .Name ()).Run () //nolint:gosec
189
+ err = exec .Command ("kubectl" , "apply" , "-f" , rm . initCrdFile .Name ()).Run () //nolint:gosec
158
190
if err != nil {
159
191
t .Fatalf ("failed to apply initial crd: %v" , err )
160
192
}
161
- err = exec .Command ("kubectl" , "apply" , "-f" , initCrFile .Name ()).Run () //nolint:gosec
193
+ err = exec .Command ("kubectl" , "apply" , "-f" , rm . initCrFile .Name ()).Run () //nolint:gosec
162
194
if err != nil {
163
195
t .Fatalf ("failed to apply initial cr: %v" , err )
164
196
}
@@ -198,11 +230,11 @@ func TestVariableVKsDiscoveryAndResolution(t *testing.T) {
198
230
}
199
231
200
232
// Apply new CRD and CR to the cluster.
201
- err = exec .Command ("kubectl" , "apply" , "-f" , newCrdFile .Name ()).Run () //nolint:gosec
233
+ err = exec .Command ("kubectl" , "apply" , "-f" , rm . newCrdFile .Name ()).Run () //nolint:gosec
202
234
if err != nil {
203
235
t .Fatalf ("failed to apply new crd: %v" , err )
204
236
}
205
- err = exec .Command ("kubectl" , "apply" , "-f" , newCrFile .Name ()).Run () //nolint:gosec
237
+ err = exec .Command ("kubectl" , "apply" , "-f" , rm . newCrFile .Name ()).Run () //nolint:gosec
206
238
if err != nil {
207
239
t .Fatalf ("failed to apply new cr: %v" , err )
208
240
}
0 commit comments