2019-04-05 15:04:19 +08:00
|
|
|
package bake
|
|
|
|
|
|
|
|
import (
|
2021-07-30 23:56:40 +08:00
|
|
|
"os"
|
2022-08-10 09:24:33 +08:00
|
|
|
"path/filepath"
|
2019-04-05 15:04:19 +08:00
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
2022-08-18 17:34:08 +08:00
|
|
|
compose "github.com/compose-spec/compose-go/types"
|
2022-08-18 17:34:35 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-04-05 15:04:19 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseCompose(t *testing.T) {
|
|
|
|
var dt = []byte(`
|
|
|
|
services:
|
|
|
|
db:
|
|
|
|
build: ./db
|
|
|
|
command: ./entrypoint.sh
|
|
|
|
image: docker.io/tonistiigi/db
|
|
|
|
webapp:
|
|
|
|
build:
|
|
|
|
context: ./dir
|
|
|
|
dockerfile: Dockerfile-alternate
|
2021-11-25 19:35:10 +08:00
|
|
|
network:
|
|
|
|
none
|
2019-04-05 15:04:19 +08:00
|
|
|
args:
|
|
|
|
buildno: 123
|
2022-06-24 03:44:06 +08:00
|
|
|
cache_from:
|
|
|
|
- type=local,src=path/to/cache
|
|
|
|
cache_to:
|
|
|
|
- type=local,dest=path/to/cache
|
2022-04-14 07:27:55 +08:00
|
|
|
secrets:
|
2022-06-15 00:45:23 +08:00
|
|
|
- token
|
2022-04-14 07:27:55 +08:00
|
|
|
- aws
|
|
|
|
secrets:
|
2022-06-15 00:45:23 +08:00
|
|
|
token:
|
|
|
|
environment: ENV_TOKEN
|
2022-04-14 07:27:55 +08:00
|
|
|
aws:
|
|
|
|
file: /root/.aws/credentials
|
2019-04-05 15:04:19 +08:00
|
|
|
`)
|
|
|
|
|
2022-08-18 17:34:08 +08:00
|
|
|
c, err := ParseCompose([]compose.ConfigFile{{Content: dt}}, nil)
|
2019-04-05 15:04:19 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-04-16 09:00:18 +08:00
|
|
|
require.Equal(t, 1, len(c.Groups))
|
2022-08-09 21:44:52 +08:00
|
|
|
require.Equal(t, "default", c.Groups[0].Name)
|
2020-04-16 09:00:18 +08:00
|
|
|
sort.Strings(c.Groups[0].Targets)
|
|
|
|
require.Equal(t, []string{"db", "webapp"}, c.Groups[0].Targets)
|
|
|
|
|
|
|
|
require.Equal(t, 2, len(c.Targets))
|
|
|
|
sort.Slice(c.Targets, func(i, j int) bool {
|
|
|
|
return c.Targets[i].Name < c.Targets[j].Name
|
|
|
|
})
|
|
|
|
require.Equal(t, "db", c.Targets[0].Name)
|
|
|
|
require.Equal(t, "./db", *c.Targets[0].Context)
|
2022-06-15 00:41:16 +08:00
|
|
|
require.Equal(t, []string{"docker.io/tonistiigi/db"}, c.Targets[0].Tags)
|
2020-04-16 09:00:18 +08:00
|
|
|
|
|
|
|
require.Equal(t, "webapp", c.Targets[1].Name)
|
|
|
|
require.Equal(t, "./dir", *c.Targets[1].Context)
|
|
|
|
require.Equal(t, "Dockerfile-alternate", *c.Targets[1].Dockerfile)
|
|
|
|
require.Equal(t, 1, len(c.Targets[1].Args))
|
|
|
|
require.Equal(t, "123", c.Targets[1].Args["buildno"])
|
2022-08-09 21:44:52 +08:00
|
|
|
require.Equal(t, []string{"type=local,src=path/to/cache"}, c.Targets[1].CacheFrom)
|
|
|
|
require.Equal(t, []string{"type=local,dest=path/to/cache"}, c.Targets[1].CacheTo)
|
2021-11-25 19:35:10 +08:00
|
|
|
require.Equal(t, "none", *c.Targets[1].NetworkMode)
|
2022-04-14 07:27:55 +08:00
|
|
|
require.Equal(t, []string{
|
2022-06-15 00:45:23 +08:00
|
|
|
"id=token,env=ENV_TOKEN",
|
2022-04-14 07:27:55 +08:00
|
|
|
"id=aws,src=/root/.aws/credentials",
|
|
|
|
}, c.Targets[1].Secrets)
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
2019-04-26 12:36:28 +08:00
|
|
|
|
2019-05-18 17:53:44 +08:00
|
|
|
func TestNoBuildOutOfTreeService(t *testing.T) {
|
|
|
|
var dt = []byte(`
|
|
|
|
services:
|
|
|
|
external:
|
|
|
|
image: "verycooldb:1337"
|
|
|
|
webapp:
|
|
|
|
build: ./db
|
|
|
|
`)
|
2022-08-18 17:34:08 +08:00
|
|
|
c, err := ParseCompose([]compose.ConfigFile{{Content: dt}}, nil)
|
2019-05-18 17:53:44 +08:00
|
|
|
require.NoError(t, err)
|
2020-04-16 09:00:18 +08:00
|
|
|
require.Equal(t, 1, len(c.Groups))
|
2022-08-18 17:34:08 +08:00
|
|
|
require.Equal(t, 1, len(c.Targets))
|
2019-05-18 17:53:44 +08:00
|
|
|
}
|
|
|
|
|
2019-04-26 12:36:28 +08:00
|
|
|
func TestParseComposeTarget(t *testing.T) {
|
|
|
|
var dt = []byte(`
|
|
|
|
services:
|
|
|
|
db:
|
|
|
|
build:
|
2019-05-18 17:53:44 +08:00
|
|
|
context: ./db
|
2019-04-26 12:36:28 +08:00
|
|
|
target: db
|
|
|
|
webapp:
|
|
|
|
build:
|
2019-05-18 17:53:44 +08:00
|
|
|
context: .
|
2019-04-26 12:36:28 +08:00
|
|
|
target: webapp
|
|
|
|
`)
|
|
|
|
|
2022-08-18 17:34:08 +08:00
|
|
|
c, err := ParseCompose([]compose.ConfigFile{{Content: dt}}, nil)
|
2019-04-26 12:36:28 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-04-16 09:00:18 +08:00
|
|
|
require.Equal(t, 2, len(c.Targets))
|
|
|
|
sort.Slice(c.Targets, func(i, j int) bool {
|
|
|
|
return c.Targets[i].Name < c.Targets[j].Name
|
|
|
|
})
|
|
|
|
require.Equal(t, "db", c.Targets[0].Name)
|
|
|
|
require.Equal(t, "db", *c.Targets[0].Target)
|
|
|
|
require.Equal(t, "webapp", c.Targets[1].Name)
|
|
|
|
require.Equal(t, "webapp", *c.Targets[1].Target)
|
2019-04-26 12:36:28 +08:00
|
|
|
}
|
2019-05-18 17:53:44 +08:00
|
|
|
|
2019-05-25 08:46:49 +08:00
|
|
|
func TestComposeBuildWithoutContext(t *testing.T) {
|
2019-05-18 17:53:44 +08:00
|
|
|
var dt = []byte(`
|
|
|
|
services:
|
|
|
|
db:
|
|
|
|
build:
|
|
|
|
target: db
|
|
|
|
webapp:
|
|
|
|
build:
|
|
|
|
context: .
|
|
|
|
target: webapp
|
|
|
|
`)
|
|
|
|
|
2022-08-18 17:34:08 +08:00
|
|
|
c, err := ParseCompose([]compose.ConfigFile{{Content: dt}}, nil)
|
2019-05-25 08:46:49 +08:00
|
|
|
require.NoError(t, err)
|
2020-04-16 09:00:18 +08:00
|
|
|
require.Equal(t, 2, len(c.Targets))
|
|
|
|
sort.Slice(c.Targets, func(i, j int) bool {
|
|
|
|
return c.Targets[i].Name < c.Targets[j].Name
|
|
|
|
})
|
2022-08-09 21:44:52 +08:00
|
|
|
require.Equal(t, "db", c.Targets[0].Name)
|
2020-04-16 09:00:18 +08:00
|
|
|
require.Equal(t, "db", *c.Targets[0].Target)
|
2022-08-09 21:44:52 +08:00
|
|
|
require.Equal(t, "webapp", c.Targets[1].Name)
|
2020-04-16 09:00:18 +08:00
|
|
|
require.Equal(t, "webapp", *c.Targets[1].Target)
|
2019-05-25 08:46:49 +08:00
|
|
|
}
|
|
|
|
|
2021-07-30 23:56:40 +08:00
|
|
|
func TestBuildArgEnvCompose(t *testing.T) {
|
|
|
|
var dt = []byte(`
|
|
|
|
version: "3.8"
|
|
|
|
services:
|
|
|
|
example:
|
|
|
|
image: example
|
|
|
|
build:
|
|
|
|
context: .
|
|
|
|
dockerfile: Dockerfile
|
|
|
|
args:
|
|
|
|
FOO:
|
|
|
|
BAR: $ZZZ_BAR
|
|
|
|
BRB: FOO
|
|
|
|
`)
|
|
|
|
|
|
|
|
os.Setenv("FOO", "bar")
|
|
|
|
defer os.Unsetenv("FOO")
|
|
|
|
os.Setenv("BAR", "foo")
|
|
|
|
defer os.Unsetenv("BAR")
|
|
|
|
os.Setenv("ZZZ_BAR", "zzz_foo")
|
|
|
|
defer os.Unsetenv("ZZZ_BAR")
|
|
|
|
|
2022-08-18 17:34:08 +08:00
|
|
|
c, err := ParseCompose([]compose.ConfigFile{{Content: dt}}, sliceToMap(os.Environ()))
|
2021-07-30 23:56:40 +08:00
|
|
|
require.NoError(t, err)
|
2022-08-09 21:44:52 +08:00
|
|
|
require.Equal(t, "bar", c.Targets[0].Args["FOO"])
|
|
|
|
require.Equal(t, "zzz_foo", c.Targets[0].Args["BAR"])
|
|
|
|
require.Equal(t, "FOO", c.Targets[0].Args["BRB"])
|
2021-07-30 23:56:40 +08:00
|
|
|
}
|
|
|
|
|
2022-06-23 19:09:13 +08:00
|
|
|
func TestInconsistentComposeFile(t *testing.T) {
|
2019-05-25 08:46:49 +08:00
|
|
|
var dt = []byte(`
|
|
|
|
services:
|
|
|
|
webapp:
|
2022-06-23 19:09:13 +08:00
|
|
|
entrypoint: echo 1
|
2019-05-25 08:46:49 +08:00
|
|
|
`)
|
|
|
|
|
2022-08-18 17:34:08 +08:00
|
|
|
_, err := ParseCompose([]compose.ConfigFile{{Content: dt}}, nil)
|
|
|
|
require.Error(t, err)
|
2021-07-12 17:20:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAdvancedNetwork(t *testing.T) {
|
|
|
|
var dt = []byte(`
|
|
|
|
services:
|
|
|
|
db:
|
|
|
|
networks:
|
|
|
|
- example.com
|
|
|
|
build:
|
|
|
|
context: ./db
|
|
|
|
target: db
|
|
|
|
|
|
|
|
networks:
|
|
|
|
example.com:
|
|
|
|
name: example.com
|
|
|
|
driver: bridge
|
|
|
|
ipam:
|
|
|
|
config:
|
|
|
|
- subnet: 10.5.0.0/24
|
|
|
|
ip_range: 10.5.0.0/24
|
|
|
|
gateway: 10.5.0.254
|
|
|
|
`)
|
|
|
|
|
2022-08-18 17:34:08 +08:00
|
|
|
_, err := ParseCompose([]compose.ConfigFile{{Content: dt}}, nil)
|
2021-07-12 17:20:43 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-06-15 00:41:16 +08:00
|
|
|
func TestTags(t *testing.T) {
|
|
|
|
var dt = []byte(`
|
|
|
|
services:
|
|
|
|
example:
|
|
|
|
image: example
|
|
|
|
build:
|
|
|
|
context: .
|
|
|
|
dockerfile: Dockerfile
|
|
|
|
tags:
|
|
|
|
- foo
|
|
|
|
- bar
|
|
|
|
`)
|
|
|
|
|
2022-08-18 17:34:08 +08:00
|
|
|
c, err := ParseCompose([]compose.ConfigFile{{Content: dt}}, nil)
|
2022-06-15 00:41:16 +08:00
|
|
|
require.NoError(t, err)
|
2022-08-09 21:44:52 +08:00
|
|
|
require.Equal(t, []string{"foo", "bar"}, c.Targets[0].Tags)
|
2022-06-15 00:41:16 +08:00
|
|
|
}
|
|
|
|
|
2021-07-12 17:20:43 +08:00
|
|
|
func TestDependsOnList(t *testing.T) {
|
|
|
|
var dt = []byte(`
|
|
|
|
version: "3.8"
|
|
|
|
|
|
|
|
services:
|
|
|
|
example-container:
|
|
|
|
image: example/fails:latest
|
|
|
|
build:
|
|
|
|
context: .
|
|
|
|
dockerfile: Dockerfile
|
|
|
|
depends_on:
|
|
|
|
other-container:
|
|
|
|
condition: service_healthy
|
|
|
|
networks:
|
|
|
|
default:
|
|
|
|
aliases:
|
|
|
|
- integration-tests
|
|
|
|
|
|
|
|
other-container:
|
|
|
|
image: example/other:latest
|
|
|
|
healthcheck:
|
|
|
|
test: ["CMD", "echo", "success"]
|
|
|
|
retries: 5
|
|
|
|
interval: 5s
|
|
|
|
timeout: 10s
|
|
|
|
start_period: 5s
|
|
|
|
|
|
|
|
networks:
|
|
|
|
default:
|
|
|
|
name: test-net
|
|
|
|
`)
|
|
|
|
|
2022-08-18 17:34:08 +08:00
|
|
|
_, err := ParseCompose([]compose.ConfigFile{{Content: dt}}, nil)
|
2021-07-12 17:20:43 +08:00
|
|
|
require.NoError(t, err)
|
2019-05-18 17:53:44 +08:00
|
|
|
}
|
2021-08-13 15:15:09 +08:00
|
|
|
|
|
|
|
func TestComposeExt(t *testing.T) {
|
|
|
|
var dt = []byte(`
|
|
|
|
services:
|
|
|
|
addon:
|
|
|
|
image: ct-addon:bar
|
|
|
|
build:
|
|
|
|
context: .
|
|
|
|
dockerfile: ./Dockerfile
|
|
|
|
cache_from:
|
|
|
|
- user/app:cache
|
2022-06-24 03:44:06 +08:00
|
|
|
cache_to:
|
|
|
|
- user/app:cache
|
2022-06-15 00:41:16 +08:00
|
|
|
tags:
|
|
|
|
- ct-addon:baz
|
2021-08-13 15:15:09 +08:00
|
|
|
args:
|
|
|
|
CT_ECR: foo
|
|
|
|
CT_TAG: bar
|
|
|
|
x-bake:
|
2022-08-05 21:21:21 +08:00
|
|
|
contexts:
|
|
|
|
alpine: docker-image://alpine:3.13
|
2021-08-13 15:15:09 +08:00
|
|
|
tags:
|
|
|
|
- ct-addon:foo
|
|
|
|
- ct-addon:alp
|
|
|
|
platforms:
|
|
|
|
- linux/amd64
|
|
|
|
- linux/arm64
|
|
|
|
cache-from:
|
|
|
|
- type=local,src=path/to/cache
|
2022-06-24 03:44:06 +08:00
|
|
|
cache-to:
|
|
|
|
- type=local,dest=path/to/cache
|
2021-08-13 15:15:09 +08:00
|
|
|
pull: true
|
|
|
|
|
|
|
|
aws:
|
|
|
|
image: ct-fake-aws:bar
|
|
|
|
build:
|
|
|
|
dockerfile: ./aws.Dockerfile
|
|
|
|
args:
|
|
|
|
CT_ECR: foo
|
|
|
|
CT_TAG: bar
|
|
|
|
x-bake:
|
|
|
|
secret:
|
|
|
|
- id=mysecret,src=/local/secret
|
|
|
|
- id=mysecret2,src=/local/secret2
|
|
|
|
ssh: default
|
|
|
|
platforms: linux/arm64
|
|
|
|
output: type=docker
|
|
|
|
no-cache: true
|
|
|
|
`)
|
|
|
|
|
2022-08-18 17:34:08 +08:00
|
|
|
c, err := ParseCompose([]compose.ConfigFile{{Content: dt}}, nil)
|
2021-08-13 15:15:09 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 2, len(c.Targets))
|
|
|
|
sort.Slice(c.Targets, func(i, j int) bool {
|
|
|
|
return c.Targets[i].Name < c.Targets[j].Name
|
|
|
|
})
|
2022-08-09 21:44:52 +08:00
|
|
|
require.Equal(t, map[string]string{"CT_ECR": "foo", "CT_TAG": "bar"}, c.Targets[0].Args)
|
|
|
|
require.Equal(t, []string{"ct-addon:baz", "ct-addon:foo", "ct-addon:alp"}, c.Targets[0].Tags)
|
|
|
|
require.Equal(t, []string{"linux/amd64", "linux/arm64"}, c.Targets[0].Platforms)
|
|
|
|
require.Equal(t, []string{"user/app:cache", "type=local,src=path/to/cache"}, c.Targets[0].CacheFrom)
|
|
|
|
require.Equal(t, []string{"user/app:cache", "type=local,dest=path/to/cache"}, c.Targets[0].CacheTo)
|
|
|
|
require.Equal(t, newBool(true), c.Targets[0].Pull)
|
|
|
|
require.Equal(t, map[string]string{"alpine": "docker-image://alpine:3.13"}, c.Targets[0].Contexts)
|
|
|
|
require.Equal(t, []string{"ct-fake-aws:bar"}, c.Targets[1].Tags)
|
|
|
|
require.Equal(t, []string{"id=mysecret,src=/local/secret", "id=mysecret2,src=/local/secret2"}, c.Targets[1].Secrets)
|
|
|
|
require.Equal(t, []string{"default"}, c.Targets[1].SSH)
|
|
|
|
require.Equal(t, []string{"linux/arm64"}, c.Targets[1].Platforms)
|
|
|
|
require.Equal(t, []string{"type=docker"}, c.Targets[1].Outputs)
|
|
|
|
require.Equal(t, newBool(true), c.Targets[1].NoCache)
|
2021-08-13 15:15:09 +08:00
|
|
|
}
|
|
|
|
|
2022-06-24 03:57:51 +08:00
|
|
|
func TestComposeExtDedup(t *testing.T) {
|
|
|
|
var dt = []byte(`
|
|
|
|
services:
|
|
|
|
webapp:
|
|
|
|
image: app:bar
|
|
|
|
build:
|
|
|
|
cache_from:
|
|
|
|
- user/app:cache
|
|
|
|
cache_to:
|
|
|
|
- user/app:cache
|
|
|
|
tags:
|
|
|
|
- ct-addon:foo
|
|
|
|
x-bake:
|
|
|
|
tags:
|
|
|
|
- ct-addon:foo
|
|
|
|
- ct-addon:baz
|
|
|
|
cache-from:
|
|
|
|
- user/app:cache
|
|
|
|
- type=local,src=path/to/cache
|
|
|
|
cache-to:
|
|
|
|
- type=local,dest=path/to/cache
|
|
|
|
`)
|
|
|
|
|
2022-08-18 17:34:08 +08:00
|
|
|
c, err := ParseCompose([]compose.ConfigFile{{Content: dt}}, nil)
|
2022-06-24 03:57:51 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(c.Targets))
|
2022-08-09 21:44:52 +08:00
|
|
|
require.Equal(t, []string{"ct-addon:foo", "ct-addon:baz"}, c.Targets[0].Tags)
|
|
|
|
require.Equal(t, []string{"user/app:cache", "type=local,src=path/to/cache"}, c.Targets[0].CacheFrom)
|
|
|
|
require.Equal(t, []string{"user/app:cache", "type=local,dest=path/to/cache"}, c.Targets[0].CacheTo)
|
2022-06-24 03:57:51 +08:00
|
|
|
}
|
|
|
|
|
2022-01-06 18:59:41 +08:00
|
|
|
func TestEnv(t *testing.T) {
|
2022-01-06 18:42:16 +08:00
|
|
|
envf, err := os.CreateTemp("", "env")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.Remove(envf.Name())
|
|
|
|
|
|
|
|
_, err = envf.WriteString("FOO=bsdf -csdf\n")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var dt = []byte(`
|
|
|
|
services:
|
|
|
|
scratch:
|
|
|
|
build:
|
|
|
|
context: .
|
2022-01-06 18:59:41 +08:00
|
|
|
args:
|
|
|
|
CT_ECR: foo
|
|
|
|
FOO:
|
|
|
|
NODE_ENV:
|
|
|
|
environment:
|
|
|
|
- NODE_ENV=test
|
|
|
|
- AWS_ACCESS_KEY_ID=dummy
|
|
|
|
- AWS_SECRET_ACCESS_KEY=dummy
|
2022-01-06 18:42:16 +08:00
|
|
|
env_file:
|
|
|
|
- ` + envf.Name() + `
|
|
|
|
`)
|
|
|
|
|
2022-08-18 17:34:08 +08:00
|
|
|
c, err := ParseCompose([]compose.ConfigFile{{Content: dt}}, nil)
|
2022-01-06 18:42:16 +08:00
|
|
|
require.NoError(t, err)
|
2022-08-09 21:44:52 +08:00
|
|
|
require.Equal(t, map[string]string{"CT_ECR": "foo", "FOO": "bsdf -csdf", "NODE_ENV": "test"}, c.Targets[0].Args)
|
2022-01-06 18:42:16 +08:00
|
|
|
}
|
|
|
|
|
2022-08-10 09:24:33 +08:00
|
|
|
func TestDotEnv(t *testing.T) {
|
|
|
|
tmpdir := t.TempDir()
|
|
|
|
|
|
|
|
err := os.WriteFile(filepath.Join(tmpdir, ".env"), []byte("FOO=bar"), 0644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var dt = []byte(`
|
|
|
|
services:
|
|
|
|
scratch:
|
|
|
|
build:
|
|
|
|
context: .
|
|
|
|
args:
|
|
|
|
FOO:
|
|
|
|
`)
|
|
|
|
|
|
|
|
chdir(t, tmpdir)
|
2022-08-18 17:34:08 +08:00
|
|
|
c, err := ParseComposeFiles([]File{{
|
|
|
|
Name: "docker-compose.yml",
|
|
|
|
Data: dt,
|
|
|
|
}})
|
2022-08-10 09:24:33 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, map[string]string{"FOO": "bar"}, c.Targets[0].Args)
|
|
|
|
}
|
|
|
|
|
2022-03-30 06:37:55 +08:00
|
|
|
func TestPorts(t *testing.T) {
|
|
|
|
var dt = []byte(`
|
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
build:
|
|
|
|
context: .
|
|
|
|
ports:
|
|
|
|
- 3306:3306
|
|
|
|
bar:
|
|
|
|
build:
|
|
|
|
context: .
|
|
|
|
ports:
|
|
|
|
- mode: ingress
|
|
|
|
target: 3306
|
|
|
|
published: "3306"
|
|
|
|
protocol: tcp
|
|
|
|
`)
|
2022-08-18 17:34:08 +08:00
|
|
|
_, err := ParseCompose([]compose.ConfigFile{{Content: dt}}, nil)
|
2022-03-30 06:37:55 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-08-13 15:15:09 +08:00
|
|
|
func newBool(val bool) *bool {
|
|
|
|
b := val
|
|
|
|
return &b
|
|
|
|
}
|
2022-01-26 17:29:17 +08:00
|
|
|
|
|
|
|
func TestServiceName(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
svc string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
svc: "a",
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
svc: "abc",
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
svc: "a.b",
|
2022-07-31 22:53:39 +08:00
|
|
|
wantErr: false,
|
2022-01-26 17:29:17 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
svc: "_a",
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
svc: "a_b",
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
svc: "AbC",
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
svc: "AbC-0123",
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range cases {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.svc, func(t *testing.T) {
|
2022-08-18 17:34:08 +08:00
|
|
|
_, err := ParseCompose([]compose.ConfigFile{{Content: []byte(`
|
2022-01-26 17:29:17 +08:00
|
|
|
services:
|
2022-08-18 17:34:08 +08:00
|
|
|
` + tt.svc + `:
|
2022-01-26 17:29:17 +08:00
|
|
|
build:
|
|
|
|
context: .
|
2022-08-18 17:34:08 +08:00
|
|
|
`)}}, nil)
|
2022-01-26 17:29:17 +08:00
|
|
|
if tt.wantErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-06-23 19:09:13 +08:00
|
|
|
|
|
|
|
func TestValidateComposeSecret(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
dt []byte
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "secret set by file",
|
|
|
|
dt: []byte(`
|
|
|
|
secrets:
|
|
|
|
foo:
|
|
|
|
file: .secret
|
|
|
|
`),
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "secret set by environment",
|
|
|
|
dt: []byte(`
|
|
|
|
secrets:
|
|
|
|
foo:
|
|
|
|
environment: TOKEN
|
|
|
|
`),
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "external secret",
|
|
|
|
dt: []byte(`
|
|
|
|
secrets:
|
|
|
|
foo:
|
|
|
|
external: true
|
|
|
|
`),
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unset secret",
|
|
|
|
dt: []byte(`
|
|
|
|
secrets:
|
|
|
|
foo: {}
|
|
|
|
`),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "undefined secret",
|
|
|
|
dt: []byte(`
|
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
build:
|
|
|
|
secrets:
|
|
|
|
- token
|
|
|
|
`),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range cases {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-08-18 17:34:08 +08:00
|
|
|
_, err := ParseCompose([]compose.ConfigFile{{Content: tt.dt}}, nil)
|
2022-06-23 19:09:13 +08:00
|
|
|
if tt.wantErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-08-10 09:24:33 +08:00
|
|
|
|
2022-08-18 17:34:35 +08:00
|
|
|
func TestValidateComposeFile(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
fn string
|
|
|
|
dt []byte
|
|
|
|
isCompose bool
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty service",
|
|
|
|
fn: "docker-compose.yml",
|
|
|
|
dt: []byte(`
|
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
`),
|
|
|
|
isCompose: true,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "build",
|
|
|
|
fn: "docker-compose.yml",
|
|
|
|
dt: []byte(`
|
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
build: .
|
|
|
|
`),
|
|
|
|
isCompose: true,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "image",
|
|
|
|
fn: "docker-compose.yml",
|
|
|
|
dt: []byte(`
|
|
|
|
services:
|
|
|
|
simple:
|
|
|
|
image: nginx
|
|
|
|
`),
|
|
|
|
isCompose: true,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unknown ext",
|
|
|
|
fn: "docker-compose.foo",
|
|
|
|
dt: []byte(`
|
|
|
|
services:
|
|
|
|
simple:
|
|
|
|
image: nginx
|
|
|
|
`),
|
|
|
|
isCompose: true,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hcl",
|
|
|
|
fn: "docker-bake.hcl",
|
|
|
|
dt: []byte(`
|
|
|
|
target "default" {
|
|
|
|
dockerfile = "test"
|
|
|
|
}
|
|
|
|
`),
|
|
|
|
isCompose: false,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range cases {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
isCompose, err := validateComposeFile(tt.dt, tt.fn)
|
|
|
|
assert.Equal(t, tt.isCompose, isCompose)
|
|
|
|
if tt.wantErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-10 09:24:33 +08:00
|
|
|
// chdir changes the current working directory to the named directory,
|
|
|
|
// and then restore the original working directory at the end of the test.
|
|
|
|
func chdir(t *testing.T, dir string) {
|
|
|
|
olddir, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("chdir: %v", err)
|
|
|
|
}
|
|
|
|
if err := os.Chdir(dir); err != nil {
|
|
|
|
t.Fatalf("chdir %s: %v", dir, err)
|
|
|
|
}
|
|
|
|
t.Cleanup(func() {
|
|
|
|
if err := os.Chdir(olddir); err != nil {
|
|
|
|
t.Errorf("chdir to original working directory %s: %v", olddir, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|