-
Notifications
You must be signed in to change notification settings - Fork 669
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added exec command in sandbox (#122)
* Added exec command Signed-off-by: Yuvraj <[email protected]>
- Loading branch information
Showing
10 changed files
with
348 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package sandbox | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
cmdCore "github.com/flyteorg/flytectl/cmd/core" | ||
"github.com/flyteorg/flytectl/pkg/docker" | ||
) | ||
|
||
const ( | ||
execShort = "Execute any command in sandbox" | ||
execLong = ` | ||
Execute command will Will run non-interactive commands and return immediately with the output. | ||
:: | ||
bin/flytectl sandbox exec -- ls -al | ||
Usage` | ||
) | ||
|
||
func sandboxClusterExec(ctx context.Context, args []string, cmdCtx cmdCore.CommandContext) error { | ||
cli, err := docker.GetDockerClient() | ||
if err != nil { | ||
return err | ||
} | ||
if len(args) > 0 { | ||
return Execute(ctx, cli, args) | ||
} | ||
return fmt.Errorf("missing argument. Please check usage examples by running flytectl sandbox exec --help") | ||
} | ||
|
||
func Execute(ctx context.Context, cli docker.Docker, args []string) error { | ||
c := docker.GetSandbox(ctx, cli) | ||
if c != nil { | ||
exec, err := docker.ExecCommend(ctx, cli, c.ID, args) | ||
if err != nil { | ||
return err | ||
} | ||
if err := docker.InspectExecResp(ctx, cli, exec.ID); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package sandbox | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"fmt" | ||
"io" | ||
"strings" | ||
"testing" | ||
|
||
cmdCore "github.com/flyteorg/flytectl/cmd/core" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/docker/docker/api/types" | ||
"github.com/flyteorg/flytectl/pkg/docker" | ||
"github.com/flyteorg/flytectl/pkg/docker/mocks" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
func TestSandboxClusterExec(t *testing.T) { | ||
mockDocker := &mocks.Docker{} | ||
mockOutStream := new(io.Writer) | ||
ctx := context.Background() | ||
cmdCtx := cmdCore.NewCommandContext(nil, *mockOutStream) | ||
reader := bufio.NewReader(strings.NewReader("test")) | ||
|
||
mockDocker.OnContainerList(ctx, types.ContainerListOptions{All: true}).Return([]types.Container{ | ||
{ | ||
ID: docker.FlyteSandboxClusterName, | ||
Names: []string{ | ||
docker.FlyteSandboxClusterName, | ||
}, | ||
}, | ||
}, nil) | ||
docker.ExecConfig.Cmd = []string{"ls -al"} | ||
mockDocker.OnContainerExecCreateMatch(ctx, mock.Anything, docker.ExecConfig).Return(types.IDResponse{}, nil) | ||
mockDocker.OnContainerExecInspectMatch(ctx, mock.Anything).Return(types.ContainerExecInspect{}, nil) | ||
mockDocker.OnContainerExecAttachMatch(ctx, mock.Anything, types.ExecStartCheck{}).Return(types.HijackedResponse{ | ||
Reader: reader, | ||
}, fmt.Errorf("Test")) | ||
docker.Client = mockDocker | ||
err := sandboxClusterExec(ctx, []string{"ls -al"}, cmdCtx) | ||
|
||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestSandboxClusterExecWithoutCmd(t *testing.T) { | ||
mockDocker := &mocks.Docker{} | ||
mockOutStream := new(io.Writer) | ||
ctx := context.Background() | ||
cmdCtx := cmdCore.NewCommandContext(nil, *mockOutStream) | ||
reader := bufio.NewReader(strings.NewReader("test")) | ||
|
||
mockDocker.OnContainerList(ctx, types.ContainerListOptions{All: true}).Return([]types.Container{ | ||
{ | ||
ID: docker.FlyteSandboxClusterName, | ||
Names: []string{ | ||
docker.FlyteSandboxClusterName, | ||
}, | ||
}, | ||
}, nil) | ||
docker.ExecConfig.Cmd = []string{} | ||
mockDocker.OnContainerExecCreateMatch(ctx, mock.Anything, docker.ExecConfig).Return(types.IDResponse{}, nil) | ||
mockDocker.OnContainerExecInspectMatch(ctx, mock.Anything).Return(types.ContainerExecInspect{}, nil) | ||
mockDocker.OnContainerExecAttachMatch(ctx, mock.Anything, types.ExecStartCheck{}).Return(types.HijackedResponse{ | ||
Reader: reader, | ||
}, fmt.Errorf("Test")) | ||
docker.Client = mockDocker | ||
err := sandboxClusterExec(ctx, []string{}, cmdCtx) | ||
|
||
assert.NotNil(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.