mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
TestDispatch: refactor to use subtests again, and fix linting (structcheck)
Instead of using a `initDispatchTestCases()` function, declare the test-table inside `TestDispatch` itself, and run the tests as subtests. ``` [2019-08-27T15:14:51.072Z] builder/dockerfile/evaluator_test.go:18:2: `name` is unused (structcheck) [2019-08-27T15:14:51.072Z] name, expectedError string ``` Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
cd9e4ec240
commit
a3f9cb5b63
1 changed files with 44 additions and 51 deletions
|
@ -24,8 +24,11 @@ func init() {
|
||||||
reexec.Init()
|
reexec.Init()
|
||||||
}
|
}
|
||||||
|
|
||||||
func initDispatchTestCases() []dispatchTestCase {
|
func TestDispatch(t *testing.T) {
|
||||||
dispatchTestCases := []dispatchTestCase{
|
if runtime.GOOS != "windows" {
|
||||||
|
skip.If(t, os.Getuid() != 0, "skipping test that requires root")
|
||||||
|
}
|
||||||
|
testCases := []dispatchTestCase{
|
||||||
{
|
{
|
||||||
name: "ADD multiple files to file",
|
name: "ADD multiple files to file",
|
||||||
cmd: &instructions.AddCommand{SourcesAndDest: instructions.SourcesAndDest{
|
cmd: &instructions.AddCommand{SourcesAndDest: instructions.SourcesAndDest{
|
||||||
|
@ -92,56 +95,46 @@ func initDispatchTestCases() []dispatchTestCase {
|
||||||
}},
|
}},
|
||||||
expectedError: "source can't be a URL for COPY",
|
expectedError: "source can't be a URL for COPY",
|
||||||
files: nil,
|
files: nil,
|
||||||
}}
|
},
|
||||||
|
|
||||||
return dispatchTestCases
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDispatch(t *testing.T) {
|
|
||||||
if runtime.GOOS != "windows" {
|
|
||||||
skip.If(t, os.Getuid() != 0, "skipping test that requires root")
|
|
||||||
}
|
}
|
||||||
testCases := initDispatchTestCases()
|
|
||||||
|
|
||||||
for _, testCase := range testCases {
|
for _, tc := range testCases {
|
||||||
executeTestCase(t, testCase)
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
|
||||||
|
defer cleanup()
|
||||||
|
|
||||||
|
for filename, content := range tc.files {
|
||||||
|
createTestTempFile(t, contextDir, filename, content, 0777)
|
||||||
|
}
|
||||||
|
|
||||||
|
tarStream, err := archive.Tar(contextDir, archive.Uncompressed)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error when creating tar stream: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if err = tarStream.Close(); err != nil {
|
||||||
|
t.Fatalf("Error when closing tar stream: %s", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
context, err := remotecontext.FromArchive(tarStream)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error when creating tar context: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if err = context.Close(); err != nil {
|
||||||
|
t.Fatalf("Error when closing tar context: %s", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
b := newBuilderWithMockBackend()
|
||||||
|
sb := newDispatchRequest(b, '`', context, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
||||||
|
err = dispatch(sb, tc.cmd)
|
||||||
|
assert.Check(t, is.ErrorContains(err, tc.expectedError))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func executeTestCase(t *testing.T, testCase dispatchTestCase) {
|
|
||||||
contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
|
|
||||||
defer cleanup()
|
|
||||||
|
|
||||||
for filename, content := range testCase.files {
|
|
||||||
createTestTempFile(t, contextDir, filename, content, 0777)
|
|
||||||
}
|
|
||||||
|
|
||||||
tarStream, err := archive.Tar(contextDir, archive.Uncompressed)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Error when creating tar stream: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
if err = tarStream.Close(); err != nil {
|
|
||||||
t.Fatalf("Error when closing tar stream: %s", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
context, err := remotecontext.FromArchive(tarStream)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Error when creating tar context: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
if err = context.Close(); err != nil {
|
|
||||||
t.Fatalf("Error when closing tar context: %s", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
b := newBuilderWithMockBackend()
|
|
||||||
sb := newDispatchRequest(b, '`', context, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
|
|
||||||
err = dispatch(sb, testCase.cmd)
|
|
||||||
assert.Check(t, is.ErrorContains(err, testCase.expectedError))
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue