github.com/fabiokung/docker@v0.11.2-0.20170222101415-4534dcd49497/daemon/config/config_test.go (about)

     1  package config
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"runtime"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/docker/docker/daemon/discovery"
    11  	"github.com/docker/docker/opts"
    12  	"github.com/docker/docker/pkg/testutil/assert"
    13  	"github.com/spf13/pflag"
    14  )
    15  
    16  func TestDaemonConfigurationNotFound(t *testing.T) {
    17  	_, err := MergeDaemonConfigurations(&Config{}, nil, "/tmp/foo-bar-baz-docker")
    18  	if err == nil || !os.IsNotExist(err) {
    19  		t.Fatalf("expected does not exist error, got %v", err)
    20  	}
    21  }
    22  
    23  func TestDaemonBrokenConfiguration(t *testing.T) {
    24  	f, err := ioutil.TempFile("", "docker-config-")
    25  	if err != nil {
    26  		t.Fatal(err)
    27  	}
    28  
    29  	configFile := f.Name()
    30  	f.Write([]byte(`{"Debug": tru`))
    31  	f.Close()
    32  
    33  	_, err = MergeDaemonConfigurations(&Config{}, nil, configFile)
    34  	if err == nil {
    35  		t.Fatalf("expected error, got %v", err)
    36  	}
    37  }
    38  
    39  func TestParseClusterAdvertiseSettings(t *testing.T) {
    40  	if runtime.GOOS == "solaris" {
    41  		t.Skip("ClusterSettings not supported on Solaris\n")
    42  	}
    43  	_, err := ParseClusterAdvertiseSettings("something", "")
    44  	if err != discovery.ErrDiscoveryDisabled {
    45  		t.Fatalf("expected discovery disabled error, got %v\n", err)
    46  	}
    47  
    48  	_, err = ParseClusterAdvertiseSettings("", "something")
    49  	if err == nil {
    50  		t.Fatalf("expected discovery store error, got %v\n", err)
    51  	}
    52  
    53  	_, err = ParseClusterAdvertiseSettings("etcd", "127.0.0.1:8080")
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  }
    58  
    59  func TestFindConfigurationConflicts(t *testing.T) {
    60  	config := map[string]interface{}{"authorization-plugins": "foobar"}
    61  	flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
    62  
    63  	flags.String("authorization-plugins", "", "")
    64  	assert.NilError(t, flags.Set("authorization-plugins", "asdf"))
    65  
    66  	assert.Error(t,
    67  		findConfigurationConflicts(config, flags),
    68  		"authorization-plugins: (from flag: asdf, from file: foobar)")
    69  }
    70  
    71  func TestFindConfigurationConflictsWithNamedOptions(t *testing.T) {
    72  	config := map[string]interface{}{"hosts": []string{"qwer"}}
    73  	flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
    74  
    75  	var hosts []string
    76  	flags.VarP(opts.NewNamedListOptsRef("hosts", &hosts, opts.ValidateHost), "host", "H", "Daemon socket(s) to connect to")
    77  	assert.NilError(t, flags.Set("host", "tcp://127.0.0.1:4444"))
    78  	assert.NilError(t, flags.Set("host", "unix:///var/run/docker.sock"))
    79  
    80  	assert.Error(t, findConfigurationConflicts(config, flags), "hosts")
    81  }
    82  
    83  func TestDaemonConfigurationMergeConflicts(t *testing.T) {
    84  	f, err := ioutil.TempFile("", "docker-config-")
    85  	if err != nil {
    86  		t.Fatal(err)
    87  	}
    88  
    89  	configFile := f.Name()
    90  	f.Write([]byte(`{"debug": true}`))
    91  	f.Close()
    92  
    93  	flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
    94  	flags.Bool("debug", false, "")
    95  	flags.Set("debug", "false")
    96  
    97  	_, err = MergeDaemonConfigurations(&Config{}, flags, configFile)
    98  	if err == nil {
    99  		t.Fatal("expected error, got nil")
   100  	}
   101  	if !strings.Contains(err.Error(), "debug") {
   102  		t.Fatalf("expected debug conflict, got %v", err)
   103  	}
   104  }
   105  
   106  func TestDaemonConfigurationMergeConflictsWithInnerStructs(t *testing.T) {
   107  	f, err := ioutil.TempFile("", "docker-config-")
   108  	if err != nil {
   109  		t.Fatal(err)
   110  	}
   111  
   112  	configFile := f.Name()
   113  	f.Write([]byte(`{"tlscacert": "/etc/certificates/ca.pem"}`))
   114  	f.Close()
   115  
   116  	flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
   117  	flags.String("tlscacert", "", "")
   118  	flags.Set("tlscacert", "~/.docker/ca.pem")
   119  
   120  	_, err = MergeDaemonConfigurations(&Config{}, flags, configFile)
   121  	if err == nil {
   122  		t.Fatal("expected error, got nil")
   123  	}
   124  	if !strings.Contains(err.Error(), "tlscacert") {
   125  		t.Fatalf("expected tlscacert conflict, got %v", err)
   126  	}
   127  }
   128  
   129  func TestFindConfigurationConflictsWithUnknownKeys(t *testing.T) {
   130  	config := map[string]interface{}{"tls-verify": "true"}
   131  	flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
   132  
   133  	flags.Bool("tlsverify", false, "")
   134  	err := findConfigurationConflicts(config, flags)
   135  	if err == nil {
   136  		t.Fatal("expected error, got nil")
   137  	}
   138  	if !strings.Contains(err.Error(), "the following directives don't match any configuration option: tls-verify") {
   139  		t.Fatalf("expected tls-verify conflict, got %v", err)
   140  	}
   141  }
   142  
   143  func TestFindConfigurationConflictsWithMergedValues(t *testing.T) {
   144  	var hosts []string
   145  	config := map[string]interface{}{"hosts": "tcp://127.0.0.1:2345"}
   146  	flags := pflag.NewFlagSet("base", pflag.ContinueOnError)
   147  	flags.VarP(opts.NewNamedListOptsRef("hosts", &hosts, nil), "host", "H", "")
   148  
   149  	err := findConfigurationConflicts(config, flags)
   150  	if err != nil {
   151  		t.Fatal(err)
   152  	}
   153  
   154  	flags.Set("host", "unix:///var/run/docker.sock")
   155  	err = findConfigurationConflicts(config, flags)
   156  	if err == nil {
   157  		t.Fatal("expected error, got nil")
   158  	}
   159  	if !strings.Contains(err.Error(), "hosts: (from flag: [unix:///var/run/docker.sock], from file: tcp://127.0.0.1:2345)") {
   160  		t.Fatalf("expected hosts conflict, got %v", err)
   161  	}
   162  }
   163  
   164  func TestValidateConfigurationErrors(t *testing.T) {
   165  	minusNumber := -10
   166  	testCases := []struct {
   167  		config *Config
   168  	}{
   169  		{
   170  			config: &Config{
   171  				CommonConfig: CommonConfig{
   172  					Labels: []string{"one"},
   173  				},
   174  			},
   175  		},
   176  		{
   177  			config: &Config{
   178  				CommonConfig: CommonConfig{
   179  					Labels: []string{"foo=bar", "one"},
   180  				},
   181  			},
   182  		},
   183  		{
   184  			config: &Config{
   185  				CommonConfig: CommonConfig{
   186  					DNS: []string{"1.1.1.1o"},
   187  				},
   188  			},
   189  		},
   190  		{
   191  			config: &Config{
   192  				CommonConfig: CommonConfig{
   193  					DNS: []string{"2.2.2.2", "1.1.1.1o"},
   194  				},
   195  			},
   196  		},
   197  		{
   198  			config: &Config{
   199  				CommonConfig: CommonConfig{
   200  					DNSSearch: []string{"123456"},
   201  				},
   202  			},
   203  		},
   204  		{
   205  			config: &Config{
   206  				CommonConfig: CommonConfig{
   207  					DNSSearch: []string{"a.b.c", "123456"},
   208  				},
   209  			},
   210  		},
   211  		{
   212  			config: &Config{
   213  				CommonConfig: CommonConfig{
   214  					MaxConcurrentDownloads: &minusNumber,
   215  					// This is weird...
   216  					ValuesSet: map[string]interface{}{
   217  						"max-concurrent-downloads": -1,
   218  					},
   219  				},
   220  			},
   221  		},
   222  		{
   223  			config: &Config{
   224  				CommonConfig: CommonConfig{
   225  					MaxConcurrentUploads: &minusNumber,
   226  					// This is weird...
   227  					ValuesSet: map[string]interface{}{
   228  						"max-concurrent-uploads": -1,
   229  					},
   230  				},
   231  			},
   232  		},
   233  	}
   234  	for _, tc := range testCases {
   235  		err := Validate(tc.config)
   236  		if err == nil {
   237  			t.Fatalf("expected error, got nil for config %v", tc.config)
   238  		}
   239  	}
   240  }
   241  
   242  func TestValidateConfiguration(t *testing.T) {
   243  	testCases := []struct {
   244  		config *Config
   245  	}{
   246  		{
   247  			config: &Config{
   248  				CommonConfig: CommonConfig{
   249  					Labels: []string{"one=two"},
   250  				},
   251  			},
   252  		},
   253  		{
   254  			config: &Config{
   255  				CommonConfig: CommonConfig{
   256  					DNS: []string{"1.1.1.1"},
   257  				},
   258  			},
   259  		},
   260  		{
   261  			config: &Config{
   262  				CommonConfig: CommonConfig{
   263  					DNSSearch: []string{"a.b.c"},
   264  				},
   265  			},
   266  		},
   267  	}
   268  	for _, tc := range testCases {
   269  		err := Validate(tc.config)
   270  		if err != nil {
   271  			t.Fatalf("expected no error, got error %v", err)
   272  		}
   273  	}
   274  }
   275  
   276  func TestModifiedDiscoverySettings(t *testing.T) {
   277  	cases := []struct {
   278  		current  *Config
   279  		modified *Config
   280  		expected bool
   281  	}{
   282  		{
   283  			current:  discoveryConfig("foo", "bar", map[string]string{}),
   284  			modified: discoveryConfig("foo", "bar", map[string]string{}),
   285  			expected: false,
   286  		},
   287  		{
   288  			current:  discoveryConfig("foo", "bar", map[string]string{"foo": "bar"}),
   289  			modified: discoveryConfig("foo", "bar", map[string]string{"foo": "bar"}),
   290  			expected: false,
   291  		},
   292  		{
   293  			current:  discoveryConfig("foo", "bar", map[string]string{}),
   294  			modified: discoveryConfig("foo", "bar", nil),
   295  			expected: false,
   296  		},
   297  		{
   298  			current:  discoveryConfig("foo", "bar", nil),
   299  			modified: discoveryConfig("foo", "bar", map[string]string{}),
   300  			expected: false,
   301  		},
   302  		{
   303  			current:  discoveryConfig("foo", "bar", nil),
   304  			modified: discoveryConfig("baz", "bar", nil),
   305  			expected: true,
   306  		},
   307  		{
   308  			current:  discoveryConfig("foo", "bar", nil),
   309  			modified: discoveryConfig("foo", "baz", nil),
   310  			expected: true,
   311  		},
   312  		{
   313  			current:  discoveryConfig("foo", "bar", nil),
   314  			modified: discoveryConfig("foo", "bar", map[string]string{"foo": "bar"}),
   315  			expected: true,
   316  		},
   317  	}
   318  
   319  	for _, c := range cases {
   320  		got := ModifiedDiscoverySettings(c.current, c.modified.ClusterStore, c.modified.ClusterAdvertise, c.modified.ClusterOpts)
   321  		if c.expected != got {
   322  			t.Fatalf("expected %v, got %v: current config %v, new config %v", c.expected, got, c.current, c.modified)
   323  		}
   324  	}
   325  }
   326  
   327  func discoveryConfig(backendAddr, advertiseAddr string, opts map[string]string) *Config {
   328  	return &Config{
   329  		CommonConfig: CommonConfig{
   330  			ClusterStore:     backendAddr,
   331  			ClusterAdvertise: advertiseAddr,
   332  			ClusterOpts:      opts,
   333  		},
   334  	}
   335  }