Skip to content

Commit

Permalink
docker: adds intgration tests for executor
Browse files Browse the repository at this point in the history
  • Loading branch information
andrestc committed Mar 29, 2018
1 parent 1f5ebac commit c707224
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
11 changes: 10 additions & 1 deletion internal/docker/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package docker

import (
"fmt"
"path/filepath"
"strings"

"github.com/fsouza/go-dockerclient"
Expand All @@ -25,11 +26,19 @@ func (d *Executor) Execute(opts exec.ExecuteOptions) error {

func (d *Executor) ExecuteAsUser(user string, opts exec.ExecuteOptions) error {
cmd := append([]string{opts.Cmd}, opts.Args...)
binary := filepath.Base(cmd[0])
if binary != "bash" && binary != "sh" {
cmd = append([]string{"/bin/sh", "-lc"}, strings.Join(cmd, " "))
}
if opts.Dir != "" {
cmd = append(cmd[:2], fmt.Sprintf("cd %s && %s", opts.Dir, strings.Join(cmd[2:], " ")))
}
if len(opts.Envs) > 0 {
cmd = append(cmd[:2], fmt.Sprintf("%s %s", strings.Join(opts.Envs, " "), strings.Join(cmd[2:], " ")))
var exports []string
for _, e := range opts.Envs {
exports = append(exports, fmt.Sprintf("export %s && ", e))
}
cmd = append(cmd[:2], fmt.Sprintf("%s %s", strings.Join(exports, ""), strings.Join(cmd[2:], " ")))
}
e, err := d.Client.api.CreateExec(docker.CreateExecOptions{
Container: d.ContainerID,
Expand Down
16 changes: 16 additions & 0 deletions internal/docker/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,22 @@ func (s *S) TestSidecarExecuteIntegration(c *check.C) {
Envs: []string{"MYENV=myval", "ANOTHERENV=anotherval"},
expectedOut: "myval\n",
},
{
Name: "dir-env",
Cmd: "/bin/sh",
Args: []string{"-lc", "pwd"},
Envs: []string{"MYDIR=/etc"},
Dir: "$MYDIR",
expectedOut: "/etc\n",
},

{
Name: "dir-env-non-bash",
Cmd: "pwd",
Envs: []string{"MYDIR=/etc"},
Dir: "$MYDIR",
expectedOut: "/etc\n",
},
}

for _, t := range tt {
Expand Down

0 comments on commit c707224

Please sign in to comment.