From 8cf946d1bcbe847d02312eb6d472b6b25bb9628e Mon Sep 17 00:00:00 2001 From: Ian Campbell Date: Mon, 28 Jan 2019 17:38:33 +0000 Subject: [PATCH] Unit test for WithContentTrustFromEnv I authored this for `contentTrustEnabled` prior to 7f207f3f957e, so this now tests the funcation argument version. Signed-off-by: Ian Campbell --- cli/command/cli_options_test.go | 37 +++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 cli/command/cli_options_test.go diff --git a/cli/command/cli_options_test.go b/cli/command/cli_options_test.go new file mode 100644 index 0000000000..10dcad8b3d --- /dev/null +++ b/cli/command/cli_options_test.go @@ -0,0 +1,37 @@ +package command + +import ( + "os" + "testing" + + "gotest.tools/assert" +) + +func contentTrustEnabled(t *testing.T) bool { + var cli DockerCli + assert.NilError(t, WithContentTrustFromEnv()(&cli)) + return cli.contentTrust +} + +// NB: Do not t.Parallel() this test -- it messes with the process environment. +func TestWithContentTrustFromEnv(t *testing.T) { + envvar := "DOCKER_CONTENT_TRUST" + if orig, ok := os.LookupEnv(envvar); ok { + defer func() { + os.Setenv(envvar, orig) + }() + } else { + defer func() { + os.Unsetenv(envvar) + }() + } + + os.Setenv(envvar, "true") + assert.Assert(t, contentTrustEnabled(t)) + os.Setenv(envvar, "false") + assert.Assert(t, !contentTrustEnabled(t)) + os.Setenv(envvar, "invalid") + assert.Assert(t, contentTrustEnabled(t)) + os.Unsetenv(envvar) + assert.Assert(t, !contentTrustEnabled(t)) +}