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 initial coder tests
  • Loading branch information
cmoog committed Jul 28, 2020
commit 149586748b7e795d90756f7884625e09aaf2e4d4
63 changes: 53 additions & 10 deletions ci/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,37 @@ import (
"cdr.dev/slog/sloggers/slogtest/assert"
)

func build(t *testing.T, path string) {
func build(path string) error {
cmd := exec.Command(
"sh", "-c",
fmt.Sprintf("cd ../../ && go build -o %s ./cmd/coder", path),
)
cmd.Env = append(os.Environ(), "GOOS=linux", "CGO_ENABLED=0")

out, err := cmd.CombinedOutput()
t.Logf("%s", string(out))
assert.Success(t, "build go binary", err)
_, err := cmd.CombinedOutput()
if err != nil {
return err
}
return nil
}

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

func init() {
cwd, err := os.Getwd()
assert.Success(t, "get working dir", err)
if err != nil {
panic(err)
}

binpath = filepath.Join(cwd, "bin", "coder")
err = build(binpath)
if err != nil {
panic(err)
}
}

binpath := filepath.Join(cwd, "bin", "coder")
build(t, binpath)
func TestTCli(t *testing.T) {
ctx := context.Background()

container, err := tcli.NewRunContainer(ctx, &tcli.ContainerConfig{
Image: "ubuntu:latest",
Expand All @@ -42,7 +53,6 @@ func TestTCli(t *testing.T) {
binpath: "/bin/coder",
},
})

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

Expand Down Expand Up @@ -73,4 +83,37 @@ func TestTCli(t *testing.T) {
tcli.StdoutMatches("/bin/coder"),
tcli.StderrEmpty(),
)

container.Run(ctx, "coder version").Assert(t,
tcli.StderrEmpty(),
tcli.Success(),
tcli.StdoutMatches("linux"),
)
}

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

c, err := tcli.NewRunContainer(ctx, &tcli.ContainerConfig{
Image: "ubuntu:latest",
Name: "test-container",
BindMounts: map[string]string{
binpath: "/bin/coder",
},
})
assert.Success(t, "new run container", err)
defer c.Close()

c.Run(ctx, "coder version").Assert(t,
tcli.StderrEmpty(),
tcli.Success(),
tcli.StdoutMatches("linux"),
)

c.Run(ctx, "coder help").Assert(t,
tcli.Success(),
tcli.StderrMatches("Commands:"),
tcli.StderrMatches("Usage: coder"),
tcli.StdoutEmpty(),
)
}
59 changes: 31 additions & 28 deletions ci/tcli/tcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,41 +118,44 @@ func (r *RunContainer) RunCmd(cmd *exec.Cmd) *Assertable {
}

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

var (
stdout bytes.Buffer
stderr bytes.Buffer
)
var (
stdout bytes.Buffer
stderr bytes.Buffer
)

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

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

if exitErr, ok := err.(*exec.ExitError); ok {
cmdResult.ExitCode = exitErr.ExitCode()
} else if err != nil {
cmdResult.ExitCode = -1
} else {
cmdResult.ExitCode = 0
}
if exitErr, ok := err.(*exec.ExitError); ok {
cmdResult.ExitCode = exitErr.ExitCode()
} else if err != nil {
cmdResult.ExitCode = -1
} else {
cmdResult.ExitCode = 0
}

cmdResult.Stdout = stdout.Bytes()
cmdResult.Stderr = stderr.Bytes()
cmdResult.Stdout = stdout.Bytes()
cmdResult.Stderr = stderr.Bytes()

for ix, o := range option {
name := fmt.Sprintf("assertion_#%v", ix)
if named, ok := o.(Named); ok {
name = named.Name()
for ix, o := range option {
name := fmt.Sprintf("assertion_#%v", ix)
if named, ok := o.(Named); ok {
name = named.Name()
}
t.Run(name, func(t *testing.T) {
t.Parallel()
err := o.Valid(cmdResult)
assert.Success(t, name, err)
})
}
t.Run(name, func(t *testing.T) {
err := o.Valid(cmdResult)
assert.Success(t, name, err)
})
}
})
}

type Assertion interface {
Expand Down