2018-02-05 16:05:59 -05:00
|
|
|
package config // import "github.com/docker/docker/daemon/config"
|
2015-12-10 18:35:10 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2022-06-06 06:29:15 -04:00
|
|
|
"reflect"
|
2015-12-10 18:35:10 -05:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/ipamutils"
|
2021-05-27 20:15:56 -04:00
|
|
|
"github.com/docker/docker/opts"
|
2022-06-06 06:29:15 -04:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
|
|
|
"github.com/imdario/mergo"
|
2016-06-22 18:36:51 -04:00
|
|
|
"github.com/spf13/pflag"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/fs"
|
|
|
|
"gotest.tools/v3/skip"
|
2015-12-10 18:35:10 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDaemonConfigurationNotFound(t *testing.T) {
|
|
|
|
_, err := MergeDaemonConfigurations(&Config{}, nil, "/tmp/foo-bar-baz-docker")
|
|
|
|
if err == nil || !os.IsNotExist(err) {
|
|
|
|
t.Fatalf("expected does not exist error, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDaemonBrokenConfiguration(t *testing.T) {
|
2021-08-24 06:10:50 -04:00
|
|
|
f, err := os.CreateTemp("", "docker-config-")
|
2015-12-10 18:35:10 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
configFile := f.Name()
|
|
|
|
f.Write([]byte(`{"Debug": tru`))
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
_, err = MergeDaemonConfigurations(&Config{}, nil, configFile)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected error, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFindConfigurationConflicts(t *testing.T) {
|
|
|
|
config := map[string]interface{}{"authorization-plugins": "foobar"}
|
2016-06-22 18:36:51 -04:00
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
2015-12-10 18:35:10 -05:00
|
|
|
|
2016-06-22 18:36:51 -04:00
|
|
|
flags.String("authorization-plugins", "", "")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, flags.Set("authorization-plugins", "asdf"))
|
2018-05-20 18:06:50 -04:00
|
|
|
assert.Check(t, is.ErrorContains(findConfigurationConflicts(config, flags), "authorization-plugins: (from flag: asdf, from file: foobar)"))
|
2015-12-10 18:35:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFindConfigurationConflictsWithNamedOptions(t *testing.T) {
|
|
|
|
config := map[string]interface{}{"hosts": []string{"qwer"}}
|
2016-06-22 18:36:51 -04:00
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
2015-12-10 18:35:10 -05:00
|
|
|
|
|
|
|
var hosts []string
|
2016-06-22 18:36:51 -04:00
|
|
|
flags.VarP(opts.NewNamedListOptsRef("hosts", &hosts, opts.ValidateHost), "host", "H", "Daemon socket(s) to connect to")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, flags.Set("host", "tcp://127.0.0.1:4444"))
|
|
|
|
assert.Check(t, flags.Set("host", "unix:///var/run/docker.sock"))
|
2018-05-20 18:06:50 -04:00
|
|
|
assert.Check(t, is.ErrorContains(findConfigurationConflicts(config, flags), "hosts"))
|
2015-12-10 18:35:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDaemonConfigurationMergeConflicts(t *testing.T) {
|
2021-08-24 06:10:50 -04:00
|
|
|
f, err := os.CreateTemp("", "docker-config-")
|
2015-12-10 18:35:10 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
configFile := f.Name()
|
|
|
|
f.Write([]byte(`{"debug": true}`))
|
|
|
|
f.Close()
|
|
|
|
|
2016-06-22 18:36:51 -04:00
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flags.Bool("debug", false, "")
|
2015-12-10 18:35:10 -05:00
|
|
|
flags.Set("debug", "false")
|
|
|
|
|
|
|
|
_, err = MergeDaemonConfigurations(&Config{}, flags, configFile)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error, got nil")
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "debug") {
|
|
|
|
t.Fatalf("expected debug conflict, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 19:58:06 -05:00
|
|
|
func TestDaemonConfigurationMergeConcurrent(t *testing.T) {
|
2021-08-24 06:10:50 -04:00
|
|
|
f, err := os.CreateTemp("", "docker-config-")
|
2017-03-01 19:58:06 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
configFile := f.Name()
|
|
|
|
f.Write([]byte(`{"max-concurrent-downloads": 1}`))
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
_, err = MergeDaemonConfigurations(&Config{}, nil, configFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("expected error, got nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDaemonConfigurationMergeConcurrentError(t *testing.T) {
|
2021-08-24 06:10:50 -04:00
|
|
|
f, err := os.CreateTemp("", "docker-config-")
|
2017-03-01 19:58:06 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
configFile := f.Name()
|
|
|
|
f.Write([]byte(`{"max-concurrent-downloads": -1}`))
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
_, err = MergeDaemonConfigurations(&Config{}, nil, configFile)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected no error, got error %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
func TestDaemonConfigurationMergeConflictsWithInnerStructs(t *testing.T) {
|
2021-08-24 06:10:50 -04:00
|
|
|
f, err := os.CreateTemp("", "docker-config-")
|
2015-12-10 18:35:10 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
configFile := f.Name()
|
|
|
|
f.Write([]byte(`{"tlscacert": "/etc/certificates/ca.pem"}`))
|
|
|
|
f.Close()
|
|
|
|
|
2016-06-22 18:36:51 -04:00
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flags.String("tlscacert", "", "")
|
2015-12-10 18:35:10 -05:00
|
|
|
flags.Set("tlscacert", "~/.docker/ca.pem")
|
|
|
|
|
|
|
|
_, err = MergeDaemonConfigurations(&Config{}, flags, configFile)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error, got nil")
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "tlscacert") {
|
|
|
|
t.Fatalf("expected tlscacert conflict, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 17:16:49 -05:00
|
|
|
|
2020-07-20 20:54:51 -04:00
|
|
|
// Test for #40711
|
|
|
|
func TestDaemonConfigurationMergeDefaultAddressPools(t *testing.T) {
|
|
|
|
emptyConfigFile := fs.NewFile(t, "config", fs.WithContent(`{}`))
|
|
|
|
defer emptyConfigFile.Remove()
|
|
|
|
configFile := fs.NewFile(t, "config", fs.WithContent(`{"default-address-pools":[{"base": "10.123.0.0/16", "size": 24 }]}`))
|
|
|
|
defer configFile.Remove()
|
|
|
|
|
|
|
|
expected := []*ipamutils.NetworkToSplit{{Base: "10.123.0.0/16", Size: 24}}
|
|
|
|
|
|
|
|
t.Run("empty config file", func(t *testing.T) {
|
|
|
|
var conf = Config{}
|
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flags.Var(&conf.NetworkConfig.DefaultAddressPools, "default-address-pool", "")
|
|
|
|
flags.Set("default-address-pool", "base=10.123.0.0/16,size=24")
|
|
|
|
|
|
|
|
config, err := MergeDaemonConfigurations(&conf, flags, emptyConfigFile.Path())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, config.DefaultAddressPools.Value(), expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("config file", func(t *testing.T) {
|
|
|
|
var conf = Config{}
|
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flags.Var(&conf.NetworkConfig.DefaultAddressPools, "default-address-pool", "")
|
|
|
|
|
|
|
|
config, err := MergeDaemonConfigurations(&conf, flags, configFile.Path())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, config.DefaultAddressPools.Value(), expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("with conflicting options", func(t *testing.T) {
|
|
|
|
var conf = Config{}
|
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flags.Var(&conf.NetworkConfig.DefaultAddressPools, "default-address-pool", "")
|
|
|
|
flags.Set("default-address-pool", "base=10.123.0.0/16,size=24")
|
|
|
|
|
|
|
|
_, err := MergeDaemonConfigurations(&conf, flags, configFile.Path())
|
|
|
|
assert.ErrorContains(t, err, "the following directives are specified both as a flag and in the configuration file")
|
|
|
|
assert.ErrorContains(t, err, "default-address-pools")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-20 17:16:49 -05:00
|
|
|
func TestFindConfigurationConflictsWithUnknownKeys(t *testing.T) {
|
|
|
|
config := map[string]interface{}{"tls-verify": "true"}
|
2016-06-22 18:36:51 -04:00
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
2016-01-20 17:16:49 -05:00
|
|
|
|
2016-06-22 18:36:51 -04:00
|
|
|
flags.Bool("tlsverify", false, "")
|
2016-01-20 17:16:49 -05:00
|
|
|
err := findConfigurationConflicts(config, flags)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error, got nil")
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "the following directives don't match any configuration option: tls-verify") {
|
|
|
|
t.Fatalf("expected tls-verify conflict, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFindConfigurationConflictsWithMergedValues(t *testing.T) {
|
|
|
|
var hosts []string
|
|
|
|
config := map[string]interface{}{"hosts": "tcp://127.0.0.1:2345"}
|
2016-06-22 18:36:51 -04:00
|
|
|
flags := pflag.NewFlagSet("base", pflag.ContinueOnError)
|
|
|
|
flags.VarP(opts.NewNamedListOptsRef("hosts", &hosts, nil), "host", "H", "")
|
2016-01-20 17:16:49 -05:00
|
|
|
|
|
|
|
err := findConfigurationConflicts(config, flags)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-06-22 18:36:51 -04:00
|
|
|
flags.Set("host", "unix:///var/run/docker.sock")
|
2016-01-20 17:16:49 -05:00
|
|
|
err = findConfigurationConflicts(config, flags)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error, got nil")
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "hosts: (from flag: [unix:///var/run/docker.sock], from file: tcp://127.0.0.1:2345)") {
|
|
|
|
t.Fatalf("expected hosts conflict, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
2016-03-11 03:50:49 -05:00
|
|
|
|
2017-01-23 06:23:07 -05:00
|
|
|
func TestValidateConfigurationErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2019-06-25 09:26:36 -04:00
|
|
|
name string
|
2022-06-06 06:29:15 -04:00
|
|
|
field string
|
2019-06-25 09:26:36 -04:00
|
|
|
config *Config
|
|
|
|
expectedErr string
|
2017-01-23 06:23:07 -05:00
|
|
|
}{
|
|
|
|
{
|
2019-06-25 09:26:36 -04:00
|
|
|
name: "single label without value",
|
2017-01-23 06:23:07 -05:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
Labels: []string{"one"},
|
|
|
|
},
|
|
|
|
},
|
2019-06-25 09:26:36 -04:00
|
|
|
expectedErr: "bad attribute format: one",
|
2016-03-11 03:50:49 -05:00
|
|
|
},
|
2017-01-23 06:23:07 -05:00
|
|
|
{
|
2019-06-25 09:26:36 -04:00
|
|
|
name: "multiple label without value",
|
2017-01-23 06:23:07 -05:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
Labels: []string{"foo=bar", "one"},
|
|
|
|
},
|
|
|
|
},
|
2019-06-25 09:26:36 -04:00
|
|
|
expectedErr: "bad attribute format: one",
|
2016-03-11 03:50:49 -05:00
|
|
|
},
|
2017-01-23 06:23:07 -05:00
|
|
|
{
|
2019-06-25 09:26:36 -04:00
|
|
|
name: "single DNS, invalid IP-address",
|
2017-01-23 06:23:07 -05:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
2019-06-05 21:36:33 -04:00
|
|
|
DNSConfig: DNSConfig{
|
|
|
|
DNS: []string{"1.1.1.1o"},
|
|
|
|
},
|
2017-01-23 06:23:07 -05:00
|
|
|
},
|
|
|
|
},
|
2019-06-25 09:26:36 -04:00
|
|
|
expectedErr: "1.1.1.1o is not an ip address",
|
2017-01-23 06:23:07 -05:00
|
|
|
},
|
|
|
|
{
|
2019-06-25 09:26:36 -04:00
|
|
|
name: "multiple DNS, invalid IP-address",
|
2017-01-23 06:23:07 -05:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
2019-06-05 21:36:33 -04:00
|
|
|
DNSConfig: DNSConfig{
|
|
|
|
DNS: []string{"2.2.2.2", "1.1.1.1o"},
|
|
|
|
},
|
2017-01-23 06:23:07 -05:00
|
|
|
},
|
|
|
|
},
|
2019-06-25 09:26:36 -04:00
|
|
|
expectedErr: "1.1.1.1o is not an ip address",
|
2017-01-23 06:23:07 -05:00
|
|
|
},
|
|
|
|
{
|
2019-06-25 09:26:36 -04:00
|
|
|
name: "single DNSSearch",
|
2017-01-23 06:23:07 -05:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
2019-06-05 21:36:33 -04:00
|
|
|
DNSConfig: DNSConfig{
|
|
|
|
DNSSearch: []string{"123456"},
|
|
|
|
},
|
2017-01-23 06:23:07 -05:00
|
|
|
},
|
|
|
|
},
|
2019-06-25 09:26:36 -04:00
|
|
|
expectedErr: "123456 is not a valid domain",
|
2017-01-23 06:23:07 -05:00
|
|
|
},
|
|
|
|
{
|
2019-06-25 09:26:36 -04:00
|
|
|
name: "multiple DNSSearch",
|
2017-01-23 06:23:07 -05:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
2019-06-05 21:36:33 -04:00
|
|
|
DNSConfig: DNSConfig{
|
|
|
|
DNSSearch: []string{"a.b.c", "123456"},
|
|
|
|
},
|
2017-01-23 06:23:07 -05:00
|
|
|
},
|
|
|
|
},
|
2019-06-25 09:26:36 -04:00
|
|
|
expectedErr: "123456 is not a valid domain",
|
2017-01-23 06:23:07 -05:00
|
|
|
},
|
2022-06-06 10:53:25 -04:00
|
|
|
{
|
|
|
|
name: "negative MTU",
|
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
Mtu: -10,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedErr: "invalid default MTU: -10",
|
|
|
|
},
|
2017-01-23 06:23:07 -05:00
|
|
|
{
|
2019-06-25 09:26:36 -04:00
|
|
|
name: "negative max-concurrent-downloads",
|
2017-01-23 06:23:07 -05:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
2022-04-24 16:59:54 -04:00
|
|
|
MaxConcurrentDownloads: -10,
|
2017-01-23 06:23:07 -05:00
|
|
|
},
|
|
|
|
},
|
2019-06-25 09:26:36 -04:00
|
|
|
expectedErr: "invalid max concurrent downloads: -10",
|
2017-01-23 06:23:07 -05:00
|
|
|
},
|
|
|
|
{
|
2019-06-25 09:26:36 -04:00
|
|
|
name: "negative max-concurrent-uploads",
|
2017-01-23 06:23:07 -05:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
2022-04-24 16:59:54 -04:00
|
|
|
MaxConcurrentUploads: -10,
|
2019-06-25 09:26:36 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedErr: "invalid max concurrent uploads: -10",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "negative max-download-attempts",
|
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
2022-04-24 16:59:54 -04:00
|
|
|
MaxDownloadAttempts: -10,
|
2017-01-23 06:23:07 -05:00
|
|
|
},
|
|
|
|
},
|
2019-06-25 09:26:36 -04:00
|
|
|
expectedErr: "invalid max download attempts: -10",
|
2016-03-11 03:50:49 -05:00
|
|
|
},
|
2022-04-24 16:42:05 -04:00
|
|
|
// TODO(thaJeztah) temporarily excluding this test as it assumes defaults are set before validating and applying updated configs
|
|
|
|
/*
|
|
|
|
{
|
2022-06-06 06:29:15 -04:00
|
|
|
name: "zero max-download-attempts",
|
|
|
|
field: "MaxDownloadAttempts",
|
2022-04-24 16:42:05 -04:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
2022-04-24 16:59:54 -04:00
|
|
|
MaxDownloadAttempts: 0,
|
2022-04-24 16:42:05 -04:00
|
|
|
},
|
2019-06-25 09:26:36 -04:00
|
|
|
},
|
2022-04-24 16:42:05 -04:00
|
|
|
expectedErr: "invalid max download attempts: 0",
|
2019-06-25 09:26:36 -04:00
|
|
|
},
|
2022-04-24 16:42:05 -04:00
|
|
|
*/
|
2019-06-25 09:26:36 -04:00
|
|
|
{
|
|
|
|
name: "generic resource without =",
|
2017-10-30 18:23:43 -04:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
NodeGenericResources: []string{"foo"},
|
|
|
|
},
|
|
|
|
},
|
2019-06-25 09:26:36 -04:00
|
|
|
expectedErr: "could not parse GenericResource: incorrect term foo, missing '=' or malformed expression",
|
2017-10-30 18:23:43 -04:00
|
|
|
},
|
|
|
|
{
|
2019-06-25 09:26:36 -04:00
|
|
|
name: "generic resource mixed named and discrete",
|
2017-10-30 18:23:43 -04:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
NodeGenericResources: []string{"foo=bar", "foo=1"},
|
|
|
|
},
|
|
|
|
},
|
2019-06-25 09:26:36 -04:00
|
|
|
expectedErr: "could not parse GenericResource: mixed discrete and named resources in expression 'foo=[bar 1]'",
|
2017-10-30 18:23:43 -04:00
|
|
|
},
|
2022-04-04 09:18:01 -04:00
|
|
|
{
|
|
|
|
name: "with invalid hosts",
|
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
Hosts: []string{"127.0.0.1:2375/path"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedErr: "invalid bind address (127.0.0.1:2375/path): should not contain a path element",
|
|
|
|
},
|
2022-04-22 09:59:23 -04:00
|
|
|
{
|
|
|
|
name: "with invalid log-level",
|
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
LogLevel: "foobar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedErr: "invalid logging level: foobar",
|
|
|
|
},
|
2016-03-11 03:50:49 -05:00
|
|
|
}
|
2017-01-23 06:23:07 -05:00
|
|
|
for _, tc := range testCases {
|
2019-06-25 09:26:36 -04:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-06-06 06:29:15 -04:00
|
|
|
cfg := New()
|
|
|
|
if tc.field != "" {
|
|
|
|
assert.Check(t, mergo.Merge(cfg, tc.config, mergo.WithOverride, withForceOverwrite(tc.field)))
|
|
|
|
} else {
|
|
|
|
assert.Check(t, mergo.Merge(cfg, tc.config, mergo.WithOverride))
|
|
|
|
}
|
|
|
|
err := Validate(cfg)
|
2019-06-25 09:26:36 -04:00
|
|
|
assert.Error(t, err, tc.expectedErr)
|
|
|
|
})
|
2016-03-11 03:50:49 -05:00
|
|
|
}
|
2017-01-23 06:23:07 -05:00
|
|
|
}
|
2016-03-11 03:50:49 -05:00
|
|
|
|
2022-06-06 06:29:15 -04:00
|
|
|
func withForceOverwrite(fieldName string) func(config *mergo.Config) {
|
|
|
|
return mergo.WithTransformers(overwriteTransformer{fieldName: fieldName})
|
|
|
|
}
|
|
|
|
|
|
|
|
type overwriteTransformer struct {
|
|
|
|
fieldName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tf overwriteTransformer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error {
|
|
|
|
if typ == reflect.TypeOf(CommonConfig{}) {
|
|
|
|
return func(dst, src reflect.Value) error {
|
|
|
|
dst.FieldByName(tf.fieldName).Set(src.FieldByName(tf.fieldName))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-23 06:23:07 -05:00
|
|
|
func TestValidateConfiguration(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2019-06-25 09:26:36 -04:00
|
|
|
name string
|
2022-06-06 06:29:15 -04:00
|
|
|
field string
|
2017-01-23 06:23:07 -05:00
|
|
|
config *Config
|
|
|
|
}{
|
|
|
|
{
|
2022-06-06 06:29:15 -04:00
|
|
|
name: "with label",
|
|
|
|
field: "Labels",
|
2017-01-23 06:23:07 -05:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
Labels: []string{"one=two"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-06 06:29:15 -04:00
|
|
|
name: "with dns",
|
|
|
|
field: "DNSConfig",
|
2017-01-23 06:23:07 -05:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
2019-06-05 21:36:33 -04:00
|
|
|
DNSConfig: DNSConfig{
|
|
|
|
DNS: []string{"1.1.1.1"},
|
|
|
|
},
|
2017-01-23 06:23:07 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-06 06:29:15 -04:00
|
|
|
name: "with dns-search",
|
|
|
|
field: "DNSConfig",
|
2017-01-23 06:23:07 -05:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
2019-06-05 21:36:33 -04:00
|
|
|
DNSConfig: DNSConfig{
|
|
|
|
DNSSearch: []string{"a.b.c"},
|
|
|
|
},
|
2017-01-23 06:23:07 -05:00
|
|
|
},
|
|
|
|
},
|
2016-03-11 03:50:49 -05:00
|
|
|
},
|
2022-06-06 10:53:25 -04:00
|
|
|
{
|
2022-06-06 06:29:15 -04:00
|
|
|
name: "with mtu",
|
|
|
|
field: "Mtu",
|
2022-06-06 10:53:25 -04:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
Mtu: 1234,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-03-01 19:58:06 -05:00
|
|
|
{
|
2022-06-06 06:29:15 -04:00
|
|
|
name: "with max-concurrent-downloads",
|
|
|
|
field: "MaxConcurrentDownloads",
|
2017-03-01 19:58:06 -05:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
2022-04-24 16:59:54 -04:00
|
|
|
MaxConcurrentDownloads: 4,
|
2017-03-01 19:58:06 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-06 06:29:15 -04:00
|
|
|
name: "with max-concurrent-uploads",
|
|
|
|
field: "MaxConcurrentUploads",
|
2017-03-01 19:58:06 -05:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
2022-04-24 16:59:54 -04:00
|
|
|
MaxConcurrentUploads: 4,
|
2017-03-01 19:58:06 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-10-30 18:23:43 -04:00
|
|
|
{
|
2022-06-06 06:29:15 -04:00
|
|
|
name: "with max-download-attempts",
|
|
|
|
field: "MaxDownloadAttempts",
|
2019-06-25 09:26:36 -04:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
2022-04-24 16:59:54 -04:00
|
|
|
MaxDownloadAttempts: 4,
|
2019-06-25 09:26:36 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-06 06:29:15 -04:00
|
|
|
name: "with multiple node generic resources",
|
|
|
|
field: "NodeGenericResources",
|
2017-10-30 18:23:43 -04:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
NodeGenericResources: []string{"foo=bar", "foo=baz"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-06 06:29:15 -04:00
|
|
|
name: "with node generic resources",
|
|
|
|
field: "NodeGenericResources",
|
2017-10-30 18:23:43 -04:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
NodeGenericResources: []string{"foo=1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-04-04 09:18:01 -04:00
|
|
|
{
|
2022-06-06 06:29:15 -04:00
|
|
|
name: "with hosts",
|
|
|
|
field: "Hosts",
|
2022-04-04 09:18:01 -04:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
Hosts: []string{"tcp://127.0.0.1:2375"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-04-22 09:59:23 -04:00
|
|
|
{
|
2022-06-06 06:29:15 -04:00
|
|
|
name: "with log-level warn",
|
|
|
|
field: "LogLevel",
|
2022-04-22 09:59:23 -04:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
LogLevel: "warn",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-03-11 03:50:49 -05:00
|
|
|
}
|
2017-01-23 06:23:07 -05:00
|
|
|
for _, tc := range testCases {
|
2019-06-25 09:26:36 -04:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-06-06 06:29:15 -04:00
|
|
|
// Start with a config with all defaults set, so that we only
|
|
|
|
cfg := New()
|
|
|
|
assert.Check(t, mergo.Merge(cfg, tc.config, mergo.WithOverride))
|
|
|
|
|
|
|
|
// Check that the override happened :)
|
|
|
|
assert.Check(t, is.DeepEqual(cfg, tc.config, field(tc.field)))
|
|
|
|
err := Validate(cfg)
|
2019-06-25 09:26:36 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
})
|
2016-03-11 03:50:49 -05:00
|
|
|
}
|
2017-01-23 06:23:07 -05:00
|
|
|
}
|
2016-03-11 03:50:49 -05:00
|
|
|
|
2022-06-06 06:29:15 -04:00
|
|
|
func field(field string) cmp.Option {
|
|
|
|
tmp := reflect.TypeOf(Config{})
|
|
|
|
ignoreFields := make([]string, 0, tmp.NumField())
|
|
|
|
for i := 0; i < tmp.NumField(); i++ {
|
|
|
|
if tmp.Field(i).Name != field {
|
|
|
|
ignoreFields = append(ignoreFields, tmp.Field(i).Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cmpopts.IgnoreFields(Config{}, ignoreFields...)
|
|
|
|
}
|
|
|
|
|
2017-10-07 17:26:50 -04:00
|
|
|
// TestReloadSetConfigFileNotExist tests that when `--config-file` is set
|
|
|
|
// and it doesn't exist the `Reload` function returns an error.
|
|
|
|
func TestReloadSetConfigFileNotExist(t *testing.T) {
|
|
|
|
configFile := "/tmp/blabla/not/exists/config.json"
|
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flags.String("config-file", "", "")
|
|
|
|
flags.Set("config-file", configFile)
|
|
|
|
|
|
|
|
err := Reload(configFile, flags, func(c *Config) {})
|
2018-05-20 18:06:50 -04:00
|
|
|
assert.Check(t, is.ErrorContains(err, "unable to configure the Docker daemon with file"))
|
2017-10-07 17:26:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestReloadDefaultConfigNotExist tests that if the default configuration file
|
|
|
|
// doesn't exist the daemon still will be reloaded.
|
|
|
|
func TestReloadDefaultConfigNotExist(t *testing.T) {
|
2018-06-11 09:32:11 -04:00
|
|
|
skip.If(t, os.Getuid() != 0, "skipping test that requires root")
|
2022-04-04 08:56:48 -04:00
|
|
|
defaultConfigFile := "/tmp/blabla/not/exists/daemon.json"
|
2017-10-07 17:26:50 -04:00
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
2022-04-04 08:56:48 -04:00
|
|
|
flags.String("config-file", defaultConfigFile, "")
|
2022-06-06 10:54:21 -04:00
|
|
|
reloaded := false
|
2022-04-04 08:56:48 -04:00
|
|
|
err := Reload(defaultConfigFile, flags, func(c *Config) {
|
2017-10-07 17:26:50 -04:00
|
|
|
reloaded = true
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err)
|
|
|
|
assert.Check(t, reloaded)
|
2017-10-07 17:26:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestReloadBadDefaultConfig tests that when `--config-file` is not set
|
|
|
|
// and the default configuration file exists and is bad return an error
|
|
|
|
func TestReloadBadDefaultConfig(t *testing.T) {
|
2021-08-24 06:10:50 -04:00
|
|
|
f, err := os.CreateTemp("", "docker-config-")
|
2017-10-07 17:26:50 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
configFile := f.Name()
|
|
|
|
f.Write([]byte(`{wrong: "configuration"}`))
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flags.String("config-file", configFile, "")
|
2022-06-06 10:54:21 -04:00
|
|
|
reloaded := false
|
|
|
|
err = Reload(configFile, flags, func(c *Config) {
|
|
|
|
reloaded = true
|
|
|
|
})
|
2018-05-20 18:06:50 -04:00
|
|
|
assert.Check(t, is.ErrorContains(err, "unable to configure the Docker daemon with file"))
|
2022-06-06 10:54:21 -04:00
|
|
|
assert.Check(t, reloaded == false)
|
2017-10-07 17:26:50 -04:00
|
|
|
}
|
2017-11-11 21:09:28 -05:00
|
|
|
|
|
|
|
func TestReloadWithConflictingLabels(t *testing.T) {
|
|
|
|
tempFile := fs.NewFile(t, "config", fs.WithContent(`{"labels":["foo=bar","foo=baz"]}`))
|
|
|
|
defer tempFile.Remove()
|
|
|
|
configFile := tempFile.Path()
|
|
|
|
|
|
|
|
var lbls []string
|
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flags.String("config-file", configFile, "")
|
|
|
|
flags.StringSlice("labels", lbls, "")
|
2022-06-06 10:54:21 -04:00
|
|
|
reloaded := false
|
|
|
|
err := Reload(configFile, flags, func(c *Config) {
|
|
|
|
reloaded = true
|
|
|
|
})
|
2018-05-20 18:06:50 -04:00
|
|
|
assert.Check(t, is.ErrorContains(err, "conflict labels for foo=baz and foo=bar"))
|
2022-06-06 10:54:21 -04:00
|
|
|
assert.Check(t, reloaded == false)
|
2017-11-11 21:09:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReloadWithDuplicateLabels(t *testing.T) {
|
|
|
|
tempFile := fs.NewFile(t, "config", fs.WithContent(`{"labels":["foo=the-same","foo=the-same"]}`))
|
|
|
|
defer tempFile.Remove()
|
|
|
|
configFile := tempFile.Path()
|
|
|
|
|
|
|
|
var lbls []string
|
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flags.String("config-file", configFile, "")
|
|
|
|
flags.StringSlice("labels", lbls, "")
|
2022-06-06 10:54:21 -04:00
|
|
|
reloaded := false
|
|
|
|
err := Reload(configFile, flags, func(c *Config) {
|
|
|
|
reloaded = true
|
|
|
|
assert.Check(t, is.DeepEqual(c.Labels, []string{"foo=the-same"}))
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err)
|
2022-06-06 10:54:21 -04:00
|
|
|
assert.Check(t, reloaded)
|
2017-11-11 21:09:28 -05:00
|
|
|
}
|
2021-08-31 08:05:49 -04:00
|
|
|
|
|
|
|
func TestMaskURLCredentials(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
rawURL string
|
|
|
|
maskedURL string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
rawURL: "",
|
|
|
|
maskedURL: "",
|
|
|
|
}, {
|
|
|
|
rawURL: "invalidURL",
|
|
|
|
maskedURL: "invalidURL",
|
|
|
|
}, {
|
|
|
|
rawURL: "http://proxy.example.com:80/",
|
|
|
|
maskedURL: "http://proxy.example.com:80/",
|
|
|
|
}, {
|
|
|
|
rawURL: "http://USER:PASSWORD@proxy.example.com:80/",
|
|
|
|
maskedURL: "http://xxxxx:xxxxx@proxy.example.com:80/",
|
|
|
|
}, {
|
|
|
|
rawURL: "http://PASSWORD:PASSWORD@proxy.example.com:80/",
|
|
|
|
maskedURL: "http://xxxxx:xxxxx@proxy.example.com:80/",
|
|
|
|
}, {
|
|
|
|
rawURL: "http://USER:@proxy.example.com:80/",
|
|
|
|
maskedURL: "http://xxxxx:xxxxx@proxy.example.com:80/",
|
|
|
|
}, {
|
|
|
|
rawURL: "http://:PASSWORD@proxy.example.com:80/",
|
|
|
|
maskedURL: "http://xxxxx:xxxxx@proxy.example.com:80/",
|
|
|
|
}, {
|
|
|
|
rawURL: "http://USER@docker:password@proxy.example.com:80/",
|
|
|
|
maskedURL: "http://xxxxx:xxxxx@proxy.example.com:80/",
|
|
|
|
}, {
|
|
|
|
rawURL: "http://USER%40docker:password@proxy.example.com:80/",
|
|
|
|
maskedURL: "http://xxxxx:xxxxx@proxy.example.com:80/",
|
|
|
|
}, {
|
|
|
|
rawURL: "http://USER%40docker:pa%3Fsword@proxy.example.com:80/",
|
|
|
|
maskedURL: "http://xxxxx:xxxxx@proxy.example.com:80/",
|
|
|
|
}, {
|
|
|
|
rawURL: "http://USER%40docker:pa%3Fsword@proxy.example.com:80/hello%20world",
|
|
|
|
maskedURL: "http://xxxxx:xxxxx@proxy.example.com:80/hello%20world",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
maskedURL := MaskCredentials(test.rawURL)
|
|
|
|
assert.Equal(t, maskedURL, test.maskedURL)
|
|
|
|
}
|
|
|
|
}
|