Skip to content

Commit 926ce0f

Browse files
committed
Added cleanup script into test env setup
1 parent 49b0003 commit 926ce0f

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ jobs:
117117
. ./internal/provider/tests/acceptance/setup/env.txt
118118
go run ./internal/provider/tests/acceptance/setup/acceptance_test_env_prepare.go
119119
go test -v -cover -coverpkg=github.com/hashicorp/terraform-provider-hypercore/internal/provider ./internal/provider/tests/acceptance/
120+
go run ./internal/provider/tests/acceptance/setup/acceptance_test_env_prepare.go "cleanup"
120121
timeout-minutes: 10
121122
unit-test:
122123
name: Go Unit Tests

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

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,22 +142,53 @@ func DoesVirtualDiskExist(host string) bool {
142142
return resp.StatusCode == http.StatusOK
143143
}
144144

145-
func main() {
146-
host := os.Getenv("HC_HOST")
147-
148-
if !AreEnvVariablesLoaded() {
149-
log.Fatal("Environment variables aren't loaded, check env file in /acceptance/setup directory")
145+
func CleanupEnv(host string) {
146+
client := SetHTTPClient()
147+
data := []byte(fmt.Sprintf(`[{"virDomainUUID": "%s", "actionType": "STOP", "cause": "INTERNAL"}]`, source_vm_uuid))
148+
req, err := http.NewRequest("POST", fmt.Sprintf("%s/rest/v1/VirDomain/action", host), bytes.NewBuffer(data))
149+
if err != nil {
150+
log.Fatal(err)
150151
}
152+
req = SetHTTPHeader(req)
151153

152-
if !DoesTestVMExist(host) {
153-
log.Fatal("Acceptance test VM is missing in your testing environment")
154+
// Execute the request
155+
resp, err := client.Do(req)
156+
if err != nil {
157+
log.Fatal(err)
154158
}
159+
defer resp.Body.Close()
155160

156-
if IsTestVMRunning(host) {
157-
log.Fatal("Acceptance test VM is RUNNING and should be turned off before the testing begins")
161+
// Read and print the response
162+
body, err := io.ReadAll(resp.Body)
163+
if err != nil {
164+
log.Fatal(err)
158165
}
159166

160-
if !DoesVirtualDiskExist(host) {
161-
log.Fatal("Acceptance test Virtual disk is missing in your testing environment")
167+
fmt.Println("Response Status:", resp.Status)
168+
fmt.Println("Response Body:", string(body))
169+
}
170+
171+
func main() {
172+
host := os.Getenv("HC_HOST")
173+
isCleanup := len(os.Args) < 1 && os.Args[1] == "cleanup"
174+
175+
if isCleanup {
176+
CleanupEnv(host)
177+
} else {
178+
if !AreEnvVariablesLoaded() {
179+
log.Fatal("Environment variables aren't loaded, check env file in /acceptance/setup directory")
180+
}
181+
182+
if !DoesTestVMExist(host) {
183+
log.Fatal("Acceptance test VM is missing in your testing environment")
184+
}
185+
186+
if IsTestVMRunning(host) {
187+
log.Fatal("Acceptance test VM is RUNNING and should be turned off before the testing begins")
188+
}
189+
190+
if !DoesVirtualDiskExist(host) {
191+
log.Fatal("Acceptance test Virtual disk is missing in your testing environment")
192+
}
162193
}
163194
}

0 commit comments

Comments
 (0)