mirror of
https://github.com/drone/drone-kaniko.git
synced 2026-06-04 18:23:49 +08:00
7b442a53ff
* add config for base connector * fix permissions code * add gar step support * add gar step support * reformat code, add support for gar and acr * remove logs * address review comments * delete bin file
73 lines
1.7 KiB
Go
73 lines
1.7 KiB
Go
package docker
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestConfig(t *testing.T) {
|
|
c := NewConfig()
|
|
assert.NotNil(t, c.Auths)
|
|
assert.NotNil(t, c.CredHelpers)
|
|
|
|
c.SetAuth(RegistryV1, "test", "password")
|
|
expectedAuth := Auth{Auth: "dGVzdDpwYXNzd29yZA=="}
|
|
assert.Equal(t, expectedAuth, c.Auths[RegistryV1])
|
|
|
|
c.SetCredHelper(RegistryECRPublic, "ecr-login")
|
|
assert.Equal(t, "ecr-login", c.CredHelpers[RegistryECRPublic])
|
|
|
|
tempDir, err := ioutil.TempDir("", "docker-config-test")
|
|
assert.NoError(t, err)
|
|
defer os.RemoveAll(tempDir)
|
|
|
|
credentials := []RegistryCredentials{
|
|
{
|
|
Registry: "https://index.docker.io/v1/",
|
|
Username: "user1",
|
|
Password: "pass1",
|
|
},
|
|
{
|
|
Registry: "gcr.io",
|
|
Username: "user2",
|
|
Password: "pass2",
|
|
},
|
|
}
|
|
|
|
err = c.CreateDockerConfig(credentials, tempDir)
|
|
assert.NoError(t, err)
|
|
|
|
configPath := filepath.Join(tempDir, "config.json")
|
|
data, err := ioutil.ReadFile(configPath)
|
|
assert.NoError(t, err)
|
|
|
|
var configFromFile Config
|
|
err = json.Unmarshal(data, &configFromFile)
|
|
assert.NoError(t, err)
|
|
|
|
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)
|
|
}
|