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
Add host runner
  • Loading branch information
cmoog committed Jul 28, 2020
commit bf3166656651cdde89f2aee5a1a06b480fa6e139
4 changes: 2 additions & 2 deletions ci/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func init() {
func TestTCli(t *testing.T) {
ctx := context.Background()

container, err := tcli.NewRunContainer(ctx, &tcli.ContainerConfig{
container, err := tcli.NewContainerRunner(ctx, &tcli.ContainerConfig{
Image: "ubuntu:latest",
Name: "test-container",
BindMounts: map[string]string{
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestTCli(t *testing.T) {
func TestCoderCLI(t *testing.T) {
ctx := context.Background()

c, err := tcli.NewRunContainer(ctx, &tcli.ContainerConfig{
c, err := tcli.NewContainerRunner(ctx, &tcli.ContainerConfig{
Image: "ubuntu:latest",
Name: "test-container",
BindMounts: map[string]string{
Expand Down
85 changes: 63 additions & 22 deletions ci/tcli/tcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io"
"os/exec"
"regexp"
"strings"
Expand All @@ -14,13 +15,18 @@ import (
"golang.org/x/xerrors"
)

// RunContainer specifies a runtime container for performing command tests
type RunContainer struct {
name string
ctx context.Context
var (
_ runnable = &ContainerRunner{}
_ runnable = &HostRunner{}
)

type runnable interface {
Run(ctx context.Context, command string) *Assertable
RunCmd(cmd *exec.Cmd) *Assertable
io.Closer
}

// ContainerConfig describes the RunContainer configuration schema for initializing a testing environment
// ContainerConfig describes the ContainerRunner configuration schema for initializing a testing environment
type ContainerConfig struct {
Name string
Image string
Expand All @@ -42,8 +48,14 @@ func preflightChecks() error {
return nil
}

// NewRunContainer starts a new docker container for executing command tests
func NewRunContainer(ctx context.Context, config *ContainerConfig) (*RunContainer, error) {
// ContainerRunner specifies a runtime container for performing command tests
type ContainerRunner struct {
name string
ctx context.Context
}

// NewContainerRunner starts a new docker container for executing command tests
func NewContainerRunner(ctx context.Context, config *ContainerConfig) (*ContainerRunner, error) {
if err := preflightChecks(); err != nil {
return nil, err
}
Expand All @@ -65,14 +77,14 @@ func NewRunContainer(ctx context.Context, config *ContainerConfig) (*RunContaine
config.Name, string(out), err)
}

return &RunContainer{
return &ContainerRunner{
name: config.Name,
ctx: ctx,
}, nil
}

// Close kills and removes the command execution testing container
func (r *RunContainer) Close() error {
func (r *ContainerRunner) Close() error {
cmd := exec.CommandContext(r.ctx,
"sh", "-c", strings.Join([]string{
"docker", "kill", r.name, "&&",
Expand All @@ -88,43 +100,72 @@ func (r *RunContainer) Close() error {
return nil
}

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.


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:]
}
return &Assertable{
cmd: exec.CommandContext(ctx, path, args...),
tname: command,
}
}

func (r *HostRunner) RunCmd(cmd *exec.Cmd) *Assertable {
return &Assertable{
cmd: cmd,
tname: strings.Join(cmd.Args, " "),
}
}

func (r *HostRunner) Close() error {
return nil
}

// Assertable describes an initialized command ready to be run and asserted against
type Assertable struct {
cmd *exec.Cmd
ctx context.Context
container *RunContainer
cmd *exec.Cmd
tname string
}

// Run executes the given command in the runtime container with reasonable defaults
func (r *RunContainer) Run(ctx context.Context, command string) *Assertable {
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,
ctx: ctx,
container: r,
cmd: cmd,
tname: command,
}
}

// RunCmd lifts the given *exec.Cmd into the runtime container
func (r *RunContainer) RunCmd(cmd *exec.Cmd) *Assertable {
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, "sh", "-c", command})
cmd.Args = []string{"docker", "exec", "-i", r.name, "sh", "-c", command}

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

// Assert runs the Assertable and
func (a Assertable) Assert(t *testing.T, option ...Assertion) {
t.Run(strings.Join(a.cmd.Args[6:], " "), func(t *testing.T) {
t.Run(a.tname, func(t *testing.T) {
var cmdResult CommandResult

var (
Expand Down Expand Up @@ -225,7 +266,7 @@ func StdoutEmpty() Assertion {
}

// GetResult offers an escape hatch from tcli
// The passed pointer will be assigned to the commands *CommandResult
// The pointer passed as "result" will be assigned to the command's *CommandResult
func GetResult(result **CommandResult) Assertion {
return simpleFuncAssert{
valid: func(r *CommandResult) error {
Expand Down