-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add test to verify basic happy path
Signed-off-by: Mike Sul <mike.sul@foundries.io>
- Loading branch information
Showing
3 changed files
with
117 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} |