Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juanvallejo committed Oct 13, 2017
1 parent 338a5d0 commit c8c6f36
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions pkg/oc/cli/secrets/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"testing"

"strings"

kapi "k8s.io/kubernetes/pkg/api"
)

Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit c8c6f36

Please sign in to comment.