Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/namespaces/instance/v1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func GetCommands() *core.Commands {
cmds.MustFind("instance", "server", "list").Override(serverListBuilder)
cmds.MustFind("instance", "server", "update").Override(serverUpdateBuilder)
cmds.MustFind("instance", "server", "get").Override(serverGetBuilder)
cmds.MustFind("instance", "server", "attach-volume").Override(serverAttachVolumeBuilder)
cmds.MustFind("instance", "server", "detach-volume").Override(serverDetachVolumeBuilder)

cmds.Merge(core.NewCommands(
serverBackupCommand(),
Expand Down
61 changes: 29 additions & 32 deletions internal/namespaces/instance/v1/custom_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,37 +242,34 @@ func Test_ImageUpdate(t *testing.T) {
),
}))

t.Run("Add extra volume", func(t *testing.T) {
t.Skip("Skipping 'Add extra volume' test temporarily")
core.Test(&core.TestConfig{
BeforeFunc: core.BeforeFuncCombine(
createNonEmptyLocalVolume("Volume", 10),
core.ExecStoreBeforeCmd(
"SnapshotVol",
`scw instance snapshot create -w name=snapVol volume-id={{ .Volume.ID }}`,
),
createImage("ImageExtraVol"),
),
Commands: instance.GetCommands(),
Cmd: "scw instance image update {{ .ImageExtraVol.Image.ID }} extra-volumes.1.id={{ .SnapshotVol.ID }}",
Check: core.TestCheckCombine(
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
assert.NotNil(t, ctx.Result)
assert.Equal(
t,
"snapVol",
ctx.Result.(*instanceSDK.UpdateImageResponse).Image.ExtraVolumes["1"].Name,
)
},
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
AfterFunc: core.AfterFuncCombine(
deleteServer("Server"),
deleteImage("ImageExtraVol"),
deleteVolume("Volume"),
t.Run("Add extra volume", core.Test(&core.TestConfig{
BeforeFunc: core.BeforeFuncCombine(
createNonEmptyLocalVolume("Volume", 10),
core.ExecStoreBeforeCmd(
"SnapshotVol",
`scw instance snapshot create -w name=snapVol volume-id={{ .Volume.ID }}`,
),
})
})
createImage("ImageExtraVol"),
),
Commands: instance.GetCommands(),
Cmd: "scw instance image update {{ .ImageExtraVol.Image.ID }} extra-volumes.1.id={{ .SnapshotVol.ID }}",
Check: core.TestCheckCombine(
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
assert.NotNil(t, ctx.Result)
assert.Equal(
t,
"snapVol",
ctx.Result.(*instanceSDK.UpdateImageResponse).Image.ExtraVolumes["1"].Name,
)
},
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
AfterFunc: core.AfterFuncCombine(
deleteServer("Server"),
deleteImage("ImageExtraVol"),
deleteVolume("Volume"),
),
}))
}
29 changes: 29 additions & 0 deletions internal/namespaces/instance/v1/custom_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,35 @@ func volumeIsFromSBS(api *block.API, zone scw.Zone, volumeID string) bool {
return err == nil
}

func serverAttachVolumeBuilder(c *core.Command) *core.Command {
c.Interceptor = func(ctx context.Context, argsI any, runner core.CommandRunner) (any, error) {
request := argsI.(*instance.AttachServerVolumeRequest)
api := instance.NewAPI(core.ExtractClient(ctx))

return api.AttachVolume(&instance.AttachVolumeRequest{
Zone: request.Zone,
VolumeID: request.VolumeID,
ServerID: request.ServerID,
})
}

return c
}

func serverDetachVolumeBuilder(c *core.Command) *core.Command {
c.Interceptor = func(ctx context.Context, argsI any, runner core.CommandRunner) (any, error) {
request := argsI.(*instance.DetachServerVolumeRequest)
api := instance.NewAPI(core.ExtractClient(ctx))

return api.DetachVolume(&instance.DetachVolumeRequest{
Zone: request.Zone,
VolumeID: request.VolumeID,
})
}

return c
}

