Skip to content

Commit b47069b

Browse files
committed
Code cleanup - added more GO convention
1 parent 98f2c25 commit b47069b

File tree

1 file changed

+74
-121
lines changed

1 file changed

+74
-121
lines changed

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

Lines changed: 74 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,29 @@ import (
1717
"time"
1818
)
1919

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+
}
2543

2644
func SetHTTPHeader(req *http.Request) *http.Request {
2745
user := os.Getenv("HC_USERNAME")
@@ -52,23 +70,8 @@ func SetHTTPClient() *http.Client {
5270

5371
return client
5472
}
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)
7275
if err != nil {
7376
log.Fatal(err)
7477
}
@@ -83,31 +86,36 @@ func DoesTestVMExist(host string) bool {
8386
fmt.Println("Response Status:", resp.Status)
8487
fmt.Println("Response Body:", string(body))
8588

86-
return resp.StatusCode == http.StatusOK
89+
return resp, body
8790
}
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))
91101
if err != nil {
92102
log.Fatal(err)
93103
}
94104
req = SetHTTPHeader(req)
95105

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)
102107

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))
105113
if err != nil {
106114
log.Fatal(err)
107115
}
116+
req = SetHTTPHeader(req)
108117

109-
fmt.Println("Response Status:", resp.Status)
110-
fmt.Println("Response Body:", string(body))
118+
_, body := SendHTTPRequest(req, client)
111119

112120
var result []map[string]interface{}
113121
errr := json.Unmarshal(body, &result)
@@ -116,55 +124,28 @@ func IsTestVMRunning(host string) bool {
116124
}
117125
return result[0]["state"] != "SHUTOFF"
118126
}
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))
122130
if err != nil {
123131
log.Fatal(err)
124132
}
125133
req = SetHTTPHeader(req)
126134

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)
133136

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))
142137
return resp.StatusCode == http.StatusOK
143138
}
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))
148143
if err != nil {
149144
log.Fatal(err)
150145
}
151146
req = SetHTTPHeader(req)
152147

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)
168149

169150
var result []map[string]interface{}
170151
errr := json.Unmarshal(body, &result)
@@ -174,70 +155,40 @@ func IsBootOrderCorrect(host string) bool {
174155
return reflect.DeepEqual(result[0]["bootDevices"], expectedBootOrder)
175156
}
176157

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))
180162
if err != nil {
181163
log.Fatal(err)
182164
}
183165
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)
201167
// wait 30 seconds for VM to shutdown and then proceed with other cleanup tasks
202168
time.Sleep(30 * time.Second)
203169
}
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}
206172
payload := map[string]interface{}{
207173
"bootDevices": bootOrder,
208174
}
209175
data, err := json.Marshal(payload)
210176
if err != nil {
211177
log.Fatalf("Failed to marshal JSON: %v", err)
212178
}
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))
214181
if err != nil {
215182
log.Fatal(err)
216183
}
217184

218185
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)
235187
}
236188

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)
241192
}
242193

243194
func main() {
@@ -247,35 +198,37 @@ func main() {
247198
2. Cleanup environment
248199
Argument we are looking to pass is "cleanup" see test.yml workflow file for more information
249200
*/
201+
env := LoadEnv()
250202
host := os.Getenv("HC_HOST")
203+
client := SetHTTPClient()
251204
isCleanup := len(os.Args) > 1 && os.Args[1] == "cleanup"
252205
fmt.Println("Are we doing Cleanup:", isCleanup)
253206

254207
if isCleanup {
255-
CleanupEnv(host)
208+
CleanupEnv(host, client, env)
256209
} else {
257210
// We are doing env prepare here, make sure all the necessary entities are setup and present
258-
if !AreEnvVariablesLoaded() {
211+
if !AreEnvVariablesLoaded(env) {
259212
log.Fatal("Environment variables aren't loaded, check env file in /acceptance/setup directory")
260213
} else {
261214
fmt.Println("Environment variables are loaded correctly")
262215
}
263-
if !DoesTestVMExist(host) {
216+
if !DoesTestVMExist(host, client, env) {
264217
log.Fatal("Acceptance test VM is missing in your testing environment")
265218
} else {
266219
fmt.Println("Acceptance test VM is present in the testing environment")
267220
}
268-
if IsTestVMRunning(host) {
221+
if IsTestVMRunning(host, client, env) {
269222
log.Fatal("Acceptance test VM is RUNNING and should be turned off before the testing begins")
270223
} else {
271224
fmt.Println("Acceptance test VM is in the correct SHUTOFF state")
272225
}
273-
if !DoesVirtualDiskExist(host) {
226+
if !DoesVirtualDiskExist(host, client, env) {
274227
log.Fatal("Acceptance test Virtual disk is missing in your testing environment")
275228
} else {
276229
fmt.Println("Acceptance test Virtual disk is present in your testing environment")
277230
}
278-
if IsBootOrderCorrect(host) {
231+
if IsBootOrderCorrect(host, client, env) {
279232
log.Fatal("Acceptance test Boot order is incorrect on the test VM, should be disk followed by network interface")
280233
} else {
281234
fmt.Println("Acceptance test Boot order is in correct order")

0 commit comments

Comments
 (0)