Skip to content
Permalink
Browse files

refactor cmd integration tests to use ephemeral daemon.

  • Loading branch information
raulk committed Dec 17, 2019
1 parent d5bb0ee commit 81ce15b31a6566f38ab904316ecc16d9accf394d
@@ -16,12 +16,32 @@ var Commands = []cli.Command{
DaemonCommand,
}

func setupClient() (*client.Client, error) {
envcfg, err := config.GetEnvConfig()
if err != nil {
return nil, err
var Flags = []cli.Flag{
cli.BoolFlag{
Name: "v",
Usage: "verbose output (equivalent to DEBUG log level)",
},
cli.BoolFlag{
Name: "vv",
Usage: "super verbose output (equivalent to DEBUG log level for now, it may accommodate TRACE in the future)",
},
cli.StringFlag{
Name: "endpoint",
Usage: "set the daemon endpoint URI (overrides .env.toml)",
},
}

func setupClient(c *cli.Context) (*client.Client, error) {
endpoint := c.Parent().String("endpoint")

if endpoint == "" {
envcfg, err := config.GetEnvConfig()
if err != nil {
return nil, err
}
endpoint = envcfg.Client.Endpoint
}

api := client.New(envcfg.Client.Endpoint)
api := client.New(endpoint)
return api, nil
}
@@ -61,7 +61,7 @@ func buildCommand(c *cli.Context) error {
builder = c.Generic("builder").(*EnumValue).String()
)

api, err := setupClient()
api, err := setupClient(c)
if err != nil {
return err
}
@@ -2,12 +2,12 @@ package cmd

import (
"context"
"github.com/ipfs/testground/pkg/server"
"net/http"
"time"

"github.com/ipfs/testground/pkg/config"
"github.com/ipfs/testground/pkg/logging"
"github.com/ipfs/testground/pkg/server"
"github.com/urfave/cli"
)

@@ -27,10 +27,11 @@ func daemonCommand(c *cli.Context) error {
return err
}

var (
listen = envcfg.Daemon.Listen
srv = server.New(listen)
)
listen := envcfg.Daemon.Listen
srv, err := server.New(listen)
if err != nil {
return err
}

exiting := make(chan struct{})
defer close(exiting)
@@ -54,8 +55,8 @@ func daemonCommand(c *cli.Context) error {
logging.S().Infow("rpc server stopped")
}()

logging.S().Infow("daemon listening", "addr", srv.Addr)
err = srv.ListenAndServe()
logging.S().Infow("listen and serve", "addr", srv.Addr)
err = srv.Start()
if err == http.ErrServerClosed {
err = nil
}
@@ -29,7 +29,7 @@ func describeCommand(c *cli.Context) error {

term := c.Args().First()

api, err := setupClient()
api, err := setupClient(c)
if err != nil {
return err
}

This file was deleted.

@@ -1,41 +1,32 @@
package cmd
package cmd_test

import (
"testing"

"github.com/urfave/cli"
)

func TestBuildExecGo(t *testing.T) {
app := cli.NewApp()
app.Name = "testground"
app.Commands = Commands

err := app.Run([]string{
"testground",
err := runSingle(t,
"build",
"placebo",
"--builder",
"exec:go",
})
)

if err != nil {
t.Fatal(err)
}
}

func TestBuildDockerGo(t *testing.T) {
app := cli.NewApp()
app.Name = "testground"
app.Commands = Commands

err := app.Run([]string{
"testground",
// TODO: this test assumes that docker is running locally, and that we can
// pick the .env.toml file this way, in case the user has defined a custom
// docker endpoint. I don't think those assumptions stand.
err := runSingle(t,
"build",
"placebo",
"--builder",
"docker:go",
})
)

if err != nil {
t.Fatal(err)
@@ -0,0 +1,36 @@
package cmd_test

import (
"context"
"testing"

"github.com/ipfs/testground/cmd"
"github.com/ipfs/testground/pkg/server"

"github.com/urfave/cli"
)

func runSingle(t *testing.T, args ...string) error {
t.Helper()

srv, err := server.New("localhost:0")
if err != nil {
t.Fatal(err)
}

err = srv.Start()
if err != nil {
t.Fatal(err)
}

defer srv.Shutdown(context.Background()) //nolint

app := cli.NewApp()
app.Name = "testground"
app.Commands = cmd.Commands
app.Flags = cmd.Flags
app.HideVersion = true

args = append([]string{"testground", "--endpoint", srv.Addr()}, args...)
return app.Run(args)
}
@@ -0,0 +1,27 @@
package cmd_test

import (
"testing"
)

func TestDescribeExistingPlan(t *testing.T) {
err := runSingle(t,
"describe",
"placebo",
)

if err != nil {
t.Fatal(err)
}
}

func TestDescribeInexistentPlan(t *testing.T) {
err := runSingle(t,
"describe",
"i-do-not-exist",
)

if err == nil {
t.Fatal("expected to get an err, due to non-existing test plan, but got nil")
}
}
@@ -0,0 +1,15 @@
package cmd_test

import (
"testing"
)

func TestList(t *testing.T) {
err := runSingle(t,
"list",
)

if err != nil {
t.Fatal(err)
}
}
@@ -1,87 +1,65 @@
package cmd
package cmd_test

import (
"testing"

"github.com/urfave/cli"
)

func TestAbortedTestShouldFailLocal(t *testing.T) {
app := cli.NewApp()
app.Name = "testground"
app.Commands = Commands

err := app.Run([]string{
"testground",
err := runSingle(t,
"run",
"placebo/abort",
"--builder",
"exec:go",
"--runner",
"local:exec",
})
)

if err == nil {
t.Fail()
}
}

func TestAbortedTestShouldFailDocker(t *testing.T) {
app := cli.NewApp()
app.Name = "testground"
app.Commands = Commands

err := app.Run([]string{
"testground",
err := runSingle(t,
"run",
"placebo/abort",
"--builder",
"docker:go",
"--runner",
"local:docker",
})
)

if err == nil {
t.Fail()
}
}

func TestIncompatibleRun(t *testing.T) {
app := cli.NewApp()
app.Name = "testground"
app.Commands = Commands

err := app.Run([]string{
"testground",
err := runSingle(t,
"run",
"placebo/ok",
"--builder",
"exec:go",
"--runner",
"local:docker",
})
)

if err == nil {
t.Fatal("expected to get an err, due to incompatible builder and runner, but got nil")
t.Fail()
}
}

func TestCompatibleRun(t *testing.T) {
app := cli.NewApp()
app.Name = "testground"
app.Commands = Commands

err := app.Run([]string{
"testground",
err := runSingle(t,
"run",
"placebo/ok",
"--builder",
"exec:go",
"--runner",
"local:exec",
})
)

if err != nil {
t.Fatal(err)
t.Fail()
}
}
@@ -19,7 +19,7 @@ func listCommand(c *cli.Context) error {
ctx, cancel := context.WithCancel(ProcessContext())
defer cancel()

api, err := setupClient()
api, err := setupClient(c)
if err != nil {
return err
}

This file was deleted.

0 comments on commit 81ce15b

Please sign in to comment.
You can’t perform that action at this time.