From 8954d973df4d28b0324dd023a363956fd73116de Mon Sep 17 00:00:00 2001 From: Daniel Shuy Date: Wed, 26 Oct 2022 02:14:36 +0800 Subject: [PATCH] Share demo/sandbox status command logic (#364) Signed-off-by: Daniel Shuy Signed-off-by: Daniel Shuy --- flytectl/cmd/demo/status.go | 19 +++------------ flytectl/cmd/sandbox/status.go | 19 +++------------ flytectl/pkg/sandbox/status.go | 22 +++++++++++++++++ flytectl/pkg/sandbox/status_test.go | 37 +++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 32 deletions(-) create mode 100644 flytectl/pkg/sandbox/status.go create mode 100644 flytectl/pkg/sandbox/status_test.go diff --git a/flytectl/cmd/demo/status.go b/flytectl/cmd/demo/status.go index 942b561d8c..7b34bed7e5 100644 --- a/flytectl/cmd/demo/status.go +++ b/flytectl/cmd/demo/status.go @@ -2,9 +2,9 @@ package demo import ( "context" - "fmt" - "github.com/enescakir/emoji" + "github.com/flyteorg/flytectl/pkg/sandbox" + cmdCore "github.com/flyteorg/flytectl/cmd/core" "github.com/flyteorg/flytectl/pkg/docker" ) @@ -28,18 +28,5 @@ func demoClusterStatus(ctx context.Context, args []string, cmdCtx cmdCore.Comman return err } - return printStatus(ctx, cli) -} - -func printStatus(ctx context.Context, cli docker.Docker) error { - c, err := docker.GetSandbox(ctx, cli) - if err != nil { - return err - } - if c == nil { - fmt.Printf("%v no demo cluster found \n", emoji.StopSign) - return nil - } - fmt.Printf("Flyte demo cluster container image [%s] with status [%s] is in state [%s]", c.Image, c.Status, c.State) - return nil + return sandbox.PrintStatus(ctx, cli) } diff --git a/flytectl/cmd/sandbox/status.go b/flytectl/cmd/sandbox/status.go index 30160ef7e8..69476a4301 100644 --- a/flytectl/cmd/sandbox/status.go +++ b/flytectl/cmd/sandbox/status.go @@ -2,9 +2,9 @@ package sandbox import ( "context" - "fmt" - "github.com/enescakir/emoji" + "github.com/flyteorg/flytectl/pkg/sandbox" + cmdCore "github.com/flyteorg/flytectl/cmd/core" "github.com/flyteorg/flytectl/pkg/docker" ) @@ -28,18 +28,5 @@ func sandboxClusterStatus(ctx context.Context, args []string, cmdCtx cmdCore.Com return err } - return printStatus(ctx, cli) -} - -func printStatus(ctx context.Context, cli docker.Docker) error { - c, err := docker.GetSandbox(ctx, cli) - if err != nil { - return err - } - if c == nil { - fmt.Printf("%v no Sandbox found \n", emoji.StopSign) - return nil - } - fmt.Printf("Flyte local sandbox cluster container image [%s] with status [%s] is in state [%s]", c.Image, c.Status, c.State) - return nil + return sandbox.PrintStatus(ctx, cli) } diff --git a/flytectl/pkg/sandbox/status.go b/flytectl/pkg/sandbox/status.go new file mode 100644 index 0000000000..dc965ab138 --- /dev/null +++ b/flytectl/pkg/sandbox/status.go @@ -0,0 +1,22 @@ +package sandbox + +import ( + "context" + "fmt" + + "github.com/enescakir/emoji" + "github.com/flyteorg/flytectl/pkg/docker" +) + +func PrintStatus(ctx context.Context, cli docker.Docker) error { + c, err := docker.GetSandbox(ctx, cli) + if err != nil { + return err + } + if c == nil { + fmt.Printf("%v no Sandbox found \n", emoji.StopSign) + return nil + } + fmt.Printf("Flyte local sandbox container image [%s] with status [%s] is in state [%s]", c.Image, c.Status, c.State) + return nil +} diff --git a/flytectl/pkg/sandbox/status_test.go b/flytectl/pkg/sandbox/status_test.go new file mode 100644 index 0000000000..9d3e847b70 --- /dev/null +++ b/flytectl/pkg/sandbox/status_test.go @@ -0,0 +1,37 @@ +package sandbox + +import ( + "testing" + + "github.com/flyteorg/flytectl/cmd/testutils" + + "github.com/docker/docker/api/types" + "github.com/flyteorg/flytectl/pkg/docker" + "github.com/flyteorg/flytectl/pkg/docker/mocks" + "github.com/stretchr/testify/assert" +) + +func TestSandboxStatus(t *testing.T) { + t.Run("Sandbox status with zero result", func(t *testing.T) { + mockDocker := &mocks.Docker{} + s := testutils.Setup() + mockDocker.OnContainerList(s.Ctx, types.ContainerListOptions{All: true}).Return([]types.Container{}, nil) + err := PrintStatus(s.Ctx, mockDocker) + assert.Nil(t, err) + }) + t.Run("Sandbox status with running sandbox", func(t *testing.T) { + s := testutils.Setup() + ctx := s.Ctx + mockDocker := &mocks.Docker{} + mockDocker.OnContainerList(ctx, types.ContainerListOptions{All: true}).Return([]types.Container{ + { + ID: docker.FlyteSandboxClusterName, + Names: []string{ + docker.FlyteSandboxClusterName, + }, + }, + }, nil) + err := PrintStatus(ctx, mockDocker) + assert.Nil(t, err) + }) +}