Skip to content

Commit 56e1c0c

Browse files
committed
Code cleanup
1 parent b47069b commit 56e1c0c

File tree

1 file changed

+65
-60
lines changed

1 file changed

+65
-60
lines changed

internal/provider/tests/acceptance/setup/acceptance_test_env_prepare.go

Lines changed: 65 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,39 @@ func SetHTTPClient() *http.Client {
7373
func SendHTTPRequest(request *http.Request, client *http.Client) (*http.Response, []byte) {
7474
resp, err := client.Do(request)
7575
if err != nil {
76-
log.Fatal(err)
76+
log.Fatalf("Sending request failed with %v", err)
7777
}
7878
defer resp.Body.Close()
7979

8080
// Read and print the response
8181
body, err := io.ReadAll(resp.Body)
8282
if err != nil {
83-
log.Fatal(err)
83+
log.Fatalf("Reading request response body failed with %v", err)
8484
}
8585

8686
fmt.Println("Response Status:", resp.Status)
8787
fmt.Println("Response Body:", string(body))
8888

8989
return resp, body
9090
}
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+
}
91109

92110
func AreEnvVariablesLoaded(env EnvConfig) bool {
93111
if env.SourceVmUUID == "" || env.ExistingVdiskUUID == "" || env.SourceVmName == "" || env.SourceDiskUUID == "" || env.SourceNicUUID == "" {
@@ -97,10 +115,8 @@ func AreEnvVariablesLoaded(env EnvConfig) bool {
97115
}
98116
func DoesTestVMExist(host string, client *http.Client, env EnvConfig) bool {
99117
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)
104120
req = SetHTTPHeader(req)
105121

106122
resp, _ := SendHTTPRequest(req, client)
@@ -109,27 +125,23 @@ func DoesTestVMExist(host string, client *http.Client, env EnvConfig) bool {
109125
}
110126
func IsTestVMRunning(host string, client *http.Client, env EnvConfig) bool {
111127
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)
116130
req = SetHTTPHeader(req)
117131

118132
_, body := SendHTTPRequest(req, client)
119133

120134
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)
124138
}
125139
return result[0]["state"] != "SHUTOFF"
126140
}
127141
func DoesVirtualDiskExist(host string, client *http.Client, env EnvConfig) bool {
128142
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)
133145
req = SetHTTPHeader(req)
134146

135147
resp, _ := SendHTTPRequest(req, client)
@@ -139,29 +151,52 @@ func DoesVirtualDiskExist(host string, client *http.Client, env EnvConfig) bool
139151
func IsBootOrderCorrect(host string, client *http.Client, env EnvConfig) bool {
140152
expectedBootOrder := []string{env.SourceDiskUUID, env.SourceNicUUID}
141153
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)
146156
req = SetHTTPHeader(req)
147157

148158
_, body := SendHTTPRequest(req, client)
149159

150160
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)
154164
}
155165
return reflect.DeepEqual(result[0]["bootDevices"], expectedBootOrder)
156166
}
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+
}
157195

158196
func CleanUpPowerState(host string, client *http.Client, env EnvConfig) {
159197
data := []byte(fmt.Sprintf(`[{"virDomainUUID": "%s", "actionType": "STOP", "cause": "INTERNAL"}]`, env.SourceVmUUID))
160198
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)
165200
req = SetHTTPHeader(req)
166201
SendHTTPRequest(req, client)
167202
// 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) {
177212
log.Fatalf("Failed to marshal JSON: %v", err)
178213
}
179214
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)
185216
req = SetHTTPHeader(req)
186217
SendHTTPRequest(req, client)
187218
}
188-
189219
func CleanupEnv(host string, client *http.Client, env EnvConfig) {
190220
CleanUpPowerState(host, client, env)
191221
CleanUpBootOrder(host, client, env)
@@ -207,31 +237,6 @@ func main() {
207237
if isCleanup {
208238
CleanupEnv(host, client, env)
209239
} 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)
236241
}
237242
}

0 commit comments

Comments
 (0)