diff --git a/.golangci.yml b/.golangci.yml index 339eac19..17581639 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -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] diff --git a/api/instance/v1/image_utils_test.go b/api/instance/v1/image_utils_test.go index 42e7982e..b0b60079 100644 --- a/api/instance/v1/image_utils_test.go +++ b/api/instance/v1/image_utils_test.go @@ -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" ) @@ -15,42 +16,42 @@ 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) @@ -58,22 +59,22 @@ func createImage(t *testing.T, instanceAPI *API, imageName string) (*Image, func 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) diff --git a/api/instance/v1/instance_sdk_server_test.go b/api/instance/v1/instance_sdk_server_test.go index e8e1cf4e..d4b001ba 100644 --- a/api/instance/v1/instance_sdk_server_test.go +++ b/api/instance/v1/instance_sdk_server_test.go @@ -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" @@ -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 @@ -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, @@ -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, @@ -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, }) @@ -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, @@ -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") @@ -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, }) @@ -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") @@ -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) } diff --git a/api/instance/v1/instance_utils_test.go b/api/instance/v1/instance_utils_test.go index a799d761..22c0278f 100644 --- a/api/instance/v1/instance_utils_test.go +++ b/api/instance/v1/instance_utils_test.go @@ -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" @@ -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 @@ -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, @@ -44,7 +45,7 @@ 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, }) @@ -52,7 +53,7 @@ func TestInstanceHelpers(t *testing.T) { ipID = createIPResponse.IP.ID // Attach IP - ipAttachResponse, err := instanceAPI.AttachIP(&AttachIPRequest{ + ipAttachResponse, err := instanceAPI.AttachIP(&instance.AttachIPRequest{ IP: ipID, Zone: zone, ServerID: serverID, @@ -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, }) @@ -71,7 +72,7 @@ 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, }) @@ -79,7 +80,7 @@ func TestInstanceHelpers(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, }) @@ -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, @@ -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, }) @@ -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 ( @@ -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, }, }, }) @@ -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, }) @@ -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, @@ -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, @@ -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, }) diff --git a/api/instance/v1/security_group_utils_test.go b/api/instance/v1/security_group_utils_test.go index 30ffec00..aa2918f1 100644 --- a/api/instance/v1/security_group_utils_test.go +++ b/api/instance/v1/security_group_utils_test.go @@ -1,9 +1,10 @@ -package instance +package instance_test import ( "net" "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" @@ -16,22 +17,22 @@ func TestAPI_UpdateSecurityGroup(t *testing.T) { testhelpers.AssertNoError(t, r.Stop()) // Make sure recorder is stopped once done with it }() - instanceAPI := NewAPI(client) + instanceAPI := instance.NewAPI(client) - createResponse, err := instanceAPI.CreateSecurityGroup(&CreateSecurityGroupRequest{ + createResponse, err := instanceAPI.CreateSecurityGroup(&instance.CreateSecurityGroupRequest{ Name: "name", Description: "description", Stateful: true, - InboundDefaultPolicy: SecurityGroupPolicyAccept, - OutboundDefaultPolicy: SecurityGroupPolicyDrop, + InboundDefaultPolicy: instance.SecurityGroupPolicyAccept, + OutboundDefaultPolicy: instance.SecurityGroupPolicyDrop, }) testhelpers.AssertNoError(t, err) - accept := SecurityGroupPolicyAccept - drop := SecurityGroupPolicyDrop + accept := instance.SecurityGroupPolicyAccept + drop := instance.SecurityGroupPolicyDrop - updateResponse, err := instanceAPI.UpdateSecurityGroup(&UpdateSecurityGroupRequest{ + updateResponse, err := instanceAPI.UpdateSecurityGroup(&instance.UpdateSecurityGroupRequest{ SecurityGroupID: createResponse.SecurityGroup.ID, Name: scw.StringPtr("new_name"), Description: scw.StringPtr("new_description"), @@ -46,14 +47,14 @@ func TestAPI_UpdateSecurityGroup(t *testing.T) { testhelpers.AssertNoError(t, err) testhelpers.Equals(t, "new_name", updateResponse.SecurityGroup.Name) testhelpers.Equals(t, "new_description", updateResponse.SecurityGroup.Description) - testhelpers.Equals(t, SecurityGroupPolicyDrop, updateResponse.SecurityGroup.InboundDefaultPolicy) - testhelpers.Equals(t, SecurityGroupPolicyAccept, updateResponse.SecurityGroup.OutboundDefaultPolicy) + testhelpers.Equals(t, instance.SecurityGroupPolicyDrop, updateResponse.SecurityGroup.InboundDefaultPolicy) + testhelpers.Equals(t, instance.SecurityGroupPolicyAccept, updateResponse.SecurityGroup.OutboundDefaultPolicy) testhelpers.Equals(t, false, updateResponse.SecurityGroup.Stateful) testhelpers.Equals(t, false, updateResponse.SecurityGroup.ProjectDefault) testhelpers.Equals(t, false, *updateResponse.SecurityGroup.OrganizationDefault) testhelpers.Equals(t, []string{"foo", "bar"}, updateResponse.SecurityGroup.Tags) - err = instanceAPI.DeleteSecurityGroup(&DeleteSecurityGroupRequest{ + err = instanceAPI.DeleteSecurityGroup(&instance.DeleteSecurityGroupRequest{ SecurityGroupID: createResponse.SecurityGroup.ID, }) testhelpers.AssertNoError(t, err) @@ -66,34 +67,34 @@ func TestAPI_UpdateSecurityGroupRule(t *testing.T) { testhelpers.AssertNoError(t, r.Stop()) // Make sure recorder is stopped once done with it }() - instanceAPI := NewAPI(client) + instanceAPI := instance.NewAPI(client) - bootstrap := func(t *testing.T) (*SecurityGroup, *SecurityGroupRule, func()) { + bootstrap := func(t *testing.T) (*instance.SecurityGroup, *instance.SecurityGroupRule, func()) { t.Helper() - createSecurityGroupResponse, err := instanceAPI.CreateSecurityGroup(&CreateSecurityGroupRequest{ + createSecurityGroupResponse, err := instanceAPI.CreateSecurityGroup(&instance.CreateSecurityGroupRequest{ Name: "name", Description: "description", Stateful: true, - InboundDefaultPolicy: SecurityGroupPolicyAccept, - OutboundDefaultPolicy: SecurityGroupPolicyDrop, + InboundDefaultPolicy: instance.SecurityGroupPolicyAccept, + OutboundDefaultPolicy: instance.SecurityGroupPolicyDrop, }) testhelpers.AssertNoError(t, err) _, ipNet, _ := net.ParseCIDR("8.8.8.8/32") - createRuleResponse, err := instanceAPI.CreateSecurityGroupRule(&CreateSecurityGroupRuleRequest{ + createRuleResponse, err := instanceAPI.CreateSecurityGroupRule(&instance.CreateSecurityGroupRuleRequest{ SecurityGroupID: createSecurityGroupResponse.SecurityGroup.ID, - Direction: SecurityGroupRuleDirectionInbound, - Protocol: SecurityGroupRuleProtocolTCP, + Direction: instance.SecurityGroupRuleDirectionInbound, + Protocol: instance.SecurityGroupRuleProtocolTCP, DestPortFrom: scw.Uint32Ptr(1), DestPortTo: scw.Uint32Ptr(1024), IPRange: scw.IPNet{IPNet: *ipNet}, - Action: SecurityGroupRuleActionAccept, + Action: instance.SecurityGroupRuleActionAccept, Position: 1, }) testhelpers.AssertNoError(t, err) return createSecurityGroupResponse.SecurityGroup, createRuleResponse.Rule, func() { - err = instanceAPI.DeleteSecurityGroup(&DeleteSecurityGroupRequest{ + err = instanceAPI.DeleteSecurityGroup(&instance.DeleteSecurityGroupRequest{ SecurityGroupID: createSecurityGroupResponse.SecurityGroup.ID, }) testhelpers.AssertNoError(t, err) @@ -105,24 +106,24 @@ func TestAPI_UpdateSecurityGroupRule(t *testing.T) { defer cleanUp() _, ipNet, _ := net.ParseCIDR("1.1.1.1/32") - updateResponse, err := instanceAPI.UpdateSecurityGroupRule(&UpdateSecurityGroupRuleRequest{ + updateResponse, err := instanceAPI.UpdateSecurityGroupRule(&instance.UpdateSecurityGroupRuleRequest{ SecurityGroupID: group.ID, SecurityGroupRuleID: rule.ID, - Action: SecurityGroupRuleActionDrop, + Action: instance.SecurityGroupRuleActionDrop, IPRange: &scw.IPNet{IPNet: *ipNet}, DestPortFrom: scw.Uint32Ptr(1), DestPortTo: scw.Uint32Ptr(2048), - Protocol: SecurityGroupRuleProtocolUDP, - Direction: SecurityGroupRuleDirectionOutbound, + Protocol: instance.SecurityGroupRuleProtocolUDP, + Direction: instance.SecurityGroupRuleDirectionOutbound, }) testhelpers.AssertNoError(t, err) - testhelpers.Equals(t, SecurityGroupRuleActionDrop, updateResponse.Rule.Action) + testhelpers.Equals(t, instance.SecurityGroupRuleActionDrop, updateResponse.Rule.Action) testhelpers.Equals(t, scw.IPNet{IPNet: net.IPNet{IP: net.IPv4(0x1, 0x1, 0x1, 0x1), Mask: net.IPMask{0xff, 0xff, 0xff, 0xff}}}, updateResponse.Rule.IPRange) testhelpers.Equals(t, scw.Uint32Ptr(1), updateResponse.Rule.DestPortFrom) testhelpers.Equals(t, scw.Uint32Ptr(2048), updateResponse.Rule.DestPortTo) - testhelpers.Equals(t, SecurityGroupRuleProtocolUDP, updateResponse.Rule.Protocol) - testhelpers.Equals(t, SecurityGroupRuleDirectionOutbound, updateResponse.Rule.Direction) + testhelpers.Equals(t, instance.SecurityGroupRuleProtocolUDP, updateResponse.Rule.Protocol) + testhelpers.Equals(t, instance.SecurityGroupRuleDirectionOutbound, updateResponse.Rule.Direction) }) t.Run("From a port range to a single port", func(t *testing.T) { @@ -130,50 +131,50 @@ func TestAPI_UpdateSecurityGroupRule(t *testing.T) { defer cleanUp() _, ipNet, _ := net.ParseCIDR("1.1.1.1/32") - updateResponse, err := instanceAPI.UpdateSecurityGroupRule(&UpdateSecurityGroupRuleRequest{ + updateResponse, err := instanceAPI.UpdateSecurityGroupRule(&instance.UpdateSecurityGroupRuleRequest{ SecurityGroupID: group.ID, SecurityGroupRuleID: rule.ID, - Action: SecurityGroupRuleActionDrop, + Action: instance.SecurityGroupRuleActionDrop, IPRange: &scw.IPNet{IPNet: *ipNet}, DestPortFrom: scw.Uint32Ptr(22), DestPortTo: scw.Uint32Ptr(22), - Protocol: SecurityGroupRuleProtocolUDP, - Direction: SecurityGroupRuleDirectionOutbound, + Protocol: instance.SecurityGroupRuleProtocolUDP, + Direction: instance.SecurityGroupRuleDirectionOutbound, }) testhelpers.AssertNoError(t, err) - testhelpers.Equals(t, SecurityGroupRuleActionDrop, updateResponse.Rule.Action) + testhelpers.Equals(t, instance.SecurityGroupRuleActionDrop, updateResponse.Rule.Action) testhelpers.Equals(t, scw.IPNet{IPNet: net.IPNet{IP: net.IPv4(0x1, 0x1, 0x1, 0x1), Mask: net.IPMask{0xff, 0xff, 0xff, 0xff}}}, updateResponse.Rule.IPRange) testhelpers.Equals(t, uint32(22), *updateResponse.Rule.DestPortFrom) testhelpers.Equals(t, (*uint32)(nil), updateResponse.Rule.DestPortTo) - testhelpers.Equals(t, SecurityGroupRuleProtocolUDP, updateResponse.Rule.Protocol) - testhelpers.Equals(t, SecurityGroupRuleDirectionOutbound, updateResponse.Rule.Direction) + testhelpers.Equals(t, instance.SecurityGroupRuleProtocolUDP, updateResponse.Rule.Protocol) + testhelpers.Equals(t, instance.SecurityGroupRuleDirectionOutbound, updateResponse.Rule.Direction) }) t.Run("Switching to ICMP", func(t *testing.T) { group, rule, cleanUp := bootstrap(t) defer cleanUp() - updateResponse, err := instanceAPI.UpdateSecurityGroupRule(&UpdateSecurityGroupRuleRequest{ + updateResponse, err := instanceAPI.UpdateSecurityGroupRule(&instance.UpdateSecurityGroupRuleRequest{ SecurityGroupID: group.ID, SecurityGroupRuleID: rule.ID, - Protocol: SecurityGroupRuleProtocolICMP, + Protocol: instance.SecurityGroupRuleProtocolICMP, }) testhelpers.AssertNoError(t, err) - testhelpers.Equals(t, SecurityGroupRuleActionAccept, updateResponse.Rule.Action) + testhelpers.Equals(t, instance.SecurityGroupRuleActionAccept, updateResponse.Rule.Action) testhelpers.Equals(t, scw.IPNet{IPNet: net.IPNet{IP: net.IPv4(0x8, 0x8, 0x8, 0x8), Mask: net.IPMask{0xff, 0xff, 0xff, 0xff}}}, updateResponse.Rule.IPRange) testhelpers.Equals(t, (*uint32)(nil), updateResponse.Rule.DestPortFrom) testhelpers.Equals(t, (*uint32)(nil), updateResponse.Rule.DestPortTo) - testhelpers.Equals(t, SecurityGroupRuleProtocolICMP, updateResponse.Rule.Protocol) - testhelpers.Equals(t, SecurityGroupRuleDirectionInbound, updateResponse.Rule.Direction) + testhelpers.Equals(t, instance.SecurityGroupRuleProtocolICMP, updateResponse.Rule.Protocol) + testhelpers.Equals(t, instance.SecurityGroupRuleDirectionInbound, updateResponse.Rule.Direction) }) t.Run("Remove ports", func(t *testing.T) { group, rule, cleanUp := bootstrap(t) defer cleanUp() - updateResponse, err := instanceAPI.UpdateSecurityGroupRule(&UpdateSecurityGroupRuleRequest{ + updateResponse, err := instanceAPI.UpdateSecurityGroupRule(&instance.UpdateSecurityGroupRuleRequest{ SecurityGroupID: group.ID, SecurityGroupRuleID: rule.ID, DestPortFrom: scw.Uint32Ptr(0), @@ -181,11 +182,11 @@ func TestAPI_UpdateSecurityGroupRule(t *testing.T) { }) testhelpers.AssertNoError(t, err) - testhelpers.Equals(t, SecurityGroupRuleActionAccept, updateResponse.Rule.Action) + testhelpers.Equals(t, instance.SecurityGroupRuleActionAccept, updateResponse.Rule.Action) testhelpers.Equals(t, scw.IPNet{IPNet: net.IPNet{IP: net.IPv4(0x8, 0x8, 0x8, 0x8), Mask: net.IPMask{0xff, 0xff, 0xff, 0xff}}}, updateResponse.Rule.IPRange) testhelpers.Equals(t, (*uint32)(nil), updateResponse.Rule.DestPortFrom) testhelpers.Equals(t, (*uint32)(nil), updateResponse.Rule.DestPortTo) - testhelpers.Equals(t, SecurityGroupRuleProtocolTCP, updateResponse.Rule.Protocol) - testhelpers.Equals(t, SecurityGroupRuleDirectionInbound, updateResponse.Rule.Direction) + testhelpers.Equals(t, instance.SecurityGroupRuleProtocolTCP, updateResponse.Rule.Protocol) + testhelpers.Equals(t, instance.SecurityGroupRuleDirectionInbound, updateResponse.Rule.Direction) }) } diff --git a/api/instance/v1/server_utils_test.go b/api/instance/v1/server_utils_test.go index cfc475d2..0f4a864d 100644 --- a/api/instance/v1/server_utils_test.go +++ b/api/instance/v1/server_utils_test.go @@ -1,4 +1,4 @@ -package instance +package instance_test import ( "bytes" @@ -6,6 +6,7 @@ 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" "github.com/scaleway/scaleway-sdk-go/namegenerator" @@ -19,9 +20,9 @@ func TestAPI_GetServerType(t *testing.T) { testhelpers.AssertNoError(t, r.Stop()) // Make sure recorder is stopped once done with it }() - instanceAPI := NewAPI(client) + instanceAPI := instance.NewAPI(client) - serverType, err := instanceAPI.GetServerType(&GetServerTypeRequest{ + serverType, err := instanceAPI.GetServerType(&instance.GetServerTypeRequest{ Zone: scw.ZoneFrPar1, Name: "GP1-XS", }) @@ -38,12 +39,12 @@ func TestAPI_ServerUserData(t *testing.T) { testhelpers.AssertNoError(t, r.Stop()) // Make sure recorder is stopped once done with it }() - instanceAPI := NewAPI(client) + instanceAPI := instance.NewAPI(client) key := "hello" contentStr := "world" - serverRes, err := instanceAPI.CreateServer(&CreateServerRequest{ + serverRes, err := instanceAPI.CreateServer(&instance.CreateServerRequest{ Zone: scw.ZoneFrPar1, CommercialType: "DEV1-S", Name: namegenerator.GetRandomName("srv"), @@ -53,7 +54,7 @@ func TestAPI_ServerUserData(t *testing.T) { testhelpers.AssertNoError(t, err) content := strings.NewReader(contentStr) - err = instanceAPI.SetServerUserData(&SetServerUserDataRequest{ + err = instanceAPI.SetServerUserData(&instance.SetServerUserDataRequest{ Zone: scw.ZoneFrPar1, ServerID: serverRes.Server.ID, Key: key, @@ -61,7 +62,7 @@ func TestAPI_ServerUserData(t *testing.T) { }) testhelpers.AssertNoError(t, err) - data, err := instanceAPI.GetServerUserData(&GetServerUserDataRequest{ + data, err := instanceAPI.GetServerUserData(&instance.GetServerUserDataRequest{ Zone: scw.ZoneFrPar1, ServerID: serverRes.Server.ID, Key: key, @@ -80,9 +81,9 @@ func TestAPI_AllServerUserData(t *testing.T) { testhelpers.AssertNoError(t, r.Stop()) // Make sure recorder is stopped once done with it }() - instanceAPI := NewAPI(client) + instanceAPI := instance.NewAPI(client) - serverRes, err := instanceAPI.CreateServer(&CreateServerRequest{ + serverRes, err := instanceAPI.CreateServer(&instance.CreateServerRequest{ Zone: scw.ZoneFrPar1, CommercialType: "DEV1-S", Name: namegenerator.GetRandomName("srv"), @@ -116,7 +117,7 @@ func TestAPI_AllServerUserData(t *testing.T) { } // set all user data - err := instanceAPI.SetAllServerUserData(&SetAllServerUserDataRequest{ + err := instanceAPI.SetAllServerUserData(&instance.SetAllServerUserDataRequest{ Zone: scw.ZoneFrPar1, ServerID: serverRes.Server.ID, UserData: userData, @@ -124,7 +125,7 @@ func TestAPI_AllServerUserData(t *testing.T) { testhelpers.AssertNoError(t, err) // get all user data - allData, err := instanceAPI.GetAllServerUserData(&GetAllServerUserDataRequest{ + allData, err := instanceAPI.GetAllServerUserData(&instance.GetAllServerUserDataRequest{ Zone: scw.ZoneFrPar1, ServerID: serverRes.Server.ID, }) @@ -151,9 +152,9 @@ func TestAPI_CreateServer(t *testing.T) { testhelpers.AssertNoError(t, r.Stop()) // Make sure recorder is stopped once done with it }() - instanceAPI := NewAPI(client) + instanceAPI := instance.NewAPI(client) - res, err := instanceAPI.CreateServer(&CreateServerRequest{ + res, err := instanceAPI.CreateServer(&instance.CreateServerRequest{ Zone: scw.ZoneFrPar1, CommercialType: "GP1-XS", Image: "ubuntu_focal", @@ -162,7 +163,7 @@ func TestAPI_CreateServer(t *testing.T) { testhelpers.AssertNoError(t, err) // this UUID might change when running the cassette later when the image "ubuntu_focal" got a new version testhelpers.Equals(t, "9c41e95b-add2-4ef8-b1b1-af8899748eda", res.Server.Image.ID) - err = instanceAPI.DeleteServer(&DeleteServerRequest{ + err = instanceAPI.DeleteServer(&instance.DeleteServerRequest{ Zone: scw.ZoneFrPar1, ServerID: res.Server.ID, }) diff --git a/api/instance/v1/snapshot_utils_test.go b/api/instance/v1/snapshot_utils_test.go index f25a6746..b23b4cfb 100644 --- a/api/instance/v1/snapshot_utils_test.go +++ b/api/instance/v1/snapshot_utils_test.go @@ -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" @@ -15,39 +16,39 @@ func TestWaitForSnapshot(t *testing.T) { testhelpers.AssertNoError(t, r.Stop()) // Make sure recorder is stopped once done with it }() - instanceAPI := NewAPI(client) + instanceAPI := instance.NewAPI(client) snapshotName := "backup" snapshot, cleanup := createSnapshot(t, instanceAPI, snapshotName) defer cleanup() - res, err := instanceAPI.WaitForSnapshot(&WaitForSnapshotRequest{ + res, err := instanceAPI.WaitForSnapshot(&instance.WaitForSnapshotRequest{ SnapshotID: snapshot.ID, }) testhelpers.AssertNoError(t, err) testhelpers.Equals(t, snapshot.ID, res.ID) - testhelpers.Equals(t, SnapshotStateAvailable, res.State) + testhelpers.Equals(t, instance.SnapshotStateAvailable, res.State) testhelpers.Equals(t, snapshotName, res.Name) } // createSnapshot is a helper that create an snapshot. // It returns the newly created snapshot and a cleanup function -func createSnapshot(t *testing.T, instanceAPI *API, snapshotName string) (*Snapshot, func()) { +func createSnapshot(t *testing.T, instanceAPI *instance.API, snapshotName string) (*instance.Snapshot, 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. - snapshot, err := instanceAPI.CreateSnapshot(&CreateSnapshotRequest{ + snapshot, err := instanceAPI.CreateSnapshot(&instance.CreateSnapshotRequest{ Name: snapshotName, VolumeID: &serverRes.Server.Volumes["0"].ID, }) testhelpers.AssertNoError(t, err) - snapshotRes, err := instanceAPI.GetSnapshot(&GetSnapshotRequest{ + snapshotRes, err := instanceAPI.GetSnapshot(&instance.GetSnapshotRequest{ SnapshotID: snapshot.Snapshot.ID, }) testhelpers.AssertNoError(t, err) @@ -55,12 +56,12 @@ func createSnapshot(t *testing.T, instanceAPI *API, snapshotName string) (*Snaps return snapshotRes.Snapshot, func() { // Delete all created resources - err := instanceAPI.DeleteServer(&DeleteServerRequest{ + err := instanceAPI.DeleteServer(&instance.DeleteServerRequest{ ServerID: serverRes.Server.ID, }) testhelpers.AssertNoError(t, err) - err = instanceAPI.DeleteSnapshot(&DeleteSnapshotRequest{ + err = instanceAPI.DeleteSnapshot(&instance.DeleteSnapshotRequest{ SnapshotID: snapshotRes.Snapshot.ID, }) testhelpers.AssertNoError(t, err) @@ -74,24 +75,24 @@ func TestAPI_UpdateSnapshot(t *testing.T) { testhelpers.AssertNoError(t, r.Stop()) // Make sure recorder is stopped once done with it }() - instanceAPI := NewAPI(client) + instanceAPI := instance.NewAPI(client) volumeSize := 1 * scw.GB - createVolume, err := instanceAPI.CreateVolume(&CreateVolumeRequest{ + createVolume, err := instanceAPI.CreateVolume(&instance.CreateVolumeRequest{ Name: "volume_name", - VolumeType: VolumeVolumeTypeBSSD, + VolumeType: instance.VolumeVolumeTypeBSSD, Size: &volumeSize, }) testhelpers.AssertNoError(t, err) - createResponse, err := instanceAPI.CreateSnapshot(&CreateSnapshotRequest{ + createResponse, err := instanceAPI.CreateSnapshot(&instance.CreateSnapshotRequest{ Name: "name", VolumeID: &createVolume.Volume.ID, }) testhelpers.AssertNoError(t, err) - updateResponse, err := instanceAPI.UpdateSnapshot(&UpdateSnapshotRequest{ + updateResponse, err := instanceAPI.UpdateSnapshot(&instance.UpdateSnapshotRequest{ SnapshotID: createResponse.Snapshot.ID, Name: scw.StringPtr("new_name"), Tags: scw.StringsPtr([]string{"foo", "bar"}), @@ -100,12 +101,12 @@ func TestAPI_UpdateSnapshot(t *testing.T) { testhelpers.Equals(t, "new_name", updateResponse.Snapshot.Name) testhelpers.Equals(t, []string{"foo", "bar"}, updateResponse.Snapshot.Tags) - _, err = instanceAPI.WaitForSnapshot(&WaitForSnapshotRequest{ + _, err = instanceAPI.WaitForSnapshot(&instance.WaitForSnapshotRequest{ SnapshotID: createResponse.Snapshot.ID, }) testhelpers.AssertNoError(t, err) - err = instanceAPI.DeleteSnapshot(&DeleteSnapshotRequest{ + err = instanceAPI.DeleteSnapshot(&instance.DeleteSnapshotRequest{ SnapshotID: createResponse.Snapshot.ID, }) testhelpers.AssertNoError(t, err) diff --git a/api/instance/v1/volume_utils_test.go b/api/instance/v1/volume_utils_test.go index 5e67236d..dbcbcbaf 100644 --- a/api/instance/v1/volume_utils_test.go +++ b/api/instance/v1/volume_utils_test.go @@ -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" @@ -15,21 +16,21 @@ func TestUpdateVolume(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 ( zone = scw.ZoneFrPar1 project = "951df375-e094-4d26-97c1-ba548eeb9c42" volumeName = "test volume" volumeSize = 20 * scw.GB - volumeType = VolumeVolumeTypeLSSD + volumeType = instance.VolumeVolumeTypeLSSD newVolumeName = "some new volume name" volumeID string ) // Create volume - createVolumeResponse, err := instanceAPI.CreateVolume(&CreateVolumeRequest{ + createVolumeResponse, err := instanceAPI.CreateVolume(&instance.CreateVolumeRequest{ Zone: zone, Name: volumeName, Project: &project, @@ -42,7 +43,7 @@ func TestUpdateVolume(t *testing.T) { volumeID = createVolumeResponse.Volume.ID // Update volume and test whether successfully updated - updateVolumeResponse, err := instanceAPI.UpdateVolume(&UpdateVolumeRequest{ + updateVolumeResponse, err := instanceAPI.UpdateVolume(&instance.UpdateVolumeRequest{ Zone: zone, Name: &newVolumeName, VolumeID: volumeID, @@ -54,7 +55,7 @@ func TestUpdateVolume(t *testing.T) { testhelpers.Equals(t, volumeSize, updateVolumeResponse.Volume.Size) // check that server is not changed // Delete Volume - err = instanceAPI.DeleteVolume(&DeleteVolumeRequest{ + err = instanceAPI.DeleteVolume(&instance.DeleteVolumeRequest{ Zone: zone, VolumeID: volumeID, }) diff --git a/api/marketplace/v2/marketplace_utils_test.go b/api/marketplace/v2/marketplace_utils_test.go index 9ffd6ee7..325fd44e 100644 --- a/api/marketplace/v2/marketplace_utils_test.go +++ b/api/marketplace/v2/marketplace_utils_test.go @@ -1,8 +1,9 @@ -package marketplace +package marketplace_test import ( "testing" + "github.com/scaleway/scaleway-sdk-go/api/marketplace/v2" "github.com/scaleway/scaleway-sdk-go/internal/testhelpers" "github.com/scaleway/scaleway-sdk-go/internal/testhelpers/httprecorder" "github.com/scaleway/scaleway-sdk-go/scw" @@ -17,9 +18,9 @@ func TestGetImageByLabel(t *testing.T) { t.Run("matching input for GetLocalImageIDByLabel", func(t *testing.T) { // Create SDK objects for Scaleway Instance product - marketplaceAPI := NewAPI(client) + marketplaceAPI := marketplace.NewAPI(client) - image, err := marketplaceAPI.GetLocalImageByLabel(&GetLocalImageByLabelRequest{ + image, err := marketplaceAPI.GetLocalImageByLabel(&marketplace.GetLocalImageByLabelRequest{ Zone: scw.ZoneFrPar1, CommercialType: "DEV1-S", ImageLabel: "ubuntu_focal", @@ -32,9 +33,9 @@ func TestGetImageByLabel(t *testing.T) { t.Run("matching input for GetLocalImageIDByLabel with lowercase image label", func(t *testing.T) { // Create SDK objects for Scaleway Instance product - marketplaceAPI := NewAPI(client) + marketplaceAPI := marketplace.NewAPI(client) - image, err := marketplaceAPI.GetLocalImageByLabel(&GetLocalImageByLabelRequest{ + image, err := marketplaceAPI.GetLocalImageByLabel(&marketplace.GetLocalImageByLabelRequest{ Zone: scw.ZoneFrPar1, CommercialType: "dev1-s", ImageLabel: "ubuntu_focal", @@ -47,9 +48,9 @@ func TestGetImageByLabel(t *testing.T) { t.Run("non-matching label for GetLocalImageIDByLabel", func(t *testing.T) { // Create SDK objects for Scaleway Instance product - marketplaceAPI := NewAPI(client) + marketplaceAPI := marketplace.NewAPI(client) - _, err := marketplaceAPI.GetLocalImageByLabel(&GetLocalImageByLabelRequest{ + _, err := marketplaceAPI.GetLocalImageByLabel(&marketplace.GetLocalImageByLabelRequest{ Zone: scw.ZoneFrPar1, CommercialType: "DEV1-S", ImageLabel: "foo-bar-image", diff --git a/internal/async/wait_test.go b/internal/async/wait_test.go index d3941ebe..a290b244 100644 --- a/internal/async/wait_test.go +++ b/internal/async/wait_test.go @@ -1,10 +1,11 @@ -package async +package async_test import ( "errors" "testing" "time" + "github.com/scaleway/scaleway-sdk-go/internal/async" "github.com/scaleway/scaleway-sdk-go/internal/testhelpers" ) @@ -40,13 +41,13 @@ func TestWaitSync(t *testing.T) { t.Parallel() testsCases := []struct { name string - config *WaitSyncConfig + config *async.WaitSyncConfig expValue interface{} expErr error }{ { name: "With default timeout and interval", - config: &WaitSyncConfig{ + config: &async.WaitSyncConfig{ Get: getMock(2, 0), }, expValue: &value{ @@ -56,7 +57,7 @@ func TestWaitSync(t *testing.T) { }, { name: "With useless timeout", - config: &WaitSyncConfig{ + config: &async.WaitSyncConfig{ Get: getMock(2, time.Second), Timeout: 4 * time.Second, }, @@ -67,7 +68,7 @@ func TestWaitSync(t *testing.T) { }, { name: "Should timeout", - config: &WaitSyncConfig{ + config: &async.WaitSyncConfig{ Get: getMock(2, 2*time.Second), Timeout: time.Second, }, @@ -76,9 +77,9 @@ func TestWaitSync(t *testing.T) { }, { name: "With interval", - config: &WaitSyncConfig{ + config: &async.WaitSyncConfig{ Get: getMock(2, 0), - IntervalStrategy: LinearIntervalStrategy(2 * time.Second), + IntervalStrategy: async.LinearIntervalStrategy(2 * time.Second), }, expValue: &value{ doneIterations: 2, @@ -87,9 +88,9 @@ func TestWaitSync(t *testing.T) { }, { name: "With fibonacci interval", - config: &WaitSyncConfig{ + config: &async.WaitSyncConfig{ Get: getMock(5, 0), - IntervalStrategy: FibonacciIntervalStrategy(time.Second, 1), + IntervalStrategy: async.FibonacciIntervalStrategy(time.Second, 1), }, expValue: &value{ doneIterations: 5, @@ -98,10 +99,10 @@ func TestWaitSync(t *testing.T) { }, { name: "Should timeout with interval", - config: &WaitSyncConfig{ + config: &async.WaitSyncConfig{ Get: getMock(2, time.Second), Timeout: 2 * time.Second, - IntervalStrategy: LinearIntervalStrategy(2 * time.Second), + IntervalStrategy: async.LinearIntervalStrategy(2 * time.Second), }, expValue: nil, expErr: errors.New("timeout after 2s"), @@ -112,7 +113,7 @@ func TestWaitSync(t *testing.T) { t.Run(c.name, func(t *testing.T) { t.Parallel() - terminalValue, err := WaitSync(c.config) + terminalValue, err := async.WaitSync(c.config) testhelpers.Equals(t, c.expErr, err) diff --git a/internal/auth/token_test.go b/internal/auth/token_test.go index 7480d07b..47100596 100644 --- a/internal/auth/token_test.go +++ b/internal/auth/token_test.go @@ -1,9 +1,10 @@ -package auth +package auth_test import ( "net/http" "testing" + "github.com/scaleway/scaleway-sdk-go/internal/auth" "github.com/scaleway/scaleway-sdk-go/internal/testhelpers" ) @@ -12,8 +13,8 @@ func TestToken_Headers(t *testing.T) { accessKey = "ACCESS_KEY" secretKey = "SECRET_KEY" ) - auth := NewToken(accessKey, secretKey) + token := auth.NewToken(accessKey, secretKey) testhelpers.Equals(t, http.Header{ "X-Auth-Token": []string{secretKey}, - }, auth.Headers()) + }, token.Headers()) } diff --git a/internal/e2e/errors_test.go b/internal/e2e/errors_test.go index 2f82e4f1..33fe919f 100644 --- a/internal/e2e/errors_test.go +++ b/internal/e2e/errors_test.go @@ -1,4 +1,4 @@ -package e2e +package e2e_test import ( "testing" diff --git a/internal/e2e/human_test.go b/internal/e2e/human_test.go index fbac3607..59ea67f6 100644 --- a/internal/e2e/human_test.go +++ b/internal/e2e/human_test.go @@ -1,4 +1,4 @@ -package e2e +package e2e_test import ( "testing" diff --git a/internal/generic/fields_test.go b/internal/generic/fields_test.go index e24cb364..45e41a28 100644 --- a/internal/generic/fields_test.go +++ b/internal/generic/fields_test.go @@ -1,6 +1,10 @@ -package generic +package generic_test -import "testing" +import ( + "testing" + + "github.com/scaleway/scaleway-sdk-go/internal/generic" +) func TestHasField(t *testing.T) { tests := []struct { @@ -52,7 +56,7 @@ func TestHasField(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := HasField(tt.i, tt.fieldName); got != tt.want { + if got := generic.HasField(tt.i, tt.fieldName); got != tt.want { t.Errorf("HasField() = %v, want %v", got, tt.want) } }) diff --git a/internal/generic/sort_test.go b/internal/generic/sort_test.go index 4c2d1d64..29469391 100644 --- a/internal/generic/sort_test.go +++ b/internal/generic/sort_test.go @@ -1,8 +1,9 @@ -package generic +package generic_test import ( "testing" + "github.com/scaleway/scaleway-sdk-go/internal/generic" "github.com/scaleway/scaleway-sdk-go/internal/testhelpers" ) @@ -15,7 +16,7 @@ func Test_SortSliceByField(t *testing.T) { {"1"}, {"3"}, } - SortSliceByField(elems, "Field", func(i interface{}, i2 interface{}) bool { + generic.SortSliceByField(elems, "Field", func(i interface{}, i2 interface{}) bool { return i.(string) < i2.(string) }) testhelpers.Assert(t, elems[0].Field == "1", "slice is not sorted") diff --git a/scw/client_option_test.go b/scw/client_option_test.go index b16ef70d..3ee0d350 100644 --- a/scw/client_option_test.go +++ b/scw/client_option_test.go @@ -1,4 +1,4 @@ -package scw +package scw_test import ( "os" @@ -7,19 +7,20 @@ import ( "github.com/scaleway/scaleway-sdk-go/internal/auth" "github.com/scaleway/scaleway-sdk-go/internal/testhelpers" + "github.com/scaleway/scaleway-sdk-go/scw" ) var ( defaultOrganizationID = "6170692e-7363-616c-6577-61792e636f6d" // hint: | xxd -ps -r defaultProjectID = "6170692e-7363-616c-6577-61792e636f6d" // hint: | xxd -ps -r - defaultRegion = RegionNlAms - defaultZone = ZoneNlAms1 + defaultRegion = scw.RegionNlAms + defaultZone = scw.ZoneNlAms1 ) func TestClientOptions(t *testing.T) { testCases := []struct { name string - clientOption ClientOption + clientOption scw.ClientOption errStr string }{ { @@ -145,7 +146,7 @@ func TestClientOptions(t *testing.T) { s := newSettings() // Apply - s.apply([]ClientOption{c.clientOption}) + s.apply([]scw.ClientOption{c.clientOption}) // Validate err := s.validate() @@ -172,20 +173,20 @@ func TestCombinedClientOptions(t *testing.T) { expectedAPIURL string expectedDefaultOrganizationID *string expectedDefaultProjectID *string - expectedDefaultRegion *Region - expectedDefaultZone *Zone + expectedDefaultRegion *scw.Region + expectedDefaultZone *scw.Zone }{ { name: "Complete config file with env variables", env: map[string]string{ - "HOME": "{HOME}", - ScwAccessKeyEnv: v2ValidAccessKey2, - ScwSecretKeyEnv: v2ValidSecretKey2, - ScwAPIURLEnv: v2ValidAPIURL2, - ScwDefaultOrganizationIDEnv: v2ValidDefaultOrganizationID2, - ScwDefaultProjectIDEnv: v2ValidDefaultProjectID2, - ScwDefaultRegionEnv: v2ValidDefaultRegion2, - ScwDefaultZoneEnv: v2ValidDefaultZone2, + "HOME": "{HOME}", + scw.ScwAccessKeyEnv: v2ValidAccessKey2, + scw.ScwSecretKeyEnv: v2ValidSecretKey2, + scw.ScwAPIURLEnv: v2ValidAPIURL2, + scw.ScwDefaultOrganizationIDEnv: v2ValidDefaultOrganizationID2, + scw.ScwDefaultProjectIDEnv: v2ValidDefaultProjectID2, + scw.ScwDefaultRegionEnv: v2ValidDefaultRegion2, + scw.ScwDefaultZoneEnv: v2ValidDefaultZone2, }, files: map[string]string{ ".config/scw/config.yaml": v2CompleteValidConfigFile, @@ -195,21 +196,21 @@ func TestCombinedClientOptions(t *testing.T) { expectedAPIURL: v2ValidAPIURL2, expectedDefaultOrganizationID: s(v2ValidDefaultOrganizationID2), expectedDefaultProjectID: s(v2ValidDefaultProjectID2), - expectedDefaultRegion: r(Region(v2ValidDefaultRegion2)), - expectedDefaultZone: z(Zone(v2ValidDefaultZone2)), + expectedDefaultRegion: r(scw.Region(v2ValidDefaultRegion2)), + expectedDefaultZone: z(scw.Zone(v2ValidDefaultZone2)), }, { name: "Complete config with active profile env variable and all env variables", env: map[string]string{ - "HOME": "{HOME}", - ScwActiveProfileEnv: v2ValidProfile, - ScwAccessKeyEnv: v2ValidAccessKey, - ScwSecretKeyEnv: v2ValidSecretKey, - ScwAPIURLEnv: v2ValidAPIURL, - ScwDefaultOrganizationIDEnv: v2ValidDefaultOrganizationID, - ScwDefaultProjectIDEnv: v2ValidDefaultProjectID, - ScwDefaultRegionEnv: v2ValidDefaultRegion, - ScwDefaultZoneEnv: v2ValidDefaultZone, + "HOME": "{HOME}", + scw.ScwActiveProfileEnv: v2ValidProfile, + scw.ScwAccessKeyEnv: v2ValidAccessKey, + scw.ScwSecretKeyEnv: v2ValidSecretKey, + scw.ScwAPIURLEnv: v2ValidAPIURL, + scw.ScwDefaultOrganizationIDEnv: v2ValidDefaultOrganizationID, + scw.ScwDefaultProjectIDEnv: v2ValidDefaultProjectID, + scw.ScwDefaultRegionEnv: v2ValidDefaultRegion, + scw.ScwDefaultZoneEnv: v2ValidDefaultZone, }, files: map[string]string{ ".config/scw/config.yaml": v2CompleteValidConfigFile, @@ -219,8 +220,8 @@ func TestCombinedClientOptions(t *testing.T) { expectedAPIURL: v2ValidAPIURL, expectedDefaultOrganizationID: s(v2ValidDefaultOrganizationID), expectedDefaultProjectID: s(v2ValidDefaultProjectID), - expectedDefaultRegion: r(Region(v2ValidDefaultRegion)), - expectedDefaultZone: z(Zone(v2ValidDefaultZone)), + expectedDefaultRegion: r(scw.Region(v2ValidDefaultRegion)), + expectedDefaultZone: z(scw.Zone(v2ValidDefaultZone)), }, } @@ -238,13 +239,13 @@ func TestCombinedClientOptions(t *testing.T) { // remove config file(s) defer cleanEnv(t, test.files, dir) - config, err := LoadConfig() + config, err := scw.LoadConfig() testhelpers.AssertNoError(t, err) p, err := config.GetActiveProfile() testhelpers.AssertNoError(t, err) - client, err := NewClient(WithProfile(p), WithEnv()) + client, err := scw.NewClient(scw.WithProfile(p), scw.WithEnv()) if test.expectedError == "" { testhelpers.AssertNoError(t, err) diff --git a/scw/client_test.go b/scw/client_test.go index d202aeed..fd8b0cbb 100644 --- a/scw/client_test.go +++ b/scw/client_test.go @@ -1,4 +1,4 @@ -package scw +package scw_test import ( "bytes" @@ -10,6 +10,7 @@ import ( "github.com/scaleway/scaleway-sdk-go/internal/auth" "github.com/scaleway/scaleway-sdk-go/internal/testhelpers" "github.com/scaleway/scaleway-sdk-go/logger" + "github.com/scaleway/scaleway-sdk-go/scw" ) const ( @@ -19,15 +20,15 @@ const ( testSecretKey = "7363616c-6577-6573-6862-6f7579616161" // hint: | xxd -ps -r testDefaultOrganizationID = "6170692e-7363-616c-6577-61792e636f6d" // hint: | xxd -ps -r testDefaultProjectID = "6170692e-7363-616c-6577-61792e636f6e" - testDefaultRegion = RegionFrPar - testDefaultZone = ZoneFrPar1 + testDefaultRegion = scw.RegionFrPar + testDefaultZone = scw.ZoneFrPar1 testDefaultPageSize = uint32(5) testInsecure = true ) func TestNewClientWithNoAuth(t *testing.T) { t.Run("Basic", func(t *testing.T) { - client, err := NewClient() + client, err := scw.NewClient() testhelpers.AssertNoError(t, err) secretKey, exist := client.GetSecretKey() @@ -39,8 +40,8 @@ func TestNewClientWithNoAuth(t *testing.T) { testhelpers.Assert(t, !exist, "accessKey must not exist") }) t.Run("Only access key", func(t *testing.T) { - client, err := NewClient(WithProfile(&Profile{ - AccessKey: StringPtr(testAccessKey), + client, err := scw.NewClient(scw.WithProfile(&scw.Profile{ + AccessKey: scw.StringPtr(testAccessKey), })) testhelpers.AssertNoError(t, err) @@ -58,13 +59,13 @@ func TestNewClientMultipleClients(t *testing.T) { t.Run("Basic", func(t *testing.T) { logger.EnableDebugMode() httpClient := &http.Client{} - _, err := NewClient(WithHTTPClient(httpClient)) + _, err := scw.NewClient(scw.WithHTTPClient(httpClient)) testhelpers.AssertNoError(t, err) _, isLogger := httpClient.Transport.(*requestLoggerTransport) testhelpers.Assert(t, isLogger, "transport should be a request logger") - _, err = NewClient(WithHTTPClient(httpClient)) + _, err = scw.NewClient(scw.WithHTTPClient(httpClient)) testhelpers.AssertNoError(t, err) transport, isLogger := httpClient.Transport.(*requestLoggerTransport) @@ -75,11 +76,11 @@ func TestNewClientMultipleClients(t *testing.T) { } func TestNewClientWithDefaults(t *testing.T) { - options := []ClientOption{ - WithInsecure(), + options := []scw.ClientOption{ + scw.WithInsecure(), } - client, err := NewClient(options...) + client, err := scw.NewClient(options...) testhelpers.AssertNoError(t, err) testhelpers.Equals(t, defaultAPIURL, client.apiURL) @@ -90,18 +91,18 @@ func TestNewClientWithOptions(t *testing.T) { t.Run("Basic", func(t *testing.T) { someHTTPClient := &http.Client{} - options := []ClientOption{ - WithAPIURL(testAPIURL), - WithAuth(testAccessKey, testSecretKey), - WithHTTPClient(someHTTPClient), - WithDefaultOrganizationID(testDefaultOrganizationID), - WithDefaultProjectID(testDefaultProjectID), - WithDefaultRegion(testDefaultRegion), - WithDefaultZone(testDefaultZone), - WithDefaultPageSize(testDefaultPageSize), + options := []scw.ClientOption{ + scw.WithAPIURL(testAPIURL), + scw.WithAuth(testAccessKey, testSecretKey), + scw.WithHTTPClient(someHTTPClient), + scw.WithDefaultOrganizationID(testDefaultOrganizationID), + scw.WithDefaultProjectID(testDefaultProjectID), + scw.WithDefaultRegion(testDefaultRegion), + scw.WithDefaultZone(testDefaultZone), + scw.WithDefaultPageSize(testDefaultPageSize), } - client, err := NewClient(options...) + client, err := scw.NewClient(options...) testhelpers.AssertNoError(t, err) testhelpers.Equals(t, testAPIURL, client.apiURL) @@ -139,7 +140,7 @@ func TestNewClientWithOptions(t *testing.T) { }) t.Run("With custom profile", func(t *testing.T) { - profile := &Profile{ + profile := &scw.Profile{ s(testAccessKey), s(testSecretKey), s(testAPIURL), @@ -151,7 +152,7 @@ func TestNewClientWithOptions(t *testing.T) { b(true), } - client, err := NewClient(WithProfile(profile)) + client, err := scw.NewClient(scw.WithProfile(profile)) testhelpers.AssertNoError(t, err) testhelpers.Equals(t, auth.NewToken(testAccessKey, testSecretKey), client.auth) diff --git a/scw/config_test.go b/scw/config_test.go index 10219434..670c4d6d 100644 --- a/scw/config_test.go +++ b/scw/config_test.go @@ -1,4 +1,4 @@ -package scw +package scw_test import ( "os" @@ -6,6 +6,8 @@ import ( "strings" "testing" + "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/scaleway-sdk-go/internal/testhelpers" "github.com/scaleway/scaleway-sdk-go/logger" ) @@ -21,8 +23,8 @@ var ( v2ValidSendTelemetry2 = "true" v2ValidDefaultOrganizationID2 = "6d6f7264-6f72-6772-6561-74616761696e" // hint: | xxd -ps -r v2ValidDefaultProjectID2 = "6d6f7264-6f72-6772-6561-74616761696f" - v2ValidDefaultRegion2 = string(RegionFrPar) - v2ValidDefaultZone2 = string(ZoneFrPar2) + v2ValidDefaultRegion2 = string(scw.RegionFrPar) + v2ValidDefaultZone2 = string(scw.ZoneFrPar2) v2ValidAccessKey = "SCW1234567890ABCDEFG" v2ValidSecretKey = "7363616c-6577-6573-6862-6f7579616161" // hint: | xxd -ps -r @@ -31,8 +33,8 @@ var ( v2ValidSendTelemetry = "true" v2ValidDefaultOrganizationID = "6170692e-7363-616c-6577-61792e636f6d" // hint: | xxd -ps -r v2ValidDefaultProjectID = "6170692e-7363-616c-6577-61792e636f6e" - v2ValidDefaultRegion = string(RegionNlAms) - v2ValidDefaultZone = string(ZoneNlAms1) + v2ValidDefaultRegion = string(scw.RegionNlAms) + v2ValidDefaultZone = string(scw.ZoneNlAms1) v2ValidProfile = "flantier" v2InvalidAccessKey = "invalid" @@ -42,8 +44,8 @@ var ( v2InvalidDefaultRegion = "invalid" v2InvalidDefaultZone = "invalid" - v2SimpleValidConfig = &Config{ - Profile: Profile{ + v2SimpleValidConfig = &scw.Config{ + Profile: scw.Profile{ AccessKey: &v2ValidAccessKey, SecretKey: &v2ValidSecretKey, DefaultOrganizationID: &v2ValidDefaultOrganizationID, @@ -133,21 +135,21 @@ func TestSaveConfig(t *testing.T) { name string env map[string]string files map[string]string - config *Config - funcUpdateConfig func(*Config) + config *scw.Config + funcUpdateConfig func(*scw.Config) expectedFiles map[string]string }{ { name: "Custom-path config", env: map[string]string{ - ScwConfigPathEnv: "{HOME}/valid1/test.conf", + scw.ScwConfigPathEnv: "{HOME}/valid1/test.conf", }, files: map[string]string{ "valid1/test.conf": emptyFile, }, - config: &Config{ - Profile: Profile{ + config: &scw.Config{ + Profile: scw.Profile{ AccessKey: s(v2ValidAccessKey), SecretKey: s(v2ValidSecretKey), DefaultOrganizationID: s(v2ValidDefaultOrganizationID), @@ -164,8 +166,8 @@ func TestSaveConfig(t *testing.T) { env: map[string]string{ "HOME": "{HOME}", }, - config: &Config{ - Profile: Profile{ + config: &scw.Config{ + Profile: scw.Profile{ AccessKey: s(v2ValidAccessKey), SecretKey: s(v2ValidSecretKey), DefaultOrganizationID: s(v2ValidDefaultOrganizationID), @@ -185,9 +187,9 @@ func TestSaveConfig(t *testing.T) { files: map[string]string{ ".config/scw/config.yaml": v2SimpleValidConfigFile, }, - config: &Config{}, - funcUpdateConfig: func(c *Config) { - *c = *MustLoadConfig() + config: &scw.Config{}, + funcUpdateConfig: func(c *scw.Config) { + *c = *scw.MustLoadConfig() c.DefaultZone = s(v2ValidDefaultZone) }, expectedFiles: map[string]string{ @@ -202,10 +204,10 @@ func TestSaveConfig(t *testing.T) { files: map[string]string{ ".config/scw/config.yaml": v2PartialValidConfigFile, }, - config: &Config{}, - funcUpdateConfig: func(c *Config) { - *c = *MustLoadConfig() - c.Profiles = map[string]*Profile{v2ValidProfile: { + config: &scw.Config{}, + funcUpdateConfig: func(c *scw.Config) { + *c = *scw.MustLoadConfig() + c.Profiles = map[string]*scw.Profile{v2ValidProfile: { AccessKey: s(v2ValidAccessKey2), SecretKey: s(v2ValidSecretKey2), APIURL: s(v2ValidAPIURL2), @@ -285,7 +287,7 @@ func TestLoadProfileAndActiveProfile(t *testing.T) { { name: "Custom-path config is empty", // custom config path env: map[string]string{ - ScwConfigPathEnv: "{HOME}/valid1/test.conf", + scw.ScwConfigPathEnv: "{HOME}/valid1/test.conf", }, files: map[string]string{ "valid1/test.conf": emptyFile, @@ -294,7 +296,7 @@ func TestLoadProfileAndActiveProfile(t *testing.T) { { name: "Custom-path config with valid V2", env: map[string]string{ - ScwConfigPathEnv: "{HOME}/valid3/test.conf", + scw.ScwConfigPathEnv: "{HOME}/valid3/test.conf", }, files: map[string]string{ "valid3/test.conf": v2SimpleValidConfigFile, @@ -375,8 +377,8 @@ func TestLoadProfileAndActiveProfile(t *testing.T) { { name: "Complete config with active profile env variable", env: map[string]string{ - "HOME": "{HOME}", - ScwActiveProfileEnv: v2ValidProfile, + "HOME": "{HOME}", + scw.ScwActiveProfileEnv: v2ValidProfile, }, files: map[string]string{ ".config/scw/config.yaml": v2CompleteValidConfigFile, @@ -423,7 +425,7 @@ func TestLoadProfileAndActiveProfile(t *testing.T) { // remove config file(s) defer cleanEnv(t, test.files, dir) - config, err := LoadConfig() + config, err := scw.LoadConfig() if test.expectedError == "" { testhelpers.AssertNoError(t, err) p, err := config.GetActiveProfile() @@ -447,14 +449,14 @@ func TestLoadProfileAndActiveProfile(t *testing.T) { } func TestConfigString(t *testing.T) { - c := &Config{ - Profile: Profile{ + c := &scw.Config{ + Profile: scw.Profile{ AccessKey: s(v2ValidAccessKey), SecretKey: s(v2ValidSecretKey), SendTelemetry: b(true), }, ActiveProfile: s(v2ValidProfile), - Profiles: map[string]*Profile{ + Profiles: map[string]*scw.Profile{ v2ValidProfile: { AccessKey: s(v2ValidAccessKey2), SecretKey: s(v2ValidSecretKey2), @@ -483,25 +485,25 @@ send_telemetry: true } func TestMergeProfiles(t *testing.T) { - p1 := &Profile{ - AccessKey: StringPtr("1"), - SecretKey: StringPtr("1"), + p1 := &scw.Profile{ + AccessKey: scw.StringPtr("1"), + SecretKey: scw.StringPtr("1"), } - p2 := &Profile{ - AccessKey: StringPtr("2"), - Insecure: BoolPtr(true), + p2 := &scw.Profile{ + AccessKey: scw.StringPtr("2"), + Insecure: scw.BoolPtr(true), } - p3 := &Profile{ - Insecure: BoolPtr(false), - DefaultZone: StringPtr(string(ZoneFrPar1)), + p3 := &scw.Profile{ + Insecure: scw.BoolPtr(false), + DefaultZone: scw.StringPtr(string(scw.ZoneFrPar1)), } - act := MergeProfiles(p1, p2, p3) - exp := &Profile{ - AccessKey: StringPtr("2"), - SecretKey: StringPtr("1"), - Insecure: BoolPtr(false), - DefaultZone: StringPtr(string(ZoneFrPar1)), + act := scw.MergeProfiles(p1, p2, p3) + exp := &scw.Profile{ + AccessKey: scw.StringPtr("2"), + SecretKey: scw.StringPtr("1"), + Insecure: scw.BoolPtr(false), + DefaultZone: scw.StringPtr(string(scw.ZoneFrPar1)), } testhelpers.Equals(t, exp, act) @@ -556,11 +558,11 @@ func s(value string) *string { return &value } -func r(value Region) *Region { +func r(value scw.Region) *scw.Region { return &value } -func z(value Zone) *Zone { +func z(value scw.Zone) *scw.Zone { return &value } @@ -570,7 +572,7 @@ func b(value bool) *bool { func TestConfig_ConfigFile(t *testing.T) { type testCase struct { - config *Config + config *scw.Config result string } @@ -588,7 +590,7 @@ func TestConfig_ConfigFile(t *testing.T) { } t.Run("empty", run(&testCase{ - config: &Config{}, + config: &scw.Config{}, result: `# Scaleway configuration file # https://github.com/scaleway/scaleway-sdk-go/tree/master/scw#scaleway-config @@ -663,8 +665,8 @@ func TestConfig_ConfigFile(t *testing.T) { })) t.Run("partial", run(&testCase{ - config: &Config{ - Profile: Profile{ + config: &scw.Config{ + Profile: scw.Profile{ AccessKey: s(v2ValidAccessKey), }, }, @@ -742,14 +744,14 @@ access_key: SCW1234567890ABCDEFG })) t.Run("full", run(&testCase{ - config: &Config{ - Profile: Profile{ + config: &scw.Config{ + Profile: scw.Profile{ AccessKey: s(v2ValidAccessKey), SecretKey: s(v2ValidSecretKey), SendTelemetry: b(true), }, ActiveProfile: s(v2ValidProfile), - Profiles: map[string]*Profile{ + Profiles: map[string]*scw.Profile{ "profile1": { AccessKey: s(v2ValidAccessKey2), SecretKey: s(v2ValidSecretKey2), @@ -845,5 +847,5 @@ profiles: } func TestEmptyConfig(t *testing.T) { - testhelpers.Assert(t, (&Config{}).IsEmpty(), "Config must be empty") + testhelpers.Assert(t, (&scw.Config{}).IsEmpty(), "Config must be empty") } diff --git a/scw/convert_test.go b/scw/convert_test.go index c78ee8ae..b008074a 100644 --- a/scw/convert_test.go +++ b/scw/convert_test.go @@ -1,10 +1,11 @@ -package scw +package scw_test import ( "testing" "time" "github.com/scaleway/scaleway-sdk-go/internal/testhelpers" + "github.com/scaleway/scaleway-sdk-go/scw" ) var ( @@ -19,14 +20,14 @@ var ( testFloat64 float64 = 47 testDuration time.Duration = 48 testTime = time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC) - testSize = 3 * GB + testSize = 3 * scw.GB ) func TestStringPtr(t *testing.T) { - pointer := StringPtr(testString) + pointer := scw.StringPtr(testString) slice := []string{testString} - sliceOfPointers := StringSlicePtr(slice) - pointerToSlice := StringsPtr(slice) + sliceOfPointers := scw.StringSlicePtr(slice) + pointerToSlice := scw.StringsPtr(slice) // value to pointer value testhelpers.Assert(t, pointer != nil, "Pointer should have value") @@ -43,9 +44,9 @@ func TestStringPtr(t *testing.T) { } func TestBytesPtr(t *testing.T) { - pointer := BytesPtr(testBytes) + pointer := scw.BytesPtr(testBytes) slice := [][]byte{testBytes} - sliceOfPointers := BytesSlicePtr(slice) + sliceOfPointers := scw.BytesSlicePtr(slice) // value to pointer value testhelpers.Assert(t, pointer != nil, "Pointer should have value") @@ -58,9 +59,9 @@ func TestBytesPtr(t *testing.T) { } func TestBoolPtr(t *testing.T) { - pointer := BoolPtr(testBool) + pointer := scw.BoolPtr(testBool) slice := []bool{testBool} - sliceOfPointers := BoolSlicePtr(slice) + sliceOfPointers := scw.BoolSlicePtr(slice) // value to pointer value testhelpers.Assert(t, pointer != nil, "Pointer should have value") @@ -73,9 +74,9 @@ func TestBoolPtr(t *testing.T) { } func TestInt32Ptr(t *testing.T) { - pointer := Int32Ptr(testInt32) + pointer := scw.Int32Ptr(testInt32) slice := []int32{testInt32} - sliceOfPointers := Int32SlicePtr(slice) + sliceOfPointers := scw.Int32SlicePtr(slice) // value to pointer value testhelpers.Assert(t, pointer != nil, "Pointer should have value") @@ -88,9 +89,9 @@ func TestInt32Ptr(t *testing.T) { } func TestInt64Ptr(t *testing.T) { - pointer := Int64Ptr(testInt64) + pointer := scw.Int64Ptr(testInt64) slice := []int64{testInt64} - sliceOfPointers := Int64SlicePtr(slice) + sliceOfPointers := scw.Int64SlicePtr(slice) // value to pointer value testhelpers.Assert(t, pointer != nil, "Pointer should have value") @@ -103,9 +104,9 @@ func TestInt64Ptr(t *testing.T) { } func TestUint32Ptr(t *testing.T) { - pointer := Uint32Ptr(testUInt32) + pointer := scw.Uint32Ptr(testUInt32) slice := []uint32{testUInt32} - sliceOfPointers := Uint32SlicePtr(slice) + sliceOfPointers := scw.Uint32SlicePtr(slice) // value to pointer value testhelpers.Assert(t, pointer != nil, "Pointer should have value") @@ -118,9 +119,9 @@ func TestUint32Ptr(t *testing.T) { } func TestUint64Ptr(t *testing.T) { - pointer := Uint64Ptr(testUInt64) + pointer := scw.Uint64Ptr(testUInt64) slice := []uint64{testUInt64} - sliceOfPointers := Uint64SlicePtr(slice) + sliceOfPointers := scw.Uint64SlicePtr(slice) // value to pointer value testhelpers.Assert(t, pointer != nil, "Pointer should have value") @@ -133,9 +134,9 @@ func TestUint64Ptr(t *testing.T) { } func TestFloat32Ptr(t *testing.T) { - pointer := Float32Ptr(testFloat32) + pointer := scw.Float32Ptr(testFloat32) slice := []float32{testFloat32} - sliceOfPointers := Float32SlicePtr(slice) + sliceOfPointers := scw.Float32SlicePtr(slice) // value to pointer value testhelpers.Assert(t, pointer != nil, "Pointer should have value") @@ -148,9 +149,9 @@ func TestFloat32Ptr(t *testing.T) { } func TestFloat64Ptr(t *testing.T) { - pointer := Float64Ptr(testFloat64) + pointer := scw.Float64Ptr(testFloat64) slice := []float64{testFloat64} - sliceOfPointers := Float64SlicePtr(slice) + sliceOfPointers := scw.Float64SlicePtr(slice) // value to pointer value testhelpers.Assert(t, pointer != nil, "Pointer should have value") @@ -163,7 +164,7 @@ func TestFloat64Ptr(t *testing.T) { } func TestDurationPtr(t *testing.T) { - pointer := TimeDurationPtr(testDuration) + pointer := scw.TimeDurationPtr(testDuration) // value to pointer value testhelpers.Assert(t, pointer != nil, "Pointer should have value") @@ -171,7 +172,7 @@ func TestDurationPtr(t *testing.T) { } func TestTimePtr(t *testing.T) { - pointer := TimePtr(testTime) + pointer := scw.TimePtr(testTime) // value to pointer value testhelpers.Assert(t, pointer != nil, "Pointer should have value") @@ -179,7 +180,7 @@ func TestTimePtr(t *testing.T) { } func TestSizePtr(t *testing.T) { - pointer := SizePtr(testSize) + pointer := scw.SizePtr(testSize) // value to pointer value testhelpers.Assert(t, pointer != nil, "Pointer should have value") diff --git a/scw/custom_types_test.go b/scw/custom_types_test.go index d8efcd19..ed6563e0 100644 --- a/scw/custom_types_test.go +++ b/scw/custom_types_test.go @@ -1,4 +1,4 @@ -package scw +package scw_test import ( "encoding/json" @@ -10,6 +10,7 @@ import ( "time" "github.com/scaleway/scaleway-sdk-go/internal/testhelpers" + "github.com/scaleway/scaleway-sdk-go/scw" ) func TestMoney_NewMoneyFromFloat(t *testing.T) { @@ -17,13 +18,13 @@ func TestMoney_NewMoneyFromFloat(t *testing.T) { value float64 currency string precision int - want *Money + want *scw.Money }{ { value: 0.0, currency: "EUR", precision: 0, - want: &Money{ + want: &scw.Money{ CurrencyCode: "EUR", Units: 0, Nanos: 0, @@ -33,7 +34,7 @@ func TestMoney_NewMoneyFromFloat(t *testing.T) { value: 1.0, currency: "EUR", precision: 3, - want: &Money{ + want: &scw.Money{ CurrencyCode: "EUR", Units: 1, Nanos: 0, @@ -43,7 +44,7 @@ func TestMoney_NewMoneyFromFloat(t *testing.T) { value: 1.3, currency: "EUR", precision: 3, - want: &Money{ + want: &scw.Money{ CurrencyCode: "EUR", Units: 1, Nanos: 300000000, @@ -53,7 +54,7 @@ func TestMoney_NewMoneyFromFloat(t *testing.T) { value: 1.333, currency: "EUR", precision: 2, - want: &Money{ + want: &scw.Money{ CurrencyCode: "EUR", Units: 1, Nanos: 330000000, @@ -63,7 +64,7 @@ func TestMoney_NewMoneyFromFloat(t *testing.T) { value: 1.04, currency: "EUR", precision: 1, - want: &Money{ + want: &scw.Money{ CurrencyCode: "EUR", Units: 1, Nanos: 0, @@ -73,7 +74,7 @@ func TestMoney_NewMoneyFromFloat(t *testing.T) { value: 1.05, currency: "EUR", precision: 1, - want: &Money{ + want: &scw.Money{ CurrencyCode: "EUR", Units: 1, Nanos: 100000000, @@ -83,7 +84,7 @@ func TestMoney_NewMoneyFromFloat(t *testing.T) { value: 1.123456789, currency: "EUR", precision: 9, - want: &Money{ + want: &scw.Money{ CurrencyCode: "EUR", Units: 1, Nanos: 123456789, @@ -93,7 +94,7 @@ func TestMoney_NewMoneyFromFloat(t *testing.T) { value: 1.999999999, currency: "EUR", precision: 9, - want: &Money{ + want: &scw.Money{ CurrencyCode: "EUR", Units: 1, Nanos: 999999999, @@ -103,25 +104,25 @@ func TestMoney_NewMoneyFromFloat(t *testing.T) { for _, c := range cases { t.Run(c.want.String(), func(t *testing.T) { - testhelpers.Equals(t, c.want, NewMoneyFromFloat(c.value, c.currency, c.precision)) + testhelpers.Equals(t, c.want, scw.NewMoneyFromFloat(c.value, c.currency, c.precision)) }) } } func TestMoney_String(t *testing.T) { cases := []struct { - money *Money + money *scw.Money want string }{ { - money: &Money{ + money: &scw.Money{ CurrencyCode: "EUR", Units: 10, }, want: "€ 10.00", }, { - money: &Money{ + money: &scw.Money{ CurrencyCode: "USD", Units: 10, Nanos: 1, @@ -129,35 +130,35 @@ func TestMoney_String(t *testing.T) { want: "$ 10.000000001", }, { - money: &Money{ + money: &scw.Money{ CurrencyCode: "EUR", Nanos: 100000000, }, want: "€ 0.10", }, { - money: &Money{ + money: &scw.Money{ CurrencyCode: "EUR", Nanos: 500000, }, want: "€ 0.0005", }, { - money: &Money{ + money: &scw.Money{ CurrencyCode: "EUR", Nanos: 333000000, }, want: "€ 0.333", }, { - money: &Money{ + money: &scw.Money{ CurrencyCode: "EUR", Nanos: 123456789, }, want: "€ 0.123456789", }, { - money: &Money{ + money: &scw.Money{ CurrencyCode: "?", }, want: "? 0.00", @@ -173,11 +174,11 @@ func TestMoney_String(t *testing.T) { func TestSize_String(t *testing.T) { cases := []struct { - size Size + size scw.Size want string }{ - {size: 42 * MB, want: "42000000"}, - {size: 42 * B, want: "42"}, + {size: 42 * scw.MB, want: "42000000"}, + {size: 42 * scw.B, want: "42"}, } for _, c := range cases { @@ -190,15 +191,15 @@ func TestSize_String(t *testing.T) { func TestTimeSeries_MarshallJSON(t *testing.T) { cases := []struct { name string - ts *TimeSeries + ts *scw.TimeSeries want string err error }{ { name: "basic", - ts: &TimeSeries{ + ts: &scw.TimeSeries{ Name: "cpu_usage", - Points: []*TimeSeriesPoint{ + Points: []*scw.TimeSeriesPoint{ { Timestamp: time.Date(2019, time.August, 8, 15, 0, 0, 0, time.UTC), Value: 0.2, @@ -232,7 +233,7 @@ func TestTimeSeries_UnmarshalJSON(t *testing.T) { cases := []struct { name string json string - want *TimeSeries + want *scw.TimeSeries err error }{ { @@ -249,9 +250,9 @@ func TestTimeSeries_UnmarshalJSON(t *testing.T) { } } `, - want: &TimeSeries{ + want: &scw.TimeSeries{ Name: "cpu_usage", - Points: []*TimeSeriesPoint{ + Points: []*scw.TimeSeriesPoint{ { Timestamp: time.Date(2019, time.August, 8, 15, 0o0, 0o0, 0, time.UTC), Value: 0.2, @@ -275,7 +276,7 @@ func TestTimeSeries_UnmarshalJSON(t *testing.T) { for _, c := range cases { t.Run(c.name, func(t *testing.T) { - ts := &TimeSeries{} + ts := &scw.TimeSeries{} err := json.Unmarshal([]byte(c.json), ts) testhelpers.Equals(t, c.err, err) @@ -289,13 +290,13 @@ func TestTimeSeries_UnmarshalJSON(t *testing.T) { func TestFile_MarshalJSON(t *testing.T) { cases := []struct { name string - file *File + file *scw.File want string err error }{ { name: "basic", - file: &File{ + file: &scw.File{ Name: "example.txt", ContentType: "text/plain", Content: strings.NewReader("Hello, world!"), @@ -304,12 +305,12 @@ func TestFile_MarshalJSON(t *testing.T) { }, { name: "empty", - file: &File{}, + file: &scw.File{}, want: `{"name":"","content_type":"","content":""}`, }, { name: "nil content", - file: &File{ + file: &scw.File{ Name: "example.txt", ContentType: "text/plain", Content: nil, @@ -341,7 +342,7 @@ func TestFile_UnmarshalJSON(t *testing.T) { run := func(c *testCase) func(t *testing.T) { return func(t *testing.T) { t.Helper() - f := File{} + f := scw.File{} err := json.Unmarshal([]byte(c.json), &f) testhelpers.AssertNoError(t, err) testhelpers.Equals(t, c.name, f.Name) @@ -375,23 +376,23 @@ func TestFile_UnmarshalJSON(t *testing.T) { func TestIPNet_MarshallJSON(t *testing.T) { cases := []struct { name string - ipRange IPNet + ipRange scw.IPNet want string err error }{ { name: "ip", - ipRange: IPNet{IPNet: net.IPNet{IP: net.IPv4(42, 42, 42, 42), Mask: net.CIDRMask(32, 32)}}, + ipRange: scw.IPNet{IPNet: net.IPNet{IP: net.IPv4(42, 42, 42, 42), Mask: net.CIDRMask(32, 32)}}, want: `"42.42.42.42/32"`, }, { name: "network", - ipRange: IPNet{IPNet: net.IPNet{IP: net.IPv4(42, 42, 42, 42), Mask: net.CIDRMask(16, 32)}}, + ipRange: scw.IPNet{IPNet: net.IPNet{IP: net.IPv4(42, 42, 42, 42), Mask: net.CIDRMask(16, 32)}}, want: `"42.42.42.42/16"`, }, { name: "network with ip", - ipRange: IPNet{IPNet: net.IPNet{IP: net.IPv4(192, 168, 1, 42), Mask: net.CIDRMask(24, 32)}}, + ipRange: scw.IPNet{IPNet: net.IPNet{IP: net.IPv4(192, 168, 1, 42), Mask: net.CIDRMask(24, 32)}}, want: `"192.168.1.42/24"`, }, } @@ -412,38 +413,38 @@ func TestIPNet_UnmarshalJSON(t *testing.T) { cases := []struct { name string json string - want IPNet + want scw.IPNet err string }{ { name: "IPv4 with CIDR", json: `"42.42.42.42/32"`, - want: IPNet{IPNet: net.IPNet{IP: net.IPv4(42, 42, 42, 42), Mask: net.CIDRMask(32, 32)}}, + want: scw.IPNet{IPNet: net.IPNet{IP: net.IPv4(42, 42, 42, 42), Mask: net.CIDRMask(32, 32)}}, }, { name: "IPv4 with network", json: `"192.0.2.1/24"`, - want: IPNet{IPNet: net.IPNet{IP: net.IPv4(192, 0, 2, 1), Mask: net.CIDRMask(24, 32)}}, + want: scw.IPNet{IPNet: net.IPNet{IP: net.IPv4(192, 0, 2, 1), Mask: net.CIDRMask(24, 32)}}, }, { name: "IPv4 with network 2", json: `"192.168.1.42/24"`, - want: IPNet{IPNet: net.IPNet{IP: net.IPv4(192, 168, 1, 42), Mask: net.CIDRMask(24, 32)}}, + want: scw.IPNet{IPNet: net.IPNet{IP: net.IPv4(192, 168, 1, 42), Mask: net.CIDRMask(24, 32)}}, }, { name: "IPv6 with network", json: `"2001:db8:abcd:8000::/50"`, - want: IPNet{IPNet: net.IPNet{IP: net.ParseIP("2001:db8:abcd:8000::"), Mask: net.CIDRMask(50, 128)}}, + want: scw.IPNet{IPNet: net.IPNet{IP: net.ParseIP("2001:db8:abcd:8000::"), Mask: net.CIDRMask(50, 128)}}, }, { name: "IPv4 alone", json: `"42.42.42.42"`, - want: IPNet{IPNet: net.IPNet{IP: net.IPv4(42, 42, 42, 42), Mask: net.CIDRMask(32, 32)}}, + want: scw.IPNet{IPNet: net.IPNet{IP: net.IPv4(42, 42, 42, 42), Mask: net.CIDRMask(32, 32)}}, }, { name: "IPv6 alone", json: `"2001:db8:abcd:8000::"`, - want: IPNet{IPNet: net.IPNet{IP: net.ParseIP("2001:db8:abcd:8000::"), Mask: net.CIDRMask(128, 128)}}, + want: scw.IPNet{IPNet: net.IPNet{IP: net.ParseIP("2001:db8:abcd:8000::"), Mask: net.CIDRMask(128, 128)}}, }, { name: "invalid CIDR error", @@ -454,7 +455,7 @@ func TestIPNet_UnmarshalJSON(t *testing.T) { for _, c := range cases { t.Run(c.name, func(t *testing.T) { - ipNet := &IPNet{} + ipNet := &scw.IPNet{} err := json.Unmarshal([]byte(c.json), ipNet) if err != nil { testhelpers.Equals(t, c.err, err.Error()) @@ -468,53 +469,53 @@ func TestIPNet_UnmarshalJSON(t *testing.T) { func TestDuration_MarshallJSON(t *testing.T) { cases := []struct { name string - duration *Duration + duration *scw.Duration want string err error }{ { name: "small seconds", - duration: &Duration{Seconds: 3, Nanos: 0}, + duration: &scw.Duration{Seconds: 3, Nanos: 0}, want: `"3.000000000s"`, }, { name: "small seconds, small nanos", - duration: &Duration{Seconds: 3, Nanos: 12e7}, + duration: &scw.Duration{Seconds: 3, Nanos: 12e7}, want: `"3.120000000s"`, }, { name: "small seconds, big nanos", - duration: &Duration{Seconds: 3, Nanos: 123456789}, + duration: &scw.Duration{Seconds: 3, Nanos: 123456789}, want: `"3.123456789s"`, }, { name: "big seconds, big nanos", - duration: &Duration{Seconds: 345679384, Nanos: 123456789}, + duration: &scw.Duration{Seconds: 345679384, Nanos: 123456789}, want: `"345679384.123456789s"`, }, { name: "negative small seconds", - duration: &Duration{Seconds: -3, Nanos: 0}, + duration: &scw.Duration{Seconds: -3, Nanos: 0}, want: `"-3.000000000s"`, }, { name: "negative small seconds, small nanos", - duration: &Duration{Seconds: -3, Nanos: -12e7}, + duration: &scw.Duration{Seconds: -3, Nanos: -12e7}, want: `"-3.120000000s"`, }, { name: "negative small seconds, big nanos", - duration: &Duration{Seconds: -3, Nanos: -123456789}, + duration: &scw.Duration{Seconds: -3, Nanos: -123456789}, want: `"-3.123456789s"`, }, { name: "negative big seconds, big nanos", - duration: &Duration{Seconds: -345679384, Nanos: -123456789}, + duration: &scw.Duration{Seconds: -345679384, Nanos: -123456789}, want: `"-345679384.123456789s"`, }, { name: "negative big seconds, big nanos", - duration: &Duration{}, + duration: &scw.Duration{}, want: `"0.000000000s"`, }, { @@ -540,7 +541,7 @@ func TestDuration_UnmarshalJSON(t *testing.T) { cases := []struct { name string json string - want *Duration + want *scw.Duration err string }{ { @@ -563,49 +564,49 @@ func TestDuration_UnmarshalJSON(t *testing.T) { { name: "small seconds", json: `{"duration":"3.00s"}`, - want: &Duration{Seconds: 3, Nanos: 0}, + want: &scw.Duration{Seconds: 3, Nanos: 0}, }, { name: "small seconds, small nanos", json: `{"duration":"3.12s"}`, - want: &Duration{Seconds: 3, Nanos: 12e7}, + want: &scw.Duration{Seconds: 3, Nanos: 12e7}, }, { name: "bug seconds", json: `{"duration":"987654321.00s"}`, - want: &Duration{Seconds: 987654321, Nanos: 0}, + want: &scw.Duration{Seconds: 987654321, Nanos: 0}, }, { name: "big seconds, big nanos", json: `{"duration":"987654321.123456789s"}`, - want: &Duration{Seconds: 987654321, Nanos: 123456789}, + want: &scw.Duration{Seconds: 987654321, Nanos: 123456789}, }, { name: "negative small seconds", json: `{"duration":"-3.00s"}`, - want: &Duration{Seconds: -3, Nanos: 0}, + want: &scw.Duration{Seconds: -3, Nanos: 0}, }, { name: "negative small seconds, small nanos", json: `{"duration":"-3.12s"}`, - want: &Duration{Seconds: -3, Nanos: -12e7}, + want: &scw.Duration{Seconds: -3, Nanos: -12e7}, }, { name: "negative bug seconds", json: `{"duration":"-987654321.00s"}`, - want: &Duration{Seconds: -987654321, Nanos: 0}, + want: &scw.Duration{Seconds: -987654321, Nanos: 0}, }, { name: "negative big seconds, big nanos", json: `{"duration":"-987654321.123456789s"}`, - want: &Duration{Seconds: -987654321, Nanos: -123456789}, + want: &scw.Duration{Seconds: -987654321, Nanos: -123456789}, }, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { var testType struct { - Duration *Duration + Duration *scw.Duration } err := json.Unmarshal([]byte(c.json), &testType) if err != nil { @@ -620,7 +621,7 @@ func TestDuration_UnmarshalJSON(t *testing.T) { func TestDuration_ToTimeDuration(t *testing.T) { cases := []struct { name string - duration *Duration + duration *scw.Duration want time.Duration }{ { @@ -630,37 +631,37 @@ func TestDuration_ToTimeDuration(t *testing.T) { }, { name: "zero duration", - duration: &Duration{Seconds: 0, Nanos: 0}, + duration: &scw.Duration{Seconds: 0, Nanos: 0}, want: time.Duration(0), }, { name: "seconds only", - duration: &Duration{Seconds: 10, Nanos: 0}, + duration: &scw.Duration{Seconds: 10, Nanos: 0}, want: time.Duration(10) * time.Second, }, { name: "nanoseconds only", - duration: &Duration{Seconds: 0, Nanos: 500}, + duration: &scw.Duration{Seconds: 0, Nanos: 500}, want: time.Duration(500), }, { name: "seconds and nanoseconds", - duration: &Duration{Seconds: 10, Nanos: 500}, + duration: &scw.Duration{Seconds: 10, Nanos: 500}, want: time.Duration(10)*time.Second + time.Duration(500), }, { name: "negative seconds", - duration: &Duration{Seconds: -10, Nanos: 0}, + duration: &scw.Duration{Seconds: -10, Nanos: 0}, want: time.Duration(-10) * time.Second, }, { name: "negative nanoseconds", - duration: &Duration{Seconds: 0, Nanos: -500}, + duration: &scw.Duration{Seconds: 0, Nanos: -500}, want: time.Duration(-500), }, { name: "negative seconds and nanoseconds", - duration: &Duration{Seconds: -10, Nanos: -500}, + duration: &scw.Duration{Seconds: -10, Nanos: -500}, want: time.Duration(-10)*time.Second + time.Duration(-500), }, } @@ -683,48 +684,48 @@ func TestDuration_FromTimeDuration(t *testing.T) { cases := []struct { name string duration time.Duration - want *Duration + want *scw.Duration }{ { name: "zero duration", - want: &Duration{Seconds: 0, Nanos: 0}, + want: &scw.Duration{Seconds: 0, Nanos: 0}, duration: time.Duration(0), }, { name: "seconds only", - want: &Duration{Seconds: 10, Nanos: 0}, + want: &scw.Duration{Seconds: 10, Nanos: 0}, duration: time.Duration(10) * time.Second, }, { name: "nanoseconds only", - want: &Duration{Seconds: 0, Nanos: 500}, + want: &scw.Duration{Seconds: 0, Nanos: 500}, duration: time.Duration(500), }, { name: "seconds and nanoseconds", - want: &Duration{Seconds: 10, Nanos: 500}, + want: &scw.Duration{Seconds: 10, Nanos: 500}, duration: time.Duration(10)*time.Second + time.Duration(500), }, { name: "negative seconds", - want: &Duration{Seconds: -10, Nanos: 0}, + want: &scw.Duration{Seconds: -10, Nanos: 0}, duration: time.Duration(-10) * time.Second, }, { name: "negative nanoseconds", - want: &Duration{Seconds: 0, Nanos: -500}, + want: &scw.Duration{Seconds: 0, Nanos: -500}, duration: time.Duration(-500), }, { name: "negative seconds and nanoseconds", - want: &Duration{Seconds: -10, Nanos: -500}, + want: &scw.Duration{Seconds: -10, Nanos: -500}, duration: time.Duration(-10)*time.Second + time.Duration(-500), }, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { - got := NewDurationFromTimeDuration(c.duration) + got := scw.NewDurationFromTimeDuration(c.duration) if got == nil { t.Errorf("got nil, want %v", c.want) } else if got.Seconds != c.want.Seconds && got.Nanos != c.want.Nanos { @@ -738,7 +739,7 @@ func TestJSONObject_UnmarshalJSON(t *testing.T) { cases := []struct { name string json string - want *JSONObject + want *scw.JSONObject err error }{ { @@ -748,7 +749,7 @@ func TestJSONObject_UnmarshalJSON(t *testing.T) { "test": "scw" } `, - want: &JSONObject{ + want: &scw.JSONObject{ "test": "scw", }, }, @@ -766,7 +767,7 @@ func TestJSONObject_UnmarshalJSON(t *testing.T) { } } `, - want: &JSONObject{ + want: &scw.JSONObject{ "firstName": "John", "lastName": "Smith", "isAlive": true, @@ -781,7 +782,7 @@ func TestJSONObject_UnmarshalJSON(t *testing.T) { for _, c := range cases { t.Run(c.name, func(t *testing.T) { - ts, err := DecodeJSONObject(c.json, NoEscape) + ts, err := scw.DecodeJSONObject(c.json, scw.NoEscape) testhelpers.Equals(t, c.err, err) if c.err == nil { testhelpers.Equals(t, *c.want, ts) @@ -793,13 +794,13 @@ func TestJSONObject_UnmarshalJSON(t *testing.T) { func TestJSONObject_MarshalJSON(t *testing.T) { cases := []struct { name string - jsonValue *JSONObject + jsonValue *scw.JSONObject want string err error }{ { name: "basic", - jsonValue: &JSONObject{ + jsonValue: &scw.JSONObject{ "test": "scw", }, want: `{"test":"scw"}`, @@ -807,7 +808,7 @@ func TestJSONObject_MarshalJSON(t *testing.T) { { name: "multi-types", want: `{"address":{"city":"Paris","country":"FR"},"age":23,"firstName":"John","isAlive":true,"lastName":"Smith"}`, - jsonValue: &JSONObject{ + jsonValue: &scw.JSONObject{ "firstName": "John", "lastName": "Smith", "isAlive": true, @@ -822,7 +823,7 @@ func TestJSONObject_MarshalJSON(t *testing.T) { for _, c := range cases { t.Run(c.name, func(t *testing.T) { - got, err := EncodeJSONObject(*c.jsonValue, NoEscape) + got, err := scw.EncodeJSONObject(*c.jsonValue, scw.NoEscape) testhelpers.Equals(t, c.err, err) if c.err == nil { testhelpers.Equals(t, c.want, got) @@ -832,10 +833,10 @@ func TestJSONObject_MarshalJSON(t *testing.T) { } func TestDecimal(t *testing.T) { - d := Decimal("1.22") + d := scw.Decimal("1.22") testhelpers.Equals(t, "1.22", d.String()) - dPtr := new(Decimal) + dPtr := new(scw.Decimal) testhelpers.Equals(t, "", dPtr.String()) *dPtr = "1.22" diff --git a/scw/env_test.go b/scw/env_test.go index fb3e77da..c8740618 100644 --- a/scw/env_test.go +++ b/scw/env_test.go @@ -1,10 +1,11 @@ -package scw +package scw_test import ( "os" "testing" "github.com/scaleway/scaleway-sdk-go/internal/testhelpers" + "github.com/scaleway/scaleway-sdk-go/scw" ) // TestLoadConfig tests config getters return correct values @@ -26,14 +27,14 @@ func TestLoadEnvProfile(t *testing.T) { { name: "No config with env variables", env: map[string]string{ - ScwAccessKeyEnv: v2ValidAccessKey, - ScwSecretKeyEnv: v2ValidSecretKey, - ScwAPIURLEnv: v2ValidAPIURL, - ScwInsecureEnv: "false", - ScwDefaultOrganizationIDEnv: v2ValidDefaultOrganizationID, - ScwDefaultProjectIDEnv: v2ValidDefaultProjectID, - ScwDefaultRegionEnv: v2ValidDefaultRegion, - ScwDefaultZoneEnv: v2ValidDefaultZone, + scw.ScwAccessKeyEnv: v2ValidAccessKey, + scw.ScwSecretKeyEnv: v2ValidSecretKey, + scw.ScwAPIURLEnv: v2ValidAPIURL, + scw.ScwInsecureEnv: "false", + scw.ScwDefaultOrganizationIDEnv: v2ValidDefaultOrganizationID, + scw.ScwDefaultProjectIDEnv: v2ValidDefaultProjectID, + scw.ScwDefaultRegionEnv: v2ValidDefaultRegion, + scw.ScwDefaultZoneEnv: v2ValidDefaultZone, }, expectedAccessKey: s(v2ValidAccessKey), expectedSecretKey: s(v2ValidSecretKey), @@ -88,7 +89,7 @@ func TestLoadEnvProfile(t *testing.T) { defer cleanEnv(t, nil, dir) // load config - p := LoadEnvProfile() + p := scw.LoadEnvProfile() // assert getters testhelpers.Equals(t, test.expectedAccessKey, p.AccessKey) diff --git a/scw/errors_test.go b/scw/errors_test.go index 33c105ba..cfed866a 100644 --- a/scw/errors_test.go +++ b/scw/errors_test.go @@ -1,4 +1,4 @@ -package scw +package scw_test import ( "bytes" @@ -9,6 +9,7 @@ import ( "testing" "github.com/scaleway/scaleway-sdk-go/internal/testhelpers" + "github.com/scaleway/scaleway-sdk-go/scw" ) func TestHasResponseErrorWithStatus200(t *testing.T) { @@ -31,7 +32,7 @@ func TestNonStandardError(t *testing.T) { resStatusCode int resBody string contentType string - expectedError SdkError + expectedError scw.SdkError } run := func(c *testCase) func(t *testing.T) { @@ -58,8 +59,8 @@ func TestNonStandardError(t *testing.T) { resStatusCode: http.StatusBadRequest, contentType: "application/json", resBody: `{"fields":{"volumes.5.id":["92 is not a valid UUID."],"volumes.5.name":["required key not provided"]},"message":"Validation Error","type":"invalid_request_error"}`, - expectedError: &InvalidArgumentsError{ - Details: []InvalidArgumentsErrorDetail{ + expectedError: &scw.InvalidArgumentsError{ + Details: []scw.InvalidArgumentsErrorDetail{ { ArgumentName: "volumes.5.id", Reason: "constraint", @@ -80,7 +81,7 @@ func TestNonStandardError(t *testing.T) { resStatusCode: http.StatusBadRequest, contentType: "application/json", resBody: `{"message": "server should be running", "type": "invalid_request_error"}`, - expectedError: &ResponseError{ + expectedError: &scw.ResponseError{ Status: "400 Bad Request", StatusCode: http.StatusBadRequest, Message: "server should be running", @@ -94,8 +95,8 @@ func TestNonStandardError(t *testing.T) { resStatusCode: http.StatusForbidden, contentType: "application/json", resBody: `{"type": "invalid_request_error", "message": "Quota exceeded for this resource.", "resource": "compute_snapshots_type_b_ssd_available"}`, - expectedError: &QuotasExceededError{ - Details: []QuotasExceededErrorDetail{ + expectedError: &scw.QuotasExceededError{ + Details: []scw.QuotasExceededErrorDetail{ { Resource: "compute_snapshots_type_b_ssd_available", Current: 0, @@ -111,7 +112,7 @@ func TestNonStandardError(t *testing.T) { resStatusCode: http.StatusNotFound, contentType: "application/json", resBody: `{"type": "unknown_resource", "message": "\"11111111-1111-4111-8111-111111111111\" not found"}`, - expectedError: &ResourceNotFoundError{ + expectedError: &scw.ResourceNotFoundError{ ResourceID: "11111111-1111-4111-8111-111111111111", RawBody: []byte(`{"type": "unknown_resource", "message": "\"11111111-1111-4111-8111-111111111111\" not found"}`), }, @@ -122,7 +123,7 @@ func TestNonStandardError(t *testing.T) { resStatusCode: http.StatusNotFound, contentType: "application/json", resBody: `{"type": "unknown_resource", "message": "Security group \"11111111-1111-4111-8111-111111111111\" not found"}`, - expectedError: &ResourceNotFoundError{ + expectedError: &scw.ResourceNotFoundError{ ResourceID: "11111111-1111-4111-8111-111111111111", Resource: "security_group", RawBody: []byte(`{"type": "unknown_resource", "message": "Security group \"11111111-1111-4111-8111-111111111111\" not found"}`), @@ -134,7 +135,7 @@ func TestNonStandardError(t *testing.T) { resStatusCode: http.StatusNotFound, contentType: "application/json", resBody: `{"type": "unknown_resource", "message": "Volume '11111111-1111-4111-8111-111111111111' not found"}`, - expectedError: &ResourceNotFoundError{ + expectedError: &scw.ResourceNotFoundError{ ResourceID: "11111111-1111-4111-8111-111111111111", Resource: "volume", RawBody: []byte(`{"type": "unknown_resource", "message": "Volume '11111111-1111-4111-8111-111111111111' not found"}`), @@ -146,7 +147,7 @@ func TestNonStandardError(t *testing.T) { resStatusCode: http.StatusConflict, contentType: "application/json", resBody: `{"message": "Group is in use. You cannot delete it.", "type": "conflict"}`, - expectedError: &ResponseError{ + expectedError: &scw.ResponseError{ Status: "409 Conflict", StatusCode: http.StatusConflict, Message: "group is in use. you cannot delete it.", @@ -160,7 +161,7 @@ func TestNonStandardError(t *testing.T) { resStatusCode: http.StatusNotFound, contentType: "text/plain", resBody: ``, - expectedError: &ResponseError{ + expectedError: &scw.ResponseError{ Status: "404 Not Found", StatusCode: http.StatusNotFound, Message: "404 Not Found", @@ -179,7 +180,7 @@ func TestHasResponseErrorWithValidError(t *testing.T) { ) // Create expected error response - testErrorReponse := &ResponseError{ + testErrorReponse := &scw.ResponseError{ Message: errorMessage, Type: errorType, Fields: errorFields, diff --git a/scw/load_config_test.go b/scw/load_config_test.go index 7f3d9a31..748461dc 100644 --- a/scw/load_config_test.go +++ b/scw/load_config_test.go @@ -1,4 +1,4 @@ -package scw +package scw_test import ( "os" @@ -7,6 +7,7 @@ import ( "testing" "github.com/scaleway/scaleway-sdk-go/internal/testhelpers" + "github.com/scaleway/scaleway-sdk-go/scw" ) // TestLoad tests all valid configuration files: @@ -18,7 +19,7 @@ func TestLoad(t *testing.T) { name string env map[string]string files map[string]string - expected *Config + expected *scw.Config expectedError string expectedFiles map[string]string }{ @@ -26,17 +27,17 @@ func TestLoad(t *testing.T) { { name: "Custom-path config is empty", // custom config path env: map[string]string{ - ScwConfigPathEnv: "{HOME}/valid1/test.conf", + scw.ScwConfigPathEnv: "{HOME}/valid1/test.conf", }, files: map[string]string{ "valid1/test.conf": emptyFile, }, - expected: &Config{}, + expected: &scw.Config{}, }, { name: "Custom-path config with valid V2", env: map[string]string{ - ScwConfigPathEnv: "{HOME}/valid3/test.conf", + scw.ScwConfigPathEnv: "{HOME}/valid3/test.conf", }, files: map[string]string{ "valid3/test.conf": v2SimpleValidConfigFile, @@ -59,8 +60,8 @@ func TestLoad(t *testing.T) { { name: "XDG config with valid V2", env: map[string]string{ - "HOME": "{HOME}", - xdgConfigDirEnv: "{HOME}/plop", + "HOME": "{HOME}", + scw.XdgConfigDirEnv: "{HOME}/plop", }, files: map[string]string{ "plop/scw/config.yaml": v2SimpleValidConfigFile, @@ -72,14 +73,14 @@ func TestLoad(t *testing.T) { { name: "Err: custom-path config does not exist", env: map[string]string{ - ScwConfigPathEnv: "{HOME}/fake/test.conf", + scw.ScwConfigPathEnv: "{HOME}/fake/test.conf", }, expectedError: "scaleway-sdk-go: cannot read config file {HOME}/fake/test.conf: no such file or directory", }, { name: "Err: custom-path config with invalid V2", env: map[string]string{ - ScwConfigPathEnv: "{HOME}/invalid1/test.conf", + scw.ScwConfigPathEnv: "{HOME}/invalid1/test.conf", }, files: map[string]string{ "invalid1/test.conf": v2SimpleInvalidConfigFile, @@ -124,7 +125,7 @@ func TestLoad(t *testing.T) { defer cleanEnv(t, test.files, dir) // load config - config, err := LoadConfig() + config, err := scw.LoadConfig() // test expected outputs if test.expectedError != "" { diff --git a/scw/locality_test.go b/scw/locality_test.go index 4233c0f4..6cbabe98 100644 --- a/scw/locality_test.go +++ b/scw/locality_test.go @@ -1,4 +1,4 @@ -package scw +package scw_test import ( "encoding/json" @@ -6,45 +6,46 @@ import ( "github.com/scaleway/scaleway-sdk-go/internal/errors" "github.com/scaleway/scaleway-sdk-go/internal/testhelpers" + "github.com/scaleway/scaleway-sdk-go/scw" ) func TestParseZone(t *testing.T) { tests := []struct { input string err error - expected Zone + expected scw.Zone }{ { input: "fr-par-1", - expected: ZoneFrPar1, + expected: scw.ZoneFrPar1, }, { input: "pl-waw-1", - expected: ZonePlWaw1, + expected: scw.ZonePlWaw1, }, { input: "pl-waw-2", - expected: ZonePlWaw2, + expected: scw.ZonePlWaw2, }, { input: "pl-waw-3", - expected: ZonePlWaw3, + expected: scw.ZonePlWaw3, }, { input: "nl-ams-2", - expected: ZoneNlAms2, + expected: scw.ZoneNlAms2, }, { input: "nl-ams-3", - expected: ZoneNlAms3, + expected: scw.ZoneNlAms3, }, { input: "par1", - expected: ZoneFrPar1, + expected: scw.ZoneFrPar1, }, { input: "ams1", - expected: ZoneNlAms1, + expected: scw.ZoneNlAms1, }, { input: "xx-xxx-1", @@ -69,7 +70,7 @@ func TestParseZone(t *testing.T) { for _, test := range tests { t.Run(test.input, func(t *testing.T) { - z, err := ParseZone(test.input) + z, err := scw.ParseZone(test.input) testhelpers.Equals(t, test.err, err) testhelpers.Equals(t, test.expected, z) }) @@ -79,22 +80,22 @@ func TestParseZone(t *testing.T) { func TestZoneJSONUnmarshall(t *testing.T) { t.Run("test with zone", func(t *testing.T) { input := `{"Test": "par1"}` - value := struct{ Test Zone }{} + value := struct{ Test scw.Zone }{} err := json.Unmarshal([]byte(input), &value) testhelpers.AssertNoError(t, err) - testhelpers.Equals(t, ZoneFrPar1, value.Test) + testhelpers.Equals(t, scw.ZoneFrPar1, value.Test) }) t.Run("test with region", func(t *testing.T) { input := `{"Test": "par1"}` - value := struct{ Test Region }{} + value := struct{ Test scw.Region }{} err := json.Unmarshal([]byte(input), &value) testhelpers.AssertNoError(t, err) - testhelpers.Equals(t, RegionFrPar, value.Test) + testhelpers.Equals(t, scw.RegionFrPar, value.Test) }) } @@ -102,23 +103,23 @@ func TestParseRegion(t *testing.T) { tests := []struct { input string err error - expected Region + expected scw.Region }{ { input: "fr-par", - expected: RegionFrPar, + expected: scw.RegionFrPar, }, { input: "par1", - expected: RegionFrPar, + expected: scw.RegionFrPar, }, { input: "ams1", - expected: RegionNlAms, + expected: scw.RegionNlAms, }, { input: "pl-waw", - expected: RegionPlWaw, + expected: scw.RegionPlWaw, }, { input: "xx-xxx", @@ -138,7 +139,7 @@ func TestParseRegion(t *testing.T) { for _, test := range tests { t.Run(test.input, func(t *testing.T) { - r, err := ParseRegion(test.input) + r, err := scw.ParseRegion(test.input) testhelpers.Equals(t, test.err, err) testhelpers.Equals(t, test.expected, r) }) diff --git a/scw/path.go b/scw/path.go index a4e7a5f9..427d3df1 100644 --- a/scw/path.go +++ b/scw/path.go @@ -8,7 +8,7 @@ import ( const ( // XDG wiki: https://wiki.archlinux.org/index.php/XDG_Base_Directory - xdgConfigDirEnv = "XDG_CONFIG_HOME" + XdgConfigDirEnv = "XDG_CONFIG_HOME" xdgCacheDirEnv = "XDG_CACHE_HOME" unixHomeDirEnv = "HOME" @@ -70,7 +70,7 @@ func getConfigV2FilePath() (string, bool) { // GetScwConfigDir returns the path to scw config folder func GetScwConfigDir() (string, error) { - if xdgPath := os.Getenv(xdgConfigDirEnv); xdgPath != "" { + if xdgPath := os.Getenv(XdgConfigDirEnv); xdgPath != "" { return filepath.Join(xdgPath, "scw"), nil } diff --git a/scw/request_test.go b/scw/request_test.go index 9474dfd1..d334a8b2 100644 --- a/scw/request_test.go +++ b/scw/request_test.go @@ -1,4 +1,4 @@ -package scw +package scw_test import ( "bytes" @@ -10,6 +10,7 @@ import ( "github.com/scaleway/scaleway-sdk-go/internal/auth" "github.com/scaleway/scaleway-sdk-go/internal/testhelpers" + "github.com/scaleway/scaleway-sdk-go/scw" ) const ( @@ -26,7 +27,7 @@ const ( ) func TestGetURL(t *testing.T) { - req := ScalewayRequest{ + req := scw.ScalewayRequest{ Path: testPath, Query: url.Values{ testKey: []string{testValue}, @@ -42,7 +43,7 @@ func TestGetURL(t *testing.T) { } func TestGetHeadersWithoutBody(t *testing.T) { - req := ScalewayRequest{ + req := scw.ScalewayRequest{ Headers: http.Header{ testHeaderKey: []string{testHeaderVal}, }, @@ -61,7 +62,7 @@ func TestGetHeadersWithoutBody(t *testing.T) { } func TestGetHeadersWithBody(t *testing.T) { - req := ScalewayRequest{ + req := scw.ScalewayRequest{ Headers: http.Header{ testHeaderKey: []string{testHeaderVal}, }, @@ -83,22 +84,22 @@ func TestGetHeadersWithBody(t *testing.T) { func TestSetBody(t *testing.T) { body := struct { - Region Region `json:"-"` + Region scw.Region `json:"-"` ID string `json:"-"` Name string `json:"name,omitempty"` Slice []string `json:"slice,omitempty"` Flag bool `json:"flag,omitempty"` Timeout *time.Duration `json:"timeout,omitempty"` }{ - Region: RegionNlAms, + Region: scw.RegionNlAms, ID: "plop", Name: "plop", Slice: []string{"plop", "plop"}, Flag: true, - Timeout: TimeDurationPtr(time.Second), + Timeout: scw.TimeDurationPtr(time.Second), } - req := ScalewayRequest{ + req := scw.ScalewayRequest{ Headers: http.Header{}, } @@ -117,12 +118,12 @@ func TestSetBody(t *testing.T) { } func TestSetFileBody(t *testing.T) { - body := &File{ + body := &scw.File{ Content: bytes.NewReader([]byte(testBody)), ContentType: "plain/text", } - req := ScalewayRequest{ + req := scw.ScalewayRequest{ Headers: http.Header{}, } diff --git a/strcase/bash_arg_test.go b/strcase/bash_arg_test.go index a788d600..38681a5b 100644 --- a/strcase/bash_arg_test.go +++ b/strcase/bash_arg_test.go @@ -1,7 +1,9 @@ -package strcase +package strcase_test import ( "testing" + + "github.com/scaleway/scaleway-sdk-go/strcase" ) func TestToKebabCase(t *testing.T) { @@ -29,7 +31,7 @@ func TestToKebabCase(t *testing.T) { } for _, c := range cases { t.Run(c.in, func(t *testing.T) { - got := toKebab(c.in) + got := strcase.ToKebab(c.in) if got != c.want { t.Errorf("toKebab(%q) == %q, want %q", c.in, got, c.want) } diff --git a/strcase/kebab_test.go b/strcase/kebab_test.go index 16ca455b..51ecccff 100644 --- a/strcase/kebab_test.go +++ b/strcase/kebab_test.go @@ -1,7 +1,9 @@ -package strcase +package strcase_test import ( "testing" + + "github.com/scaleway/scaleway-sdk-go/strcase" ) func TestToKebab(t *testing.T) { @@ -27,7 +29,7 @@ func TestToKebab(t *testing.T) { for _, i := range cases { in := i[0] out := i[1] - result := ToKebab(in) + result := strcase.ToKebab(in) if result != out { t.Error("'" + result + "' != '" + out + "'") } diff --git a/strcase/pascal_test.go b/strcase/pascal_test.go index 5844f979..1dfa77ef 100644 --- a/strcase/pascal_test.go +++ b/strcase/pascal_test.go @@ -1,6 +1,10 @@ -package strcase +package strcase_test -import "testing" +import ( + "testing" + + "github.com/scaleway/scaleway-sdk-go/strcase" +) func TestToPascal(t *testing.T) { cases := [][]string{ @@ -17,7 +21,7 @@ func TestToPascal(t *testing.T) { for _, i := range cases { in := i[0] out := i[1] - result := ToPascal(in) + result := strcase.ToPascal(in) if result != out { t.Error("'" + result + "' != '" + out + "'") } diff --git a/strcase/snake_test.go b/strcase/snake_test.go index 866a913f..556ee3bc 100644 --- a/strcase/snake_test.go +++ b/strcase/snake_test.go @@ -1,7 +1,9 @@ -package strcase +package strcase_test import ( "testing" + + "github.com/scaleway/scaleway-sdk-go/strcase" ) func TestToSnake(t *testing.T) { @@ -31,7 +33,7 @@ func TestToSnake(t *testing.T) { for _, i := range cases { in := i[0] out := i[1] - result := ToSnake(in) + result := strcase.ToSnake(in) if result != out { t.Error("'" + result + "' != '" + out + "'") } diff --git a/strcase/strcase_test.go b/strcase/strcase_test.go index 868c79e8..35ef8fe9 100644 --- a/strcase/strcase_test.go +++ b/strcase/strcase_test.go @@ -1,7 +1,9 @@ -package strcase +package strcase_test import ( "testing" + + "github.com/scaleway/scaleway-sdk-go/strcase" ) func TestAllStrCases(t *testing.T) { @@ -52,15 +54,15 @@ func TestAllStrCases(t *testing.T) { {"dhcp-id", "DHCPID", "dhcpID", "dhcp-id"}, } for _, test := range tests { - got := ToPublicGoName(test.name) + got := strcase.ToPublicGoName(test.name) if got != test.publicGoName { t.Errorf("ToPublicGoName(%q) == %q, want %q", test.name, got, test.publicGoName) } - got = ToPrivateGoName(test.name) + got = strcase.ToPrivateGoName(test.name) if got != test.privateGoName { t.Errorf("ToPrivateGoName(%q) == %q, want %q", test.name, got, test.privateGoName) } - got = ToBashArg(test.name) + got = strcase.ToBashArg(test.name) if got != test.bashArgName { t.Errorf("ToBashArg(%q) == %q, want %q", test.name, got, test.bashArgName) }