Exemple #1
0
func (s *S) TestDockerLogUpdateRun(c *check.C) {
	var stdout, stderr bytes.Buffer
	context := cmd.Context{
		Stdout: &stdout,
		Stderr: &stderr,
	}
	msg := tsuruIo.SimpleJsonMessage{Message: "success!!!"}
	result, _ := json.Marshal(msg)
	trans := &cmdtest.ConditionalTransport{
		Transport: cmdtest.Transport{Message: string(result), Status: http.StatusNoContent},
		CondFunc: func(req *http.Request) bool {
			defer req.Body.Close()
			body, err := ioutil.ReadAll(req.Body)
			c.Assert(err, check.IsNil)
			expected := provision.ScopedConfig{
				Envs: []provision.Entry{{Name: "a", Value: "1"}, {Name: "b", Value: "2"}, {Name: "log-driver", Value: "x"}},
			}
			var data logsSetData
			err = json.Unmarshal(body, &data)
			c.Assert(err, check.IsNil)
			sort.Sort(provision.ConfigEntryList(data.Config.Envs))
			c.Assert(data.Config, check.DeepEquals, expected)
			return req.URL.Path == "/1.0/docker/logs" && req.Method == "POST"
		},
	}
	manager := cmd.NewManager("admin", "0.1", "admin-ver", &stdout, &stderr, nil, nil)
	client := cmd.NewClient(&http.Client{Transport: trans}, nil, manager)
	cmd := dockerLogUpdate{}
	err := cmd.Flags().Parse(true, []string{"--log-driver", "x", "--log-opt", "a=1", "--log-opt", "b=2"})
	c.Assert(err, check.IsNil)
	err = cmd.Run(&context, client)
	c.Assert(err, check.IsNil)
	c.Assert(stdout.String(), check.Equals, "success!!!")
}
Exemple #2
0
func (s *S) TestDockerLogUpdate(c *check.C) {
	testCases := []struct {
		conf    provision.ScopedConfig
		entries []provision.Entry
		pools   []provision.PoolEntry
		err     error
	}{
		{
			provision.ScopedConfig{
				Envs: []provision.Entry{
					{Name: "log-driver", Value: "fluentd"},
					{Name: "fluentd-address", Value: "localhost:24224"},
				},
			}, []provision.Entry{
				{Name: "log-driver", Value: "fluentd"},
				{Name: "fluentd-address", Value: "localhost:24224"},
			}, []provision.PoolEntry{}, nil,
		},
		{
			provision.ScopedConfig{
				Envs: []provision.Entry{
					{Name: "log-driver", Value: "bs"},
					{Name: "tag", Value: "ahoy"},
				},
			}, []provision.Entry{
				{Name: "log-driver", Value: "fluentd"},
				{Name: "fluentd-address", Value: "localhost:24224"},
			}, []provision.PoolEntry{}, ErrLogDriverBSNoParams,
		},
		{
			provision.ScopedConfig{
				Envs: []provision.Entry{
					{Name: "tag", Value: "ahoy"},
				},
			}, []provision.Entry{
				{Name: "log-driver", Value: "fluentd"},
				{Name: "fluentd-address", Value: "localhost:24224"},
			}, []provision.PoolEntry{}, ErrLogDriverMandatory,
		},
		{
			provision.ScopedConfig{
				Envs: []provision.Entry{
					{Name: "log-driver", Value: "bs"},
				},
			}, []provision.Entry{
				{Name: "log-driver", Value: "bs"},
			}, []provision.PoolEntry{}, nil,
		},
		{
			provision.ScopedConfig{
				Envs: []provision.Entry{
					{Name: "log-driver", Value: "fluentd"},
					{Name: "tag", Value: "x"},
				},
			}, []provision.Entry{
				{Name: "log-driver", Value: "fluentd"},
				{Name: "tag", Value: "x"},
			}, []provision.PoolEntry{}, nil,
		},
		{
			provision.ScopedConfig{
				Pools: []provision.PoolEntry{
					{Name: "p1", Envs: []provision.Entry{
						{Name: "log-driver", Value: "journald"},
						{Name: "tag", Value: "y"},
					}},
				},
			}, []provision.Entry{
				{Name: "log-driver", Value: "fluentd"},
				{Name: "tag", Value: "x"},
			}, []provision.PoolEntry{
				{Name: "p1", Envs: []provision.Entry{
					{Name: "log-driver", Value: "journald"},
					{Name: "tag", Value: "y"},
				}},
			}, nil,
		},
	}
	logConf := DockerLog{}
	for _, testData := range testCases {
		err := logConf.Update(&testData.conf)
		c.Assert(err, check.DeepEquals, testData.err)
		conf, err := provision.FindScopedConfig(dockerLogConfigEntry)
		c.Assert(err, check.IsNil)
		sort.Sort(provision.ConfigEntryList(conf.Envs))
		sort.Sort(provision.ConfigEntryList(testData.entries))
		sort.Sort(provision.ConfigPoolEntryList(conf.Pools))
		sort.Sort(provision.ConfigPoolEntryList(testData.pools))
		for i := range conf.Pools {
			sort.Sort(provision.ConfigEntryList(conf.Pools[i].Envs))
		}
		for i := range testData.pools {
			sort.Sort(provision.ConfigEntryList(testData.pools[i].Envs))
		}
		c.Assert(conf.Envs, check.DeepEquals, testData.entries)
		c.Assert(conf.Pools, check.DeepEquals, testData.pools)
	}
	driver, opts, err := logConf.LogOpts("p1")
	c.Assert(err, check.IsNil)
	c.Assert(driver, check.Equals, "journald")
	c.Assert(opts, check.DeepEquals, map[string]string{"tag": "y"})
	driver, opts, err = logConf.LogOpts("other")
	c.Assert(err, check.IsNil)
	c.Assert(driver, check.Equals, "fluentd")
	c.Assert(opts, check.DeepEquals, map[string]string{"tag": "x"})
}