Exec should be easier to reuse as a command

We have a few use cases where we want to have shortcut commands
for common behavior. In these cases, we want to default commands
with a simpler syntax, so being able to reuse Exec from code but
not share the same arguments is useful.

In this case, we're introducing 'rsh' which tries to run exec -itp --
bash.
This commit is contained in:
Clayton Coleman
2015-07-22 22:05:04 -04:00
parent 94d62eba21
commit 0b5410f30f
2 changed files with 139 additions and 72 deletions

View File

@@ -44,7 +44,7 @@ func (f *fakeRemoteExecutor) Execute(req *client.Request, config *client.Config,
func TestPodAndContainer(t *testing.T) {
tests := []struct {
args []string
p *execParams
p *ExecOptions
name string
expectError bool
expectedPod string
@@ -52,42 +52,42 @@ func TestPodAndContainer(t *testing.T) {
expectedArgs []string
}{
{
p: &execParams{},
p: &ExecOptions{},
expectError: true,
name: "empty",
},
{
p: &execParams{podName: "foo"},
p: &ExecOptions{PodName: "foo"},
expectError: true,
name: "no cmd",
},
{
p: &execParams{podName: "foo", containerName: "bar"},
p: &ExecOptions{PodName: "foo", ContainerName: "bar"},
expectError: true,
name: "no cmd, w/ container",
},
{
p: &execParams{podName: "foo"},
p: &ExecOptions{PodName: "foo"},
args: []string{"cmd"},
expectedPod: "foo",
expectedArgs: []string{"cmd"},
name: "pod in flags",
},
{
p: &execParams{},
p: &ExecOptions{},
args: []string{"foo"},
expectError: true,
name: "no cmd, w/o flags",
},
{
p: &execParams{},
p: &ExecOptions{},
args: []string{"foo", "cmd"},
expectedPod: "foo",
expectedArgs: []string{"cmd"},
name: "cmd, w/o flags",
},
{
p: &execParams{containerName: "bar"},
p: &ExecOptions{ContainerName: "bar"},
args: []string{"foo", "cmd"},
expectedPod: "foo",
expectedContainer: "bar",
@@ -96,22 +96,34 @@ func TestPodAndContainer(t *testing.T) {
},
}
for _, test := range tests {
f, tf, codec := NewAPIFactory()
tf.Client = &client.FakeRESTClient{
Codec: codec,
Client: client.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { return nil, nil }),
}
tf.Namespace = "test"
tf.ClientConfig = &client.Config{}
cmd := &cobra.Command{}
podName, containerName, args, err := extractPodAndContainer(cmd, test.args, test.p)
if podName != test.expectedPod {
t.Errorf("expected: %s, got: %s (%s)", test.expectedPod, podName, test.name)
}
if containerName != test.expectedContainer {
t.Errorf("expected: %s, got: %s (%s)", test.expectedContainer, containerName, test.name)
}
options := test.p
err := options.Complete(f, cmd, test.args)
if test.expectError && err == nil {
t.Errorf("unexpected non-error (%s)", test.name)
}
if !test.expectError && err != nil {
t.Errorf("unexpected error: %v (%s)", err, test.name)
}
if !reflect.DeepEqual(test.expectedArgs, args) {
t.Errorf("expected: %v, got %v (%s)", test.expectedArgs, args, test.name)
if err != nil {
continue
}
if options.PodName != test.expectedPod {
t.Errorf("expected: %s, got: %s (%s)", test.expectedPod, options.PodName, test.name)
}
if options.ContainerName != test.expectedContainer {
t.Errorf("expected: %s, got: %s (%s)", test.expectedContainer, options.ContainerName, test.name)
}
if !reflect.DeepEqual(test.expectedArgs, options.Command) {
t.Errorf("expected: %v, got %v (%s)", test.expectedArgs, options.Command, test.name)
}
}
}
@@ -150,8 +162,8 @@ func TestExec(t *testing.T) {
return &http.Response{StatusCode: 200, Body: body}, nil
default:
// Ensures no GET is performed when deleting by name
t.Errorf("%s: unexpected request: %#v\n%#v", test.name, req.URL, req)
return nil, nil
t.Errorf("%s: unexpected request: %s %#v\n%#v", test.name, req.Method, req.URL, req)
return nil, fmt.Errorf("unexpected request")
}
}),
}
@@ -164,20 +176,30 @@ func TestExec(t *testing.T) {
if test.execErr {
ex.execErr = fmt.Errorf("exec error")
}
params := &execParams{
podName: "foo",
containerName: "bar",
params := &ExecOptions{
PodName: "foo",
ContainerName: "bar",
In: bufIn,
Out: bufOut,
Err: bufErr,
Executor: ex,
}
cmd := &cobra.Command{}
err := RunExec(f, cmd, bufIn, bufOut, bufErr, params, []string{"test", "command"}, ex)
if err := params.Complete(f, cmd, []string{"test", "command"}); err != nil {
t.Fatal(err)
}
err := params.Run()
if test.execErr && err != ex.execErr {
t.Errorf("%s: Unexpected exec error: %v", test.name, err)
}
if !test.execErr && ex.req.URL().Path != test.execPath {
t.Errorf("%s: Did not get expected path for exec request", test.name)
continue
}
if !test.execErr && err != nil {
t.Errorf("%s: Unexpected error: %v", test.name, err)
continue
}
if !test.execErr && ex.req.URL().Path != test.execPath {
t.Errorf("%s: Did not get expected path for exec request", test.name)
continue
}
}
}