Exemple #1
0
func (s *S) TestDockerLogsUpdateHandlerWithRestartSomeApps(c *check.C) {
	appPools := [][]string{{"app1", "POOL1"}, {"app2", "POOL2"}, {"app3", "POOL2"}}
	for _, appPool := range appPools {
		opts := provision.AddPoolOptions{Name: appPool[1]}
		provision.AddPool(opts)
		err := s.newFakeImage(s.p, "tsuru/app-"+appPool[0], nil)
		c.Assert(err, check.IsNil)
		appInstance := provisiontest.NewFakeApp(appPool[0], "python", 0)
		appStruct := &app.App{
			Name:     appInstance.GetName(),
			Platform: appInstance.GetPlatform(),
			Pool:     opts.Name,
		}
		err = s.storage.Apps().Insert(appStruct)
		c.Assert(err, check.IsNil)
		err = s.p.Provision(appStruct)
		c.Assert(err, check.IsNil)
	}
	values := url.Values{
		"pool":                   []string{"POOL2"},
		"restart":                []string{"true"},
		"Driver":                 []string{"awslogs"},
		"LogOpts.awslogs-region": []string{"sa-east1"},
	}
	recorder := httptest.NewRecorder()
	reader := strings.NewReader(values.Encode())
	request, err := http.NewRequest("POST", "/docker/logs", reader)
	c.Assert(err, check.IsNil)
	request.Header.Set("Authorization", "bearer "+s.token.GetValue())
	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	server := api.RunServer(true)
	server.ServeHTTP(recorder, request)
	responseParts := strings.Split(recorder.Body.String(), "\n")
	c.Assert(responseParts, check.HasLen, 17)
	c.Assert(responseParts[0], check.Equals, "{\"Message\":\"Log config successfully updated.\\n\"}")
	c.Assert(responseParts[1], check.Equals, "{\"Message\":\"Restarting 2 applications: [app2, app3]\\n\"}")
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	entries, err := container.LogLoadAll()
	c.Assert(err, check.IsNil)
	c.Assert(entries, check.DeepEquals, map[string]container.DockerLogConfig{
		"":      {},
		"POOL2": {Driver: "awslogs", LogOpts: map[string]string{"awslogs-region": "sa-east1"}},
	})
}
Exemple #2
0
func logsConfigGetHandler(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	pools, err := listContextValues(t, permission.PermPoolUpdateLogs, true)
	if err != nil {
		return err
	}
	configEntries, err := container.LogLoadAll()
	if err != nil {
		return err
	}
	if len(pools) == 0 {
		return json.NewEncoder(w).Encode(configEntries)
	}
	newMap := map[string]container.DockerLogConfig{}
	for _, p := range pools {
		if entry, ok := configEntries[p]; ok {
			newMap[p] = entry
		}
	}
	return json.NewEncoder(w).Encode(newMap)
}
Exemple #3
0
func (s *HandlersSuite) TestDockerLogsUpdateHandlerWithRestartNoApps(c *check.C) {
	values := url.Values{
		"restart":                []string{"true"},
		"Driver":                 []string{"awslogs"},
		"LogOpts.awslogs-region": []string{"sa-east1"},
	}
	recorder := httptest.NewRecorder()
	reader := strings.NewReader(values.Encode())
	request, err := http.NewRequest("POST", "/docker/logs", reader)
	c.Assert(err, check.IsNil)
	request.Header.Set("Authorization", "bearer "+s.token.GetValue())
	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	server := api.RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Body.String(), check.Equals, "{\"Message\":\"Log config successfully updated.\\n\"}\n")
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	entries, err := container.LogLoadAll()
	c.Assert(err, check.IsNil)
	c.Assert(entries, check.DeepEquals, map[string]container.DockerLogConfig{
		"": {Driver: "awslogs", LogOpts: map[string]string{"awslogs-region": "sa-east1"}},
	})
}
Exemple #4
0
func (s *HandlersSuite) TestDockerLogsUpdateHandler(c *check.C) {
	values1 := url.Values{
		"Driver":                 []string{"awslogs"},
		"LogOpts.awslogs-region": []string{"sa-east1"},
	}
	values2 := url.Values{
		"pool":   []string{"POOL1"},
		"Driver": []string{"bs"},
	}
	values3 := url.Values{
		"pool":                    []string{"POOL2"},
		"Driver":                  []string{"fluentd"},
		"LogOpts.fluentd-address": []string{"localhost:2222"},
	}
	doReq := func(val url.Values) {
		reader := strings.NewReader(val.Encode())
		recorder := httptest.NewRecorder()
		request, err := http.NewRequest("POST", "/docker/logs", reader)
		c.Assert(err, check.IsNil)
		request.Header.Set("Authorization", "bearer "+s.token.GetValue())
		request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
		server := api.RunServer(true)
		server.ServeHTTP(recorder, request)
		c.Assert(recorder.Body.String(), check.Equals, "{\"Message\":\"Log config successfully updated.\\n\"}\n")
		c.Assert(recorder.Code, check.Equals, http.StatusOK)
		c.Assert(recorder.Header().Get("Content-Type"), check.Equals, "application/x-json-stream")
		var pool string
		var customData []map[string]interface{}
		for k, v := range val {
			if k == "pool" {
				pool = v[0]
				continue
			}
			customData = append(customData, map[string]interface{}{"name": k, "value": v[0]})
		}
		c.Assert(eventtest.EventDesc{
			Target:          event.Target{Type: event.TargetTypePool, Value: pool},
			Owner:           s.token.GetUserName(),
			Kind:            "pool.update.logs",
			StartCustomData: customData,
		}, eventtest.HasEvent)
	}
	doReq(values1)
	entries, err := container.LogLoadAll()
	c.Assert(err, check.IsNil)
	c.Assert(entries, check.DeepEquals, map[string]container.DockerLogConfig{
		"": {Driver: "awslogs", LogOpts: map[string]string{"awslogs-region": "sa-east1"}},
	})
	doReq(values2)
	entries, err = container.LogLoadAll()
	c.Assert(err, check.IsNil)
	c.Assert(entries, check.DeepEquals, map[string]container.DockerLogConfig{
		"":      {Driver: "awslogs", LogOpts: map[string]string{"awslogs-region": "sa-east1"}},
		"POOL1": {Driver: "bs", LogOpts: map[string]string{}},
	})
	doReq(values3)
	entries, err = container.LogLoadAll()
	c.Assert(err, check.IsNil)
	c.Assert(entries, check.DeepEquals, map[string]container.DockerLogConfig{
		"":      {Driver: "awslogs", LogOpts: map[string]string{"awslogs-region": "sa-east1"}},
		"POOL1": {Driver: "bs", LogOpts: map[string]string{}},
		"POOL2": {Driver: "fluentd", LogOpts: map[string]string{"fluentd-address": "localhost:2222"}},
	})
}