Skip to content

Commit

Permalink
Teardown logic shared for demo/sandbox cmds (flyteorg#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
dav009 authored Jul 11, 2022
1 parent 305adae commit 09f9d21
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 110 deletions.
1 change: 0 additions & 1 deletion flytectl/cmd/demo/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ eg : for passing multiple environment variables
Usage
`
demoContextName = "flyte-sandbox"
)

func startDemoCluster(ctx context.Context, args []string, cmdCtx cmdCore.CommandContext) error {
Expand Down
37 changes: 2 additions & 35 deletions flytectl/cmd/demo/teardown.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@ package demo

import (
"context"
"fmt"

"github.com/flyteorg/flytectl/pkg/configutil"
"github.com/flyteorg/flytectl/pkg/sandbox"

"github.com/flyteorg/flytectl/pkg/docker"

"github.com/docker/docker/api/types"
"github.com/enescakir/emoji"

cmdCore "github.com/flyteorg/flytectl/cmd/core"
"github.com/flyteorg/flytectl/pkg/k8s"
)

const (
Expand All @@ -33,33 +28,5 @@ func teardownDemoCluster(ctx context.Context, args []string, cmdCtx cmdCore.Comm
if err != nil {
return err
}

return tearDownDemo(ctx, cli)
}

func tearDownDemo(ctx context.Context, cli docker.Docker) error {
c, err := docker.GetSandbox(ctx, cli)
if err != nil {
return err
}
if c != nil {
if err := cli.ContainerRemove(context.Background(), c.ID, types.ContainerRemoveOptions{
Force: true,
}); err != nil {
return err
}
}
if err := configutil.ConfigCleanup(); err != nil {
fmt.Printf("Config cleanup failed. Which Failed due to %v \n ", err)
}
if err := removeDemoKubeContext(); err != nil {
fmt.Printf("Kubecontext cleanup failed. Which Failed due to %v \n ", err)
}
fmt.Printf("%v %v Demo cluster is removed successfully. \n", emoji.Broom, emoji.Broom)
return nil
}

func removeDemoKubeContext() error {
k8sCtxMgr := k8s.NewK8sContextManager()
return k8sCtxMgr.RemoveContext(demoContextName)
return sandbox.Teardown(ctx, cli)
}
13 changes: 11 additions & 2 deletions flytectl/cmd/demo/teardown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/flyteorg/flytectl/pkg/docker/mocks"
"github.com/flyteorg/flytectl/pkg/k8s"
k8sMocks "github.com/flyteorg/flytectl/pkg/k8s/mocks"
"github.com/flyteorg/flytectl/pkg/sandbox"
"github.com/flyteorg/flytectl/pkg/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand All @@ -36,15 +37,23 @@ func TestTearDownFunc(t *testing.T) {
mockK8sContextMgr := &k8sMocks.ContextOps{}
k8s.ContextMgr = mockK8sContextMgr
mockK8sContextMgr.OnRemoveContextMatch(mock.Anything).Return(nil)
err := tearDownDemo(ctx, mockDocker)
err := sandbox.Teardown(ctx, mockDocker)
assert.Nil(t, err)
})
t.Run("Error", func(t *testing.T) {
ctx := context.Background()
mockDocker := &mocks.Docker{}
mockDocker.OnContainerList(ctx, types.ContainerListOptions{All: true}).Return(containers, nil)
mockDocker.OnContainerRemove(ctx, mock.Anything, types.ContainerRemoveOptions{Force: true}).Return(fmt.Errorf("err"))
err := tearDownDemo(ctx, mockDocker)
err := sandbox.Teardown(ctx, mockDocker)
assert.NotNil(t, err)
})

t.Run("Error", func(t *testing.T) {
ctx := context.Background()
mockDocker := &mocks.Docker{}
mockDocker.OnContainerList(ctx, types.ContainerListOptions{All: true}).Return(nil, fmt.Errorf("err"))
err := sandbox.Teardown(ctx, mockDocker)
assert.NotNil(t, err)
})

Expand Down
1 change: 0 additions & 1 deletion flytectl/cmd/sandbox/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ eg : for passing multiple environment variables
Usage
`
sandboxContextName = "flyte-sandbox"
)

