Skip to content

Commit

Permalink
test: Add test to verify basic happy path
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Sul <mike.sul@foundries.io>
  • Loading branch information
mike-sul committed Sep 11, 2024
1 parent ebe5393 commit 79f222f
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 47 deletions.
6 changes: 3 additions & 3 deletions cmd/composectl/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type (
listOptions struct {
Format string
}
appJsonOutput struct {
AppJsonOutput struct {
Name string `json:"name"`
URI string `json:"uri"`
}
Expand All @@ -42,9 +42,9 @@ func listApps(cmd *cobra.Command, args []string, opts *listOptions) {
apps, err := cs.ListApps(cmd.Context())
DieNotNil(err)
if opts.Format == "json" {
var lsOutput []appJsonOutput
var lsOutput []AppJsonOutput
for _, app := range apps {
lsOutput = append(lsOutput, appJsonOutput{
lsOutput = append(lsOutput, AppJsonOutput{
Name: app.Name,
URI: app.String(),
})
Expand Down
44 changes: 0 additions & 44 deletions test/integration/e2e_test.go

This file was deleted.

114 changes: 114 additions & 0 deletions test/integration/smoke_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package main

import (
"encoding/json"
composectl "github.com/foundriesio/composeapp/cmd/composectl/cmd"
"os"
"os/exec"
"path"
"testing"
)

var (
composeExec = os.Getenv("COMPOSECTL_EXE")
)

func TestSmoke(t *testing.T) {
appName := "app"
appBaseUri := "registry:5000/factory/" + appName
appComposeDef := "services:\n busybox:\n image: busybox:latest\n command: sh -c \"while true; do sleep 60; done\""

appDir := path.Join(t.TempDir(), appName)
digestFile := path.Join(t.TempDir(), "app.sha256")
err := os.MkdirAll(appDir, 0o755)
if err != nil {
t.Fatal(err)
}
err = os.WriteFile(path.Join(appDir, "docker-compose.yml"), []byte(appComposeDef), 0o640)
if err != nil {
t.Fatal(err)
}

runCmd := func(t *testing.T, args ...string) []byte {
c := exec.Command(composeExec, args...)
c.Dir = appDir
output, err := c.CombinedOutput()
if err != nil {
t.Errorf("failed to run `%s` command: %s\n", args[0], output)
}
return output
}

var appUri string
removeApp := func(t *testing.T) {
t.Run("remove app", func(t *testing.T) {
runCmd(t, "rm", appUri)
})
}
uninstallApp := func(t *testing.T) {
t.Run("uninstall app", func(t *testing.T) {
runCmd(t, "uninstall", appName)
})
}
stopApp := func(t *testing.T) {
t.Run("stop app", func(t *testing.T) {
runCmd(t, "stop", appName)
})
}

t.Run("publish app", func(t *testing.T) {
runCmd(t, "publish", "-d", digestFile, appBaseUri+":asdsa", "amd64")
if b, err := os.ReadFile(digestFile); err == nil {
appUri = appBaseUri + "@" + string(b)
} else {
t.Errorf("failed to read the published app digest: %s\n", err)
}
})

t.Run("pull app", func(t *testing.T) {
runCmd(t, "pull", appUri, "-u", "90")
})
defer removeApp(t)

t.Run("list app", func(t *testing.T) {
output := runCmd(t, "ls", "--format", "json")
var lsOutput []composectl.AppJsonOutput
if err := json.Unmarshal(output, &lsOutput); err != nil {
t.Errorf("failed to unmarshal app list output: %s\n", err)
}
if appUri != lsOutput[0].URI {
t.Errorf("app uri in the list output does not equal to the published app;"+
" published app uri: %s, app list uri: %s\n", appUri, lsOutput[0].URI)
}
})

t.Run("install app", func(t *testing.T) {
runCmd(t, "install", appUri)
})
defer uninstallApp(t)

// TODO: Check whether app is installed

t.Run("run app", func(t *testing.T) {
runCmd(t, "run", appName)
})
defer stopApp(t)

t.Run("check if running", func(t *testing.T) {
output := runCmd(t, "ps", appUri, "--format", "json")
var psOutput map[string]composectl.App
if err := json.Unmarshal(output, &psOutput); err != nil {
t.Errorf("failed to unmarshal app ps output: %s\n", err)
}
if len(psOutput) != 1 {
t.Errorf("expected one element in ps output, got: %d\n", len(psOutput))
}
appStatus, ok := psOutput[appUri]
if !ok {
t.Errorf("no app URI in the ps output: %+v\n", psOutput)
}
if appStatus.State != "running" {
t.Errorf("app is not running, its state: %+s\n", appStatus.State)
}
})
}

0 comments on commit 79f222f

Please sign in to comment.