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 <[email protected]>
  • Loading branch information
mike-sul committed Sep 11, 2024
1 parent ebe5393 commit 2ce73c5
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 63 deletions.
33 changes: 17 additions & 16 deletions cmd/composectl/cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,24 @@ type (
CheckInstall bool
}

checkAppResult struct {
CheckAppResult struct {
MissingBlobs map[digest.Digest]compose.BlobInfo `json:"missing_blobs"`
TotalPullSize int64 `json:"total_pull_size"`
TotalStoreSize int64 `json:"total_store_size"`
TotalRuntimeSize int64 `json:"total_runtime_size"`
}

CheckAndInstallResult struct {
FetchCheck *CheckAppResult `json:"fetch_check"`
InstallCheck *InstallCheckResult `json:"install_check"`
}

appInstallCheckResult struct {
AppName string `json:"app_name"`
MissingImages []string `json:"missing_images"`
}

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

const (
Expand Down Expand Up @@ -90,21 +95,17 @@ func checkAppsCmd(cmd *cobra.Command, args []string, opts *checkOptions) {
quietCheck = true
}
cr, ui, _ := checkApps(cmd.Context(), args, *opts.UsageWatermark, *opts.SrcStorePath, quietCheck)
var ir installCheckResult
var ir InstallCheckResult
var err error
if opts.CheckInstall {
ir, err = checkIfInstalled(cmd.Context(), args, *opts.SrcStorePath, config.DockerHost)
DieNotNil(err)
}
if opts.Format == "json" {
aggregatedCheckRes :=
struct {
FetchCheck *checkAppResult `json:"fetch_check"`
InstallCheck *installCheckResult `json:"install_check"`
}{
FetchCheck: cr,
InstallCheck: &ir,
}
aggregatedCheckRes := CheckAndInstallResult{
FetchCheck: cr,
InstallCheck: &ir,
}
if b, err := json.MarshalIndent(aggregatedCheckRes, "", " "); err == nil {
fmt.Println(string(b))
} else {
Expand All @@ -128,7 +129,7 @@ func checkAppsCmd(cmd *cobra.Command, args []string, opts *checkOptions) {
}
}

func checkApps(ctx context.Context, appRefs []string, usageWatermark uint, srcStorePath string, quiet bool) (*checkAppResult, *compose.UsageInfo, []compose.App) {
func checkApps(ctx context.Context, appRefs []string, usageWatermark uint, srcStorePath string, quiet bool) (*CheckAppResult, *compose.UsageInfo, []compose.App) {
if usageWatermark < MinUsageWatermark {
DieNotNil(fmt.Errorf("the specified usage watermark is lower than the minimum allowed; %d < %d", usageWatermark, MinUsageWatermark))
}
Expand All @@ -151,7 +152,7 @@ func checkApps(ctx context.Context, appRefs []string, usageWatermark uint, srcSt

var apps []compose.App
blobsToPull := map[digest.Digest]compose.BlobInfo{}
checkRes := checkAppResult{MissingBlobs: blobsToPull}
checkRes := CheckAppResult{MissingBlobs: blobsToPull}

for _, appRef := range appRefs {
if !quiet {
Expand Down Expand Up @@ -229,12 +230,12 @@ func checkApps(ctx context.Context, appRefs []string, usageWatermark uint, srcSt
return &checkRes, ui, apps
}

func (cr *checkAppResult) print() {
func (cr *CheckAppResult) print() {
fmt.Printf("%d blobs to pull; total download size: %s, total store size: %s, total runtime size of missing blobs: %s, total required: %s\n",
len(cr.MissingBlobs), units.BytesSize(float64(cr.TotalPullSize)), units.BytesSize(float64(cr.TotalStoreSize)), units.BytesSize(float64(cr.TotalRuntimeSize)), units.BytesSize(float64(cr.TotalStoreSize+cr.TotalRuntimeSize)))
}

func checkIfInstalled(ctx context.Context, appRefs []string, srcStorePath string, dockerHost string) (installCheckResult, error) {
func checkIfInstalled(ctx context.Context, appRefs []string, srcStorePath string, dockerHost string) (InstallCheckResult, error) {
cli, err := compose.GetDockerClient(dockerHost)
if err != nil {
return nil, err
Expand All @@ -254,7 +255,7 @@ func checkIfInstalled(ctx context.Context, appRefs []string, srcStorePath string
}
}

checkResult := installCheckResult{}
checkResult := InstallCheckResult{}
blobProvider := compose.NewStoreBlobProvider(path.Join(srcStorePath, "blobs", "sha256"))
for _, appRef := range appRefs {
app, _, err := v1.NewAppLoader().LoadAppTree(ctx, blobProvider, platforms.OnlyStrict(config.Platform), appRef)
Expand Down
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.

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

import (
"encoding/json"
"fmt"
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: ghcr.io/foundriesio/busybox:1.36\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)
}
fmt.Printf("published app uri: %s\n", appUri)
})

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

t.Run("check app", func(t *testing.T) {
output := runCmd(t, "check", "--local", appUri, "--format", "json")
checkResult := composectl.CheckAndInstallResult{}
if err := json.Unmarshal(output, &checkResult); err != nil {
t.Errorf("failed to unmarshal check app result: %s\n", err)
}
if len(checkResult.FetchCheck.MissingBlobs) > 0 {
t.Errorf("There are missing app blobs: %+v\n", checkResult.FetchCheck.MissingBlobs)
}
})

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 2ce73c5

Please sign in to comment.