Skip to content

Commit 9b33bf4

Browse files
authored
fix(baremetal): update test to fix nightly (scaleway#4881)
1 parent ec78169 commit 9b33bf4

File tree

31 files changed

+97830
-16489
lines changed

31 files changed

+97830
-16489
lines changed

internal/namespaces/baremetal/v1/custom_offer.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88
"github.com/scaleway/scaleway-cli/v2/core"
99
"github.com/scaleway/scaleway-cli/v2/core/human"
1010
"github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
11-
product_catalog "github.com/scaleway/scaleway-sdk-go/api/product_catalog/v2alpha1"
11+
productcatalog "github.com/scaleway/scaleway-sdk-go/api/product_catalog/v2alpha1"
12+
"github.com/scaleway/scaleway-sdk-go/scw"
1213
)
1314

1415
var offerAvailabilityMarshalSpecs = human.EnumMarshalSpecs{
@@ -84,30 +85,28 @@ func serverOfferListBuilder(c *core.Command) *core.Command {
8485

8586
c.Interceptor = func(ctx context.Context, argsI any, runner core.CommandRunner) (any, error) {
8687
req := argsI.(*baremetal.ListOffersRequest)
87-
rawResp, err := runner(ctx, argsI)
88-
if err != nil {
89-
return nil, err
90-
}
9188

92-
offers, _ := rawResp.([]*baremetal.Offer)
9389
client := core.ExtractClient(ctx)
90+
baremetalAPI := baremetal.NewAPI(client)
91+
offers, _ := baremetalAPI.ListOffers(req, scw.WithAllPages())
9492

95-
productAPI := product_catalog.NewPublicCatalogAPI(client)
93+
productAPI := productcatalog.NewPublicCatalogAPI(client)
9694
environmentalImpact, _ := productAPI.ListPublicCatalogProducts(
97-
&product_catalog.PublicCatalogAPIListPublicCatalogProductsRequest{
98-
ProductTypes: []product_catalog.ListPublicCatalogProductsRequestProductType{
99-
product_catalog.ListPublicCatalogProductsRequestProductTypeElasticMetal,
95+
&productcatalog.PublicCatalogAPIListPublicCatalogProductsRequest{
96+
ProductTypes: []productcatalog.ListPublicCatalogProductsRequestProductType{
97+
productcatalog.ListPublicCatalogProductsRequestProductTypeElasticMetal,
10098
},
10199
Zone: &req.Zone,
102100
},
101+
scw.WithAllPages(),
103102
)
104103

105-
unitOfMeasure := product_catalog.PublicCatalogProductUnitOfMeasureCountableUnitMonth
106-
if req.SubscriptionPeriod == "hour" {
107-
unitOfMeasure = product_catalog.PublicCatalogProductUnitOfMeasureCountableUnitHour
104+
unitOfMeasure := productcatalog.PublicCatalogProductUnitOfMeasureCountableUnitHour
105+
if req.SubscriptionPeriod == "month" {
106+
unitOfMeasure = productcatalog.PublicCatalogProductUnitOfMeasureCountableUnitMonth
108107
}
109108

110-
impactMap := make(map[string]*product_catalog.PublicCatalogProduct)
109+
impactMap := make(map[string]*productcatalog.PublicCatalogProduct)
111110
for _, impact := range environmentalImpact.Products {
112111
if impact != nil && impact.UnitOfMeasure.Unit == unitOfMeasure {
113112
key := strings.TrimSpace(strings.TrimPrefix(impact.Product, "Elastic Metal "))
@@ -116,9 +115,15 @@ func serverOfferListBuilder(c *core.Command) *core.Command {
116115
}
117116

118117
var customOfferRes []customOffer
119-
for _, offer := range offers {
118+
for _, offer := range offers.Offers {
120119
impact, ok := impactMap[offer.Name]
121-
if !ok || impact == nil {
120+
if !ok {
121+
customOfferRes = append(customOfferRes, customOffer{
122+
Offer: offer,
123+
KgCo2Equivalent: nil,
124+
M3WaterUsage: nil,
125+
})
126+
122127
continue
123128
}
124129
customOfferRes = append(customOfferRes, customOffer{

internal/namespaces/baremetal/v1/custom_offer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func Test_baremetalGetOffer(t *testing.T) {
1717
}
1818

1919
func Test_baremetalListOffer(t *testing.T) {
20-
t.Run("Monthly", core.Test(&core.TestConfig{
20+
t.Run("Simple", core.Test(&core.TestConfig{
2121
Commands: baremetal.GetCommands(),
2222
Cmd: "scw baremetal offer list",
2323
Check: core.TestCheckGolden(),

internal/namespaces/baremetal/v1/custom_server_create_test.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@ import (
77
"github.com/scaleway/scaleway-cli/v2/core"
88
"github.com/scaleway/scaleway-cli/v2/internal/namespaces/baremetal/v1"
99
baremetalSDK "github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
10+
"github.com/scaleway/scaleway-sdk-go/scw"
1011
"github.com/stretchr/testify/assert"
1112
)
1213

14+
var (
15+
offerNameNVME = getenv("OFFER_NAME_NVME", "EM-I215E-NVME")
16+
offerNameSATA = getenv("OFFER_NAME_SATA", "EM-B111X-SATA")
17+
zone = getenv("zone", "fr-par-2")
18+
)
19+
1320
// All test below should succeed to create an instance.
1421
func Test_CreateServer(t *testing.T) {
1522
// Simple use cases
@@ -19,22 +26,22 @@ func Test_CreateServer(t *testing.T) {
1926
BeforeFunc: func(ctx *core.BeforeFuncCtx) error {
2027
api := baremetalSDK.NewAPI(ctx.Client)
2128
server, _ := api.GetOfferByName(&baremetalSDK.GetOfferByNameRequest{
22-
OfferName: offerName,
23-
Zone: region,
29+
OfferName: offerNameNVME,
30+
Zone: scw.Zone(zone),
2431
})
2532
if server.Stock != baremetalSDK.OfferStockAvailable {
2633
return errors.New("offer out of stock")
2734
}
2835

2936
return nil
3037
},
31-
Cmd: "scw baremetal server create zone=" + region + " type=" + offerName + " -w",
38+
Cmd: "scw baremetal server create zone=" + zone + " type=" + offerNameNVME + " -w",
3239
Check: core.TestCheckCombine(
3340
core.TestCheckGolden(),
3441
core.TestCheckExitCode(0),
3542
),
3643
AfterFunc: core.ExecAfterCmd(
37-
"scw baremetal server delete {{ .CmdResult.ID }} zone=" + region,
44+
"scw baremetal server delete {{ .CmdResult.ID }} zone=" + zone,
3845
),
3946
},
4047
))
@@ -44,16 +51,16 @@ func Test_CreateServer(t *testing.T) {
4451
BeforeFunc: func(ctx *core.BeforeFuncCtx) error {
4552
api := baremetalSDK.NewAPI(ctx.Client)
4653
server, _ := api.GetOfferByName(&baremetalSDK.GetOfferByNameRequest{
47-
OfferName: offerName,
48-
Zone: region,
54+
OfferName: offerNameNVME,
55+
Zone: scw.Zone(zone),
4956
})
5057
if server.Stock != baremetalSDK.OfferStockAvailable {
5158
return errors.New("offer out of stock")
5259
}
5360

5461
return nil
5562
},
56-
Cmd: "scw baremetal server create name=test-create-server-with-name zone=" + region + " type=" + offerName + " -w",
63+
Cmd: "scw baremetal server create name=test-create-server-with-name zone=" + zone + " type=" + offerNameNVME + " -w",
5764
Check: core.TestCheckCombine(
5865
func(t *testing.T, ctx *core.CheckFuncCtx) {
5966
t.Helper()
@@ -66,7 +73,7 @@ func Test_CreateServer(t *testing.T) {
6673
core.TestCheckExitCode(0),
6774
),
6875
AfterFunc: core.ExecAfterCmd(
69-
"scw baremetal server delete {{ .CmdResult.ID }} zone=" + region,
76+
"scw baremetal server delete {{ .CmdResult.ID }} zone=" + zone,
7077
),
7178
}))
7279

@@ -75,16 +82,16 @@ func Test_CreateServer(t *testing.T) {
7582
BeforeFunc: func(ctx *core.BeforeFuncCtx) error {
7683
api := baremetalSDK.NewAPI(ctx.Client)
7784
server, _ := api.GetOfferByName(&baremetalSDK.GetOfferByNameRequest{
78-
OfferName: offerName,
79-
Zone: region,
85+
OfferName: offerNameNVME,
86+
Zone: scw.Zone(zone),
8087
})
8188
if server.Stock != baremetalSDK.OfferStockAvailable {
8289
return errors.New("offer out of stock")
8390
}
8491

8592
return nil
8693
},
87-
Cmd: "scw baremetal server create tags.0=prod tags.1=blue zone=" + region + " type=" + offerName + " -w",
94+
Cmd: "scw baremetal server create tags.0=prod tags.1=blue zone=" + zone + " type=" + offerNameNVME + " -w",
8895
Check: core.TestCheckCombine(
8996
func(t *testing.T, ctx *core.CheckFuncCtx) {
9097
t.Helper()
@@ -94,7 +101,7 @@ func Test_CreateServer(t *testing.T) {
94101
core.TestCheckExitCode(0),
95102
),
96103
AfterFunc: core.ExecAfterCmd(
97-
"scw baremetal server delete {{ .CmdResult.ID }} zone=" + region,
104+
"scw baremetal server delete {{ .CmdResult.ID }} zone=" + zone,
98105
),
99106
}))
100107
})

internal/namespaces/baremetal/v1/custom_server_fip_test.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/scaleway/scaleway-cli/v2/internal/namespaces/baremetal/v1"
1010
flexibleip "github.com/scaleway/scaleway-cli/v2/internal/namespaces/flexibleip/v1alpha1"
1111
baremetalSDK "github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
12+
"github.com/scaleway/scaleway-sdk-go/scw"
1213
)
1314

1415
func Test_CreateFlexibleIPInteractive(t *testing.T) {
@@ -24,24 +25,24 @@ func Test_CreateFlexibleIPInteractive(t *testing.T) {
2425
func(ctx *core.BeforeFuncCtx) error {
2526
api := baremetalSDK.NewAPI(ctx.Client)
2627
server, _ := api.GetOfferByName(&baremetalSDK.GetOfferByNameRequest{
27-
OfferName: offerName,
28-
Zone: region,
28+
OfferName: offerNameNVME,
29+
Zone: scw.Zone(zone),
2930
})
3031
if server.Stock != baremetalSDK.OfferStockAvailable {
3132
return errors.New("offer out of stock")
3233
}
3334

3435
return nil
3536
},
36-
createServerAndWaitDefault("Server"),
37+
createServerAndWait(),
3738
),
38-
Cmd: "scw baremetal server add-flexible-ip {{ .Server.ID }}",
39+
Cmd: "scw baremetal server add-flexible-ip {{ .Server.ID }} zone=" + zone,
3940
Check: core.TestCheckCombine(
4041
core.TestCheckGolden(),
4142
),
4243
AfterFunc: core.AfterFuncCombine(
43-
deleteServerDefault("Server"),
44-
core.ExecAfterCmd("scw fip ip delete {{ .CmdResult.ID }}"),
44+
deleteServer("Server"),
45+
core.ExecAfterCmd("scw fip ip delete {{ .CmdResult.ID }} zone="+zone),
4546
),
4647
PromptResponseMocks: promptResponse,
4748
}))
@@ -57,24 +58,24 @@ func Test_CreateFlexibleIP(t *testing.T) {
5758
func(ctx *core.BeforeFuncCtx) error {
5859
api := baremetalSDK.NewAPI(ctx.Client)
5960
server, _ := api.GetOfferByName(&baremetalSDK.GetOfferByNameRequest{
60-
OfferName: offerName,
61-
Zone: region,
61+
OfferName: offerNameNVME,
62+
Zone: scw.Zone(zone),
6263
})
6364
if server.Stock != baremetalSDK.OfferStockAvailable {
6465
return errors.New("offer out of stock")
6566
}
6667

6768
return nil
6869
},
69-
createServerAndWaitDefault("Server"),
70+
createServerAndWait(),
7071
),
71-
Cmd: "scw baremetal server add-flexible-ip {{ .Server.ID }} ip-type=IPv4",
72+
Cmd: "scw baremetal server add-flexible-ip {{ .Server.ID }} ip-type=IPv4 zone=" + zone,
7273
Check: core.TestCheckCombine(
7374
core.TestCheckGolden(),
7475
),
7576
AfterFunc: core.AfterFuncCombine(
76-
deleteServerDefault("Server"),
77-
core.ExecAfterCmd("scw fip ip delete {{ .CmdResult.ID }}"),
77+
deleteServer("Server"),
78+
core.ExecAfterCmd("scw fip ip delete {{ .CmdResult.ID }} zone="+zone),
7879
),
7980
}))
8081
}

internal/namespaces/baremetal/v1/custom_server_install_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/scaleway/scaleway-cli/v2/internal/namespaces/baremetal/v1"
99
iam "github.com/scaleway/scaleway-cli/v2/internal/namespaces/iam/v1alpha1"
1010
baremetalSDK "github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
11+
"github.com/scaleway/scaleway-sdk-go/scw"
1112
)
1213

1314
func Test_InstallServer(t *testing.T) {
@@ -24,8 +25,8 @@ func Test_InstallServer(t *testing.T) {
2425
func(ctx *core.BeforeFuncCtx) error {
2526
api := baremetalSDK.NewAPI(ctx.Client)
2627
server, _ := api.GetOfferByName(&baremetalSDK.GetOfferByNameRequest{
27-
OfferName: offerName,
28-
Zone: region,
28+
OfferName: offerNameNVME,
29+
Zone: scw.Zone(zone),
2930
})
3031
if server.Stock != baremetalSDK.OfferStockAvailable {
3132
err := errors.New("offer out of stock")
@@ -36,10 +37,10 @@ func Test_InstallServer(t *testing.T) {
3637
return nil
3738
},
3839
addSSH("key", sshKey),
39-
createServerAndWait("Server"),
40+
createServerAndWait(),
4041
),
4142
Commands: cmds,
42-
Cmd: "scw baremetal server install {{ .Server.ID }} zone=" + region + " hostname=test-install-server ssh-key-ids.0={{ .key.ID }} os-id=" + osID + " -w",
43+
Cmd: "scw baremetal server install {{ .Server.ID }} zone=" + zone + " hostname=test-install-server ssh-key-ids.0={{ .key.ID }} os-id=" + osID + " -w",
4344
Check: core.TestCheckCombine(
4445
core.TestCheckGolden(),
4546
core.TestCheckExitCode(0),
@@ -56,8 +57,8 @@ func Test_InstallServer(t *testing.T) {
5657
func(ctx *core.BeforeFuncCtx) error {
5758
api := baremetalSDK.NewAPI(ctx.Client)
5859
server, _ := api.GetOfferByName(&baremetalSDK.GetOfferByNameRequest{
59-
OfferName: offerName,
60-
Zone: region,
60+
OfferName: offerNameNVME,
61+
Zone: scw.Zone(zone),
6162
})
6263
if server.Stock != baremetalSDK.OfferStockAvailable {
6364
return errors.New("offer out of stock")
@@ -66,9 +67,9 @@ func Test_InstallServer(t *testing.T) {
6667
return nil
6768
},
6869
addSSH("key", sshKey),
69-
createServerAndWait("Server"),
70+
createServerAndWait(),
7071
),
71-
Cmd: "scw baremetal server install {{ .Server.ID }} zone=" + region + " hostname=test-install-server all-ssh-keys=true os-id=" + osID + " -w",
72+
Cmd: "scw baremetal server install {{ .Server.ID }} zone=" + zone + " hostname=test-install-server all-ssh-keys=true os-id=" + osID + " -w",
7273
Check: core.TestCheckCombine(
7374
core.TestCheckGolden(),
7475
core.TestCheckExitCode(0),

internal/namespaces/baremetal/v1/custom_server_test.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,25 @@ import (
1010
"github.com/scaleway/scaleway-sdk-go/scw"
1111
)
1212

13-
const (
14-
offerName = "EM-B220E-NVME"
15-
region = "fr-par-1"
16-
)
17-
1813
func Test_StartServerErrors(t *testing.T) {
1914
t.Run("Error: cannot be started while not delivered", core.Test(&core.TestConfig{
2015
BeforeFunc: core.BeforeFuncCombine(
2116
func(ctx *core.BeforeFuncCtx) error {
2217
api := baremetalSDK.NewAPI(ctx.Client)
2318
server, _ := api.GetOfferByName(&baremetalSDK.GetOfferByNameRequest{
24-
OfferName: "EM-B111X-SATA",
25-
Zone: region,
19+
OfferName: offerNameSATA,
20+
Zone: scw.Zone(zone),
2621
})
2722
if server.Stock != baremetalSDK.OfferStockAvailable {
2823
return errors.New("offer out of stock")
2924
}
3025

3126
return nil
3227
},
33-
createServer("Server", "EM-B111X-SATA"),
28+
createServer("Server", offerNameSATA),
3429
),
3530
Commands: baremetal.GetCommands(),
36-
Cmd: "scw baremetal server start zone=" + region + " {{ .Server.ID }}",
31+
Cmd: "scw baremetal server start zone=" + zone + " {{ .Server.ID }}",
3732
Check: core.TestCheckCombine(
3833
core.TestCheckGolden(),
3934
core.TestCheckExitCode(1),
@@ -62,19 +57,19 @@ func Test_StopServerErrors(t *testing.T) {
6257
func(ctx *core.BeforeFuncCtx) error {
6358
api := baremetalSDK.NewAPI(ctx.Client)
6459
server, _ := api.GetOfferByName(&baremetalSDK.GetOfferByNameRequest{
65-
OfferName: "EM-B111X-SATA",
66-
Zone: region,
60+
OfferName: offerNameSATA,
61+
Zone: scw.Zone(zone),
6762
})
6863
if server.Stock != baremetalSDK.OfferStockAvailable {
6964
return errors.New("offer out of stock")
7065
}
7166

7267
return nil
7368
},
74-
createServer("Server", "EM-B111X-SATA"),
69+
createServer("Server", offerNameSATA),
7570
),
7671
Commands: baremetal.GetCommands(),
77-
Cmd: "scw baremetal server stop zone=" + region + " {{ .Server.ID }}",
72+
Cmd: "scw baremetal server stop zone=" + zone + " {{ .Server.ID }}",
7873
Check: core.TestCheckCombine(
7974
core.TestCheckGolden(),
8075
core.TestCheckExitCode(1),
@@ -103,19 +98,19 @@ func Test_RebootServerErrors(t *testing.T) {
10398
func(ctx *core.BeforeFuncCtx) error {
10499
api := baremetalSDK.NewAPI(ctx.Client)
105100
server, _ := api.GetOfferByName(&baremetalSDK.GetOfferByNameRequest{
106-
OfferName: "EM-B111X-SATA",
107-
Zone: region,
101+
OfferName: offerNameSATA,
102+
Zone: scw.Zone(zone),
108103
})
109104
if server.Stock != baremetalSDK.OfferStockAvailable {
110105
return errors.New("offer out of stock")
111106
}
112107

113108
return nil
114109
},
115-
createServer("Server", "EM-B111X-SATA"),
110+
createServer("Server", offerNameSATA),
116111
),
117112
Commands: baremetal.GetCommands(),
118-
Cmd: "scw baremetal server reboot zone=" + region + " {{ .Server.ID }}",
113+
Cmd: "scw baremetal server reboot zone=" + zone + " {{ .Server.ID }}",
119114
Check: core.TestCheckCombine(
120115
core.TestCheckGolden(),
121116
core.TestCheckExitCode(1),

0 commit comments

Comments
 (0)