2018-02-05 16:05:59 -05:00
|
|
|
package dockerfile // import "github.com/docker/docker/builder/dockerfile"
|
2017-04-06 17:38:02 -04:00
|
|
|
|
|
|
|
import (
|
2017-04-13 18:44:36 -04:00
|
|
|
"bytes"
|
2017-12-22 16:30:49 -05:00
|
|
|
"strings"
|
2017-04-06 17:38:02 -04:00
|
|
|
"testing"
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
2017-04-06 17:38:02 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func strPtr(source string) *string {
|
|
|
|
return &source
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAllAllowed(t *testing.T) {
|
2018-05-07 13:49:13 -04:00
|
|
|
buildArgs := NewBuildArgs(map[string]*string{
|
2017-04-06 17:38:02 -04:00
|
|
|
"ArgNotUsedInDockerfile": strPtr("fromopt1"),
|
|
|
|
"ArgOverriddenByOptions": strPtr("fromopt2"),
|
|
|
|
"ArgNoDefaultInDockerfileFromOptions": strPtr("fromopt3"),
|
|
|
|
"HTTP_PROXY": strPtr("theproxy"),
|
|
|
|
})
|
|
|
|
|
|
|
|
buildArgs.AddMetaArg("ArgFromMeta", strPtr("frommeta1"))
|
2017-05-21 19:24:07 -04:00
|
|
|
buildArgs.AddMetaArg("ArgFromMetaOverridden", strPtr("frommeta2"))
|
2017-04-06 17:38:02 -04:00
|
|
|
buildArgs.AddMetaArg("ArgFromMetaNotUsed", strPtr("frommeta3"))
|
|
|
|
|
|
|
|
buildArgs.AddArg("ArgOverriddenByOptions", strPtr("fromdockerfile2"))
|
|
|
|
buildArgs.AddArg("ArgWithDefaultInDockerfile", strPtr("fromdockerfile1"))
|
|
|
|
buildArgs.AddArg("ArgNoDefaultInDockerfile", nil)
|
|
|
|
buildArgs.AddArg("ArgNoDefaultInDockerfileFromOptions", nil)
|
|
|
|
buildArgs.AddArg("ArgFromMeta", nil)
|
2017-05-21 19:24:07 -04:00
|
|
|
buildArgs.AddArg("ArgFromMetaOverridden", strPtr("fromdockerfile3"))
|
2017-04-06 17:38:02 -04:00
|
|
|
|
|
|
|
all := buildArgs.GetAllAllowed()
|
|
|
|
expected := map[string]string{
|
|
|
|
"HTTP_PROXY": "theproxy",
|
|
|
|
"ArgOverriddenByOptions": "fromopt2",
|
|
|
|
"ArgWithDefaultInDockerfile": "fromdockerfile1",
|
|
|
|
"ArgNoDefaultInDockerfileFromOptions": "fromopt3",
|
|
|
|
"ArgFromMeta": "frommeta1",
|
2017-05-21 19:24:07 -04:00
|
|
|
"ArgFromMetaOverridden": "fromdockerfile3",
|
2017-04-06 17:38:02 -04:00
|
|
|
}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(expected, all))
|
2017-04-06 17:38:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAllMeta(t *testing.T) {
|
2018-05-07 13:49:13 -04:00
|
|
|
buildArgs := NewBuildArgs(map[string]*string{
|
2017-04-06 17:38:02 -04:00
|
|
|
"ArgNotUsedInDockerfile": strPtr("fromopt1"),
|
|
|
|
"ArgOverriddenByOptions": strPtr("fromopt2"),
|
|
|
|
"ArgNoDefaultInMetaFromOptions": strPtr("fromopt3"),
|
|
|
|
"HTTP_PROXY": strPtr("theproxy"),
|
|
|
|
})
|
|
|
|
|
|
|
|
buildArgs.AddMetaArg("ArgFromMeta", strPtr("frommeta1"))
|
|
|
|
buildArgs.AddMetaArg("ArgOverriddenByOptions", strPtr("frommeta2"))
|
|
|
|
buildArgs.AddMetaArg("ArgNoDefaultInMetaFromOptions", nil)
|
|
|
|
|
|
|
|
all := buildArgs.GetAllMeta()
|
|
|
|
expected := map[string]string{
|
|
|
|
"HTTP_PROXY": "theproxy",
|
|
|
|
"ArgFromMeta": "frommeta1",
|
|
|
|
"ArgOverriddenByOptions": "fromopt2",
|
|
|
|
"ArgNoDefaultInMetaFromOptions": "fromopt3",
|
|
|
|
}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(expected, all))
|
2017-04-06 17:38:02 -04:00
|
|
|
}
|
2017-05-09 11:25:33 -04:00
|
|
|
|
|
|
|
func TestWarnOnUnusedBuildArgs(t *testing.T) {
|
2018-05-07 13:49:13 -04:00
|
|
|
buildArgs := NewBuildArgs(map[string]*string{
|
2017-05-09 11:25:33 -04:00
|
|
|
"ThisArgIsUsed": strPtr("fromopt1"),
|
|
|
|
"ThisArgIsNotUsed": strPtr("fromopt2"),
|
|
|
|
"HTTPS_PROXY": strPtr("referenced builtin"),
|
|
|
|
"HTTP_PROXY": strPtr("unreferenced builtin"),
|
|
|
|
})
|
|
|
|
buildArgs.AddArg("ThisArgIsUsed", nil)
|
|
|
|
buildArgs.AddArg("HTTPS_PROXY", nil)
|
|
|
|
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
buildArgs.WarnOnUnusedBuildArgs(buffer)
|
|
|
|
out := buffer.String()
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.Assert(t, !strings.Contains(out, "ThisArgIsUsed"), out)
|
|
|
|
assert.Assert(t, !strings.Contains(out, "HTTPS_PROXY"), out)
|
|
|
|
assert.Assert(t, !strings.Contains(out, "HTTP_PROXY"), out)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Contains(out, "ThisArgIsNotUsed"))
|
2017-05-09 11:25:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsUnreferencedBuiltin(t *testing.T) {
|
2018-05-07 13:49:13 -04:00
|
|
|
buildArgs := NewBuildArgs(map[string]*string{
|
2017-05-09 11:25:33 -04:00
|
|
|
"ThisArgIsUsed": strPtr("fromopt1"),
|
|
|
|
"ThisArgIsNotUsed": strPtr("fromopt2"),
|
|
|
|
"HTTPS_PROXY": strPtr("referenced builtin"),
|
|
|
|
"HTTP_PROXY": strPtr("unreferenced builtin"),
|
|
|
|
})
|
|
|
|
buildArgs.AddArg("ThisArgIsUsed", nil)
|
|
|
|
buildArgs.AddArg("HTTPS_PROXY", nil)
|
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, buildArgs.IsReferencedOrNotBuiltin("ThisArgIsUsed"))
|
|
|
|
assert.Check(t, buildArgs.IsReferencedOrNotBuiltin("ThisArgIsNotUsed"))
|
|
|
|
assert.Check(t, buildArgs.IsReferencedOrNotBuiltin("HTTPS_PROXY"))
|
|
|
|
assert.Check(t, !buildArgs.IsReferencedOrNotBuiltin("HTTP_PROXY"))
|
2017-05-09 11:25:33 -04:00
|
|
|
}
|