Example #1
0
func (s *S) TestNodeContainerListLimited(c *check.C) {
	err := nodecontainer.AddNewContainer("", &nodecontainer.NodeContainerConfig{
		Name: "c1",
		Config: docker.Config{
			Image: "img1",
			Env:   []string{"A=1"},
		},
	})
	c.Assert(err, check.IsNil)
	err = nodecontainer.AddNewContainer("p1", &nodecontainer.NodeContainerConfig{
		Name: "c1",
		Config: docker.Config{
			Env: []string{"A=2"},
		},
	})
	c.Assert(err, check.IsNil)
	err = nodecontainer.AddNewContainer("p3", &nodecontainer.NodeContainerConfig{
		Name: "c1",
		Config: docker.Config{
			Env: []string{"A=3"},
		},
	})
	c.Assert(err, check.IsNil)
	t := userWithPermission(c, permission.Permission{
		Scheme:  permission.PermNodecontainerRead,
		Context: permission.Context(permission.CtxPool, "p3"),
	})
	request, err := http.NewRequest("GET", "/1.2/nodecontainers", nil)
	c.Assert(err, check.IsNil)
	request.Header.Set("Authorization", "bearer "+t.GetValue())
	recorder := httptest.NewRecorder()
	server := RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	var configEntries []nodecontainer.NodeContainerConfigGroup
	json.Unmarshal(recorder.Body.Bytes(), &configEntries)
	sort.Sort(nodecontainer.NodeContainerConfigGroupSlice(configEntries))
	c.Assert(configEntries, check.DeepEquals, []nodecontainer.NodeContainerConfigGroup{
		{Name: "c1", ConfigPools: map[string]nodecontainer.NodeContainerConfig{
			"":   {Name: "c1", Config: docker.Config{Image: "img1", Env: []string{"A=1"}}},
			"p3": {Name: "c1", Config: docker.Config{Env: []string{"A=3"}}},
		}},
	})
}
Example #2
0
func (s *S) TestNodeContainerCreate(c *check.C) {
	doReq := func(cont nodecontainer.NodeContainerConfig, expected []nodecontainer.NodeContainerConfigGroup, pool ...string) {
		values, err := form.EncodeToValues(cont)
		c.Assert(err, check.IsNil)
		if len(pool) > 0 {
			values.Set("pool", pool[0])
		}
		reader := strings.NewReader(values.Encode())
		request, err := http.NewRequest("POST", "/1.2/nodecontainers", reader)
		c.Assert(err, check.IsNil)
		request.Header.Set("Authorization", "bearer "+s.token.GetValue())
		request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
		recorder := httptest.NewRecorder()
		server := RunServer(true)
		server.ServeHTTP(recorder, request)
		c.Assert(recorder.Code, check.Equals, http.StatusOK)
		request, err = http.NewRequest("GET", "/1.2/nodecontainers", nil)
		c.Assert(err, check.IsNil)
		request.Header.Set("Authorization", "bearer "+s.token.GetValue())
		recorder = httptest.NewRecorder()
		server.ServeHTTP(recorder, request)
		c.Assert(recorder.Code, check.Equals, http.StatusOK)
		var configEntries []nodecontainer.NodeContainerConfigGroup
		json.Unmarshal(recorder.Body.Bytes(), &configEntries)
		sort.Sort(nodecontainer.NodeContainerConfigGroupSlice(configEntries))
		c.Assert(configEntries, check.DeepEquals, expected)
	}
	doReq(nodecontainer.NodeContainerConfig{Name: "c1", Config: docker.Config{Image: "img1"}}, []nodecontainer.NodeContainerConfigGroup{
		{Name: "c1", ConfigPools: map[string]nodecontainer.NodeContainerConfig{
			"": {Name: "c1", Config: docker.Config{Image: "img1", Healthcheck: &docker.HealthConfig{}}},
		}},
	})
	c.Assert(eventtest.EventDesc{
		Target: event.Target{Type: event.TargetTypeNodeContainer, Value: "c1"},
		Owner:  s.token.GetUserName(),
		Kind:   "nodecontainer.create",
		StartCustomData: []map[string]interface{}{
			{"name": "Name", "value": "c1"},
			{"name": "Config.Image", "value": "img1"},
		},
	}, eventtest.HasEvent)
	doReq(nodecontainer.NodeContainerConfig{
		Name:       "c2",
		Config:     docker.Config{Env: []string{"A=1"}, Image: "img2"},
		HostConfig: docker.HostConfig{Memory: 256, Privileged: true},
	}, []nodecontainer.NodeContainerConfigGroup{
		{Name: "c1", ConfigPools: map[string]nodecontainer.NodeContainerConfig{
			"": {Name: "c1", Config: docker.Config{Image: "img1", Healthcheck: &docker.HealthConfig{}}},
		}},
		{Name: "c2", ConfigPools: map[string]nodecontainer.NodeContainerConfig{
			"": {Name: "c2", Config: docker.Config{Env: []string{"A=1"}, Image: "img2", Healthcheck: &docker.HealthConfig{}}, HostConfig: docker.HostConfig{Memory: 256, Privileged: true}},
		}},
	})
	doReq(nodecontainer.NodeContainerConfig{
		Name:       "c2",
		Config:     docker.Config{Env: []string{"Z=9"}, Image: "img2"},
		HostConfig: docker.HostConfig{Memory: 256},
	}, []nodecontainer.NodeContainerConfigGroup{
		{Name: "c1", ConfigPools: map[string]nodecontainer.NodeContainerConfig{
			"": {Name: "c1", Config: docker.Config{Image: "img1", Healthcheck: &docker.HealthConfig{}}},
		}},
		{Name: "c2", ConfigPools: map[string]nodecontainer.NodeContainerConfig{
			"": {Name: "c2", Config: docker.Config{Env: []string{"Z=9"}, Image: "img2", Healthcheck: &docker.HealthConfig{}}, HostConfig: docker.HostConfig{Memory: 256}},
		}},
	})
	doReq(nodecontainer.NodeContainerConfig{
		Name:   "c2",
		Config: docker.Config{Env: []string{"X=1"}},
	}, []nodecontainer.NodeContainerConfigGroup{
		{Name: "c1", ConfigPools: map[string]nodecontainer.NodeContainerConfig{
			"": {Name: "c1", Config: docker.Config{Image: "img1", Healthcheck: &docker.HealthConfig{}}},
		}},
		{Name: "c2", ConfigPools: map[string]nodecontainer.NodeContainerConfig{
			"":   {Name: "c2", Config: docker.Config{Env: []string{"Z=9"}, Image: "img2", Healthcheck: &docker.HealthConfig{}}, HostConfig: docker.HostConfig{Memory: 256}},
			"p1": {Name: "c2", Config: docker.Config{Env: []string{"X=1"}, Healthcheck: &docker.HealthConfig{}}},
		}},
	}, "p1")
}