Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Initial setup for integration tests #80

Merged
merged 19 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Cleanup when commands are executed in sh or not
  • Loading branch information
cmoog committed Jul 29, 2020
commit 18b5f50abcca7b5c7e79cd46ee93b084be0bf5fd
2 changes: 1 addition & 1 deletion ci/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func init() {
// write session tokens to the given container runner
func headlessLogin(ctx context.Context, t *testing.T, runner *tcli.ContainerRunner) {
creds := login(ctx, t)
cmd := exec.CommandContext(ctx, "mkdir -p ~/.config/coder && cat > ~/.config/coder/session")
cmd := exec.CommandContext(ctx, "sh", "-c", "mkdir -p ~/.config/coder && cat > ~/.config/coder/session")

// !IMPORTANT: be careful that this does not appear in logs
cmd.Stdin = strings.NewReader(creds.token)
Expand Down
2 changes: 1 addition & 1 deletion ci/tcli/doc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package tcli provides a framework for CLI integration testing.
// Execute commands on the raw host of inside docker container.
// Execute commands on the raw host or inside a docker container.
// Define custom Assertion types to extend test functionality.
package tcli
71 changes: 31 additions & 40 deletions ci/tcli/tcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,31 +102,48 @@ func (r *ContainerRunner) Close() error {
return nil
}

// Run executes the given command in the runtime container with reasonable defaults.
// "command" is executed in a shell as an argument to "sh -c".
func (r *ContainerRunner) Run(ctx context.Context, command string) *Assertable {
cmd := exec.CommandContext(ctx,
"docker", "exec", "-i", r.name,
"sh", "-c", command,
)

return &Assertable{
cmd: cmd,
tname: command,
}
}

// RunCmd lifts the given *exec.Cmd into the runtime container
func (r *ContainerRunner) RunCmd(cmd *exec.Cmd) *Assertable {
path, _ := exec.LookPath("docker")
cmd.Path = path
command := strings.Join(cmd.Args, " ")
cmd.Args = append([]string{"docker", "exec", "-i", r.name}, cmd.Args...)

return &Assertable{
cmd: cmd,
tname: command,
}
}

// HostRunner executes command tests on the host, outside of a container
type HostRunner struct{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you foresee us using HostRunner in the coder-cli tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not at the moment, but I do think it has a place in tcli as an escape hatch.


// Run executes the given command on the host
// Run executes the given command on the host.
// "command" is executed in a shell as an argument to "sh -c".
func (r *HostRunner) Run(ctx context.Context, command string) *Assertable {
var (
args []string
path string
parts = strings.Split(command, " ")
)
if len(parts) > 0 {
path = parts[0]
}
if len(parts) > 1 {
args = parts[1:]
}
cmd := exec.CommandContext(ctx, path, args...)
cmd := exec.CommandContext(ctx, "sh", "-c", command)

return &Assertable{
cmd: cmd,
tname: command,
}
}

// RunCmd executes the given command on the host
// RunCmd executes the given *exec.Cmd on the host
func (r *HostRunner) RunCmd(cmd *exec.Cmd) *Assertable {
return &Assertable{
cmd: cmd,
Expand All @@ -145,32 +162,6 @@ type Assertable struct {
tname string
}

// Run executes the given command in the runtime container with reasonable defaults
func (r *ContainerRunner) Run(ctx context.Context, command string) *Assertable {
cmd := exec.CommandContext(ctx,
"docker", "exec", "-i", r.name,
"sh", "-c", command,
)

return &Assertable{
cmd: cmd,
tname: command,
}
}

// RunCmd lifts the given *exec.Cmd into the runtime container
func (r *ContainerRunner) RunCmd(cmd *exec.Cmd) *Assertable {
path, _ := exec.LookPath("docker")
cmd.Path = path
command := strings.Join(cmd.Args, " ")
cmd.Args = []string{"docker", "exec", "-i", r.name, "sh", "-c", command}

return &Assertable{
cmd: cmd,
tname: command,
}
}

// Assert runs the Assertable and
func (a Assertable) Assert(t *testing.T, option ...Assertion) {
slog.Helper()
Expand Down