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
Execute commands in container
  • Loading branch information
cmoog committed Jul 28, 2020
commit 8e8b215e1a5949c93611632749ad8f534d4bc0e1
5 changes: 4 additions & 1 deletion ci/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import (
"time"

"cdr.dev/coder-cli/ci/tcli"
"cdr.dev/slog/sloggers/slogtest/assert"
)

func TestTCli(t *testing.T) {
ctx := context.Background()

container := tcli.NewRunContainer(ctx, "", "test-container")
container, err := tcli.NewRunContainer(ctx, "ubuntu:latest", "test-container")
assert.Success(t, "new run container", err)
defer container.Close()

container.Run(ctx, "echo testing").Assert(t,
tcli.Success(),
Expand Down
58 changes: 46 additions & 12 deletions ci/tcli/tcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os/exec"
"regexp"
"strings"
"testing"
"time"

Expand All @@ -14,35 +15,68 @@ import (
)

type RunContainer struct {
name string
ctx context.Context
}

func NewRunContainer(ctx context.Context, image, name string) *RunContainer {
//exec.CommandContext(ctx, "docker", "start")
// TODO: startup docker container
return &RunContainer{}
func NewRunContainer(ctx context.Context, image, name string) (*RunContainer, error) {
cmd := exec.CommandContext(ctx,
"docker", "run",
"--name", name,
"-it", "-d",
image,
)

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

return &RunContainer{
name: name,
ctx: ctx,
}, nil
}

func (r RunContainer) Teardown() error {
// TODO: teardown run environment
func (r *RunContainer) Close() error {
cmd := exec.CommandContext(r.ctx,
"sh", "-c", strings.Join([]string{
"docker", "kill", r.name, "&&",
"docker", "rm", r.name,
}, " "))

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

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

func (*RunContainer) Run(ctx context.Context, cmd string) *Assertable {
func (r *RunContainer) Run(ctx context.Context, cmd string) *Assertable {
return &Assertable{
cmd: cmd,
ctx: ctx,
cmd: cmd,
ctx: ctx,
container: r,
}
}

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

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