Skip to content

Commit

Permalink
chore: add support for testpackage
Browse files Browse the repository at this point in the history
  • Loading branch information
remyleone committed Aug 28, 2024
1 parent 45558a0 commit f3c435d
Show file tree
Hide file tree
Showing 31 changed files with 499 additions and 460 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ linters:
- staticcheck #(megacheck): Staticcheck is a go vet on steroids, applying a ton of static analysis checks [fast: false, auto-fix: false]
- tenv # tenv is analyzer that detects using os.Setenv instead of t.Setenv since Go1.17 [fast: false, auto-fix: false]
- testifylint # Checks usage of github.com/stretchr/testify. [fast: false, auto-fix: false]
- testpackage # linter that makes you use a separate _test package [fast: true, auto-fix: false]
- thelper # thelper detects golang test helpers without t.Helper() call and checks the consistency of test helpers [fast: false, auto-fix: false]
- tparallel # tparallel detects inappropriate usage of t.Parallel() method in your Go test codes [fast: false, auto-fix: false]
- typecheck # Like the front-end of a Go compiler, parses and type-checks Go code [fast: false, auto-fix: false]
Expand Down
27 changes: 14 additions & 13 deletions api/instance/v1/image_utils_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package instance
package instance_test

import (
"strings"
"testing"

"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/internal/testhelpers"
"github.com/scaleway/scaleway-sdk-go/internal/testhelpers/httprecorder"
)
Expand All @@ -15,65 +16,65 @@ func TestWaitForImage(t *testing.T) {
testhelpers.AssertNoError(t, r.Stop()) // Make sure recorder is stopped once done with it
}()

instanceAPI := NewAPI(client)
instanceAPI := instance.NewAPI(client)
imageName := "backup"
image, cleanup := createImage(t, instanceAPI, imageName)
defer cleanup()

