Skip to content

Commit 281aa3e

Browse files
committed
Add integration tests for fix #44
1 parent afec93a commit 281aa3e

File tree

1 file changed

+94
-4
lines changed

1 file changed

+94
-4
lines changed

upcloud/service/service_test.go

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package service
22

33
import (
4-
"github.com/UpCloudLtd/upcloud-go-api/upcloud"
5-
"github.com/UpCloudLtd/upcloud-go-api/upcloud/client"
6-
"github.com/UpCloudLtd/upcloud-go-api/upcloud/request"
74
"log"
85
"os"
96
"reflect"
107
"strings"
118
"testing"
129
"time"
10+
11+
"github.com/UpCloudLtd/upcloud-go-api/upcloud"
12+
"github.com/UpCloudLtd/upcloud-go-api/upcloud/client"
13+
"github.com/UpCloudLtd/upcloud-go-api/upcloud/request"
1314
)
1415

1516
// The service object used by the tests
@@ -18,7 +19,7 @@ var svc *Service
1819
// TestMain is the main test method
1920
func TestMain(m *testing.M) {
2021
setup()
21-
retCode := m.Run()
22+
retCode := m.Run()
2223

2324
// Optionally perform teardown
2425
deleteResources := os.Getenv("UPCLOUD_GO_SDK_TEST_DELETE_RESOURCES")
@@ -137,6 +138,16 @@ func TestCreateModifyDeleteServer(t *testing.T) {
137138
serverDetails := createServer("TestCreateModifyDeleteServer")
138139
t.Logf("Server %s with UUID %s created", serverDetails.Title, serverDetails.UUID)
139140

141+
// Get details about the storage (UUID is required for testing)
142+
if len(serverDetails.StorageDevices) == 0 {
143+
t.Errorf("Server %s with UUID %s has no storages attached", serverDetails.Title, serverDetails.UUID)
144+
}
145+
146+
firstStorage := serverDetails.StorageDevices[0]
147+
storageUUID := firstStorage.UUID
148+
149+
t.Logf("First storage of server with UUID %s has UUID %s", serverDetails.UUID, storageUUID)
150+
140151
// Modify the server
141152
t.Log("Modifying the server ...")
142153

@@ -166,6 +177,76 @@ func TestCreateModifyDeleteServer(t *testing.T) {
166177
t.Logf("Deleting the server with UUID %s...", serverDetails.UUID)
167178
deleteServer(serverDetails.UUID)
168179
t.Log("Server is now deleted")
180+
181+
// Check if the storage still exists
182+
storages, err := svc.GetStorages(&request.GetStoragesRequest{
183+
Access: upcloud.StorageAccessPrivate,
184+
})
185+
handleError(err)
186+
187+
found := false
188+
189+
for _, storage := range storages.Storages {
190+
if storage.UUID == storageUUID {
191+
found = true
192+
break
193+
}
194+
}
195+
196+
if !found {
197+
t.Errorf("Storage with UUID %s not found. It should still exist after deleting server with UUID %s", storageUUID, serverDetails.UUID)
198+
}
199+
200+
t.Log("Storage still exists")
201+
}
202+
203+
// TestCreateDeleteServerAndStorage performs the following actions:
204+
//
205+
// - creates a server
206+
// - deletes the server including storage
207+
func TestCreateDeleteServerAndStorage(t *testing.T) {
208+
if testing.Short() {
209+
t.Skip("Skipping test in short mode")
210+
}
211+
t.Parallel()
212+
213+
// Create a server
214+
serverDetails := createServer("TestCreateDeleteServerAndStorage")
215+
t.Logf("Server %s with UUID %s created", serverDetails.Title, serverDetails.UUID)
216+
217+
// Get details about the storage (UUID is required for testing)
218+
if len(serverDetails.StorageDevices) == 0 {
219+
t.Errorf("Server %s with UUID %s has no storages attached", serverDetails.Title, serverDetails.UUID)
220+
}
221+
222+
firstStorage := serverDetails.StorageDevices[0]
223+
storageUUID := firstStorage.UUID
224+
225+
t.Logf("First storage of server with UUID %s has UUID %s", serverDetails.UUID, storageUUID)
226+
227+
// Stop the server
228+
t.Logf("Stopping server with UUID %s ...", serverDetails.UUID)
229+
stopServer(serverDetails.UUID)
230+
t.Log("Server is now stopped")
231+
232+
// Delete the server and storage
233+
t.Logf("Deleting the server with UUID %s, including storages...", serverDetails.UUID)
234+
deleteServerAndStorages(serverDetails.UUID)
235+
t.Log("Server is now deleted")
236+
237+
// Check if the storage was deleted
238+
storages, err := svc.GetStorages(&request.GetStoragesRequest{
239+
Access: upcloud.StorageAccessPrivate,
240+
})
241+
handleError(err)
242+
243+
for _, storage := range storages.Storages {
244+
if storage.UUID == storageUUID {
245+
t.Errorf("Storage with UUID %s still exists. It should have been deleted with server with UUID %s", storageUUID, serverDetails.UUID)
246+
}
247+
}
248+
249+
t.Log("Storage was deleted, too")
169250
}
170251

171252
// TestCreateModifyDeleteStorage performs the following actions:
@@ -683,6 +764,15 @@ func deleteServer(uuid string) {
683764
handleError(err)
684765
}
685766

767+
// Deletes the specified server and storages
768+
func deleteServerAndStorages(uuid string) {
769+
err := svc.DeleteServerAndStorages(&request.DeleteServerAndStoragesRequest{
770+
UUID: uuid,
771+
})
772+
773+
handleError(err)
774+
}
775+
686776
// Creates a piece of storage and returns the details about it, panic if creation fails
687777
func createStorage() *upcloud.StorageDetails {
688778
createStorageRequest := request.CreateStorageRequest{

0 commit comments

Comments
 (0)