@@ -17,11 +17,29 @@ import (
17
17
"time"
18
18
)
19
19
20
- var source_vm_uuid = os .Getenv ("SOURCE_VM_UUID" )
21
- var existing_vdisk_uuid = os .Getenv ("EXISTING_VDISK_UUID" )
22
- var source_vm_name = os .Getenv ("SOURCE_VM_NAME" )
23
- var source_disk_uuid = os .Getenv ("SOURCE_DISK_UUID" )
24
- var source_nic_uuid = os .Getenv ("SOURCE_NIC_UUID" )
20
+ type EnvConfig struct {
21
+ SourceVmUUID string
22
+ ExistingVdiskUUID string
23
+ SourceVmName string
24
+ SourceDiskUUID string
25
+ SourceNicUUID string
26
+ }
27
+
28
+ const (
29
+ VirDomainEndpoint = "/rest/v1/VirDomain/"
30
+ VirtualDiskEndpoint = "/rest/v1/VirtualDisk/"
31
+ VirDomainActionEndpoint = "/rest/v1/VirDomain/action"
32
+ )
33
+
34
+ func LoadEnv () EnvConfig {
35
+ return EnvConfig {
36
+ SourceVmUUID : os .Getenv ("SOURCE_VM_UUID" ),
37
+ ExistingVdiskUUID : os .Getenv ("EXISTING_VDISK_UUID" ),
38
+ SourceVmName : os .Getenv ("SOURCE_VM_NAME" ),
39
+ SourceDiskUUID : os .Getenv ("SOURCE_DISK_UUID" ),
40
+ SourceNicUUID : os .Getenv ("SOURCE_NIC_UUID" ),
41
+ }
42
+ }
25
43
26
44
func SetHTTPHeader (req * http.Request ) * http.Request {
27
45
user := os .Getenv ("HC_USERNAME" )
@@ -52,23 +70,8 @@ func SetHTTPClient() *http.Client {
52
70
53
71
return client
54
72
}
55
-
56
- func AreEnvVariablesLoaded () bool {
57
- if source_vm_uuid == "" || existing_vdisk_uuid == "" || source_vm_name == "" {
58
- return false
59
- }
60
- return true
61
- }
62
- func DoesTestVMExist (host string ) bool {
63
- client := SetHTTPClient ()
64
- req , err := http .NewRequest ("GET" , fmt .Sprintf ("%s/rest/v1/VirDomain/%s" , host , source_vm_uuid ), bytes .NewBuffer (nil ))
65
- if err != nil {
66
- log .Fatal (err )
67
- }
68
- req = SetHTTPHeader (req )
69
-
70
- // Execute the request
71
- resp , err := client .Do (req )
73
+ func SendHTTPRequest (request * http.Request , client * http.Client ) (* http.Response , []byte ) {
74
+ resp , err := client .Do (request )
72
75
if err != nil {
73
76
log .Fatal (err )
74
77
}
@@ -83,31 +86,36 @@ func DoesTestVMExist(host string) bool {
83
86
fmt .Println ("Response Status:" , resp .Status )
84
87
fmt .Println ("Response Body:" , string (body ))
85
88
86
- return resp . StatusCode == http . StatusOK
89
+ return resp , body
87
90
}
88
- func IsTestVMRunning (host string ) bool {
89
- client := SetHTTPClient ()
90
- req , err := http .NewRequest ("GET" , fmt .Sprintf ("%s/rest/v1/VirDomain/%s" , host , source_vm_uuid ), bytes .NewBuffer (nil ))
91
+
92
+ func AreEnvVariablesLoaded (env EnvConfig ) bool {
93
+ if env .SourceVmUUID == "" || env .ExistingVdiskUUID == "" || env .SourceVmName == "" || env .SourceDiskUUID == "" || env .SourceNicUUID == "" {
94
+ return false
95
+ }
96
+ return true
97
+ }
98
+ func DoesTestVMExist (host string , client * http.Client , env EnvConfig ) bool {
99
+ url := fmt .Sprintf ("%s%s%s" , host , VirDomainEndpoint , env .SourceVmUUID )
100
+ req , err := http .NewRequest ("GET" , url , bytes .NewBuffer (nil ))
91
101
if err != nil {
92
102
log .Fatal (err )
93
103
}
94
104
req = SetHTTPHeader (req )
95
105
96
- // Execute the request
97
- resp , err := client .Do (req )
98
- if err != nil {
99
- log .Fatal (err )
100
- }
101
- defer resp .Body .Close ()
106
+ resp , _ := SendHTTPRequest (req , client )
102
107
103
- // Read and print the response
104
- body , err := io .ReadAll (resp .Body )
108
+ return resp .StatusCode == http .StatusOK
109
+ }
110
+ func IsTestVMRunning (host string , client * http.Client , env EnvConfig ) bool {
111
+ url := fmt .Sprintf ("%s%s%s" , host , VirDomainEndpoint , env .SourceVmUUID )
112
+ req , err := http .NewRequest ("GET" , url , bytes .NewBuffer (nil ))
105
113
if err != nil {
106
114
log .Fatal (err )
107
115
}
116
+ req = SetHTTPHeader (req )
108
117
109
- fmt .Println ("Response Status:" , resp .Status )
110
- fmt .Println ("Response Body:" , string (body ))
118
+ _ , body := SendHTTPRequest (req , client )
111
119
112
120
var result []map [string ]interface {}
113
121
errr := json .Unmarshal (body , & result )
@@ -116,55 +124,28 @@ func IsTestVMRunning(host string) bool {
116
124
}
117
125
return result [0 ]["state" ] != "SHUTOFF"
118
126
}
119
- func DoesVirtualDiskExist (host string ) bool {
120
- client := SetHTTPClient ( )
121
- req , err := http .NewRequest ("GET" , fmt . Sprintf ( "%s/rest/v1/VirtualDisk/%s" , host , existing_vdisk_uuid ) , bytes .NewBuffer (nil ))
127
+ func DoesVirtualDiskExist (host string , client * http. Client , env EnvConfig ) bool {
128
+ url := fmt . Sprintf ( "%s%s%s" , host , VirtualDiskEndpoint , env . ExistingVdiskUUID )
129
+ req , err := http .NewRequest ("GET" , url , bytes .NewBuffer (nil ))
122
130
if err != nil {
123
131
log .Fatal (err )
124
132
}
125
133
req = SetHTTPHeader (req )
126
134
127
- // Execute the request
128
- resp , err := client .Do (req )
129
- if err != nil {
130
- log .Fatal (err )
131
- }
132
- defer resp .Body .Close ()
135
+ resp , _ := SendHTTPRequest (req , client )
133
136
134
- // Read and print the response
135
- body , err := io .ReadAll (resp .Body )
136
- if err != nil {
137
- log .Fatal (err )
138
- }
139
-
140
- fmt .Println ("Response Status:" , resp .Status )
141
- fmt .Println ("Response Body:" , string (body ))
142
137
return resp .StatusCode == http .StatusOK
143
138
}
144
- func IsBootOrderCorrect (host string ) bool {
145
- expectedBootOrder := []string {source_disk_uuid , source_nic_uuid }
146
- client := SetHTTPClient ( )
147
- req , err := http .NewRequest ("GET" , fmt . Sprintf ( "%s/rest/v1/VirDomain/%s" , host , source_vm_uuid ) , bytes .NewBuffer (nil ))
139
+ func IsBootOrderCorrect (host string , client * http. Client , env EnvConfig ) bool {
140
+ expectedBootOrder := []string {env . SourceDiskUUID , env . SourceNicUUID }
141
+ url := fmt . Sprintf ( "%s%s%s" , host , VirDomainEndpoint , env . SourceVmUUID )
142
+ req , err := http .NewRequest ("GET" , url , bytes .NewBuffer (nil ))
148
143
if err != nil {
149
144
log .Fatal (err )
150
145
}
151
146
req = SetHTTPHeader (req )
152
147
153
- // Execute the request
154
- resp , err := client .Do (req )
155
- if err != nil {
156
- log .Fatal (err )
157
- }
158
- defer resp .Body .Close ()
159
-
160
- // Read and print the response
161
- body , err := io .ReadAll (resp .Body )
162
- if err != nil {
163
- log .Fatal (err )
164
- }
165
-
166
- fmt .Println ("Response Status:" , resp .Status )
167
- fmt .Println ("Response Body:" , string (body ))
148
+ _ , body := SendHTTPRequest (req , client )
168
149
169
150
var result []map [string ]interface {}
170
151
errr := json .Unmarshal (body , & result )
@@ -174,70 +155,40 @@ func IsBootOrderCorrect(host string) bool {
174
155
return reflect .DeepEqual (result [0 ]["bootDevices" ], expectedBootOrder )
175
156
}
176
157
177
- func CleanUpPowerState (host string , client * http.Client ) {
178
- data := []byte (fmt .Sprintf (`[{"virDomainUUID": "%s", "actionType": "STOP", "cause": "INTERNAL"}]` , source_vm_uuid ))
179
- req , err := http .NewRequest ("POST" , fmt .Sprintf ("%s/rest/v1/VirDomain/action" , host ), bytes .NewBuffer (data ))
158
+ func CleanUpPowerState (host string , client * http.Client , env EnvConfig ) {
159
+ data := []byte (fmt .Sprintf (`[{"virDomainUUID": "%s", "actionType": "STOP", "cause": "INTERNAL"}]` , env .SourceVmUUID ))
160
+ url := fmt .Sprintf ("%s%s" , host , VirDomainActionEndpoint )
161
+ req , err := http .NewRequest ("POST" , url , bytes .NewBuffer (data ))
180
162
if err != nil {
181
163
log .Fatal (err )
182
164
}
183
165
req = SetHTTPHeader (req )
184
-
185
- // Execute the request
186
- resp , err := client .Do (req )
187
- if err != nil {
188
- log .Fatal (err )
189
- }
190
- defer resp .Body .Close ()
191
-
192
- // Read and print the response
193
- body , err := io .ReadAll (resp .Body )
194
- if err != nil {
195
- log .Fatal (err )
196
- }
197
-
198
- fmt .Println ("Response Status:" , resp .Status )
199
- fmt .Println ("Response Body:" , string (body ))
200
-
166
+ SendHTTPRequest (req , client )
201
167
// wait 30 seconds for VM to shutdown and then proceed with other cleanup tasks
202
168
time .Sleep (30 * time .Second )
203
169
}
204
- func CleanUpBootOrder (host string , client * http.Client ) {
205
- bootOrder := []string {source_disk_uuid , source_nic_uuid }
170
+ func CleanUpBootOrder (host string , client * http.Client , env EnvConfig ) {
171
+ bootOrder := []string {env . SourceDiskUUID , env . SourceNicUUID }
206
172
payload := map [string ]interface {}{
207
173
"bootDevices" : bootOrder ,
208
174
}
209
175
data , err := json .Marshal (payload )
210
176
if err != nil {
211
177
log .Fatalf ("Failed to marshal JSON: %v" , err )
212
178
}
213
- req , err := http .NewRequest ("POST" , fmt .Sprintf ("%s/rest/v1/VirDomain/%s" , host , source_vm_uuid ), bytes .NewBuffer (data ))
179
+ url := fmt .Sprintf ("%s%s%s" , host , VirDomainEndpoint , env .SourceVmUUID )
180
+ req , err := http .NewRequest ("POST" , url , bytes .NewBuffer (data ))
214
181
if err != nil {
215
182
log .Fatal (err )
216
183
}
217
184
218
185
req = SetHTTPHeader (req )
219
-
220
- // Execute the request
221
- resp , err := client .Do (req )
222
- if err != nil {
223
- log .Fatal (err )
224
- }
225
- defer resp .Body .Close ()
226
-
227
- // Read and print the response
228
- body , err := io .ReadAll (resp .Body )
229
- if err != nil {
230
- log .Fatal (err )
231
- }
232
-
233
- fmt .Println ("Response Status:" , resp .Status )
234
- fmt .Println ("Response Body:" , string (body ))
186
+ SendHTTPRequest (req , client )
235
187
}
236
188
237
- func CleanupEnv (host string ) {
238
- client := SetHTTPClient ()
239
- CleanUpPowerState (host , client )
240
- CleanUpBootOrder (host , client )
189
+ func CleanupEnv (host string , client * http.Client , env EnvConfig ) {
190
+ CleanUpPowerState (host , client , env )
191
+ CleanUpBootOrder (host , client , env )
241
192
}
242
193
243
194
func main () {
@@ -247,35 +198,37 @@ func main() {
247
198
2. Cleanup environment
248
199
Argument we are looking to pass is "cleanup" see test.yml workflow file for more information
249
200
*/
201
+ env := LoadEnv ()
250
202
host := os .Getenv ("HC_HOST" )
203
+ client := SetHTTPClient ()
251
204
isCleanup := len (os .Args ) > 1 && os .Args [1 ] == "cleanup"
252
205
fmt .Println ("Are we doing Cleanup:" , isCleanup )
253
206
254
207
if isCleanup {
255
- CleanupEnv (host )
208
+ CleanupEnv (host , client , env )
256
209
} else {
257
210
// We are doing env prepare here, make sure all the necessary entities are setup and present
258
- if ! AreEnvVariablesLoaded () {
211
+ if ! AreEnvVariablesLoaded (env ) {
259
212
log .Fatal ("Environment variables aren't loaded, check env file in /acceptance/setup directory" )
260
213
} else {
261
214
fmt .Println ("Environment variables are loaded correctly" )
262
215
}
263
- if ! DoesTestVMExist (host ) {
216
+ if ! DoesTestVMExist (host , client , env ) {
264
217
log .Fatal ("Acceptance test VM is missing in your testing environment" )
265
218
} else {
266
219
fmt .Println ("Acceptance test VM is present in the testing environment" )
267
220
}
268
- if IsTestVMRunning (host ) {
221
+ if IsTestVMRunning (host , client , env ) {
269
222
log .Fatal ("Acceptance test VM is RUNNING and should be turned off before the testing begins" )
270
223
} else {
271
224
fmt .Println ("Acceptance test VM is in the correct SHUTOFF state" )
272
225
}
273
- if ! DoesVirtualDiskExist (host ) {
226
+ if ! DoesVirtualDiskExist (host , client , env ) {
274
227
log .Fatal ("Acceptance test Virtual disk is missing in your testing environment" )
275
228
} else {
276
229
fmt .Println ("Acceptance test Virtual disk is present in your testing environment" )
277
230
}
278
- if IsBootOrderCorrect (host ) {
231
+ if IsBootOrderCorrect (host , client , env ) {
279
232
log .Fatal ("Acceptance test Boot order is incorrect on the test VM, should be disk followed by network interface" )
280
233
} else {
281
234
fmt .Println ("Acceptance test Boot order is in correct order" )
0 commit comments