res, err := instanceAPI.WaitForImage(&WaitForImageRequest{
res, err := instanceAPI.WaitForImage(&instance.WaitForImageRequest{
ImageID: image.ID,
})

testhelpers.AssertNoError(t, err)
testhelpers.Equals(t, image.ID, res.ID)
testhelpers.Equals(t, ImageStateAvailable, res.State)
testhelpers.Equals(t, instance.ImageStateAvailable, res.State)
testhelpers.Equals(t, imageName, res.Name)
}

// createImage cis a helper that create an image.
// It return the newly created image and a cleanup function
func createImage(t *testing.T, instanceAPI *API, imageName string) (*Image, func()) {
func createImage(t *testing.T, instanceAPI *instance.API, imageName string) (*instance.Image, func()) {
t.Helper()
serverRes, err := instanceAPI.CreateServer(&CreateServerRequest{
serverRes, err := instanceAPI.CreateServer(&instance.CreateServerRequest{
CommercialType: "DEV1-M",
Image: "ubuntu_focal",
})
testhelpers.AssertNoError(t, err)

// Backup will create a snapshot for each volume + an image base on all snapshots.
backupRes, err := instanceAPI.ServerAction(&ServerActionRequest{
backupRes, err := instanceAPI.ServerAction(&instance.ServerActionRequest{
ServerID: serverRes.Server.ID,
Action: ServerActionBackup,
Action: instance.ServerActionBackup,
Name: &imageName,
})
testhelpers.AssertNoError(t, err)

tmp := strings.Split(backupRes.Task.HrefResult, "/")
imageID := tmp[2]
imageRes, err := instanceAPI.GetImage(&GetImageRequest{
imageRes, err := instanceAPI.GetImage(&instance.GetImageRequest{
ImageID: imageID,
})
testhelpers.AssertNoError(t, err)

return imageRes.Image, func() {
// Delete all created resources

err := instanceAPI.DeleteServer(&DeleteServerRequest{
err := instanceAPI.DeleteServer(&instance.DeleteServerRequest{
ServerID: serverRes.Server.ID,
})
testhelpers.AssertNoError(t, err)

err = instanceAPI.DeleteVolume(&DeleteVolumeRequest{
err = instanceAPI.DeleteVolume(&instance.DeleteVolumeRequest{
VolumeID: serverRes.Server.Volumes["0"].ID,
})
testhelpers.AssertNoError(t, err)

err = instanceAPI.DeleteImage(&DeleteImageRequest{
err = instanceAPI.DeleteImage(&instance.DeleteImageRequest{
ImageID: imageRes.Image.ID,
})
testhelpers.AssertNoError(t, err)

err = instanceAPI.DeleteSnapshot(&DeleteSnapshotRequest{
err = instanceAPI.DeleteSnapshot(&instance.DeleteSnapshotRequest{
SnapshotID: imageRes.Image.RootVolume.ID,
})
testhelpers.AssertNoError(t, err)
Expand Down
31 changes: 16 additions & 15 deletions api/instance/v1/instance_sdk_server_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package instance
package instance_test

import (
"testing"

"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/internal/testhelpers"
"github.com/scaleway/scaleway-sdk-go/internal/testhelpers/httprecorder"
"github.com/scaleway/scaleway-sdk-go/scw"
Expand All @@ -15,7 +16,7 @@ func TestServerUpdate(t *testing.T) {
testhelpers.AssertNoError(t, r.Stop()) // Make sure recorder is stopped once done with it
}()

instanceAPI := NewAPI(client)
instanceAPI := instance.NewAPI(client)

var (
serverID string
Expand All @@ -26,14 +27,14 @@ func TestServerUpdate(t *testing.T) {
commercialType = "START1-S"
image = "f974feac-abae-4365-b988-8ec7d1cec10d"
enableIPv6 = scw.BoolPtr(true)
bootType = BootTypeLocal
bootType = instance.BootTypeLocal
tags = []string{"foo", "bar"}
project = "14d2f7ae-9775-414c-9bed-6810e060d500"
)

t.Run("create server", func(t *testing.T) {
// Create server
createServerResponse, err := instanceAPI.CreateServer(&CreateServerRequest{
createServerResponse, err := instanceAPI.CreateServer(&instance.CreateServerRequest{
Zone: zone,
Name: name,
Project: &project,
Expand Down Expand Up @@ -66,7 +67,7 @@ func TestServerUpdate(t *testing.T) {

t.Run("create server with orga (deprecated)", func(t *testing.T) {
// Create server
createServerResponse, err := instanceAPI.CreateServer(&CreateServerRequest{
createServerResponse, err := instanceAPI.CreateServer(&instance.CreateServerRequest{
Zone: zone,
Name: name,
Organization: &project,
Expand All @@ -78,7 +79,7 @@ func TestServerUpdate(t *testing.T) {
testhelpers.Equals(t, project, createServerResponse.Server.Organization)

// Delete Server
err = instanceAPI.DeleteServer(&DeleteServerRequest{
err = instanceAPI.DeleteServer(&instance.DeleteServerRequest{
Zone: zone,
ServerID: createServerResponse.Server.ID,
})
Expand All @@ -92,7 +93,7 @@ func TestServerUpdate(t *testing.T) {
)

// Update server
updateServerResponse, err := instanceAPI.updateServer(&UpdateServerRequest{
updateServerResponse, err := instanceAPI.UpdateServer(&instance.UpdateServerRequest{
ServerID: serverID,
Zone: zone,
Name: &newName,
Expand All @@ -117,10 +118,10 @@ func TestServerUpdate(t *testing.T) {

t.Run("remove server volumes", func(t *testing.T) {
// Remove/detach volumes
updateServerResponse, err := instanceAPI.updateServer(&UpdateServerRequest{
updateServerResponse, err := instanceAPI.UpdateServer(&instance.UpdateServerRequest{
ServerID: serverID,
Zone: zone,
Volumes: &map[string]*VolumeServerTemplate{},
Volumes: &map[string]*instance.VolumeServerTemplate{},
})
testhelpers.AssertNoError(t, err)
testhelpers.Assert(t, updateServerResponse.Server != nil, "Should have server in response")
Expand All @@ -129,14 +130,14 @@ func TestServerUpdate(t *testing.T) {

t.Run("cleanup server and volume", func(t *testing.T) {
// Delete Server
err = instanceAPI.DeleteServer(&DeleteServerRequest{
err = instanceAPI.DeleteServer(&instance.DeleteServerRequest{
Zone: zone,
ServerID: serverID,
})
testhelpers.AssertNoError(t, err)

// Delete Volume
err = instanceAPI.DeleteVolume(&DeleteVolumeRequest{
err = instanceAPI.DeleteVolume(&instance.DeleteVolumeRequest{
Zone: zone,
VolumeID: volumeID,
})
Expand All @@ -151,12 +152,12 @@ func TestCreateServerWithIncorrectBody(t *testing.T) {
testhelpers.AssertNoError(t, r.Stop()) // Make sure recorder is stopped once done with it
}()

instanceAPI := NewAPI(client)
instanceAPI := instance.NewAPI(client)

zone := scw.ZoneFrPar1

// Create server
_, err = instanceAPI.CreateServer(&CreateServerRequest{
_, err = instanceAPI.CreateServer(&instance.CreateServerRequest{
Zone: zone,
})
testhelpers.Assert(t, err != nil, "This request should error")
Expand All @@ -169,9 +170,9 @@ func TestListServerMultipleZones(t *testing.T) {
testhelpers.AssertNoError(t, r.Stop()) // Make sure recorder is stopped once done with it
}()

instanceAPI := NewAPI(client)
instanceAPI := instance.NewAPI(client)

// Create server
_, err = instanceAPI.ListServers(&ListServersRequest{}, scw.WithZones(instanceAPI.Zones()...))
_, err = instanceAPI.ListServers(&instance.ListServersRequest{}, scw.WithZones(instanceAPI.Zones()...))
testhelpers.Assert(t, err == nil, "This request should not error: %s", err)
}
39 changes: 20 additions & 19 deletions api/instance/v1/instance_utils_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package instance
package instance_test

import (
"testing"

block "github.com/scaleway/scaleway-sdk-go/api/block/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/internal/testhelpers"
"github.com/scaleway/scaleway-sdk-go/internal/testhelpers/httprecorder"
"github.com/scaleway/scaleway-sdk-go/scw"
Expand All @@ -16,7 +17,7 @@ func TestInstanceHelpers(t *testing.T) {
testhelpers.AssertNoError(t, r.Stop()) // Make sure recorder is stopped once done with it
}()

instanceAPI := NewAPI(client)
instanceAPI := instance.NewAPI(client)

var (
serverID string
Expand All @@ -28,7 +29,7 @@ func TestInstanceHelpers(t *testing.T) {
)

t.Run("create server", func(t *testing.T) {
createServerResponse, err := instanceAPI.CreateServer(&CreateServerRequest{
createServerResponse, err := instanceAPI.CreateServer(&instance.CreateServerRequest{
Zone: zone,
Name: "instance_utils_test",
Project: &project,
Expand All @@ -44,15 +45,15 @@ func TestInstanceHelpers(t *testing.T) {

t.Run("test ip related functions", func(t *testing.T) {
// Create IP
createIPResponse, err := instanceAPI.CreateIP(&CreateIPRequest{
createIPResponse, err := instanceAPI.CreateIP(&instance.CreateIPRequest{
Zone: zone,
Project: &project,
})
testhelpers.AssertNoError(t, err)
ipID = createIPResponse.IP.ID

// Attach IP
ipAttachResponse, err := instanceAPI.AttachIP(&AttachIPRequest{
ipAttachResponse, err := instanceAPI.AttachIP(&instance.AttachIPRequest{
IP: ipID,
Zone: zone,
ServerID: serverID,
Expand All @@ -62,7 +63,7 @@ func TestInstanceHelpers(t *testing.T) {
testhelpers.Equals(t, serverID, ipAttachResponse.IP.Server.ID)

// Detach IP
ipDetachResponse, err := instanceAPI.DetachIP(&DetachIPRequest{
ipDetachResponse, err := instanceAPI.DetachIP(&instance.DetachIPRequest{
IP: ipID,
Zone: zone,
})
Expand All @@ -71,15 +72,15 @@ func TestInstanceHelpers(t *testing.T) {
testhelpers.Assert(t, nil == ipDetachResponse.IP.Server, "Server object should be nil for detached IP.")

// Delete IP
err = instanceAPI.DeleteIP(&DeleteIPRequest{
err = instanceAPI.DeleteIP(&instance.DeleteIPRequest{
Zone: zone,
IP: ipID,
})
testhelpers.AssertNoError(t, err)
})

t.Run("Test attach and detach volume", func(t *testing.T) {
detachVolumeResponse, err := instanceAPI.DetachVolume(&DetachVolumeRequest{
detachVolumeResponse, err := instanceAPI.DetachVolume(&instance.DetachVolumeRequest{
Zone: zone,
VolumeID: volumeID,
})
Expand All @@ -89,7 +90,7 @@ func TestInstanceHelpers(t *testing.T) {
testhelpers.Assert(t, detachVolumeResponse.Server.Volumes != nil, "Should have volumes in response")
testhelpers.Assert(t, len(detachVolumeResponse.Server.Volumes) == 0, "Server should have zero volumes after detaching")

attachVolumeResponse, err := instanceAPI.AttachVolume(&AttachVolumeRequest{
attachVolumeResponse, err := instanceAPI.AttachVolume(&instance.AttachVolumeRequest{
Zone: zone,
ServerID: serverID,
VolumeID: volumeID,
Expand All @@ -104,14 +105,14 @@ func TestInstanceHelpers(t *testing.T) {

t.Run("teardown: delete server and volume", func(t *testing.T) {
// Delete Server
err = instanceAPI.DeleteServer(&DeleteServerRequest{
err = instanceAPI.DeleteServer(&instance.DeleteServerRequest{
Zone: zone,
ServerID: serverID,
})
testhelpers.AssertNoError(t, err)

// Delete Volume
err = instanceAPI.DeleteVolume(&DeleteVolumeRequest{
err = instanceAPI.DeleteVolume(&instance.DeleteVolumeRequest{
Zone: zone,
VolumeID: volumeID,
})
Expand All @@ -126,7 +127,7 @@ func TestInstanceHelpers_BlockVolume(t *testing.T) {
testhelpers.AssertNoError(t, r.Stop()) // Make sure recorder is stopped once done with it
}()

instanceAPI := NewAPI(client)
instanceAPI := instance.NewAPI(client)
blockAPI := block.NewAPI(client)

var (
Expand Down Expand Up @@ -165,16 +166,16 @@ func TestInstanceHelpers_BlockVolume(t *testing.T) {

volumeID2 = createVolumeResponse.ID

createServerResponse, err := instanceAPI.CreateServer(&CreateServerRequest{
createServerResponse, err := instanceAPI.CreateServer(&instance.CreateServerRequest{
Zone: zone,
Name: "instance_utils_test",
Project: &project,
Image: image,
CommercialType: "PRO2-XXS",
Volumes: map[string]*VolumeServerTemplate{
Volumes: map[string]*instance.VolumeServerTemplate{
"0": {
ID: &volumeID,
VolumeType: VolumeVolumeTypeSbsVolume,
VolumeType: instance.VolumeVolumeTypeSbsVolume,
},
},
})
Expand All @@ -183,7 +184,7 @@ func TestInstanceHelpers_BlockVolume(t *testing.T) {
})

t.Run("Test attach and detach volume", func(t *testing.T) {
detachVolumeResponse, err := instanceAPI.DetachVolume(&DetachVolumeRequest{
detachVolumeResponse, err := instanceAPI.DetachVolume(&instance.DetachVolumeRequest{
Zone: zone,
VolumeID: volumeID,
})
Expand All @@ -193,7 +194,7 @@ func TestInstanceHelpers_BlockVolume(t *testing.T) {
testhelpers.Assert(t, detachVolumeResponse.Server.Volumes != nil, "Should have volumes in response")
testhelpers.Assert(t, len(detachVolumeResponse.Server.Volumes) == 0, "Server should have zero volumes after detaching")

attachVolumeResponse, err := instanceAPI.AttachVolume(&AttachVolumeRequest{
attachVolumeResponse, err := instanceAPI.AttachVolume(&instance.AttachVolumeRequest{
Zone: zone,
ServerID: serverID,
VolumeID: volumeID,
Expand All @@ -205,7 +206,7 @@ func TestInstanceHelpers_BlockVolume(t *testing.T) {
testhelpers.Assert(t, len(attachVolumeResponse.Server.Volumes) == 1, "Server should have one volumes after attaching")
testhelpers.Equals(t, volumeID, attachVolumeResponse.Server.Volumes["0"].ID)

attachVolumeResponse, err = instanceAPI.AttachVolume(&AttachVolumeRequest{
attachVolumeResponse, err = instanceAPI.AttachVolume(&instance.AttachVolumeRequest{
Zone: zone,
ServerID: serverID,
VolumeID: volumeID2,
Expand All @@ -220,7 +221,7 @@ func TestInstanceHelpers_BlockVolume(t *testing.T) {

t.Run("teardown: delete server and volume", func(t *testing.T) {
// Delete Server
err = instanceAPI.DeleteServer(&DeleteServerRequest{
err = instanceAPI.DeleteServer(&instance.DeleteServerRequest{
Zone: zone,
ServerID: serverID,
})
Expand Down
Loading

0 comments on commit f3c435d

Please sign in to comment.