2017-08-25 20:08:18 +00:00
|
|
|
package dockerfile2llb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-07-20 10:17:54 +00:00
|
|
|
"github.com/moby/buildkit/frontend/dockerfile/instructions"
|
2017-08-25 20:08:18 +00:00
|
|
|
"github.com/moby/buildkit/util/appcontext"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDockerfileParsing(t *testing.T) {
|
2017-12-09 02:19:08 +00:00
|
|
|
t.Parallel()
|
2017-08-25 20:08:18 +00:00
|
|
|
df := `FROM busybox
|
|
|
|
ENV FOO bar
|
|
|
|
COPY f1 f2 /sub/
|
|
|
|
RUN ls -l
|
|
|
|
`
|
2017-09-11 05:35:07 +00:00
|
|
|
_, _, err := Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{})
|
2017-08-25 20:08:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
df = `FROM busybox AS foo
|
|
|
|
ENV FOO bar
|
|
|
|
FROM foo
|
|
|
|
COPY --from=foo f1 /
|
|
|
|
COPY --from=0 f2 /
|
|
|
|
`
|
2017-09-11 05:35:07 +00:00
|
|
|
_, _, err = Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{})
|
2017-08-25 20:08:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
df = `FROM busybox AS foo
|
|
|
|
ENV FOO bar
|
|
|
|
FROM foo
|
|
|
|
COPY --from=foo f1 /
|
|
|
|
COPY --from=0 f2 /
|
|
|
|
`
|
2017-09-11 05:35:07 +00:00
|
|
|
_, _, err = Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{
|
2017-08-25 20:08:18 +00:00
|
|
|
Target: "Foo",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-09-11 05:35:07 +00:00
|
|
|
_, _, err = Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{
|
2017-08-25 20:08:18 +00:00
|
|
|
Target: "nosuch",
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
2018-06-27 23:46:29 +00:00
|
|
|
|
2018-07-03 11:28:08 +00:00
|
|
|
df = `FROM busybox
|
|
|
|
ADD http://github.com/moby/buildkit/blob/master/README.md /
|
|
|
|
`
|
|
|
|
_, _, err = Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
df = `FROM busybox
|
|
|
|
COPY http://github.com/moby/buildkit/blob/master/README.md /
|
|
|
|
`
|
|
|
|
_, _, err = Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{})
|
|
|
|
assert.EqualError(t, err, "source can't be a URL for COPY")
|
|
|
|
|
2018-06-27 23:46:29 +00:00
|
|
|
df = `FROM "" AS foo`
|
|
|
|
_, _, err = Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{})
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
df = `FROM ${BLANK} AS foo`
|
|
|
|
_, _, err = Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{})
|
|
|
|
assert.Error(t, err)
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
2018-07-19 00:39:22 +00:00
|
|
|
|
|
|
|
func TestAddEnv(t *testing.T) {
|
|
|
|
// k exists in env as key
|
|
|
|
// override = true
|
2018-07-20 10:46:50 +00:00
|
|
|
env := []string{"key1=val1", "key2=val2"}
|
|
|
|
result := addEnv(env, "key1", "value1")
|
2018-07-19 00:39:22 +00:00
|
|
|
assert.Equal(t, []string{"key1=value1", "key2=val2"}, result)
|
|
|
|
|
|
|
|
// k does not exist in env as key
|
|
|
|
// override = true
|
|
|
|
env = []string{"key1=val1", "key2=val2"}
|
2018-07-20 10:46:50 +00:00
|
|
|
result = addEnv(env, "key3", "val3")
|
2018-07-19 00:39:22 +00:00
|
|
|
assert.Equal(t, []string{"key1=val1", "key2=val2", "key3=val3"}, result)
|
|
|
|
|
|
|
|
// env has same keys
|
|
|
|
// override = true
|
|
|
|
env = []string{"key1=val1", "key1=val2"}
|
2018-07-20 10:46:50 +00:00
|
|
|
result = addEnv(env, "key1", "value1")
|
2018-07-19 00:39:22 +00:00
|
|
|
assert.Equal(t, []string{"key1=value1", "key1=val2"}, result)
|
|
|
|
|
|
|
|
// k matches with key only string in env
|
|
|
|
// override = true
|
|
|
|
env = []string{"key1=val1", "key2=val2", "key3"}
|
2018-07-20 10:46:50 +00:00
|
|
|
result = addEnv(env, "key3", "val3")
|
2018-07-19 00:39:22 +00:00
|
|
|
assert.Equal(t, []string{"key1=val1", "key2=val2", "key3=val3"}, result)
|
|
|
|
}
|
2018-07-19 12:36:07 +00:00
|
|
|
|
|
|
|
func TestParseKeyValue(t *testing.T) {
|
|
|
|
k, v := parseKeyValue("key=val")
|
|
|
|
assert.Equal(t, "key", k)
|
|
|
|
assert.Equal(t, "val", v)
|
|
|
|
|
|
|
|
k, v = parseKeyValue("key=")
|
|
|
|
assert.Equal(t, "key", k)
|
|
|
|
assert.Equal(t, "", v)
|
|
|
|
|
|
|
|
k, v = parseKeyValue("key")
|
|
|
|
assert.Equal(t, "key", k)
|
|
|
|
assert.Equal(t, "", v)
|
|
|
|
}
|
2018-07-20 10:17:54 +00:00
|
|
|
|
|
|
|
func TestToEnvList(t *testing.T) {
|
|
|
|
// args has no duplicated key with env
|
|
|
|
v := "val2"
|
|
|
|
args := []instructions.KeyValuePairOptional{{Key: "key2", Value: &v}}
|
|
|
|
env := []string{"key1=val1"}
|
2018-07-20 23:30:47 +00:00
|
|
|
resutl := toEnvMap(args, env)
|
2018-07-20 10:41:15 +00:00
|
|
|
assert.Equal(t, map[string]string{"key1": "val1", "key2": "val2"}, resutl)
|
2018-07-20 10:17:54 +00:00
|
|
|
|
|
|
|
// value of args is nil
|
|
|
|
args = []instructions.KeyValuePairOptional{{Key: "key2", Value: nil}}
|
|
|
|
env = []string{"key1=val1"}
|
2018-07-20 23:30:47 +00:00
|
|
|
resutl = toEnvMap(args, env)
|
2018-07-20 10:41:15 +00:00
|
|
|
assert.Equal(t, map[string]string{"key1": "val1", "key2": ""}, resutl)
|
2018-07-20 10:17:54 +00:00
|
|
|
|
|
|
|
// args has duplicated key with env
|
|
|
|
v = "val2"
|
|
|
|
args = []instructions.KeyValuePairOptional{{Key: "key1", Value: &v}}
|
|
|
|
env = []string{"key1=val1"}
|
2018-07-20 23:30:47 +00:00
|
|
|
resutl = toEnvMap(args, env)
|
2018-07-20 10:41:15 +00:00
|
|
|
assert.Equal(t, map[string]string{"key1": "val1"}, resutl)
|
2018-07-20 10:17:54 +00:00
|
|
|
|
|
|
|
v = "val2"
|
|
|
|
args = []instructions.KeyValuePairOptional{{Key: "key1", Value: &v}}
|
|
|
|
env = []string{"key1="}
|
2018-07-20 23:30:47 +00:00
|
|
|
resutl = toEnvMap(args, env)
|
2018-07-20 10:41:15 +00:00
|
|
|
assert.Equal(t, map[string]string{"key1": ""}, resutl)
|
2018-07-20 10:17:54 +00:00
|
|
|
|
|
|
|
v = "val2"
|
|
|
|
args = []instructions.KeyValuePairOptional{{Key: "key1", Value: &v}}
|
|
|
|
env = []string{"key1"}
|
2018-07-20 23:30:47 +00:00
|
|
|
resutl = toEnvMap(args, env)
|
2018-07-20 10:41:15 +00:00
|
|
|
assert.Equal(t, map[string]string{"key1": ""}, resutl)
|
2018-07-20 10:17:54 +00:00
|
|
|
|
|
|
|
// env has duplicated keys
|
|
|
|
v = "val2"
|
|
|
|
args = []instructions.KeyValuePairOptional{{Key: "key2", Value: &v}}
|
|
|
|
env = []string{"key1=val1", "key1=val1_2"}
|
2018-07-20 23:30:47 +00:00
|
|
|
resutl = toEnvMap(args, env)
|
2018-07-20 10:41:15 +00:00
|
|
|
assert.Equal(t, map[string]string{"key1": "val1", "key2": "val2"}, resutl)
|
2018-07-20 10:17:54 +00:00
|
|
|
|
|
|
|
// args has duplicated keys
|
|
|
|
v1 := "v1"
|
|
|
|
v2 := "v2"
|
|
|
|
args = []instructions.KeyValuePairOptional{{Key: "key2", Value: &v1}, {Key: "key2", Value: &v2}}
|
|
|
|
env = []string{"key1=val1"}
|
2018-07-20 23:30:47 +00:00
|
|
|
resutl = toEnvMap(args, env)
|
2018-07-20 10:41:15 +00:00
|
|
|
assert.Equal(t, map[string]string{"key1": "val1", "key2": "v1"}, resutl)
|
2018-07-20 10:17:54 +00:00
|
|
|
}
|