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
Next Next commit
Initial setup for integration tests
  • Loading branch information
cmoog committed Jul 28, 2020
commit 8b3f0563861ef78a5866031f1e5c714a983faa77
28 changes: 28 additions & 0 deletions ci/integration/integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package integration

import (
"context"
"testing"
"time"

"cdr.dev/coder-cli/ci/tcli"
)

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

container := tcli.NewRunContainer(ctx, "", "test-container")

container.Run(ctx, "echo testing").Assert(t,
tcli.Success(),
tcli.StderrEmpty(),
tcli.StdoutMatches("esting"),
)

container.Run(ctx, "sleep 1.5 && echo 1>&2 stderr-message").Assert(t,
tcli.Success(),
tcli.StdoutEmpty(),
tcli.StderrMatches("message"),
tcli.DurationGreaterThan(time.Second),
)
}
212 changes: 212 additions & 0 deletions ci/tcli/tcli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package tcli

import (
"bytes"
"context"
"fmt"
"os/exec"
"regexp"
"testing"
"time"

"cdr.dev/slog/sloggers/slogtest/assert"
"golang.org/x/xerrors"
)

type RunContainer struct {
}

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

func (r RunContainer) Teardown() error {
// TODO: teardown run environment
return nil
}

type Assertable struct {
cmd string
ctx context.Context
}

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

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

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

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

start := time.Now()
err := 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
}

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()
}
t.Run(name, func(t *testing.T) {
err := o.Valid(cmdResult)
assert.Success(t, name, err)
})
}
}

type Assertion interface {
Valid(r CommandResult) error
}

type Named interface {
Name() string
}

type CommandResult struct {
Stdout, Stderr []byte
ExitCode int
Duration time.Duration
}

type simpleFuncAssert struct {
valid func(r CommandResult) error
name string
}

func (s simpleFuncAssert) Valid(r CommandResult) error {
return s.valid(r)
}

func (s simpleFuncAssert) Name() string {
return s.name
}

func Success() Assertion {
return ExitCodeIs(0)
}

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 nil
},
name: fmt.Sprintf("exitcode"),
}
}

func StdoutEmpty() Assertion {
return simpleFuncAssert{
valid: func(r CommandResult) error {
return empty("stdout", r.Stdout)
},
name: fmt.Sprintf("stdout-empty"),
}
}

func StderrEmpty() Assertion {
return simpleFuncAssert{
valid: func(r CommandResult) error {
return empty("stderr", r.Stderr)
},
name: fmt.Sprintf("stderr-empty"),
}
}

func StdoutMatches(pattern string) Assertion {
return simpleFuncAssert{
valid: func(r CommandResult) error {
return matches("stdout", pattern, r.Stdout)
},
name: fmt.Sprintf("stdout-matches"),
}
}

func StderrMatches(pattern string) Assertion {
return simpleFuncAssert{
valid: func(r CommandResult) error {
return matches("stderr", pattern, r.Stderr)
},
name: fmt.Sprintf("stderr-matches"),
}
}

func CombinedMatches(pattern string) Assertion {
return simpleFuncAssert{
valid: func(r CommandResult) error {
//stdoutValid := StdoutMatches(pattern).Valid(r)
//stderrValid := StderrMatches(pattern).Valid(r)
// TODO: combine errors
return nil
},
name: fmt.Sprintf("combined-matches"),
}
}

func matches(name, pattern string, target []byte) error {
ok, err := regexp.Match(pattern, target)
if err != nil {
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 nil
}

func empty(name string, a []byte) error {
if len(a) > 0 {
return xerrors.Errorf("expected %s to be empty, got (%s)", name, string(a))
}
return nil
}

func DurationLessThan(dur time.Duration) Assertion {
return simpleFuncAssert{
valid: func(r CommandResult) error {
if r.Duration > dur {
return xerrors.Errorf("expected duration less than %s, took %s", dur.String(), r.Duration.String())
}
return nil
},
name: fmt.Sprintf("duration-lessthan"),
}
}

func DurationGreaterThan(dur time.Duration) Assertion {
return simpleFuncAssert{
valid: func(r CommandResult) error {
if r.Duration < dur {
return xerrors.Errorf("expected duration greater than %s, took %s", dur.String(), r.Duration.String())
}
return nil
},
name: fmt.Sprintf("duration-greaterthan"),
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module cdr.dev/coder-cli
go 1.14

require (
cdr.dev/slog v1.3.0
cdr.dev/wsep v0.0.0-20200728013649-82316a09813f
github.com/fatih/color v1.9.0 // indirect
github.com/gorilla/websocket v1.4.1
Expand Down