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 test to verify the app bundle integrity check #43

Merged
merged 3 commits into from
Dec 17, 2024
Merged
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
6 changes: 3 additions & 3 deletions cmd/composectl/cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ type (
InstallCheck *InstallCheckResult `json:"install_check"`
}

appInstallCheckResult struct {
AppInstallCheckResult struct {
AppName string `json:"app_name"`
MissingImages []string `json:"missing_images"`
BundleErrors compose.AppBundleErrs `json:"bundle_errors"`
}

InstallCheckResult map[string]*appInstallCheckResult
InstallCheckResult map[string]*AppInstallCheckResult
)

const (
Expand Down Expand Up @@ -305,7 +305,7 @@ func checkIfInstalled(ctx context.Context, appRefs []string, srcStorePath string
if err != nil {
return nil, err
}
checkResult[appRef] = &appInstallCheckResult{
checkResult[appRef] = &AppInstallCheckResult{
AppName: app.Name(),
MissingImages: missingImages,
BundleErrors: errMap,
Expand Down
4 changes: 4 additions & 0 deletions cmd/composectl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ var (
}
)

func GetConfig() Config {
return config
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
Expand Down
37 changes: 37 additions & 0 deletions test/fixtures/composectl_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,26 @@ func (a *App) CheckInstalled(t *testing.T) {
})
}

func (a *App) GetInstallCheckRes(t *testing.T) (checkRes *composectl.AppInstallCheckResult) {
t.Run("check if installed", func(t *testing.T) {
output := runCmd(t, a.Dir, "check", "--local", "--install", a.PublishedUri, "--format", "json")
checkResult := composectl.CheckAndInstallResult{}
check(t, json.Unmarshal(output, &checkResult))
if len(checkResult.FetchCheck.MissingBlobs) > 0 {
t.Errorf("there are missing app blobs: %+v\n", checkResult.FetchCheck.MissingBlobs)
}
if checkResult.InstallCheck == nil || len(*checkResult.InstallCheck) != 1 {
t.Errorf("invalid install check result: %+v\n", checkResult.InstallCheck)
}
var ok bool
checkRes, ok = (*checkResult.InstallCheck)[a.PublishedUri]
if !ok {
t.Errorf("no app in the install check result: %+v\n", *checkResult.InstallCheck)
}
})
return
}

func (a *App) CheckRunning(t *testing.T) {
t.Run("check if running", func(t *testing.T) {
output := runCmd(t, "", "ps", a.PublishedUri, "--format", "json")
Expand All @@ -241,6 +261,23 @@ func (a *App) CheckRunning(t *testing.T) {
})
}

func (a *App) GetRunningStatus(t *testing.T) (appStatus *composectl.App) {
t.Run("check if running", func(t *testing.T) {
output := runCmd(t, "", "ps", a.PublishedUri, "--format", "json")
var psOutput map[string]composectl.App
check(t, json.Unmarshal(output, &psOutput))
if len(psOutput) != 1 {
t.Errorf("expected one element in ps output, got: %d\n", len(psOutput))
}
if appStatusRes, ok := psOutput[a.PublishedUri]; ok {
appStatus = &appStatusRes
} else {
t.Errorf("no app URI in the ps output: %+v\n", psOutput)
}
})
return
}

func (a *App) runCmd(t *testing.T, desc string, args ...string) {
t.Run(desc, func(t *testing.T) {
runCmd(t, a.Dir, args...)
Expand Down
52 changes: 52 additions & 0 deletions test/integration/edge_cases_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package e2e_tests

import (
composectl "github.com/foundriesio/composeapp/cmd/composectl/cmd"
f "github.com/foundriesio/composeapp/test/fixtures"
"os"
"path"
"testing"
)

Expand Down Expand Up @@ -67,3 +70,52 @@ services:
defer app.Stop(t)
app.CheckRunning(t)
}

func TestAppBundleBroken(t *testing.T) {
appComposeDef := `
services:
srvs-01:
image: registry:5000/factory/runner-image:v0.1
command: sh -c "while true; do sleep 60; done"
ports:
- 8080:80
srvs-02:
image: registry:5000/factory/runner-image:v0.1
command: sh -c "while true; do sleep 60; done"
`
app := f.NewApp(t, appComposeDef)
app.Publish(t)

app.Pull(t)
defer app.Remove(t)

app.Install(t)
defer app.Uninstall(t)
app.CheckInstalled(t)

composeFilePath := path.Join(composectl.GetConfig().ComposeRoot, app.Name, "docker-compose.yml")
if err := os.WriteFile(composeFilePath, []byte("foo bar"), 0x644); err != nil {
t.Fatal(err)
}
checkRes := app.GetInstallCheckRes(t)
if len(checkRes.BundleErrors) != 1 {
t.Fatalf("expected 1 app bundle integrity error, got: %d", len(checkRes.BundleErrors))
}
if _, ok := checkRes.BundleErrors["docker-compose.yml"]; !ok {
t.Fatalf("expected error for: %s, got: %+v", "docker-compose.yml", checkRes.BundleErrors)
}

app.Run(t)
defer app.Stop(t)
app.CheckRunning(t)

if err := os.WriteFile(composeFilePath, []byte("foo bar"), 0x644); err != nil {
t.Fatal(err)
}
appStatus := app.GetRunningStatus(t)
if appStatus.State != "running with an invalid app bundle" {
t.Fatalf("expected `running with an invalid app bundle`, got: %s", appStatus.State)
}
// Install app again, so it can be stopped without any error
app.Install(t)
}
Loading