Skip to content

Commit

Permalink
fix tests (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aishwarya-Lad authored May 1, 2024
1 parent e4a0486 commit 04885c8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 72 deletions.
19 changes: 2 additions & 17 deletions cmd/kaniko-acr/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

const (
v2RegistryURL string = "https://index.docker.io/v2/" // v2 registry is not supported
v2RegistryURL string = "https://index.docker.io/v2/" // v2 registry is not supported
)

func TestCreateDockerConfigWithBaseRegistry(t *testing.T) {
Expand Down Expand Up @@ -100,22 +100,7 @@ func TestCreateDockerConfigWithBaseRegistry(t *testing.T) {
Password: "",
},
}, tempDir)
assert.NoError(t, err)

// v2 registry but without username password
err = config.CreateDockerConfig([]docker.RegistryCredentials{
{
Registry: registry,
Username: username,
Password: password,
},
{
Registry: v2RegistryURL,
Username: "",
Password: "",
},
}, tempDir)
assert.NoError(t, err)
assert.EqualError(t, err, "Username must be specified for registry: "+dockerRegistry)

// private base registry without username/password
err = config.CreateDockerConfig([]docker.RegistryCredentials{
Expand Down
104 changes: 66 additions & 38 deletions cmd/kaniko-docker/main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package main

import "testing"
import (
"io/ioutil"
"os"
"testing"

"github.com/drone/drone-kaniko/pkg/docker"
)

func Test_buildRepo(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -36,58 +42,80 @@ func Test_buildRepo(t *testing.T) {
}
}

func TestCreateDockerConfigFromGivenRegistry(t *testing.T) {
func TestCreateDockerConfig(t *testing.T) {
config := docker.NewConfig()
tempDir, err := ioutil.TempDir("", "docker-config-test")
if err != nil {
t.Fatalf("Failed to create temporary directory: %v", err)
}
defer os.RemoveAll(tempDir)

tests := []struct {
name string
username string
password string
registry string
dockerUsername string
dockerPassword string
dockerRegistry string
wantErr bool
name string
credentials []docker.RegistryCredentials
wantErr bool
}{
{
name: "valid credentials",
username: "testuser",
password: "testpassword",
registry: "https://index.docker.io/v1/",
wantErr: false,
name: "valid credentials",
credentials: []docker.RegistryCredentials{
{
Registry: "https://index.docker.io/v1/",
Username: "testuser",
Password: "testpassword",
},
},
wantErr: false,
},
{
name: "v2 registry",
username: "testuser",
password: "testpassword",
registry: "https://index.docker.io/v2/",
wantErr: false,
name: "v2 registry",
credentials: []docker.RegistryCredentials{
{
Registry: "https://index.docker.io/v2/",
Username: "testuser",
Password: "testpassword",
},
},
wantErr: false,
},
{
name: "docker registry credentials",
username: "testuser",
password: "testpassword",
registry: "https://index.docker.io/v1/",
dockerUsername: "dockeruser",
dockerPassword: "dockerpassword",
dockerRegistry: "https://docker.io",
wantErr: false,
name: "docker registry credentials",
credentials: []docker.RegistryCredentials{
{
Registry: "https://index.docker.io/v1/",
Username: "testuser",
Password: "testpassword",
},
{
Registry: "https://docker.io",
Username: "dockeruser",
Password: "dockerpassword",
},
},
wantErr: false,
},
{
name: "empty docker registry",
username: "testuser",
password: "testpassword",
registry: "https://index.docker.io/v1/",
dockerUsername: "dockeruser",
dockerPassword: "",
dockerRegistry: "https://docker.io",
wantErr: false,
name: "empty docker registry",
credentials: []docker.RegistryCredentials{
{
Registry: "https://index.docker.io/v1/",
Username: "testuser",
Password: "testpassword",
},
{
Registry: "https://docker.io",
Username: "dockeruser",
Password: "",
},
},
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := createDockerConfig(tt.username, tt.password, tt.registry, tt.dockerUsername, tt.dockerPassword, tt.dockerRegistry)
err := config.CreateDockerConfig(tt.credentials, tempDir)
if (err != nil) != tt.wantErr {
t.Errorf("createDockerConfig() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("CreateDockerConfig() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
Expand Down
17 changes: 0 additions & 17 deletions pkg/docker/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,3 @@ func TestConfig(t *testing.T) {
assert.Equal(t, c.Auths, configFromFile.Auths)
assert.Equal(t, c.CredHelpers, configFromFile.CredHelpers)
}

func TestWriteDockerConfig(t *testing.T) {
tempDir, err := ioutil.TempDir("", "docker-config-test")
assert.NoError(t, err)
defer os.RemoveAll(tempDir)

data := []byte(`{"auths":{"https://index.docker.io/v1/":{"auth":"dGVzdDpwYXNzd29yZA=="}}}`)
err = WriteDockerConfig(data, tempDir)
assert.NoError(t, err)

configPath := filepath.Join(tempDir, "config.json")
_, err = os.Stat(configPath)
assert.NoError(t, err)

err = WriteDockerConfig(data, "/invalid/path")
assert.Error(t, err)
}

0 comments on commit 04885c8

Please sign in to comment.