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 RunCmd manual command
  • Loading branch information
cmoog committed Jul 28, 2020
commit 2932fe9188ea0ca1ba28816792ef0b31e3928089
17 changes: 16 additions & 1 deletion ci/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package integration

import (
"context"
"os/exec"
"strings"
"testing"
"time"

Expand All @@ -12,7 +14,11 @@ import (
func TestTCli(t *testing.T) {
ctx := context.Background()

container, err := tcli.NewRunContainer(ctx, "ubuntu:latest", "test-container")
container, err := tcli.NewRunContainer(ctx, &tcli.ContainerConfig{
Image: "ubuntu:latest",
Name: "test-container",
})

assert.Success(t, "new run container", err)
defer container.Close()

Expand All @@ -28,4 +34,13 @@ func TestTCli(t *testing.T) {
tcli.StderrMatches("message"),
tcli.DurationGreaterThan(time.Second),
)

cmd := exec.CommandContext(ctx, "cat")
cmd.Stdin = strings.NewReader("testing")

container.RunCmd(cmd).Assert(t,
tcli.Success(),
tcli.StderrEmpty(),
tcli.StdoutMatches("testing"),
)
}
84 changes: 65 additions & 19 deletions ci/tcli/tcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,51 @@ type RunContainer struct {
ctx context.Context
}

func NewRunContainer(ctx context.Context, image, name string) (*RunContainer, error) {
cmd := exec.CommandContext(ctx,
"docker", "run",
"--name", name,
type ContainerConfig struct {
Name string
Image string
Mounts map[string]string
}

func mountArgs(m map[string]string) (args []string) {
for src, dest := range m {
args = append(args, "--mount", fmt.Sprintf("source=%s,target=%s", src, dest))
}
return args
}

func preflightChecks() error {
_, err := exec.LookPath("docker")
if err != nil {
return xerrors.Errorf(`"docker" not found in $PATH`)
}
return nil
}

func NewRunContainer(ctx context.Context, config *ContainerConfig) (*RunContainer, error) {
if err := preflightChecks(); err != nil {
return nil, err
}

args := []string{
"run",
"--name", config.Name,
"-it", "-d",
image,
)
}
args = append(args, mountArgs(config.Mounts)...)
args = append(args, config.Image)

cmd := exec.CommandContext(ctx, "docker", args...)

out, err := cmd.CombinedOutput()
if err != nil {
return nil, xerrors.Errorf(
"failed to start testing container %q, (%s): %w",
name, string(out), err)
config.Name, string(out), err)
}

return &RunContainer{
name: name,
name: config.Name,
ctx: ctx,
}, nil
}
Expand All @@ -57,36 +85,51 @@ func (r *RunContainer) Close() error {
}

type Assertable struct {
cmd string
cmd *exec.Cmd
ctx context.Context
container *RunContainer
}

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

// RunCmd lifts the given *exec.Cmd into the runtime container
func (r *RunContainer) 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})

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

func (a Assertable) Assert(t *testing.T, option ...Assertion) {
var cmdResult CommandResult

cmd := exec.CommandContext(a.ctx,
"docker", "exec", a.container.name,
"sh", "-c", a.cmd,
)
var (
stdout bytes.Buffer
stderr bytes.Buffer
)

cmd.Stdout = &stdout
cmd.Stderr = &stderr
a.cmd.Stdout = &stdout
a.cmd.Stderr = &stderr

start := time.Now()
err := cmd.Run()
err := a.cmd.Run()
cmdResult.Duration = time.Since(start)

if exitErr, ok := err.(*exec.ExitError); ok {
Expand Down Expand Up @@ -147,7 +190,7 @@ func ExitCodeIs(code int) Assertion {
return simpleFuncAssert{
valid: func(r CommandResult) error {
if r.ExitCode != code {
return xerrors.Errorf("exit code of %s expected, got %v", code, r.ExitCode)
return xerrors.Errorf("exit code of %v expected, got %v", code, r.ExitCode)
}
return nil
},
Expand Down Expand Up @@ -209,7 +252,10 @@ func matches(name, pattern string, target []byte) error {
return xerrors.Errorf("failed to attempt regexp match: %w", err)
}
if !ok {
return xerrors.Errorf("expected to find pattern (%s) in %s, no match found", pattern, name)
return xerrors.Errorf(
"expected to find pattern (%s) in %s, no match found in (%v)",
pattern, name, string(target),
)
}
return nil
}
Expand Down