func serverGetBuilder(c *core.Command) *core.Command {
// This method is here as a proof of concept before we find the correct way to implement it at larger scale
c.ArgSpecs.GetPositionalArg().AutoCompleteFunc = func(ctx context.Context, prefix string, request any) core.AutocompleteSuggestions {
Expand Down
72 changes: 34 additions & 38 deletions internal/namespaces/instance/v1/custom_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
)

func Test_ServerVolumeUpdate(t *testing.T) {
t.Skip("Skipping temporarily")
t.Run("Attach", func(t *testing.T) {
t.Run("simple block volume", core.Test(&core.TestConfig{
Commands: core.NewCommandsMerge(
Expand Down Expand Up @@ -100,12 +99,12 @@ func Test_ServerVolumeUpdate(t *testing.T) {
Check: func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
require.NoError(t, ctx.Err)
resp := testhelpers.Value[*instanceSDK.DetachServerVolumeResponse](t, ctx.Result)
resp := testhelpers.Value[*instanceSDK.DetachVolumeResponse](t, ctx.Result)
assert.NotZero(t, resp.Server.Volumes["0"])
assert.Nil(t, resp.Server.Volumes["1"])
assert.Len(
t,
ctx.Result.(*instanceSDK.DetachServerVolumeResponse).Server.Volumes,
ctx.Result.(*instanceSDK.DetachVolumeResponse).Server.Volumes,
1,
)
},
Expand Down Expand Up @@ -412,44 +411,41 @@ func Test_ServerDelete(t *testing.T) {
DisableParallel: true,
}))

t.Run("with sbs volumes", func(t *testing.T) {
t.Skip("Skipping 'with sbs volumes' test temporarily")
core.Test(&core.TestConfig{
Commands: core.NewCommandsMerge(
instance.GetCommands(),
block.GetCommands(),
t.Run("with sbs volumes", core.Test(&core.TestConfig{
Commands: core.NewCommandsMerge(
instance.GetCommands(),
block.GetCommands(),
),
BeforeFunc: core.BeforeFuncCombine(
core.ExecStoreBeforeCmd(
"BlockVolume",
"scw block volume create perf-iops=5000 from-empty.size=10G name=cli-test-server-delete-with-sbs-volumes",
),
BeforeFunc: core.BeforeFuncCombine(
core.ExecStoreBeforeCmd(
"BlockVolume",
"scw block volume create perf-iops=5000 from-empty.size=10G name=cli-test-server-delete-with-sbs-volumes",
),
core.ExecStoreBeforeCmd(
"Server",
testServerCommand("stopped=true image=ubuntu-jammy"),
),
core.ExecBeforeCmd(
"scw instance server attach-volume server-id={{ .Server.ID }} volume-id={{ .BlockVolume.ID }}",
),
core.ExecStoreBeforeCmd(
"Server",
testServerCommand("stopped=true image=ubuntu-jammy"),
),
Cmd: `scw instance server delete {{ .Server.ID }} with-ip=true with-volumes=all`,
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
api := blockSDK.NewAPI(ctx.Client)
blockVolume := ctx.Meta["BlockVolume"].(*blockSDK.Volume)
resp, err := api.GetVolume(&blockSDK.GetVolumeRequest{
Zone: blockVolume.Zone,
VolumeID: blockVolume.ID,
})
assert.Error(t, err, "%v", resp)
},
core.ExecBeforeCmd(
"scw instance server attach-volume server-id={{ .Server.ID }} volume-id={{ .BlockVolume.ID }}",
),
DisableParallel: true,
})
})
),
Cmd: `scw instance server delete {{ .Server.ID }} with-ip=true with-volumes=all`,
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
api := blockSDK.NewAPI(ctx.Client)
blockVolume := ctx.Meta["BlockVolume"].(*blockSDK.Volume)
resp, err := api.GetVolume(&blockSDK.GetVolumeRequest{
Zone: blockVolume.Zone,
VolumeID: blockVolume.ID,
})
assert.Error(t, err, "%v", resp)
},
),
DisableParallel: true,
}))

t.Run("with multiple IPs", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Expand Down
1 change: 0 additions & 1 deletion internal/namespaces/instance/v1/custom_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
)

func Test_UpdateSnapshot(t *testing.T) {
t.Skip("Skipping 'UpdateSnapshot' test temporarily")
t.Run("Simple", func(t *testing.T) {
t.Run("Change tags", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Expand Down
Loading
Loading