Ejemplo n.º 1
0
func (s *RouterSuite) TestSwapTwice(c *check.C) {
	backend1 := "mybackend1"
	backend2 := "mybackend2"
	addr1, _ := url.Parse("http://127.0.0.1")
	addr2, _ := url.Parse("http://10.10.10.10")
	err := s.Router.AddBackend(backend1)
	c.Assert(err, check.IsNil)
	err = s.Router.AddRoute(backend1, addr1)
	c.Assert(err, check.IsNil)
	err = s.Router.AddBackend(backend2)
	c.Assert(err, check.IsNil)
	err = s.Router.AddRoute(backend2, addr2)
	c.Assert(err, check.IsNil)
	isSwapped, swappedWith, err := router.IsSwapped(backend1)
	c.Assert(err, check.IsNil)
	c.Assert(isSwapped, check.Equals, false)
	isSwapped, swappedWith, err = router.IsSwapped(backend2)
	c.Assert(err, check.IsNil)
	c.Assert(isSwapped, check.Equals, false)
	c.Assert(swappedWith, check.Equals, backend2)
	err = s.Router.Swap(backend1, backend2)
	c.Assert(err, check.IsNil)
	isSwapped, swappedWith, err = router.IsSwapped(backend1)
	c.Assert(err, check.IsNil)
	c.Assert(isSwapped, check.Equals, true)
	c.Assert(swappedWith, check.Equals, backend2)
	isSwapped, swappedWith, err = router.IsSwapped(backend2)
	c.Assert(err, check.IsNil)
	c.Assert(isSwapped, check.Equals, true)
	c.Assert(swappedWith, check.Equals, backend1)
	backAddr1, err := s.Router.Addr(backend1)
	c.Assert(err, check.IsNil)
	c.Assert(backAddr1[:len(backend2)], check.Equals, backend2)
	backAddr2, err := s.Router.Addr(backend2)
	c.Assert(err, check.IsNil)
	c.Assert(backAddr2[:len(backend1)], check.Equals, backend1)
	routes, err := s.Router.Routes(backend1)
	c.Assert(err, check.IsNil)
	c.Check(routes, check.DeepEquals, []*url.URL{addr1})
	routes, err = s.Router.Routes(backend2)
	c.Assert(err, check.IsNil)
	c.Check(routes, check.DeepEquals, []*url.URL{addr2})
	err = s.Router.Swap(backend1, backend2)
	c.Assert(err, check.IsNil)
	isSwapped, swappedWith, err = router.IsSwapped(backend1)
	c.Assert(err, check.IsNil)
	c.Assert(isSwapped, check.Equals, false)
	c.Assert(swappedWith, check.Equals, backend1)
	backAddr1, err = s.Router.Addr(backend1)
	c.Assert(err, check.IsNil)
	c.Assert(backAddr1[:len(backend1)], check.Equals, backend1)
	backAddr2, err = s.Router.Addr(backend2)
	c.Assert(err, check.IsNil)
	c.Assert(backAddr2[:len(backend2)], check.Equals, backend2)
	routes, err = s.Router.Routes(backend1)
	c.Assert(err, check.IsNil)
	c.Check(routes, check.DeepEquals, []*url.URL{addr1})
	routes, err = s.Router.Routes(backend2)
	c.Assert(err, check.IsNil)
	c.Check(routes, check.DeepEquals, []*url.URL{addr2})
	err = s.Router.RemoveBackend(backend1)
	c.Assert(err, check.IsNil)
	err = s.Router.RemoveBackend(backend2)
	c.Assert(err, check.IsNil)
}
Ejemplo n.º 2
0
// Delete deletes an app.
func Delete(app *App, w io.Writer) error {
	isSwapped, swappedWith, err := router.IsSwapped(app.GetName())
	if err != nil {
		return fmt.Errorf("unable to check if app is swapped: %s", err)
	}
	if isSwapped {
		return fmt.Errorf("application is swapped with %q, cannot remove it", swappedWith)
	}
	appName := app.Name
	if w == nil {
		w = ioutil.Discard
	}
	fmt.Fprintf(w, "---- Removing application %q...\n", appName)
	var hasErrors bool
	defer func() {
		var problems string
		if hasErrors {
			problems = " Some errors occurred during removal."
		}
		fmt.Fprintf(w, "---- Done removing application.%s\n", problems)
	}()
	logErr := func(msg string, err error) {
		msg = fmt.Sprintf("%s: %s", msg, err)
		fmt.Fprintf(w, "%s\n", msg)
		log.Errorf("[delete-app: %s] %s", appName, msg)
		hasErrors = true
	}
	err = Provisioner.Destroy(app)
	if err != nil {
		logErr("Unable to destroy app in provisioner", err)
	}
	err = app.unbind()
	if err != nil {
		logErr("Unable to unbind app", err)
	}
	err = repository.Manager().RemoveRepository(appName)
	if err != nil {
		logErr("Unable to remove app from repository manager", err)
	}
	token := app.Env["TSURU_APP_TOKEN"].Value
	err = AuthScheme.AppLogout(token)
	if err != nil {
		logErr("Unable to remove app token in destroy", err)
	}
	owner, err := auth.GetUserByEmail(app.Owner)
	if err == nil {
		err = auth.ReleaseApp(owner)
	}
	if err != nil {
		logErr("Unable to release app quota", err)
	}
	logConn, err := db.LogConn()
	if err == nil {
		defer logConn.Close()
		err = logConn.Logs(appName).DropCollection()
	}
	if err != nil {
		logErr("Unable to remove logs collection", err)
	}
	conn, err := db.Conn()
	if err == nil {
		defer conn.Close()
		err = conn.Apps().Remove(bson.M{"name": appName})
	}
	if err != nil {
		logErr("Unable to remove app from db", err)
	}
	err = markDeploysAsRemoved(appName)
	if err != nil {
		logErr("Unable to mark old deploys as removed", err)
	}
	return nil
}
Ejemplo n.º 3
0
func (s *RouterSuite) TestSwapTwice(c *check.C) {
	addr1, _ := url.Parse("http://127.0.0.1:8080")
	addr2, _ := url.Parse("http://10.10.10.10:8080")
	err := s.Router.AddBackend(testBackend1)
	c.Assert(err, check.IsNil)
	backend1OrigAddr, err := s.Router.Addr(testBackend1)
	c.Assert(err, check.IsNil)
	err = s.Router.AddRoute(testBackend1, addr1)
	c.Assert(err, check.IsNil)
	err = s.Router.AddBackend(testBackend2)
	c.Assert(err, check.IsNil)
	backend2OrigAddr, err := s.Router.Addr(testBackend2)
	c.Assert(err, check.IsNil)
	err = s.Router.AddRoute(testBackend2, addr2)
	c.Assert(err, check.IsNil)
	isSwapped, _, err := router.IsSwapped(testBackend1)
	c.Assert(err, check.IsNil)
	c.Assert(isSwapped, check.Equals, false)
	isSwapped, swappedWith, err := router.IsSwapped(testBackend2)
	c.Assert(err, check.IsNil)
	c.Assert(isSwapped, check.Equals, false)
	c.Assert(swappedWith, check.Equals, testBackend2)
	err = s.Router.Swap(testBackend1, testBackend2, false)
	c.Assert(err, check.IsNil)
	isSwapped, swappedWith, err = router.IsSwapped(testBackend1)
	c.Assert(err, check.IsNil)
	c.Assert(isSwapped, check.Equals, true)
	c.Assert(swappedWith, check.Equals, testBackend2)
	isSwapped, swappedWith, err = router.IsSwapped(testBackend2)
	c.Assert(err, check.IsNil)
	c.Assert(isSwapped, check.Equals, true)
	c.Assert(swappedWith, check.Equals, testBackend1)
	backAddr1, err := s.Router.Addr(testBackend1)
	c.Assert(err, check.IsNil)
	c.Assert(backAddr1, check.Equals, backend2OrigAddr)
	backAddr2, err := s.Router.Addr(testBackend2)
	c.Assert(err, check.IsNil)
	c.Assert(backAddr2, check.Equals, backend1OrigAddr)
	routes, err := s.Router.Routes(testBackend1)
	c.Assert(err, check.IsNil)
	c.Assert(routes, HostEquals, []*url.URL{addr1})
	routes, err = s.Router.Routes(testBackend2)
	c.Assert(err, check.IsNil)
	c.Assert(routes, HostEquals, []*url.URL{addr2})
	err = s.Router.Swap(testBackend1, testBackend2, false)
	c.Assert(err, check.IsNil)
	isSwapped, swappedWith, err = router.IsSwapped(testBackend1)
	c.Assert(err, check.IsNil)
	c.Assert(isSwapped, check.Equals, false)
	c.Assert(swappedWith, check.Equals, testBackend1)
	backAddr1, err = s.Router.Addr(testBackend1)
	c.Assert(err, check.IsNil)
	c.Assert(backAddr1, check.Equals, backend1OrigAddr)
	backAddr2, err = s.Router.Addr(testBackend2)
	c.Assert(err, check.IsNil)
	c.Assert(backAddr2, check.Equals, backend2OrigAddr)
	routes, err = s.Router.Routes(testBackend1)
	c.Assert(err, check.IsNil)
	c.Assert(routes, HostEquals, []*url.URL{addr1})
	routes, err = s.Router.Routes(testBackend2)
	c.Assert(err, check.IsNil)
	c.Assert(routes, HostEquals, []*url.URL{addr2})
	err = s.Router.RemoveBackend(testBackend1)
	c.Assert(err, check.IsNil)
	err = s.Router.RemoveBackend(testBackend2)
	c.Assert(err, check.IsNil)
}