Example #1
0
func (s *S) TestNodeContainerDelete(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)
	request, err := http.NewRequest("DELETE", "/1.2/nodecontainers/c1", nil)
	c.Assert(err, check.IsNil)
	request.Header.Set("Authorization", "bearer "+s.token.GetValue())
	recorder := httptest.NewRecorder()
	server := RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	all, err := nodecontainer.AllNodeContainers()
	c.Assert(err, check.IsNil)
	c.Assert(all, check.DeepEquals, []nodecontainer.NodeContainerConfigGroup{
		{Name: "c1", ConfigPools: map[string]nodecontainer.NodeContainerConfig{
			"p1": {Name: "c1", Config: docker.Config{Env: []string{"A=2"}}},
		}},
	})
	c.Assert(eventtest.EventDesc{
		Target: event.Target{Type: event.TargetTypeNodeContainer, Value: "c1"},
		Owner:  s.token.GetUserName(),
		Kind:   "nodecontainer.delete",
		StartCustomData: []map[string]interface{}{
			{"name": ":name", "value": "c1"},
		},
	}, eventtest.HasEvent)
	s.provisioner.UpgradeNodeContainer("c1", "p1", ioutil.Discard)
	request, err = http.NewRequest("DELETE", "/1.2/nodecontainers/c1?pool=p1", 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)
	all, err = nodecontainer.AllNodeContainers()
	c.Assert(err, check.IsNil)
	c.Assert(all, check.DeepEquals, []nodecontainer.NodeContainerConfigGroup{})
	c.Assert(s.provisioner.HasNodeContainer("c1", "p1"), check.Equals, true)
}
Example #2
0
func (s *S) TestNodeContainerUpgrade(c *check.C) {
	err := nodecontainer.AddNewContainer("", &nodecontainer.NodeContainerConfig{
		Name:        "c1",
		PinnedImage: "tsuru/c1@sha256:abcef384829283eff",
		Config: docker.Config{
			Image: "img1",
			Env:   []string{"A=1"},
		},
	})
	c.Assert(err, check.IsNil)
	recorder := httptest.NewRecorder()
	request, err := http.NewRequest("POST", "/docker/nodecontainers/c1/upgrade", nil)
	c.Assert(err, check.IsNil)
	request.Header.Set("Authorization", "bearer "+s.token.GetValue())
	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	server := RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	c.Assert(recorder.Header().Get("Content-Type"), check.Equals, "application/x-json-stream")
	all, err := nodecontainer.AllNodeContainers()
	c.Assert(err, check.IsNil)
	c.Assert(all, check.DeepEquals, []nodecontainer.NodeContainerConfigGroup{
		{Name: "c1", ConfigPools: map[string]nodecontainer.NodeContainerConfig{
			"": {Name: "c1", Config: docker.Config{Env: []string{"A=1"}, Image: "img1"}},
		}},
	})
	c.Assert(eventtest.EventDesc{
		Target: event.Target{Type: event.TargetTypeNodeContainer, Value: "c1"},
		Owner:  s.token.GetUserName(),
		Kind:   "nodecontainer.update.upgrade",
		StartCustomData: []map[string]interface{}{
			{"name": ":name", "value": "c1"},
		},
	}, eventtest.HasEvent)
}
Example #3
0
// title: remove node container list
// path: /docker/nodecontainers
// method: GET
// produce: application/json
// responses:
//   200: Ok
//   401: Unauthorized
func nodeContainerList(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	pools, err := permission.ListContextValues(t, permission.PermNodecontainerRead, true)
	if err != nil {
		return err
	}
	lst, err := nodecontainer.AllNodeContainers()
	if err != nil {
		return err
	}
	if pools != nil {
		poolMap := map[string]struct{}{}
		for _, p := range pools {
			poolMap[p] = struct{}{}
		}
		for i, entry := range lst {
			for poolName := range entry.ConfigPools {
				if poolName == "" {
					continue
				}
				if _, ok := poolMap[poolName]; !ok {
					delete(entry.ConfigPools, poolName)
				}
			}
			lst[i] = entry
		}
	}
	w.Header().Set("Content-Type", "application/json")
	return json.NewEncoder(w).Encode(lst)
}