Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple start counter test #498

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
43 changes: 43 additions & 0 deletions test/e2etests.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"strings"
"time"

"github.com/deref/exo/test/tester"
Expand Down Expand Up @@ -85,6 +86,48 @@ var tests = map[string]tester.ExoTest{
return err
}

return nil
},
},
"start-counter": {
FixtureDir: "start-counter",
Test: func(ctx context.Context, t tester.ExoTester) error {
// FIXME: This doesn't work right now because we don't have an easy way of
// removing volumes that are still attached to a previous container.
if _, stderr, err := t.RunCmd(ctx, "docker", []string{"volume", "rm", "e2etest-start-counter"}); err != nil {
if !strings.Contains(stderr, "No such volume") {
return err
}
}

if _, _, err := t.RunExo(ctx, "init"); err != nil {
return err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this stuff is only used in tests, it's probably perfectly fine to simply panic on error inside RunExo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intent was to allow people to create end to end tests where failure is expected — which becomes a bit ugly if they have to recover from panics all the time.

}
if _, _, err := t.RunExo(ctx, "start"); err != nil {
return err
}

if err := t.WaitTillProcessesReachState(ctx, "running", []string{"counter"}); err != nil {
return err
}
if err := tester.ExpectResponse(ctx, "http://localhost:44225/count", "1"); err != nil {
return err
}

if _, _, err := t.RunExo(ctx, "stop"); err != nil {
return err
}
if _, _, err := t.RunExo(ctx, "start"); err != nil {
return err
}

if err := t.WaitTillProcessesReachState(ctx, "running", []string{"counter"}); err != nil {
return err
}
if err := tester.ExpectResponse(ctx, "http://localhost:44225/count", "2"); err != nil {
return err
}

return nil
},
},
Expand Down
3 changes: 0 additions & 3 deletions test/image/fixtures/simple-example/exo.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,4 @@ components {
}
env_file = "./env"
}

volume "logvolume01" {}

}
13 changes: 13 additions & 0 deletions test/image/fixtures/start-counter/exo.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
exo = "0.1"

components {
container "counter" {
image = "python:3"
working_dir = "/my-count"
command = "expr $(cat count) + 1 > count ; python3 -m http.server"
volumes = ["e2etest-start-counter:/my-count"]
ports = ["44225:8000"]
}

volume "e2etest-start-counter" {}
}
16 changes: 11 additions & 5 deletions test/tester/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (et ExoTester) RunTest(ctx context.Context, test ExoTest) (io.Reader, error
}

func (et ExoTester) WaitTillProcessesReachState(ctx context.Context, state string, names []string) error {
et.logger.Info("Waiting for ", names, " to reach state ", state)
errTimeout := fmt.Errorf("timed out waiting for %q to reach %s", strings.Join(names, ", "), state)
for {
processes, err := et.PS(ctx)
Expand Down Expand Up @@ -88,9 +89,9 @@ func (et ExoTester) WaitTillProcessesReachState(ctx context.Context, state strin
}

// Runs an exo CLI command and blocks until it terminates.
func (et ExoTester) RunExo(ctx context.Context, arguments ...string) (stdout, stderr string, err error) {
func (et ExoTester) RunCmd(ctx context.Context, program string, arguments []string) (stdout, stderr string, err error) {
path, _ := os.LookupEnv("PATH")
cmd := exec.CommandContext(ctx, et.exoBinary, arguments...)
cmd := exec.CommandContext(ctx, program, arguments...)
cmd.Dir = et.fixtureDir
cmd.Env = append(cmd.Env, "EXO_HOME="+et.exoHome)
cmd.Env = append(cmd.Env, "PATH="+path)
Expand All @@ -101,14 +102,19 @@ func (et ExoTester) RunExo(ctx context.Context, arguments ...string) (stdout, st
stderrWriter := io.MultiWriter(&stderrBuffer, logWriter)
cmd.Stderr = stderrWriter
cmd.Stdout = stdoutWriter
et.logger.Debug("Running exo ", cmd.Args)
et.logger.Debug("env ", cmd.Env)
et.logger.Debug("wd ", cmd.Dir)
et.logger.Debug("Running ", cmd.Args)
et.logger.Debug("env: ", cmd.Env)
et.logger.Debug("working dir: ", cmd.Dir)
err = cmd.Run()
et.logger.Debug("exit code: ", cmd.ProcessState.ExitCode())
return stdoutBuffer.String(), stderrBuffer.String(), err
}

// Runs an exo CLI command and blocks until it terminates.
func (et ExoTester) RunExo(ctx context.Context, arguments ...string) (stdout, stderr string, err error) {
return et.RunCmd(ctx, et.exoBinary, arguments)
}

type psProcessInfo struct {
Name string
ID string
Expand Down
2 changes: 2 additions & 0 deletions test/tester/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tester

import (
"bytes"
"context"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -42,6 +43,7 @@ func ExpectResponse(ctx context.Context, endpoint string, expectedResponse strin
return fmt.Errorf("failed to read response body: %w", err)
}

body = bytes.TrimSpace(body)
if string(body) != expectedResponse {
return fmt.Errorf("expected response body to be %q, got %q", expectedResponse, string(body))
}
Expand Down