func TestExternalCAOptionMultiple(t *testing.T) { opt := &ExternalCAOption{} assert.NilError(t, opt.Set("protocol=cfssl,url=https://example.com")) assert.NilError(t, opt.Set("protocol=CFSSL,url=anything")) assert.Equal(t, len(opt.Value()), 2) assert.Equal(t, opt.String(), "cfssl: https://example.com, cfssl: anything") }
// Check the deserialization of the meta stats structure func TestNodeMetastate_FromBytes(t *testing.T) { metastate := NewNodeMetastate(0) // Serialize into bytes array bytes, err := metastate.Bytes() assert.NilError(t, err) if bytes == nil { t.Fatal("Was not able to serialize meta state into byte array.") } // Deserialize back and check, that state still have same // height value state, err := FromBytes(bytes) assert.NilError(t, err) assert.Equal(t, state.Height(), uint64(0)) // Update state to the new height and serialize it again state.Update(17) bytes, err = state.Bytes() assert.NilError(t, err) if bytes == nil { t.Fatal("Was not able to serialize meta state into byte array.") } // Restore state from byte array and validate // that stored height is still the same updatedState, err := FromBytes(bytes) assert.NilError(t, err) assert.Equal(t, updatedState.Height(), uint64(17)) }
func TestDurationOptSetAndValue(t *testing.T) { var duration DurationOpt assert.NilError(t, duration.Set("300s")) assert.Equal(t, *duration.Value(), time.Duration(300*10e8)) assert.NilError(t, duration.Set("-300s")) assert.Equal(t, *duration.Value(), time.Duration(-300*10e8)) }
func TestBuildContainerListOptions(t *testing.T) { filters := opts.NewFilterOpt() assert.NilError(t, filters.Set("foo=bar")) assert.NilError(t, filters.Set("baz=foo")) contexts := []struct { psOpts *psOptions expectedAll bool expectedSize bool expectedLimit int expectedFilters map[string]string }{ { psOpts: &psOptions{ all: true, size: true, last: 5, filter: filters, }, expectedAll: true, expectedSize: true, expectedLimit: 5, expectedFilters: map[string]string{ "foo": "bar", "baz": "foo", }, }, { psOpts: &psOptions{ all: true, size: true, last: -1, nLatest: true, }, expectedAll: true, expectedSize: true, expectedLimit: 1, expectedFilters: make(map[string]string), }, } for _, c := range contexts { options, err := buildContainerListOptions(c.psOpts) assert.NilError(t, err) assert.Equal(t, c.expectedAll, options.All) assert.Equal(t, c.expectedSize, options.Size) assert.Equal(t, c.expectedLimit, options.Limit) assert.Equal(t, options.Filter.Len(), len(c.expectedFilters)) for k, v := range c.expectedFilters { f := options.Filter if !f.ExactMatch(k, v) { t.Fatalf("Expected filter with key %s to be %s but got %s", k, v, f.Get(k)) } } } }
func TestAutoAcceptOptionSetConflict(t *testing.T) { opt := NewAutoAcceptOption() assert.NilError(t, opt.Set("manager")) assert.Error(t, opt.Set("none"), "value NONE is incompatible with MANAGER") opt = NewAutoAcceptOption() assert.NilError(t, opt.Set("none")) assert.Error(t, opt.Set("worker"), "value NONE is incompatible with WORKER") }
func TestAutoAcceptOptionString(t *testing.T) { opt := NewAutoAcceptOption() assert.NilError(t, opt.Set("manager")) assert.NilError(t, opt.Set("worker")) repr := opt.String() assert.Contains(t, repr, "worker=true") assert.Contains(t, repr, "manager=true") }
// NewTempFile returns a new temp file with contents func NewTempFile(t assert.TestingT, prefix string, content string) *TempFile { file, err := ioutil.TempFile("", prefix+"-") assert.NilError(t, err) _, err = file.Write([]byte(content)) assert.NilError(t, err) file.Close() return &TempFile{File: file} }
func TestFindConfigurationConflictsWithNamedOptions(t *testing.T) { config := map[string]interface{}{"hosts": []string{"qwer"}} flags := pflag.NewFlagSet("test", pflag.ContinueOnError) var hosts []string 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")) assert.Error(t, findConfigurationConflicts(config, flags), "hosts") }
func TestLoadDaemonCliConfigWithConflicts(t *testing.T) { tempFile := tempfile.NewTempFile(t, "config", `{"labels": ["l3=foo"]}`) defer tempFile.Remove() configFile := tempFile.Name() opts := defaultOptions(configFile) flags := opts.flags assert.NilError(t, flags.Set(flagDaemonConfigFile, configFile)) assert.NilError(t, flags.Set("label", "l1=bar")) assert.NilError(t, flags.Set("label", "l2=baz")) _, err := loadDaemonCliConfig(opts) assert.Error(t, err, "as a flag and in the configuration file: labels") }
func TestConvertVolumeToMountNamedVolume(t *testing.T) { stackVolumes := volumes{ "normal": composetypes.VolumeConfig{ Driver: "glusterfs", DriverOpts: map[string]string{ "opt": "value", }, Labels: map[string]string{ "something": "labeled", }, }, } namespace := NewNamespace("foo") expected := mount.Mount{ Type: mount.TypeVolume, Source: "foo_normal", Target: "/foo", ReadOnly: true, VolumeOptions: &mount.VolumeOptions{ Labels: map[string]string{ LabelNamespace: "foo", "something": "labeled", }, DriverConfig: &mount.Driver{ Name: "glusterfs", Options: map[string]string{ "opt": "value", }, }, }, } mount, err := convertVolumeToMount("normal:/foo:ro", stackVolumes, namespace) assert.NilError(t, err) assert.DeepEqual(t, mount, expected) }
func TestDaemonCommand(t *testing.T) { cmd := newDaemonCommand() cmd.RunE = stubRun cmd.SetArgs([]string{"--containerd", "/foo"}) err := cmd.Execute() assert.NilError(t, err) }
func TestQuotedStringSetWithQuotes(t *testing.T) { value := "" qs := NewQuotedString(&value) assert.NilError(t, qs.Set("\"something\"")) assert.Equal(t, qs.String(), "something") assert.Equal(t, value, "something") }
func TestConvertResourcesFull(t *testing.T) { source := composetypes.Resources{ Limits: &composetypes.Resource{ NanoCPUs: "0.003", MemoryBytes: composetypes.UnitBytes(300000000), }, Reservations: &composetypes.Resource{ NanoCPUs: "0.002", MemoryBytes: composetypes.UnitBytes(200000000), }, } resources, err := convertResources(source) assert.NilError(t, err) expected := &swarm.ResourceRequirements{ Limits: &swarm.Resources{ NanoCPUs: 3000000, MemoryBytes: 300000000, }, Reservations: &swarm.Resources{ NanoCPUs: 2000000, MemoryBytes: 200000000, }, } assert.DeepEqual(t, resources, expected) }
func TestDaemonCommandHelp(t *testing.T) { cmd := newDaemonCommand() cmd.RunE = stubRun cmd.SetArgs([]string{"--help"}) err := cmd.Execute() assert.NilError(t, err) }
func TestSwarmLeave(t *testing.T) { buf := new(bytes.Buffer) cmd := newLeaveCommand( test.NewFakeCli(&fakeClient{}, buf)) assert.NilError(t, cmd.Execute()) assert.Equal(t, strings.TrimSpace(buf.String()), "Node left the swarm.") }
func TestExternalCAOption(t *testing.T) { testCases := []struct { externalCA string expected string }{ { externalCA: "protocol=cfssl,url=anything", expected: "cfssl: anything", }, { externalCA: "protocol=CFSSL,url=anything", expected: "cfssl: anything", }, { externalCA: "protocol=Cfssl,url=https://example.com", expected: "cfssl: https://example.com", }, { externalCA: "protocol=Cfssl,url=https://example.com,foo=bar", expected: "cfssl: https://example.com", }, { externalCA: "protocol=Cfssl,url=https://example.com,foo=bar,foo=baz", expected: "cfssl: https://example.com", }, } for _, tc := range testCases { opt := &ExternalCAOption{} assert.NilError(t, opt.Set(tc.externalCA)) assert.Equal(t, opt.String(), tc.expected) } }
func TestMountOptDefaultEnableReadOnly(t *testing.T) { var m MountOpt assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo")) assert.Equal(t, m.values[0].ReadOnly, false) m = MountOpt{} assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly")) assert.Equal(t, m.values[0].ReadOnly, true) m = MountOpt{} assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=1")) assert.Equal(t, m.values[0].ReadOnly, true) m = MountOpt{} assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=0")) assert.Equal(t, m.values[0].ReadOnly, false) }
func TestConvertRestartPolicyFromAlways(t *testing.T) { policy, err := convertRestartPolicy("always", nil) expected := &swarm.RestartPolicy{ Condition: swarm.RestartPolicyConditionAny, } assert.NilError(t, err) assert.DeepEqual(t, policy, expected) }
func TestHealthCheckOptionsToHealthConfigNoHealthcheck(t *testing.T) { opt := healthCheckOptions{ noHealthcheck: true, } config, err := opt.toHealthConfig() assert.NilError(t, err) assert.Equal(t, reflect.DeepEqual(config, &container.HealthConfig{ Test: []string{"NONE"}, }), true) }
func TestLoadDaemonCliConfigWithTLS(t *testing.T) { opts := defaultOptions("") opts.common.TLSOptions.CAFile = "/tmp/ca.pem" opts.common.TLS = true loadedConfig, err := loadDaemonCliConfig(opts) assert.NilError(t, err) assert.NotNil(t, loadedConfig) assert.Equal(t, loadedConfig.CommonTLSOptions.CAFile, "/tmp/ca.pem") }
func TestConvertHealthcheckDisable(t *testing.T) { source := &composetypes.HealthCheckConfig{Disable: true} expected := &container.HealthConfig{ Test: []string{"NONE"}, } healthcheck, err := convertHealthcheck(source) assert.NilError(t, err) assert.DeepEqual(t, healthcheck, expected) }
func TestConvertRestartPolicyFromFailure(t *testing.T) { policy, err := convertRestartPolicy("on-failure:4", nil) attempts := uint64(4) expected := &swarm.RestartPolicy{ Condition: swarm.RestartPolicyConditionOnFailure, MaxAttempts: &attempts, } assert.NilError(t, err) assert.DeepEqual(t, policy, expected) }
func TestLoadDaemonConfigWithLegacyRegistryOptions(t *testing.T) { content := `{"disable-legacy-registry": true}` tempFile := tempfile.NewTempFile(t, "config", content) defer tempFile.Remove() opts := defaultOptions(tempFile.Name()) loadedConfig, err := loadDaemonCliConfig(opts) assert.NilError(t, err) assert.NotNil(t, loadedConfig) assert.Equal(t, loadedConfig.V2Only, true) }
func TestLoadDaemonConfigWithTrueDefaultValues(t *testing.T) { content := `{ "userland-proxy": false }` tempFile := tempfile.NewTempFile(t, "config", content) defer tempFile.Remove() opts := defaultOptions(tempFile.Name()) loadedConfig, err := loadDaemonCliConfig(opts) assert.NilError(t, err) assert.NotNil(t, loadedConfig) assert.NotNil(t, loadedConfig.ClusterOpts) assert.Equal(t, loadedConfig.EnableUserlandProxy, false) // make sure reloading doesn't generate configuration // conflicts after normalizing boolean values. reload := func(reloadedConfig *daemon.Config) { assert.Equal(t, reloadedConfig.EnableUserlandProxy, false) } assert.NilError(t, daemon.ReloadConfiguration(opts.configFile, opts.flags, reload)) }
func TestFindConfigurationConflicts(t *testing.T) { config := map[string]interface{}{"authorization-plugins": "foobar"} flags := pflag.NewFlagSet("test", pflag.ContinueOnError) flags.String("authorization-plugins", "", "") assert.NilError(t, flags.Set("authorization-plugins", "asdf")) assert.Error(t, findConfigurationConflicts(config, flags), "authorization-plugins: (from flag: asdf, from file: foobar)") }
func TestClientDebugEnabled(t *testing.T) { defer debug.Disable() cmd := newDockerCommand(&command.DockerCli{}) cmd.Flags().Set("debug", "true") err := cmd.PersistentPreRunE(cmd, []string{}) assert.NilError(t, err) assert.Equal(t, os.Getenv("DEBUG"), "1") assert.Equal(t, logrus.GetLevel(), logrus.DebugLevel) }
func TestLoadDaemonCliConfigWithoutOverriding(t *testing.T) { opts := defaultOptions("") opts.common.Debug = true loadedConfig, err := loadDaemonCliConfig(opts) assert.NilError(t, err) assert.NotNil(t, loadedConfig) if !loadedConfig.Debug { t.Fatalf("expected debug to be copied from the common flags, got false") } }
func TestLoadDaemonCliConfigWithLogLevel(t *testing.T) { tempFile := tempfile.NewTempFile(t, "config", `{"log-level": "warn"}`) defer tempFile.Remove() opts := defaultOptions(tempFile.Name()) loadedConfig, err := loadDaemonCliConfig(opts) assert.NilError(t, err) assert.NotNil(t, loadedConfig) assert.Equal(t, loadedConfig.LogLevel, "warn") assert.Equal(t, logrus.GetLevel(), logrus.WarnLevel) }
func TestConvertVolumeToMountAnonymousVolume(t *testing.T) { stackVolumes := volumes{} namespace := NewNamespace("foo") expected := mount.Mount{ Type: mount.TypeVolume, Target: "/foo/bar", } mount, err := convertVolumeToMount("/foo/bar", stackVolumes, namespace) assert.NilError(t, err) assert.DeepEqual(t, mount, expected) }
func TestLoadDaemonCliConfigWithDaemonFlags(t *testing.T) { content := `{"log-opts": {"max-size": "1k"}}` tempFile := tempfile.NewTempFile(t, "config", content) defer tempFile.Remove() opts := defaultOptions(tempFile.Name()) opts.common.Debug = true opts.common.LogLevel = "info" assert.NilError(t, opts.flags.Set("selinux-enabled", "true")) loadedConfig, err := loadDaemonCliConfig(opts) assert.NilError(t, err) assert.NotNil(t, loadedConfig) assert.Equal(t, loadedConfig.Debug, true) assert.Equal(t, loadedConfig.LogLevel, "info") assert.Equal(t, loadedConfig.EnableSelinuxSupport, true) assert.Equal(t, loadedConfig.LogConfig.Type, "json-file") assert.Equal(t, loadedConfig.LogConfig.Config["max-size"], "1k") }