Skip to content

Commit

Permalink
Enable end-to-end docker tests (#185)
Browse files Browse the repository at this point in the history
* Enable e2e docker tests

* Make e2e test more reliable
  • Loading branch information
phillipleblanc committed Sep 9, 2021
1 parent 07d1e4f commit 0efb825
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 98 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/e2e_test.yml
Expand Up @@ -206,7 +206,7 @@ jobs:
fail-fast: false
matrix:
algorithm: [vpg, dql]
context: [metal]
context: [metal, docker]
steps:
- uses: actions/checkout@v2

Expand Down
8 changes: 8 additions & 0 deletions .vscode/launch.json
Expand Up @@ -22,6 +22,14 @@
"program": "${workspaceFolder}/cmd/spice/main.go",
"args": ["action", "add", "hold"]
},
{
"name": "spice e2e test docker",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/test/e2e/e2e_test.go",
"args": ["-e2e"]
},
{
"name": "Test Current File",
"type": "go",
Expand Down
5 changes: 3 additions & 2 deletions pkg/cli/runtime/runtime.go
Expand Up @@ -6,6 +6,7 @@ import (
"os"

"github.com/spiceai/spiceai/pkg/context"
"github.com/spiceai/spiceai/pkg/util"
)

func Run(contextFlag string, manifestPath string) error {
Expand Down Expand Up @@ -52,10 +53,10 @@ func Run(contextFlag string, manifestPath string) error {
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout

err = cmd.Start()
err = util.RunCommand(cmd)
if err != nil {
return err
}

return cmd.Wait()
return nil
}
51 changes: 51 additions & 0 deletions pkg/util/command.go
@@ -0,0 +1,51 @@
package util

import (
"os"
"os/exec"
"os/signal"
"syscall"
)

func RunCommand(cmd *exec.Cmd) error {
if cmd == nil {
return nil
}

cmdErr := make(chan error, 1)
cmdStopped := make(chan bool, 1)
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGTERM, os.Interrupt)

err := cmd.Start()
if err != nil {
return err
}

go func() {
appErr := cmd.Wait()

if appErr != nil {
cmdErr <- appErr
}
cmdStopped <- true
sigCh <- os.Interrupt
}()

<-sigCh

if cmd.ProcessState == nil || !cmd.ProcessState.Exited() {
err := cmd.Process.Signal(os.Interrupt)
if err != nil {
return err
}
}

<-cmdStopped

if len(cmdErr) > 0 {
return <-cmdErr
}

return nil
}
21 changes: 18 additions & 3 deletions test/e2e/cli.go
Expand Up @@ -11,14 +11,29 @@ type cli struct {
}

func (c *cli) runCliCmd(args ...string) error {
cmd, err := c.startCliCmd(args...)
if err != nil {
return err
}

err = cmd.Wait()
if err != nil {
return err
}

return nil
}

func (c *cli) startCliCmd(args ...string) (*exec.Cmd, error) {
cmd := exec.Command(c.cliPath, args...)
cmd.Dir = c.workingDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()

err := cmd.Start()
if err != nil {
return err
return nil, err
}

return nil
return cmd, nil
}
32 changes: 12 additions & 20 deletions test/e2e/e2e_test.go
Expand Up @@ -26,7 +26,6 @@ var (
testDir string
repoRoot string
workingDirectory string
spicePodsDir string
runtimePath string
cliClient *cli
runtime *runtimeServer
Expand Down Expand Up @@ -78,14 +77,7 @@ func TestMain(m *testing.M) {
os.Exit(1)
}

stat, err := os.Stat(testDir)
if err != nil {
log.Println(err.Error())
os.Exit(1)
}

spicePodsDir = filepath.Join(testDir, ".spice", "pods")
err = os.MkdirAll(spicePodsDir, stat.Mode())
_, err = os.Stat(testDir)
if err != nil {
log.Println(err.Error())
os.Exit(1)
Expand All @@ -97,7 +89,11 @@ func TestMain(m *testing.M) {
}

runtime = &runtimeServer{
baseUrl: BaseUrl,
baseUrl: BaseUrl,
runtimePath: runtimePath,
workingDirectory: testDir,
cli: cliClient,
context: spicedContext,
}

err = copyFile(filepath.Join(repoRoot, "test/assets/data/csv/COINBASE_BTCUSD, 30.csv"), testDir)
Expand All @@ -106,12 +102,6 @@ func TestMain(m *testing.M) {
os.Exit(1)
}

err = copyFile(filepath.Join(workingDirectory, "pods/trader.yaml"), spicePodsDir)
if err != nil {
log.Println(err.Error())
os.Exit(1)
}

err = cliClient.runCliCmd("add", testPod)
if err != nil {
log.Println(err.Error())
Expand All @@ -134,7 +124,7 @@ func TestObservations(t *testing.T) {
return
}

runtimeCmd, err := runtime.startRuntime(runtimePath, testDir)
runtimeCmd, err := runtime.startRuntime()
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -170,6 +160,8 @@ func TestObservations(t *testing.T) {
t.Fatal(err)
}

time.Sleep(1 * time.Second)

observation, err = runtime.getObservations("trader")
if err != nil {
t.Fatal(err)
Expand All @@ -187,7 +179,7 @@ func TestTrainingOutput(t *testing.T) {
return
}

runtimeCmd, err := runtime.startRuntime(runtimePath, testDir)
runtimeCmd, err := runtime.startRuntime()
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -243,7 +235,7 @@ func TestImportExport(t *testing.T) {
return
}

runtimeCmd, err := runtime.startRuntime(runtimePath, testDir)
runtimeCmd, err := runtime.startRuntime()
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -297,7 +289,7 @@ func TestImportExport(t *testing.T) {
t.Fatal(err)
}

runtimeCmd, err = runtime.startRuntime(runtimePath, testDir)
runtimeCmd, err = runtime.startRuntime()
if err != nil {
t.Fatal(err)
}
Expand Down
65 changes: 0 additions & 65 deletions test/e2e/pods/trader.yaml

This file was deleted.

43 changes: 36 additions & 7 deletions test/e2e/runtime.go
Expand Up @@ -16,15 +16,22 @@ import (
)

type runtimeServer struct {
baseUrl string
baseUrl string
runtimePath string
workingDirectory string
cli *cli
context string
}

func (r *runtimeServer) startRuntime(runtimePath string, workingDirectory string) (*exec.Cmd, error) {
runtimeCmd := exec.Command(runtimePath)
runtimeCmd.Dir = workingDirectory
runtimeCmd.Stdout = os.Stdout
runtimeCmd.Stderr = os.Stderr
err := runtimeCmd.Start()
func (r *runtimeServer) startRuntime() (*exec.Cmd, error) {
var runtimeCmd *exec.Cmd
var err error

if r.context == "docker" {
runtimeCmd, err = r.startDockerRuntime()
} else {
runtimeCmd, err = r.startMetalRuntime()
}
if err != nil {
return nil, err
}
Expand All @@ -37,6 +44,28 @@ func (r *runtimeServer) startRuntime(runtimePath string, workingDirectory string
return runtimeCmd, nil
}

func (r *runtimeServer) startDockerRuntime() (*exec.Cmd, error) {
runtimeCmd, err := r.cli.startCliCmd("run")
if err != nil {
return nil, err
}

return runtimeCmd, nil
}

func (r *runtimeServer) startMetalRuntime() (*exec.Cmd, error) {
runtimeCmd := exec.Command(r.runtimePath)
runtimeCmd.Dir = r.workingDirectory
runtimeCmd.Stdout = os.Stdout
runtimeCmd.Stderr = os.Stderr
err := runtimeCmd.Start()
if err != nil {
return nil, err
}

return runtimeCmd, nil
}

func (r *runtimeServer) getRecommendation(podName string, tag string) (*aiengine_pb.InferenceResult, error) {
var inference aiengine_pb.InferenceResult
url := fmt.Sprintf("%s/api/v0.1/pods/%s/models/%s/recommendation", r.baseUrl, podName, tag)
Expand Down

0 comments on commit 0efb825

Please sign in to comment.