Пример #1
0
func (s *S) TestMoveContainers(c *gocheck.C) {
	cluster, err := s.startMultipleServersCluster()
	c.Assert(err, gocheck.IsNil)
	defer s.stopMultipleServersCluster(cluster)
	err = newImage("tsuru/python", s.server.URL())
	c.Assert(err, gocheck.IsNil)
	appInstance := testing.NewFakeApp("myapp", "python", 0)
	var p dockerProvisioner
	defer p.Destroy(appInstance)
	p.Provision(appInstance)
	provisionMutex.Lock()
	oldProvisioner := app.Provisioner
	app.Provisioner = &p
	provisionMutex.Unlock()
	defer func() {
		provisionMutex.Lock()
		app.Provisioner = oldProvisioner
		provisionMutex.Unlock()
	}()
	coll := collection()
	defer coll.Close()
	coll.Insert(container{ID: "container-id", AppName: appInstance.GetName(), Version: "container-version", Image: "tsuru/python"})
	defer coll.RemoveAll(bson.M{"appname": appInstance.GetName()})
	units, err := addUnitsWithHost(appInstance, 2, "localhost")
	c.Assert(err, gocheck.IsNil)
	conn, err := db.Conn()
	c.Assert(err, gocheck.IsNil)
	defer conn.Close()
	appStruct := &app.App{
		Name:     appInstance.GetName(),
		Platform: appInstance.GetPlatform(),
	}
	err = conn.Apps().Insert(appStruct)
	c.Assert(err, gocheck.IsNil)
	err = conn.Apps().Update(
		bson.M{"name": appStruct.Name},
		bson.M{"$set": bson.M{"units": units}},
	)
	c.Assert(err, gocheck.IsNil)
	defer conn.Apps().Remove(bson.M{"name": appStruct.Name})
	var buf bytes.Buffer
	encoder := json.NewEncoder(&buf)
	err = moveContainers("localhost", "127.0.0.1", encoder)
	c.Assert(err, gocheck.IsNil)
	containers, err := listContainersByHost("localhost")
	c.Assert(len(containers), gocheck.Equals, 0)
	containers, err = listContainersByHost("127.0.0.1")
	c.Assert(len(containers), gocheck.Equals, 2)
	parts := strings.Split(buf.String(), "\n")
	var logEntry progressLog
	json.Unmarshal([]byte(parts[0]), &logEntry)
	c.Assert(logEntry.Message, gocheck.Matches, ".*Moving 2 units.*")
	json.Unmarshal([]byte(parts[1]), &logEntry)
	c.Assert(logEntry.Message, gocheck.Matches, ".*Moving unit.*for.*myapp.*localhost.*127.0.0.1.*")
	json.Unmarshal([]byte(parts[2]), &logEntry)
	c.Assert(logEntry.Message, gocheck.Matches, ".*Moving unit.*for.*myapp.*localhost.*127.0.0.1.*")
	testing.CleanQ("tsuru-app")
}
Пример #2
0
func (s *S) TestRebalanceContainersDryBodyHandler(c *gocheck.C) {
	cluster, err := s.startMultipleServersCluster()
	c.Assert(err, gocheck.IsNil)
	defer s.stopMultipleServersCluster(cluster)
	err = newImage("tsuru/python", s.server.URL())
	c.Assert(err, gocheck.IsNil)
	appInstance := testing.NewFakeApp("myapp", "python", 0)
	var p dockerProvisioner
	defer p.Destroy(appInstance)
	p.Provision(appInstance)
	provisionMutex.Lock()
	oldProvisioner := app.Provisioner
	app.Provisioner = &p
	provisionMutex.Unlock()
	defer func() {
		provisionMutex.Lock()
		app.Provisioner = oldProvisioner
		provisionMutex.Unlock()
	}()
	coll := collection()
	defer coll.Close()
	coll.Insert(container{ID: "container-id", AppName: appInstance.GetName(), Version: "container-version", Image: "tsuru/python"})
	defer coll.RemoveAll(bson.M{"appname": appInstance.GetName()})
	units, err := addUnitsWithHost(appInstance, 5, "localhost")
	c.Assert(err, gocheck.IsNil)
	conn, err := db.Conn()
	c.Assert(err, gocheck.IsNil)
	defer conn.Close()
	appStruct := &app.App{
		Name:     appInstance.GetName(),
		Platform: appInstance.GetPlatform(),
	}
	err = conn.Apps().Insert(appStruct)
	c.Assert(err, gocheck.IsNil)
	defer conn.Apps().Remove(bson.M{"name": appStruct.Name})
	err = conn.Apps().Update(
		bson.M{"name": appStruct.Name},
		bson.M{"$set": bson.M{"units": units}},
	)
	c.Assert(err, gocheck.IsNil)
	b := bytes.NewBufferString(`{"dry": "true"}`)
	req, err := http.NewRequest("POST", "/containers/move", b)
	rec := httptest.NewRecorder()
	err = rebalanceContainersHandler(rec, req)
	c.Assert(err, gocheck.IsNil)
	body, err := ioutil.ReadAll(rec.Body)
	c.Assert(err, gocheck.IsNil)
	validJson := fmt.Sprintf("[%s]", strings.Replace(strings.Trim(string(body), "\n "), "\n", ",", -1))
	var result []progressLog
	err = json.Unmarshal([]byte(validJson), &result)
	c.Assert(err, gocheck.IsNil)
	c.Assert(len(result), gocheck.Equals, 4)
	c.Assert(result[0].Message, gocheck.Equals, "Trying to move 2 units from localhost...")
	c.Assert(result[1].Message, gocheck.Matches, "Would move unit .*")
	c.Assert(result[2].Message, gocheck.Matches, "Would move unit .*")
	c.Assert(result[3].Message, gocheck.Matches, "Containers rebalanced successfully!")
	testing.CleanQ("tsuru-app")
}
Пример #3
0
func (s *S) TearDownTest(c *gocheck.C) {
	s.t.RollbackGitConfs(c)
	s.provisioner.Reset()
	LogRemove(nil)
	s.conn.Users().Update(
		bson.M{"email": s.user.Email},
		bson.M{"$set": bson.M{"quota": quota.Unlimited}},
	)
	ttesting.CleanQ(queueName)
}
Пример #4
0
func (s *S) TestRebalanceContainers(c *gocheck.C) {
	cluster, err := s.startMultipleServersCluster()
	c.Assert(err, gocheck.IsNil)
	defer s.stopMultipleServersCluster(cluster)
	err = newImage("tsuru/python", s.server.URL())
	c.Assert(err, gocheck.IsNil)
	appInstance := testing.NewFakeApp("myapp", "python", 0)
	var p dockerProvisioner
	defer p.Destroy(appInstance)
	p.Provision(appInstance)
	provisionMutex.Lock()
	oldProvisioner := app.Provisioner
	app.Provisioner = &p
	provisionMutex.Unlock()
	defer func() {
		provisionMutex.Lock()
		app.Provisioner = oldProvisioner
		provisionMutex.Unlock()
	}()
	coll := collection()
	defer coll.Close()
	coll.Insert(container{ID: "container-id", AppName: appInstance.GetName(), Version: "container-version", Image: "tsuru/python"})
	defer coll.RemoveAll(bson.M{"appname": appInstance.GetName()})
	units, err := addUnitsWithHost(appInstance, 5, "localhost")
	c.Assert(err, gocheck.IsNil)
	conn, err := db.Conn()
	c.Assert(err, gocheck.IsNil)
	defer conn.Close()
	appStruct := &app.App{
		Name:     appInstance.GetName(),
		Platform: appInstance.GetPlatform(),
	}
	err = conn.Apps().Insert(appStruct)
	c.Assert(err, gocheck.IsNil)
	defer conn.Apps().Remove(bson.M{"name": appStruct.Name})
	err = conn.Apps().Update(
		bson.M{"name": appStruct.Name},
		bson.M{"$set": bson.M{"units": units}},
	)
	c.Assert(err, gocheck.IsNil)
	var buf bytes.Buffer
	encoder := json.NewEncoder(&buf)
	err = rebalanceContainers(encoder, false)
	c.Assert(err, gocheck.IsNil)
	c1, err := listContainersByHost("localhost")
	c.Assert(len(c1), gocheck.Equals, 3)
	c2, err := listContainersByHost("127.0.0.1")
	c.Assert(len(c2), gocheck.Equals, 2)
	time.Sleep(1e9)
	testing.CleanQ("tsuru-app")
}
Пример #5
0
func (s *S) TestMoveContainer(c *gocheck.C) {
	cluster, err := s.startMultipleServersCluster()
	c.Assert(err, gocheck.IsNil)
	defer s.stopMultipleServersCluster(cluster)
	err = newImage("tsuru/python", s.server.URL())
	c.Assert(err, gocheck.IsNil)
	appInstance := testing.NewFakeApp("myapp", "python", 0)
	var p dockerProvisioner
	defer p.Destroy(appInstance)
	p.Provision(appInstance)
	coll := collection()
	defer coll.Close()
	coll.Insert(container{ID: "container-id", AppName: appInstance.GetName(), Version: "container-version", Image: "tsuru/python"})
	defer coll.RemoveAll(bson.M{"appname": appInstance.GetName()})
	units, err := addUnitsWithHost(appInstance, 2, "localhost")
	c.Assert(err, gocheck.IsNil)
	conn, err := db.Conn()
	c.Assert(err, gocheck.IsNil)
	defer conn.Close()
	appStruct := &app.App{
		Name:     appInstance.GetName(),
		Platform: appInstance.GetPlatform(),
	}
	err = conn.Apps().Insert(appStruct)
	c.Assert(err, gocheck.IsNil)
	err = conn.Apps().Update(
		bson.M{"name": appStruct.Name},
		bson.M{"$set": bson.M{"units": units}},
	)
	c.Assert(err, gocheck.IsNil)
	defer conn.Apps().Remove(bson.M{"name": appStruct.Name})
	var buf bytes.Buffer
	encoder := json.NewEncoder(&buf)
	err = moveContainer(units[0].Name[0:6], "127.0.0.1", encoder)
	c.Assert(err, gocheck.IsNil)
	containers, err := listContainersByHost("localhost")
	c.Assert(len(containers), gocheck.Equals, 1)
	containers, err = listContainersByHost("127.0.0.1")
	c.Assert(len(containers), gocheck.Equals, 1)
	testing.CleanQ("tsuru-app")
}