@@ -73,21 +73,39 @@ func SetHTTPClient() *http.Client {
73
73
func SendHTTPRequest (request * http.Request , client * http.Client ) (* http.Response , []byte ) {
74
74
resp , err := client .Do (request )
75
75
if err != nil {
76
- log .Fatal ( err )
76
+ log .Fatalf ( "Sending request failed with %v" , err )
77
77
}
78
78
defer resp .Body .Close ()
79
79
80
80
// Read and print the response
81
81
body , err := io .ReadAll (resp .Body )
82
82
if err != nil {
83
- log .Fatal ( err )
83
+ log .Fatalf ( "Reading request response body failed with %v" , err )
84
84
}
85
85
86
86
fmt .Println ("Response Status:" , resp .Status )
87
87
fmt .Println ("Response Body:" , string (body ))
88
88
89
89
return resp , body
90
90
}
91
+ func SetHTTPRequest (method string , url string , data []byte ) * http.Request {
92
+ var req * http.Request
93
+ var err error
94
+
95
+ // Set request method and body
96
+ if method == "GET" {
97
+ req , err = http .NewRequest ("GET" , url , bytes .NewBuffer (nil ))
98
+ } else {
99
+ req , err = http .NewRequest ("POST" , url , bytes .NewBuffer (data ))
100
+ }
101
+
102
+ // Handle any errors that occur
103
+ if err != nil {
104
+ log .Fatalf ("%s request to url: %s failed with error: %v" , method , url , err )
105
+ }
106
+
107
+ return req
108
+ }
91
109
92
110
func AreEnvVariablesLoaded (env EnvConfig ) bool {
93
111
if env .SourceVmUUID == "" || env .ExistingVdiskUUID == "" || env .SourceVmName == "" || env .SourceDiskUUID == "" || env .SourceNicUUID == "" {
@@ -97,10 +115,8 @@ func AreEnvVariablesLoaded(env EnvConfig) bool {
97
115
}
98
116
func DoesTestVMExist (host string , client * http.Client , env EnvConfig ) bool {
99
117
url := fmt .Sprintf ("%s%s%s" , host , VirDomainEndpoint , env .SourceVmUUID )
100
- req , err := http .NewRequest ("GET" , url , bytes .NewBuffer (nil ))
101
- if err != nil {
102
- log .Fatal (err )
103
- }
118
+
119
+ req := SetHTTPRequest ("GET" , url , nil )
104
120
req = SetHTTPHeader (req )
105
121
106
122
resp , _ := SendHTTPRequest (req , client )
@@ -109,27 +125,23 @@ func DoesTestVMExist(host string, client *http.Client, env EnvConfig) bool {
109
125
}
110
126
func IsTestVMRunning (host string , client * http.Client , env EnvConfig ) bool {
111
127
url := fmt .Sprintf ("%s%s%s" , host , VirDomainEndpoint , env .SourceVmUUID )
112
- req , err := http .NewRequest ("GET" , url , bytes .NewBuffer (nil ))
113
- if err != nil {
114
- log .Fatal (err )
115
- }
128
+
129
+ req := SetHTTPRequest ("GET" , url , nil )
116
130
req = SetHTTPHeader (req )
117
131
118
132
_ , body := SendHTTPRequest (req , client )
119
133
120
134
var result []map [string ]interface {}
121
- errr := json .Unmarshal (body , & result )
122
- if errr != nil {
123
- log .Fatal (errr )
135
+ err := json .Unmarshal (body , & result )
136
+ if err != nil {
137
+ log .Fatal (err )
124
138
}
125
139
return result [0 ]["state" ] != "SHUTOFF"
126
140
}
127
141
func DoesVirtualDiskExist (host string , client * http.Client , env EnvConfig ) bool {
128
142
url := fmt .Sprintf ("%s%s%s" , host , VirtualDiskEndpoint , env .ExistingVdiskUUID )
129
- req , err := http .NewRequest ("GET" , url , bytes .NewBuffer (nil ))
130
- if err != nil {
131
- log .Fatal (err )
132
- }
143
+
144
+ req := SetHTTPRequest ("GET" , url , nil )
133
145
req = SetHTTPHeader (req )
134
146
135
147
resp , _ := SendHTTPRequest (req , client )
@@ -139,29 +151,52 @@ func DoesVirtualDiskExist(host string, client *http.Client, env EnvConfig) bool
139
151
func IsBootOrderCorrect (host string , client * http.Client , env EnvConfig ) bool {
140
152
expectedBootOrder := []string {env .SourceDiskUUID , env .SourceNicUUID }
141
153
url := fmt .Sprintf ("%s%s%s" , host , VirDomainEndpoint , env .SourceVmUUID )
142
- req , err := http .NewRequest ("GET" , url , bytes .NewBuffer (nil ))
143
- if err != nil {
144
- log .Fatal (err )
145
- }
154
+
155
+ req := SetHTTPRequest ("GET" , url , nil )
146
156
req = SetHTTPHeader (req )
147
157
148
158
_ , body := SendHTTPRequest (req , client )
149
159
150
160
var result []map [string ]interface {}
151
- errr := json .Unmarshal (body , & result )
152
- if errr != nil {
153
- log .Fatal (errr )
161
+ err := json .Unmarshal (body , & result )
162
+ if err != nil {
163
+ log .Fatal (err )
154
164
}
155
165
return reflect .DeepEqual (result [0 ]["bootDevices" ], expectedBootOrder )
156
166
}
167
+ func PrepareEnv (host string , client * http.Client , env EnvConfig ) {
168
+ // We are doing env prepare here, make sure all the necessary entities are setup and present
169
+ if ! AreEnvVariablesLoaded (env ) {
170
+ log .Fatal ("Environment variables aren't loaded, check env file in /acceptance/setup directory" )
171
+ } else {
172
+ fmt .Println ("Environment variables are loaded correctly" )
173
+ }
174
+ if ! DoesTestVMExist (host , client , env ) {
175
+ log .Fatal ("Acceptance test VM is missing in your testing environment" )
176
+ } else {
177
+ fmt .Println ("Acceptance test VM is present in the testing environment" )
178
+ }
179
+ if IsTestVMRunning (host , client , env ) {
180
+ log .Fatal ("Acceptance test VM is RUNNING and should be turned off before the testing begins" )
181
+ } else {
182
+ fmt .Println ("Acceptance test VM is in the correct SHUTOFF state" )
183
+ }
184
+ if ! DoesVirtualDiskExist (host , client , env ) {
185
+ log .Fatal ("Acceptance test Virtual disk is missing in your testing environment" )
186
+ } else {
187
+ fmt .Println ("Acceptance test Virtual disk is present in your testing environment" )
188
+ }
189
+ if IsBootOrderCorrect (host , client , env ) {
190
+ log .Fatal ("Acceptance test Boot order is incorrect on the test VM, should be disk followed by network interface" )
191
+ } else {
192
+ fmt .Println ("Acceptance test Boot order is in correct order" )
193
+ }
194
+ }
157
195
158
196
func CleanUpPowerState (host string , client * http.Client , env EnvConfig ) {
159
197
data := []byte (fmt .Sprintf (`[{"virDomainUUID": "%s", "actionType": "STOP", "cause": "INTERNAL"}]` , env .SourceVmUUID ))
160
198
url := fmt .Sprintf ("%s%s" , host , VirDomainActionEndpoint )
161
- req , err := http .NewRequest ("POST" , url , bytes .NewBuffer (data ))
162
- if err != nil {
163
- log .Fatal (err )
164
- }
199
+ req := SetHTTPRequest ("POST" , url , data )
165
200
req = SetHTTPHeader (req )
166
201
SendHTTPRequest (req , client )
167
202
// wait 30 seconds for VM to shutdown and then proceed with other cleanup tasks
@@ -177,15 +212,10 @@ func CleanUpBootOrder(host string, client *http.Client, env EnvConfig) {
177
212
log .Fatalf ("Failed to marshal JSON: %v" , err )
178
213
}
179
214
url := fmt .Sprintf ("%s%s%s" , host , VirDomainEndpoint , env .SourceVmUUID )
180
- req , err := http .NewRequest ("POST" , url , bytes .NewBuffer (data ))
181
- if err != nil {
182
- log .Fatal (err )
183
- }
184
-
215
+ req := SetHTTPRequest ("POST" , url , data )
185
216
req = SetHTTPHeader (req )
186
217
SendHTTPRequest (req , client )
187
218
}
188
-
189
219
func CleanupEnv (host string , client * http.Client , env EnvConfig ) {
190
220
CleanUpPowerState (host , client , env )
191
221
CleanUpBootOrder (host , client , env )
@@ -207,31 +237,6 @@ func main() {
207
237
if isCleanup {
208
238
CleanupEnv (host , client , env )
209
239
} else {
210
- // We are doing env prepare here, make sure all the necessary entities are setup and present
211
- if ! AreEnvVariablesLoaded (env ) {
212
- log .Fatal ("Environment variables aren't loaded, check env file in /acceptance/setup directory" )
213
- } else {
214
- fmt .Println ("Environment variables are loaded correctly" )
215
- }
216
- if ! DoesTestVMExist (host , client , env ) {
217
- log .Fatal ("Acceptance test VM is missing in your testing environment" )
218
- } else {
219
- fmt .Println ("Acceptance test VM is present in the testing environment" )
220
- }
221
- if IsTestVMRunning (host , client , env ) {
222
- log .Fatal ("Acceptance test VM is RUNNING and should be turned off before the testing begins" )
223
- } else {
224
- fmt .Println ("Acceptance test VM is in the correct SHUTOFF state" )
225
- }
226
- if ! DoesVirtualDiskExist (host , client , env ) {
227
- log .Fatal ("Acceptance test Virtual disk is missing in your testing environment" )
228
- } else {
229
- fmt .Println ("Acceptance test Virtual disk is present in your testing environment" )
230
- }
231
- if IsBootOrderCorrect (host , client , env ) {
232
- log .Fatal ("Acceptance test Boot order is incorrect on the test VM, should be disk followed by network interface" )
233
- } else {
234
- fmt .Println ("Acceptance test Boot order is in correct order" )
235
- }
240
+ PrepareEnv (host , client , env )
236
241
}
237
242
}
0 commit comments