2015-12-10 18:35:10 -05:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/opts"
|
2016-06-22 18:36:51 -04:00
|
|
|
"github.com/docker/docker/pkg/testutil/assert"
|
|
|
|
"github.com/spf13/pflag"
|
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) {
|
|
|
|
f, err := ioutil.TempFile("", "docker-config-")
|
|
|
|
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 TestParseClusterAdvertiseSettings(t *testing.T) {
|
|
|
|
_, err := parseClusterAdvertiseSettings("something", "")
|
|
|
|
if err != errDiscoveryDisabled {
|
|
|
|
t.Fatalf("expected discovery disabled error, got %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = parseClusterAdvertiseSettings("", "something")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected discovery store error, got %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = parseClusterAdvertiseSettings("etcd", "127.0.0.1:8080")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(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", "", "")
|
|
|
|
assert.NilError(t, flags.Set("authorization-plugins", "asdf"))
|
2015-12-10 18:35:10 -05:00
|
|
|
|
2016-06-22 18:36:51 -04:00
|
|
|
assert.Error(t,
|
|
|
|
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")
|
|
|
|
assert.NilError(t, flags.Set("host", "tcp://127.0.0.1:4444"))
|
|
|
|
assert.NilError(t, flags.Set("host", "unix:///var/run/docker.sock"))
|
2015-12-10 18:35:10 -05:00
|
|
|
|
2016-06-22 18:36:51 -04:00
|
|
|
assert.Error(t, findConfigurationConflicts(config, flags), "hosts")
|
2015-12-10 18:35:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDaemonConfigurationMergeConflicts(t *testing.T) {
|
|
|
|
f, err := ioutil.TempFile("", "docker-config-")
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDaemonConfigurationMergeConflictsWithInnerStructs(t *testing.T) {
|
|
|
|
f, err := ioutil.TempFile("", "docker-config-")
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
func TestValidateConfiguration(t *testing.T) {
|
|
|
|
c1 := &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
Labels: []string{"one"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-05-23 17:49:50 -04:00
|
|
|
err := ValidateConfiguration(c1)
|
2016-03-11 03:50:49 -05:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error, got nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
c2 := &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
Labels: []string{"one=two"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-05-23 17:49:50 -04:00
|
|
|
err = ValidateConfiguration(c2)
|
2016-03-11 03:50:49 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected no error, got error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c3 := &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
DNS: []string{"1.1.1.1"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-05-23 17:49:50 -04:00
|
|
|
err = ValidateConfiguration(c3)
|
2016-03-11 03:50:49 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected no error, got error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c4 := &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
DNS: []string{"1.1.1.1o"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-05-23 17:49:50 -04:00
|
|
|
err = ValidateConfiguration(c4)
|
2016-03-11 03:50:49 -05:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error, got nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
c5 := &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
DNSSearch: []string{"a.b.c"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-05-23 17:49:50 -04:00
|
|
|
err = ValidateConfiguration(c5)
|
2016-03-11 03:50:49 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected no error, got error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c6 := &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
DNSSearch: []string{"123456"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-05-23 17:49:50 -04:00
|
|
|
err = ValidateConfiguration(c6)
|
2016-03-11 03:50:49 -05:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error, got nil")
|
|
|
|
}
|
|
|
|
}
|