diff --git a/pkg/oc/cli/secrets/new_test.go b/pkg/oc/cli/secrets/new_test.go index d12086fbf34c..9e62c965de5a 100644 --- a/pkg/oc/cli/secrets/new_test.go +++ b/pkg/oc/cli/secrets/new_test.go @@ -6,6 +6,8 @@ import ( "os" "testing" + "strings" + kapi "k8s.io/kubernetes/pkg/api" ) @@ -181,3 +183,66 @@ func TestSecretTypeDiscovered(t *testing.T) { t.Errorf("expected %v, got %v", kapi.SecretTypeDockercfg, secret.Type) } } + +func TestDockerSecretConfigFormatSpecified(t *testing.T) { + tests := []struct { + testName string + opts CreateDockerConfigOptions + expectStr string + noExpectStr string + expectErr bool + }{ + { + testName: "testJsonConfigFormat", + opts: CreateDockerConfigOptions{ + SecretName: "private-registry", + DockerCfgFormat: "config.json", + }, + expectStr: "\"auths\"", + }, + { + testName: "testLegacyConfigFormat", + opts: CreateDockerConfigOptions{ + SecretName: "private-registry", + DockerCfgFormat: ".dockercfg", + }, + noExpectStr: "\"auths\"", + }, + { + testName: "testLegacyConfigFormatWhenFormatNotSpecified", + opts: CreateDockerConfigOptions{ + SecretName: "private-registry", + }, + noExpectStr: "\"auths\"", + }, + { + testName: "testInvalidConfigFormatError", + opts: CreateDockerConfigOptions{ + SecretName: "private-registry", + DockerCfgFormat: "invalid", + }, + expectErr: true, + }, + } + + for _, test := range tests { + s, err := test.opts.NewDockerSecret() + if err != nil && !test.expectErr { + t.Errorf("unexpected error: %v", err) + } + if err == nil && test.expectErr { + t.Errorf("expected error, but no error received") + } + if err != nil { + continue + } + + actual := string(s.Data[".dockercfg"]) + if len(test.expectStr) > 0 && !strings.Contains(actual, test.expectStr) { + t.Errorf("expected string %q, got %v", test.expectStr, actual) + } + if len(test.noExpectStr) > 0 && strings.Contains(actual, test.noExpectStr) { + t.Errorf("expected %v not to have string %q", test.noExpectStr, actual) + } + } +}