Files
package-updater/internal/agent/updater/build_test.go

45 lines
2.0 KiB
Go

package updater
import (
"context"
"errors"
"os"
"path/filepath"
"testing"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestBuildChecker_BuiltServices(t *testing.T) {
root, _ := filepath.EvalSymlinks(t.TempDir())
stack := filepath.Join(root, "app")
require.NoError(t, os.MkdirAll(stack, 0o755))
self := fakeSelf{mounts: []container.MountPoint{{Type: mount.TypeBind, Source: root, Destination: root, RW: true}}}
files := filepath.Join(stack, "compose.yaml") + "," + filepath.Join(stack, OverrideName)
t.Run("reports only services with build", func(t *testing.T) {
o := &outRunner{json: `{"services":{"web":{"image":"nginx","build":{"context":"."}},"db":{"image":"pg"},"x":{"build":null}}}`}
got, err := NewBuildChecker(self, "me", o.run).BuiltServices(context.Background(), "app", stack, files, "")
require.NoError(t, err)
assert.Equal(t, map[string]bool{"web": true, "db": false, "x": false}, got)
require.Len(t, o.cmds, 1)
assert.Equal(t, stack, o.cmds[0].dir)
assert.Equal(t, []string{"compose", "-p", "app", "--project-directory", stack, "-f", filepath.Join(stack, "compose.yaml"), "--profile", "*", "config", "--format", "json"}, o.cmds[0].args)
})
t.Run("files outside the roots are refused without running docker", func(t *testing.T) {
o := &outRunner{json: plainConfig}
_, err := NewBuildChecker(self, "me", o.run).BuiltServices(context.Background(), "app", "", "/elsewhere/compose.yaml", "")
assert.ErrorContains(t, err, "not mounted")
assert.Empty(t, o.cmds)
})
t.Run("runner and parse errors", func(t *testing.T) {
_, err := NewBuildChecker(self, "me", (&outRunner{err: errors.New("boom")}).run).BuiltServices(context.Background(), "app", stack, files, "")
assert.ErrorContains(t, err, "boom")
_, err = NewBuildChecker(self, "me", (&outRunner{json: "not json"}).run).BuiltServices(context.Background(), "app", stack, files, "")
assert.ErrorContains(t, err, "parse")
})
}