From bb447f2f1e8471f11381fcdf3e3622b2052591b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Fri, 23 Jul 2021 17:01:52 +0200 Subject: [PATCH] Test both imageOptions and imageDestOptions in TestTLSVerifyFlags MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miloslav Trmač --- cmd/skopeo/utils_test.go | 78 ++++++++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 26 deletions(-) diff --git a/cmd/skopeo/utils_test.go b/cmd/skopeo/utils_test.go index b9fe3e834d..0a00ed01cd 100644 --- a/cmd/skopeo/utils_test.go +++ b/cmd/skopeo/utils_test.go @@ -170,6 +170,8 @@ func TestImageDestOptionsNewSystemContext(t *testing.T) { BigFilesTemporaryDir: "/srv", }, res) + // Global/per-command tlsVerify behavior is tested in TestTLSVerifyFlags. + // Invalid option values in imageOptions opts = fakeImageDestOptions(t, "dest-", []string{}, []string{"--dest-creds", ""}) _, err = opts.newSystemContext() @@ -177,34 +179,58 @@ func TestImageDestOptionsNewSystemContext(t *testing.T) { } func TestTLSVerifyFlags(t *testing.T) { - for _, c := range []struct { - global, cmd string - expectedDocker types.OptionalBool - expectedDockerDaemon bool + type systemContextOpts interface { // Either *imageOptions or *imageDestOptions + newSystemContext() (*types.SystemContext, error) + } + + for _, creator := range []struct { + name string + newOpts func(globalFlags, cmdFlags []string) systemContextOpts }{ - {"", "", types.OptionalBoolUndefined, false}, - {"", "false", types.OptionalBoolTrue, true}, - {"", "true", types.OptionalBoolFalse, false}, - {"false", "", types.OptionalBoolTrue, false}, - {"false", "false", types.OptionalBoolTrue, true}, - {"false", "true", types.OptionalBoolFalse, false}, - {"true", "", types.OptionalBoolFalse, false}, - {"true", "false", types.OptionalBoolTrue, true}, - {"true", "true", types.OptionalBoolFalse, false}, + { + "imageFlags", + func(globalFlags, cmdFlags []string) systemContextOpts { + return fakeImageOptions(t, "dest-", globalFlags, cmdFlags) + }, + }, + { + "imageDestFlags", + func(globalFlags, cmdFlags []string) systemContextOpts { + return fakeImageDestOptions(t, "dest-", globalFlags, cmdFlags) + }, + }, } { - globalFlags := []string{} - if c.global != "" { - globalFlags = append(globalFlags, "--tls-verify="+c.global) - } - cmdFlags := []string{} - if c.cmd != "" { - cmdFlags = append(cmdFlags, "--dest-tls-verify="+c.cmd) - } - opts := fakeImageOptions(t, "dest-", globalFlags, cmdFlags) - res, err := opts.newSystemContext() - require.NoError(t, err) - assert.Equal(t, c.expectedDocker, res.DockerInsecureSkipTLSVerify, "%#v", c) - assert.Equal(t, c.expectedDockerDaemon, res.DockerDaemonInsecureSkipTLSVerify, "%#v", c) + t.Run(creator.name, func(t *testing.T) { + for _, c := range []struct { + global, cmd string + expectedDocker types.OptionalBool + expectedDockerDaemon bool + }{ + {"", "", types.OptionalBoolUndefined, false}, + {"", "false", types.OptionalBoolTrue, true}, + {"", "true", types.OptionalBoolFalse, false}, + {"false", "", types.OptionalBoolTrue, false}, + {"false", "false", types.OptionalBoolTrue, true}, + {"false", "true", types.OptionalBoolFalse, false}, + {"true", "", types.OptionalBoolFalse, false}, + {"true", "false", types.OptionalBoolTrue, true}, + {"true", "true", types.OptionalBoolFalse, false}, + } { + globalFlags := []string{} + if c.global != "" { + globalFlags = append(globalFlags, "--tls-verify="+c.global) + } + cmdFlags := []string{} + if c.cmd != "" { + cmdFlags = append(cmdFlags, "--dest-tls-verify="+c.cmd) + } + opts := creator.newOpts(globalFlags, cmdFlags) + res, err := opts.newSystemContext() + require.NoError(t, err) + assert.Equal(t, c.expectedDocker, res.DockerInsecureSkipTLSVerify, "%#v", c) + assert.Equal(t, c.expectedDockerDaemon, res.DockerDaemonInsecureSkipTLSVerify, "%#v", c) + } + }) } }