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

Improve e2e tests #42

Merged
merged 4 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
10 changes: 10 additions & 0 deletions cmd/composectl/cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package composectl

import (
"fmt"
"github.com/docker/docker/api/types/filters"
"github.com/foundriesio/composeapp/pkg/compose"
"github.com/spf13/cobra"
"os"
"path/filepath"
Expand All @@ -17,13 +19,15 @@ var uninstallCmd = &cobra.Command{
type (
uninstallOptions struct {
ignoreNonInstalled bool
prune bool
}
)

func init() {
opts := uninstallOptions{}
uninstallCmd.Flags().BoolVar(&opts.ignoreNonInstalled, "ignore-non-installed", false,
"Do not yield error if app installation is not found")
uninstallCmd.Flags().BoolVar(&opts.prune, "prune", false, "prune unused images in the docker store")
uninstallCmd.Run = func(cmd *cobra.Command, args []string) {
uninstallApps(cmd, args, &opts)
}
Expand All @@ -44,4 +48,10 @@ func uninstallApps(cmd *cobra.Command, args []string, opts *uninstallOptions) {
}
DieNotNil(os.RemoveAll(appComposeDir))
}
if opts.prune {
cli, err := compose.GetDockerClient(dockerHost)
DieNotNil(err)
_, err = cli.ImagesPrune(cmd.Context(), filters.NewArgs(filters.Arg("dangling", "false")))
DieNotNil(err)
}
}
56 changes: 48 additions & 8 deletions test/fixtures/composectl_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"encoding/json"
"fmt"
composectl "github.com/foundriesio/composeapp/cmd/composectl/cmd"
"gopkg.in/yaml.v3"
rand2 "math/rand"
"os"
"os/exec"
"path"
"path/filepath"
"testing"
"time"
)
Expand All @@ -27,7 +29,7 @@ type (

PublishOpts struct {
PublishLayersManifest bool
LayersMetaFile string
PublishLayersMetaFile bool
Registry string
}
)
Expand All @@ -54,9 +56,9 @@ func WithLayersManifest(addLayerManifest bool) func(opts *PublishOpts) {
opts.PublishLayersManifest = addLayerManifest
}
}
func WithLayersMeta(layersMetaFile string) func(opts *PublishOpts) {
func WithLayersMeta(layersMetaFile bool) func(opts *PublishOpts) {
return func(opts *PublishOpts) {
opts.LayersMetaFile = layersMetaFile
opts.PublishLayersMetaFile = layersMetaFile
}
}

Expand All @@ -73,8 +75,44 @@ func NewApp(t *testing.T, composeDef string, name ...string) *App {
return app
}

func (a *App) pullImages(t *testing.T) error {
b, err := os.ReadFile(path.Join(a.Dir, "docker-compose.yml"))
check(t, err)
var composeProj map[string]interface{}
check(t, yaml.Unmarshal(b, &composeProj))
services := composeProj["services"]
for _, v := range services.(map[string]interface{}) {
image := v.(map[string]interface{})["image"]
c := exec.Command("docker", "pull", image.(string))
output, cmdErr := c.CombinedOutput()
checkf(t, cmdErr, "failed to pull app images: %s\n", output)
}
return err
}

func (a *App) removeImages(t *testing.T) error {
b, err := os.ReadFile(path.Join(a.Dir, "docker-compose.yml"))
check(t, err)
var composeProj map[string]interface{}
check(t, yaml.Unmarshal(b, &composeProj))
services := composeProj["services"]
removedImages := map[string]bool{}
for _, v := range services.(map[string]interface{}) {
image := v.(map[string]interface{})["image"]
if _, ok := removedImages[image.(string)]; ok {
continue
}
c := exec.Command("docker", "image", "rm", image.(string))
output, cmdErr := c.CombinedOutput()
checkf(t, cmdErr, "failed to pull app images: %s\n", output)
removedImages[image.(string)] = true
}
return err
}

func (a *App) Publish(t *testing.T, publishOpts ...func(*PublishOpts)) {
opts := PublishOpts{PublishLayersManifest: true}
check(t, a.pullImages(t))
opts := PublishOpts{PublishLayersManifest: true, PublishLayersMetaFile: true}
for _, o := range publishOpts {
o(&opts)
}
Expand All @@ -93,15 +131,18 @@ func (a *App) Publish(t *testing.T, publishOpts ...func(*PublishOpts)) {
if !opts.PublishLayersManifest {
args = append(args, "--layers-manifest=false")
}
if len(opts.LayersMetaFile) > 0 {
args = append(args, "--layers-meta", opts.LayersMetaFile)
if opts.PublishLayersMetaFile {
layersMetaFile := GenerateLayersMetaFile(t, filepath.Dir(a.Dir))
defer os.RemoveAll(layersMetaFile)
args = append(args, "--layers-meta", layersMetaFile)
}
runCmd(t, a.Dir, args...)
b, err := os.ReadFile(digestFile)
check(t, err)
a.PublishedUri = baseUri + "@" + string(b)
fmt.Printf("published app uri: %s\n", a.PublishedUri)
})
check(t, a.removeImages(t))
}

func (a *App) Pull(t *testing.T) {
Expand All @@ -117,7 +158,7 @@ func (a *App) Install(t *testing.T) {
}

func (a *App) Uninstall(t *testing.T) {
a.runCmd(t, "uninstall app", "uninstall", a.Name)
a.runCmd(t, "uninstall app", "uninstall", "--prune=true", a.Name)
}

func (a *App) Run(t *testing.T) {
Expand Down Expand Up @@ -166,7 +207,6 @@ func (a *App) CheckInstalled(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)
}
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/preload-images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ if ! check_image; then
docker pull ${SRC_IMAGE}
docker tag ${SRC_IMAGE} ${IMAGE_URI}
docker push ${IMAGE_URI}
docker image rm ${SRC_IMAGE}
docker image rm ${IMAGE_URI}
else
echo "Image ${IMAGE_URI} exists in the registry."
fi
4 changes: 1 addition & 3 deletions test/integration/smoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package e2e_tests

import (
f "github.com/foundriesio/composeapp/test/fixtures"
"path/filepath"
"testing"
)

Expand All @@ -14,10 +13,9 @@ services:
command: sh -c "while true; do sleep 60; done"
`
app := f.NewApp(t, appComposeDef)
layersMetaFile := f.GenerateLayersMetaFile(t, filepath.Dir(app.Dir))

smokeTest := func(registry string, layersManifest bool) {
app.Publish(t, f.WithRegistry(registry), f.WithLayersManifest(layersManifest), f.WithLayersMeta(layersMetaFile))
app.Publish(t, f.WithRegistry(registry), f.WithLayersManifest(layersManifest))

app.Pull(t)
defer app.Remove(t)
Expand Down
Loading