Skip to content

Commit

Permalink
KAIZEN: Improve docker/config coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanilves committed Apr 10, 2018
1 parent 040cff3 commit 2755579
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
45 changes: 33 additions & 12 deletions docker/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import (
)

const configFile = "../../fixtures/docker/config.json"
const irrelevantConfigFile = "../../fixtures/docker/config.json.irrelevant"
const invalidConfigFile = "../../fixtures/docker/config.json.invalid"
const corruptConfigFile = "../../fixtures/docker/config.json.corrupt"

func TestGetRegistryAuth(t *testing.T) {
examples := map[string]string{
Expand Down Expand Up @@ -73,6 +70,8 @@ func TestLoad(t *testing.T) {
}

func TestLoadWithIrrelevantConfigFile(t *testing.T) {
const irrelevantConfigFile = "../../fixtures/docker/config.json.irrelevant"

c, err := Load(irrelevantConfigFile)

if err != nil {
Expand All @@ -85,6 +84,8 @@ func TestLoadWithIrrelevantConfigFile(t *testing.T) {
}

func TestLoadWithInvalidConfigFile(t *testing.T) {
const invalidConfigFile = "../../fixtures/docker/config.json.invalid"

c, err := Load(invalidConfigFile)

if err == nil {
Expand All @@ -111,9 +112,35 @@ func TestLoadWithAbsentConfigFile(t *testing.T) {
}
}

// Badauth file = valid JSON file with auth encoded well, but NOT having username:password form
func TestLoadWithBadAuthConfigFile(t *testing.T) {
const badAuthConfigFile = "../../fixtures/docker/config.json.badauth"
const badAuthRegistry = "registry.valencia.io"

_, err := Load(badAuthConfigFile)
if err == nil {
t.Fatalf("Expected to fail while loading config file with incorrect auth")
}

DefaultDockerJSON = badAuthConfigFile

c, err := Load(badAuthConfigFile)
if err != nil {
t.Fatalf(
"Expected NOT to fail while loading config file with incorrect auth from a default path: %s",
err.Error(),
)
}

_, _, defined := c.GetCredentials(badAuthRegistry)
if defined {
t.Fatalf("Should NOT get credentials from a registry record with incorrect auth: %s", badAuthRegistry)
}
}

// Corrupt file = valid JSON file with badly encoded auth
func TestLoadWithCorruptConfigFile(t *testing.T) {
const corruptRegistry = "registry.valencia.io"
const corruptConfigFile = "../../fixtures/docker/config.json.corrupt"

_, err := Load(corruptConfigFile)
if err == nil {
Expand All @@ -122,16 +149,10 @@ func TestLoadWithCorruptConfigFile(t *testing.T) {

DefaultDockerJSON = corruptConfigFile

c, err := Load(corruptConfigFile)
if err != nil {
if _, err := Load(corruptConfigFile); err == nil {
t.Fatalf(
"Expected NOT to fail while loading corrupt config file from a default path: %s",
"Expected to fail while loading corrupt config file from a default path: %s",
err.Error(),
)
}

_, _, defined := c.GetCredentials(corruptRegistry)
if defined {
t.Fatalf("Should NOT get credentials from a corrupt registry record: %s", corruptRegistry)
}
}
10 changes: 10 additions & 0 deletions fixtures/docker/config.json.badauth
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"auths": {
"registry.galicia.io": {
"auth": "ZnJhbmNvOmZyYW5jbw=="
},
"registry.valencia.io": {
"auth": "bWFjb3N1YXU="
}
}
}
2 changes: 1 addition & 1 deletion fixtures/docker/config.json.corrupt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"auth": "ZnJhbmNvOmZyYW5jbw=="
},
"registry.valencia.io": {
"auth": "bWFjb3N1YXU="
"auth": "bW@1YXU="
}
}
}

0 comments on commit 2755579

Please sign in to comment.