2016-12-25 14:31:52 -05:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2017-06-22 12:03:58 -04:00
|
|
|
"bytes"
|
2019-03-07 09:28:42 -05:00
|
|
|
"fmt"
|
2018-03-06 13:44:06 -05:00
|
|
|
"io"
|
2016-12-25 14:31:52 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-09-23 19:27:03 -04:00
|
|
|
"runtime"
|
2016-12-25 14:31:52 -05:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
2017-06-22 12:03:58 -04:00
|
|
|
"github.com/docker/cli/cli/config/credentials"
|
2018-03-06 13:44:06 -05:00
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2020-04-09 07:37:39 -04:00
|
|
|
"gotest.tools/v3/env"
|
2016-12-25 14:31:52 -05:00
|
|
|
)
|
|
|
|
|
2019-09-23 19:27:03 -04:00
|
|
|
var homeKey = "HOME"
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
homeKey = "USERPROFILE"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-22 12:03:58 -04:00
|
|
|
func setupConfigDir(t *testing.T) (string, func()) {
|
|
|
|
tmpdir, err := ioutil.TempDir("", "config-test")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2017-06-22 12:03:58 -04:00
|
|
|
oldDir := Dir()
|
|
|
|
SetDir(tmpdir)
|
|
|
|
|
|
|
|
return tmpdir, func() {
|
|
|
|
SetDir(oldDir)
|
|
|
|
os.RemoveAll(tmpdir)
|
2016-12-25 14:31:52 -05:00
|
|
|
}
|
2017-06-22 12:03:58 -04:00
|
|
|
}
|
2016-12-25 14:31:52 -05:00
|
|
|
|
2017-06-22 12:03:58 -04:00
|
|
|
func TestEmptyConfigDir(t *testing.T) {
|
|
|
|
tmpHome, cleanup := setupConfigDir(t)
|
|
|
|
defer cleanup()
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
config, err := Load("")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
expectedConfigFilename := filepath.Join(tmpHome, ConfigFileName)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(expectedConfigFilename, config.Filename))
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
// Now save it and make sure it shows up in new form
|
|
|
|
saveConfigAndValidateNewFormat(t, config, tmpHome)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMissingFile(t *testing.T) {
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
|
|
|
config, err := Load(tmpHome)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
// Now save it and make sure it shows up in new form
|
|
|
|
saveConfigAndValidateNewFormat(t, config, tmpHome)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveFileToDirs(t *testing.T) {
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
|
|
|
tmpHome += "/.docker"
|
|
|
|
|
|
|
|
config, err := Load(tmpHome)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
// Now save it and make sure it shows up in new form
|
|
|
|
saveConfigAndValidateNewFormat(t, config, tmpHome)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptyFile(t *testing.T) {
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
|
|
|
fn := filepath.Join(tmpHome, ConfigFileName)
|
2017-06-22 12:03:58 -04:00
|
|
|
err = ioutil.WriteFile(fn, []byte(""), 0600)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
_, err = Load(tmpHome)
|
2020-05-09 15:28:48 -04:00
|
|
|
assert.Assert(t, errors.Is(err, io.EOF))
|
2018-03-06 13:44:06 -05:00
|
|
|
assert.ErrorContains(t, err, ConfigFileName)
|
2016-12-25 14:31:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptyJSON(t *testing.T) {
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
|
|
|
fn := filepath.Join(tmpHome, ConfigFileName)
|
2017-06-22 12:03:58 -04:00
|
|
|
err = ioutil.WriteFile(fn, []byte("{}"), 0600)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
config, err := Load(tmpHome)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
// Now save it and make sure it shows up in new form
|
|
|
|
saveConfigAndValidateNewFormat(t, config, tmpHome)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOldInvalidsAuth(t *testing.T) {
|
|
|
|
invalids := map[string]string{
|
|
|
|
`username = test`: "The Auth config file is empty",
|
|
|
|
`username
|
|
|
|
password`: "Invalid Auth config file",
|
|
|
|
`username = test
|
|
|
|
email`: "Invalid auth configuration file",
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
defer os.RemoveAll(tmpHome)
|
2020-04-09 07:37:39 -04:00
|
|
|
defer env.Patch(t, homeKey, tmpHome)()
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
for content, expectedError := range invalids {
|
|
|
|
fn := filepath.Join(tmpHome, oldConfigfile)
|
2017-06-22 12:03:58 -04:00
|
|
|
err := ioutil.WriteFile(fn, []byte(content), 0600)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
|
2017-06-22 12:03:58 -04:00
|
|
|
_, err = Load(tmpHome)
|
2018-03-05 19:59:53 -05:00
|
|
|
assert.ErrorContains(t, err, expectedError)
|
2016-12-25 14:31:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOldValidAuth(t *testing.T) {
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
defer os.RemoveAll(tmpHome)
|
2020-04-09 07:37:39 -04:00
|
|
|
defer env.Patch(t, homeKey, tmpHome)()
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
fn := filepath.Join(tmpHome, oldConfigfile)
|
|
|
|
js := `username = am9lam9lOmhlbGxv
|
|
|
|
email = user@example.com`
|
2017-06-22 12:03:58 -04:00
|
|
|
err = ioutil.WriteFile(fn, []byte(js), 0600)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
config, err := Load(tmpHome)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
// defaultIndexserver is https://index.docker.io/v1/
|
|
|
|
ac := config.AuthConfigs["https://index.docker.io/v1/"]
|
2019-03-04 12:44:07 -05:00
|
|
|
assert.Equal(t, ac.Username, "joejoe")
|
|
|
|
assert.Equal(t, ac.Password, "hello")
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
// Now save it and make sure it shows up in new form
|
|
|
|
configStr := saveConfigAndValidateNewFormat(t, config, tmpHome)
|
|
|
|
|
|
|
|
expConfStr := `{
|
|
|
|
"auths": {
|
|
|
|
"https://index.docker.io/v1/": {
|
|
|
|
"auth": "am9lam9lOmhlbGxv"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(expConfStr, configStr))
|
2016-12-25 14:31:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOldJSONInvalid(t *testing.T) {
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
defer os.RemoveAll(tmpHome)
|
2020-04-09 07:37:39 -04:00
|
|
|
defer env.Patch(t, homeKey, tmpHome)()
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
fn := filepath.Join(tmpHome, oldConfigfile)
|
|
|
|
js := `{"https://index.docker.io/v1/":{"auth":"test","email":"user@example.com"}}`
|
|
|
|
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := Load(tmpHome)
|
|
|
|
// Use Contains instead of == since the file name will change each time
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "Invalid auth configuration file") {
|
|
|
|
t.Fatalf("Expected an error got : %v, %v", config, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOldJSON(t *testing.T) {
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
defer os.RemoveAll(tmpHome)
|
2020-04-09 07:37:39 -04:00
|
|
|
defer env.Patch(t, homeKey, tmpHome)()
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
fn := filepath.Join(tmpHome, oldConfigfile)
|
|
|
|
js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}`
|
|
|
|
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := Load(tmpHome)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
ac := config.AuthConfigs["https://index.docker.io/v1/"]
|
2019-03-04 12:44:07 -05:00
|
|
|
assert.Equal(t, ac.Username, "joejoe")
|
|
|
|
assert.Equal(t, ac.Password, "hello")
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
// Now save it and make sure it shows up in new form
|
|
|
|
configStr := saveConfigAndValidateNewFormat(t, config, tmpHome)
|
|
|
|
|
|
|
|
expConfStr := `{
|
|
|
|
"auths": {
|
|
|
|
"https://index.docker.io/v1/": {
|
|
|
|
"auth": "am9lam9lOmhlbGxv",
|
|
|
|
"email": "user@example.com"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
if configStr != expConfStr {
|
|
|
|
t.Fatalf("Should have save in new form: \n'%s'\n not \n'%s'\n", configStr, expConfStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewJSON(t *testing.T) {
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
|
|
|
fn := filepath.Join(tmpHome, ConfigFileName)
|
|
|
|
js := ` { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv" } } }`
|
|
|
|
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := Load(tmpHome)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
ac := config.AuthConfigs["https://index.docker.io/v1/"]
|
2019-03-04 12:44:07 -05:00
|
|
|
assert.Equal(t, ac.Username, "joejoe")
|
|
|
|
assert.Equal(t, ac.Password, "hello")
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
// Now save it and make sure it shows up in new form
|
|
|
|
configStr := saveConfigAndValidateNewFormat(t, config, tmpHome)
|
|
|
|
|
|
|
|
expConfStr := `{
|
|
|
|
"auths": {
|
|
|
|
"https://index.docker.io/v1/": {
|
|
|
|
"auth": "am9lam9lOmhlbGxv"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
if configStr != expConfStr {
|
|
|
|
t.Fatalf("Should have save in new form: \n%s\n not \n%s", configStr, expConfStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewJSONNoEmail(t *testing.T) {
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
|
|
|
fn := filepath.Join(tmpHome, ConfigFileName)
|
|
|
|
js := ` { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv" } } }`
|
|
|
|
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := Load(tmpHome)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
ac := config.AuthConfigs["https://index.docker.io/v1/"]
|
2019-03-04 12:44:07 -05:00
|
|
|
assert.Equal(t, ac.Username, "joejoe")
|
|
|
|
assert.Equal(t, ac.Password, "hello")
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
// Now save it and make sure it shows up in new form
|
|
|
|
configStr := saveConfigAndValidateNewFormat(t, config, tmpHome)
|
|
|
|
|
|
|
|
expConfStr := `{
|
|
|
|
"auths": {
|
|
|
|
"https://index.docker.io/v1/": {
|
|
|
|
"auth": "am9lam9lOmhlbGxv"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
if configStr != expConfStr {
|
|
|
|
t.Fatalf("Should have save in new form: \n%s\n not \n%s", configStr, expConfStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJSONWithPsFormat(t *testing.T) {
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
|
|
|
fn := filepath.Join(tmpHome, ConfigFileName)
|
|
|
|
js := `{
|
|
|
|
"auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } },
|
|
|
|
"psFormat": "table {{.ID}}\\t{{.Label \"com.docker.label.cpu\"}}"
|
|
|
|
}`
|
|
|
|
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := Load(tmpHome)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
if config.PsFormat != `table {{.ID}}\t{{.Label "com.docker.label.cpu"}}` {
|
|
|
|
t.Fatalf("Unknown ps format: %s\n", config.PsFormat)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now save it and make sure it shows up in new form
|
|
|
|
configStr := saveConfigAndValidateNewFormat(t, config, tmpHome)
|
|
|
|
if !strings.Contains(configStr, `"psFormat":`) ||
|
|
|
|
!strings.Contains(configStr, "{{.ID}}") {
|
|
|
|
t.Fatalf("Should have save in new form: %s", configStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJSONWithCredentialStore(t *testing.T) {
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
|
|
|
fn := filepath.Join(tmpHome, ConfigFileName)
|
|
|
|
js := `{
|
|
|
|
"auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } },
|
|
|
|
"credsStore": "crazy-secure-storage"
|
|
|
|
}`
|
|
|
|
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := Load(tmpHome)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
if config.CredentialsStore != "crazy-secure-storage" {
|
|
|
|
t.Fatalf("Unknown credential store: %s\n", config.CredentialsStore)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now save it and make sure it shows up in new form
|
|
|
|
configStr := saveConfigAndValidateNewFormat(t, config, tmpHome)
|
|
|
|
if !strings.Contains(configStr, `"credsStore":`) ||
|
|
|
|
!strings.Contains(configStr, "crazy-secure-storage") {
|
|
|
|
t.Fatalf("Should have save in new form: %s", configStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJSONWithCredentialHelpers(t *testing.T) {
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
|
|
|
fn := filepath.Join(tmpHome, ConfigFileName)
|
|
|
|
js := `{
|
|
|
|
"auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } },
|
|
|
|
"credHelpers": { "images.io": "images-io", "containers.com": "crazy-secure-storage" }
|
|
|
|
}`
|
|
|
|
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := Load(tmpHome)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
if config.CredentialHelpers == nil {
|
|
|
|
t.Fatal("config.CredentialHelpers was nil")
|
|
|
|
} else if config.CredentialHelpers["images.io"] != "images-io" ||
|
|
|
|
config.CredentialHelpers["containers.com"] != "crazy-secure-storage" {
|
|
|
|
t.Fatalf("Credential helpers not deserialized properly: %v\n", config.CredentialHelpers)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now save it and make sure it shows up in new form
|
|
|
|
configStr := saveConfigAndValidateNewFormat(t, config, tmpHome)
|
|
|
|
if !strings.Contains(configStr, `"credHelpers":`) ||
|
|
|
|
!strings.Contains(configStr, "images.io") ||
|
|
|
|
!strings.Contains(configStr, "images-io") ||
|
|
|
|
!strings.Contains(configStr, "containers.com") ||
|
|
|
|
!strings.Contains(configStr, "crazy-secure-storage") {
|
|
|
|
t.Fatalf("Should have save in new form: %s", configStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save it and make sure it shows up in new form
|
2017-06-22 12:03:58 -04:00
|
|
|
func saveConfigAndValidateNewFormat(t *testing.T, config *configfile.ConfigFile, configDir string) string {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, config.Save())
|
2016-12-25 14:31:52 -05:00
|
|
|
|
2017-06-22 12:03:58 -04:00
|
|
|
buf, err := ioutil.ReadFile(filepath.Join(configDir, ConfigFileName))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Contains(string(buf), `"auths":`))
|
2016-12-25 14:31:52 -05:00
|
|
|
return string(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigDir(t *testing.T) {
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
|
|
|
if Dir() == tmpHome {
|
|
|
|
t.Fatalf("Expected ConfigDir to be different than %s by default, but was the same", tmpHome)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update configDir
|
|
|
|
SetDir(tmpHome)
|
|
|
|
|
|
|
|
if Dir() != tmpHome {
|
|
|
|
t.Fatalf("Expected ConfigDir to %s, but was %s", tmpHome, Dir())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJSONReaderNoFile(t *testing.T) {
|
|
|
|
js := ` { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } } }`
|
|
|
|
|
|
|
|
config, err := LoadFromReader(strings.NewReader(js))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
ac := config.AuthConfigs["https://index.docker.io/v1/"]
|
2019-03-04 12:44:07 -05:00
|
|
|
assert.Equal(t, ac.Username, "joejoe")
|
|
|
|
assert.Equal(t, ac.Password, "hello")
|
2016-12-25 14:31:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOldJSONReaderNoFile(t *testing.T) {
|
|
|
|
js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}`
|
|
|
|
|
|
|
|
config, err := LegacyLoadFromReader(strings.NewReader(js))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
ac := config.AuthConfigs["https://index.docker.io/v1/"]
|
2019-03-04 12:44:07 -05:00
|
|
|
assert.Equal(t, ac.Username, "joejoe")
|
|
|
|
assert.Equal(t, ac.Password, "hello")
|
2016-12-25 14:31:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestJSONWithPsFormatNoFile(t *testing.T) {
|
|
|
|
js := `{
|
|
|
|
"auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } },
|
|
|
|
"psFormat": "table {{.ID}}\\t{{.Label \"com.docker.label.cpu\"}}"
|
|
|
|
}`
|
|
|
|
config, err := LoadFromReader(strings.NewReader(js))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
if config.PsFormat != `table {{.ID}}\t{{.Label "com.docker.label.cpu"}}` {
|
|
|
|
t.Fatalf("Unknown ps format: %s\n", config.PsFormat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJSONSaveWithNoFile(t *testing.T) {
|
|
|
|
js := `{
|
|
|
|
"auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv" } },
|
|
|
|
"psFormat": "table {{.ID}}\\t{{.Label \"com.docker.label.cpu\"}}"
|
|
|
|
}`
|
|
|
|
config, err := LoadFromReader(strings.NewReader(js))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
err = config.Save()
|
2018-03-05 19:59:53 -05:00
|
|
|
assert.ErrorContains(t, err, "with empty filename")
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
|
|
|
fn := filepath.Join(tmpHome, ConfigFileName)
|
|
|
|
f, _ := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
defer f.Close()
|
|
|
|
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, config.SaveToWriter(f))
|
2016-12-25 14:31:52 -05:00
|
|
|
buf, err := ioutil.ReadFile(filepath.Join(tmpHome, ConfigFileName))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
expConfStr := `{
|
|
|
|
"auths": {
|
|
|
|
"https://index.docker.io/v1/": {
|
|
|
|
"auth": "am9lam9lOmhlbGxv"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"psFormat": "table {{.ID}}\\t{{.Label \"com.docker.label.cpu\"}}"
|
|
|
|
}`
|
|
|
|
if string(buf) != expConfStr {
|
|
|
|
t.Fatalf("Should have save in new form: \n%s\nnot \n%s", string(buf), expConfStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLegacyJSONSaveWithNoFile(t *testing.T) {
|
|
|
|
js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}`
|
|
|
|
config, err := LegacyLoadFromReader(strings.NewReader(js))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
err = config.Save()
|
2018-03-05 19:59:53 -05:00
|
|
|
assert.ErrorContains(t, err, "with empty filename")
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
|
|
|
fn := filepath.Join(tmpHome, ConfigFileName)
|
|
|
|
f, _ := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
defer f.Close()
|
|
|
|
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, config.SaveToWriter(f))
|
2016-12-25 14:31:52 -05:00
|
|
|
buf, err := ioutil.ReadFile(filepath.Join(tmpHome, ConfigFileName))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
expConfStr := `{
|
|
|
|
"auths": {
|
|
|
|
"https://index.docker.io/v1/": {
|
|
|
|
"auth": "am9lam9lOmhlbGxv",
|
|
|
|
"email": "user@example.com"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
if string(buf) != expConfStr {
|
|
|
|
t.Fatalf("Should have save in new form: \n%s\n not \n%s", string(buf), expConfStr)
|
|
|
|
}
|
|
|
|
}
|
2017-06-22 12:03:58 -04:00
|
|
|
|
|
|
|
func TestLoadDefaultConfigFile(t *testing.T) {
|
|
|
|
dir, cleanup := setupConfigDir(t)
|
|
|
|
defer cleanup()
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
|
|
|
|
filename := filepath.Join(dir, ConfigFileName)
|
|
|
|
content := []byte(`{"PsFormat": "format"}`)
|
|
|
|
err := ioutil.WriteFile(filename, content, 0644)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2017-06-22 12:03:58 -04:00
|
|
|
|
|
|
|
configFile := LoadDefaultConfigFile(buffer)
|
|
|
|
credStore := credentials.DetectDefaultStore("")
|
2017-06-27 10:31:38 -04:00
|
|
|
expected := configfile.New(filename)
|
2017-06-22 12:03:58 -04:00
|
|
|
expected.CredentialsStore = credStore
|
|
|
|
expected.PsFormat = "format"
|
|
|
|
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.DeepEqual(expected, configFile))
|
2017-06-22 12:03:58 -04:00
|
|
|
}
|
2019-01-10 10:49:06 -05:00
|
|
|
|
|
|
|
func TestConfigPath(t *testing.T) {
|
|
|
|
oldDir := Dir()
|
|
|
|
|
2019-03-07 09:28:42 -05:00
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
dir string
|
|
|
|
path []string
|
|
|
|
expected string
|
|
|
|
expectedErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "valid_path",
|
|
|
|
dir: "dummy",
|
|
|
|
path: []string{"a", "b"},
|
|
|
|
expected: filepath.Join("dummy", "a", "b"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid_path_absolute_dir",
|
|
|
|
dir: "/dummy",
|
|
|
|
path: []string{"a", "b"},
|
|
|
|
expected: filepath.Join("/dummy", "a", "b"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid_relative_path",
|
|
|
|
dir: "dummy",
|
|
|
|
path: []string{"e", "..", "..", "f"},
|
|
|
|
expectedErr: fmt.Sprintf("is outside of root config directory %q", "dummy"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid_absolute_path",
|
|
|
|
dir: "dummy",
|
|
|
|
path: []string{"/a", "..", ".."},
|
|
|
|
expectedErr: fmt.Sprintf("is outside of root config directory %q", "dummy"),
|
|
|
|
},
|
|
|
|
} {
|
2019-10-29 08:33:25 -04:00
|
|
|
tc := tc
|
2019-03-07 09:28:42 -05:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
SetDir(tc.dir)
|
|
|
|
f, err := Path(tc.path...)
|
|
|
|
assert.Equal(t, f, tc.expected)
|
|
|
|
if tc.expectedErr == "" {
|
|
|
|
assert.NilError(t, err)
|
|
|
|
} else {
|
|
|
|
assert.ErrorContains(t, err, tc.expectedErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-01-10 10:49:06 -05:00
|
|
|
|
|
|
|
SetDir(oldDir)
|
|
|
|
}
|