func startSandboxCluster(ctx context.Context, args []string, cmdCtx cmdCore.CommandContext) error {
Expand Down
38 changes: 2 additions & 36 deletions flytectl/cmd/sandbox/teardown.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@ package sandbox

import (
"context"
"fmt"

"github.com/flyteorg/flytectl/pkg/configutil"

"github.com/flyteorg/flytectl/pkg/docker"

"github.com/docker/docker/api/types"
"github.com/enescakir/emoji"
"github.com/flyteorg/flytectl/pkg/sandbox"

cmdCore "github.com/flyteorg/flytectl/cmd/core"
"github.com/flyteorg/flytectl/pkg/k8s"
)

const (
Expand All @@ -33,33 +27,5 @@ func teardownSandboxCluster(ctx context.Context, args []string, cmdCtx cmdCore.C
if err != nil {
return err
}

return tearDownSandbox(ctx, cli)
}

func tearDownSandbox(ctx context.Context, cli docker.Docker) error {
c, err := docker.GetSandbox(ctx, cli)
if err != nil {
return err
}
if c != nil {
if err := cli.ContainerRemove(context.Background(), c.ID, types.ContainerRemoveOptions{
Force: true,
}); err != nil {
return err
}
}
if err := configutil.ConfigCleanup(); err != nil {
fmt.Printf("Config cleanup failed. Which Failed due to %v \n ", err)
}
if err := removeSandboxKubeContext(); err != nil {
fmt.Printf("Kubecontext cleanup failed. Which Failed due to %v \n ", err)
}
fmt.Printf("%v %v Sandbox cluster is removed successfully. \n", emoji.Broom, emoji.Broom)
return nil
}

func removeSandboxKubeContext() error {
k8sCtxMgr := k8s.NewK8sContextManager()
return k8sCtxMgr.RemoveContext(sandboxContextName)
return sandbox.Teardown(ctx, cli)
}
40 changes: 5 additions & 35 deletions flytectl/cmd/sandbox/teardown_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package sandbox

import (
"context"
"fmt"
"testing"

"github.com/docker/docker/api/types"
Expand All @@ -17,47 +15,19 @@ import (
"github.com/stretchr/testify/mock"
)

var containers []types.Container

func TestTearDownFunc(t *testing.T) {
container1 := types.Container{
ID: "FlyteSandboxClusterName",
Names: []string{
docker.FlyteSandboxClusterName,
},
}
containers = append(containers, container1)

t.Run("Success", func(t *testing.T) {
ctx := context.Background()
mockDocker := &mocks.Docker{}
mockDocker.OnContainerList(ctx, types.ContainerListOptions{All: true}).Return(containers, nil)
mockDocker.OnContainerRemove(ctx, mock.Anything, types.ContainerRemoveOptions{Force: true}).Return(nil)
mockK8sContextMgr := &k8sMocks.ContextOps{}
k8s.ContextMgr = mockK8sContextMgr
mockK8sContextMgr.OnRemoveContextMatch(mock.Anything).Return(nil)
err := tearDownSandbox(ctx, mockDocker)
assert.Nil(t, err)
})
t.Run("Error", func(t *testing.T) {
ctx := context.Background()
mockDocker := &mocks.Docker{}
mockDocker.OnContainerList(ctx, types.ContainerListOptions{All: true}).Return(containers, nil)
mockDocker.OnContainerRemove(ctx, mock.Anything, types.ContainerRemoveOptions{Force: true}).Return(fmt.Errorf("err"))
err := tearDownSandbox(ctx, mockDocker)
assert.NotNil(t, err)
})

}

func TestTearDownClusterFunc(t *testing.T) {
var containers []types.Container
_ = util.SetupFlyteDir()
_ = util.WriteIntoFile([]byte("data"), configutil.FlytectlConfig)
s := testutils.Setup()
ctx := s.Ctx
mockDocker := &mocks.Docker{}
mockDocker.OnContainerList(ctx, types.ContainerListOptions{All: true}).Return(containers, nil)
mockDocker.OnContainerRemove(ctx, mock.Anything, types.ContainerRemoveOptions{Force: true}).Return(nil)
mockK8sContextMgr := &k8sMocks.ContextOps{}
mockK8sContextMgr.OnRemoveContext(mock.Anything).Return(nil)
k8s.ContextMgr = mockK8sContextMgr

docker.Client = mockDocker
err := teardownSandboxCluster(ctx, []string{}, s.CmdCtx)
assert.Nil(t, err)
Expand Down
39 changes: 39 additions & 0 deletions flytectl/pkg/sandbox/teardown.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package sandbox

import (
"context"
"fmt"

"github.com/docker/docker/api/types"
"github.com/enescakir/emoji"
"github.com/flyteorg/flytectl/pkg/configutil"
"github.com/flyteorg/flytectl/pkg/docker"
"github.com/flyteorg/flytectl/pkg/k8s"
)

func Teardown(ctx context.Context, cli docker.Docker) error {
c, err := docker.GetSandbox(ctx, cli)
if err != nil {
return err
}
if c != nil {
if err := cli.ContainerRemove(context.Background(), c.ID, types.ContainerRemoveOptions{
Force: true,
}); err != nil {
return err
}
}
if err := configutil.ConfigCleanup(); err != nil {
fmt.Printf("Config cleanup failed. Which Failed due to %v \n ", err)
}
if err := removeSandboxKubeContext(); err != nil {
fmt.Printf("Kubecontext cleanup failed. Which Failed due to %v \n ", err)
}
fmt.Printf("%v %v Sandbox cluster is removed successfully. \n", emoji.Broom, emoji.Broom)
return nil
}

func removeSandboxKubeContext() error {
k8sCtxMgr := k8s.NewK8sContextManager()
return k8sCtxMgr.RemoveContext(sandboxContextName)
}
48 changes: 48 additions & 0 deletions flytectl/pkg/sandbox/teardown_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package sandbox

import (
"context"
"fmt"
"testing"

"github.com/docker/docker/api/types"
"github.com/flyteorg/flytectl/pkg/docker"
"github.com/flyteorg/flytectl/pkg/docker/mocks"
"github.com/flyteorg/flytectl/pkg/k8s"
k8sMocks "github.com/flyteorg/flytectl/pkg/k8s/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestTearDownFunc(t *testing.T) {
var containers []types.Container
container1 := types.Container{
ID: "FlyteSandboxClusterName",
Names: []string{
docker.FlyteSandboxClusterName,
},
}
containers = append(containers, container1)
ctx := context.Background()

mockDocker := &mocks.Docker{}
mockDocker.OnContainerList(ctx, types.ContainerListOptions{All: true}).Return(containers, nil)
mockDocker.OnContainerRemove(ctx, mock.Anything, types.ContainerRemoveOptions{Force: true}).Return(fmt.Errorf("err"))
err := Teardown(ctx, mockDocker)
assert.NotNil(t, err)

mockDocker = &mocks.Docker{}
mockDocker.OnContainerList(ctx, types.ContainerListOptions{All: true}).Return(nil, fmt.Errorf("err"))
err = Teardown(ctx, mockDocker)
assert.NotNil(t, err)

mockDocker = &mocks.Docker{}
mockDocker.OnContainerList(ctx, types.ContainerListOptions{All: true}).Return(containers, nil)
mockDocker.OnContainerRemove(ctx, mock.Anything, types.ContainerRemoveOptions{Force: true}).Return(nil)
mockK8sContextMgr := &k8sMocks.ContextOps{}
mockK8sContextMgr.OnRemoveContext(mock.Anything).Return(nil)
k8s.ContextMgr = mockK8sContextMgr
err = Teardown(ctx, mockDocker)
assert.Nil(t, err)

}

0 comments on commit 09f9d21

Please sign in to comment.