Esempio n. 1
0
func (s *ELBSuite) TestSwap(c *gocheck.C) {
	var p JujuProvisioner
	app1 := testing.NewFakeApp("app1", "python", 1)
	app2 := testing.NewFakeApp("app2", "python", 1)
	id1 := s.server.NewInstance()
	defer s.server.RemoveInstance(id1)
	id2 := s.server.NewInstance()
	defer s.server.RemoveInstance(id2)
	router, err := Router()
	c.Assert(err, gocheck.IsNil)
	err = router.AddBackend(app1.GetName())
	c.Assert(err, gocheck.IsNil)
	defer router.RemoveBackend(app1.GetName())
	err = router.AddRoute(app1.GetName(), id1)
	c.Assert(err, gocheck.IsNil)
	err = router.AddBackend(app2.GetName())
	c.Assert(err, gocheck.IsNil)
	defer router.RemoveBackend(app2.GetName())
	err = router.AddRoute(app2.GetName(), id2)
	c.Assert(err, gocheck.IsNil)
	err = p.Swap(app1, app2)
	c.Assert(err, gocheck.IsNil)
	app2Routes, err := router.Routes(app2.GetName())
	c.Assert(err, gocheck.IsNil)
	c.Assert(app2Routes, gocheck.DeepEquals, []string{id2})
	app1Routes, err := router.Routes(app1.GetName())
	c.Assert(err, gocheck.IsNil)
	c.Assert(app1Routes, gocheck.DeepEquals, []string{id1})
	addr, err := router.Addr(app1.GetName())
	c.Assert(err, gocheck.IsNil)
	c.Assert(addr, gocheck.Equals, "app2-some-aws-stuff.us-east-1.elb.amazonaws.com")
	addr, err = router.Addr(app2.GetName())
	c.Assert(err, gocheck.IsNil)
	c.Assert(addr, gocheck.Equals, "app1-some-aws-stuff.us-east-1.elb.amazonaws.com")
}
Esempio n. 2
0
func (s *S) TestSwap(c *gocheck.C) {
	var p dockerProvisioner
	app1 := testing.NewFakeApp("app1", "python", 1)
	app2 := testing.NewFakeApp("app2", "python", 1)
	rtesting.FakeRouter.AddBackend(app1.GetName())
	rtesting.FakeRouter.AddRoute(app1.GetName(), "127.0.0.1")
	rtesting.FakeRouter.AddBackend(app2.GetName())
	rtesting.FakeRouter.AddRoute(app2.GetName(), "127.0.0.2")
	err := p.Swap(app1, app2)
	c.Assert(err, gocheck.IsNil)
	c.Assert(rtesting.FakeRouter.HasBackend(app1.GetName()), gocheck.Equals, true)
	c.Assert(rtesting.FakeRouter.HasBackend(app2.GetName()), gocheck.Equals, true)
	c.Assert(rtesting.FakeRouter.HasRoute(app2.GetName(), "127.0.0.1"), gocheck.Equals, true)
	c.Assert(rtesting.FakeRouter.HasRoute(app1.GetName(), "127.0.0.2"), gocheck.Equals, true)
}
Esempio n. 3
0
func (s *S) TestSetEnvironVariablesToAppBackward(c *gocheck.C) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(`{"DATABASE_USER":"******","DATABASE_PASSWORD":"******"}`))
	}))
	defer ts.Close()
	service := Service{Name: "mysql", Endpoint: map[string]string{"production": ts.URL}}
	err := service.Create()
	c.Assert(err, gocheck.IsNil)
	defer s.conn.Services().RemoveId(service.Name)
	si := ServiceInstance{
		Name:        "my-mysql",
		ServiceName: "mysql",
		Teams:       []string{s.team.Name},
	}
	err = si.Create()
	c.Assert(err, gocheck.IsNil)
	defer s.conn.ServiceInstances().RemoveId(si.Name)
	a := testing.NewFakeApp("myapp", "static", 1)
	ctx := action.FWContext{
		Params: []interface{}{a, si},
	}
	r, err := setEnvironVariablesToApp.Forward(ctx)
	c.Assert(err, gocheck.IsNil)
	bwCtx := action.BWContext{
		Params:   []interface{}{a},
		FWResult: r,
	}
	setEnvironVariablesToApp.Backward(bwCtx)
	c.Assert(a.Envs(), gocheck.DeepEquals, map[string]bind.EnvVar{})
}
func (s *S) TestContainerCreate(c *gocheck.C) {
	app := testing.NewFakeApp("app-name", "brainfuck", 1)
	app.Memory = 15
	rtesting.FakeRouter.AddBackend(app.GetName())
	defer rtesting.FakeRouter.RemoveBackend(app.GetName())
	dockerCluster().PullImage(
		docker.PullImageOptions{Repository: "tsuru/brainfuck"},
		docker.AuthConfiguration{},
	)
	cont := container{Name: "myName", AppName: app.GetName(), Type: app.GetPlatform(), Status: "created"}
	err := cont.create(app, getImage(app), []string{"docker", "run"})
	c.Assert(err, gocheck.IsNil)
	defer s.removeTestContainer(&cont)
	c.Assert(cont.ID, gocheck.Not(gocheck.Equals), "")
	c.Assert(cont, gocheck.FitsTypeOf, container{})
	c.Assert(cont.AppName, gocheck.Equals, app.GetName())
	c.Assert(cont.Type, gocheck.Equals, app.GetPlatform())
	u, _ := url.Parse(s.server.URL())
	host, _, _ := net.SplitHostPort(u.Host)
	c.Assert(cont.HostAddr, gocheck.Equals, host)
	user, err := config.GetString("docker:ssh:user")
	c.Assert(err, gocheck.IsNil)
	c.Assert(cont.User, gocheck.Equals, user)
	dcli, _ := docker.NewClient(s.server.URL())
	container, err := dcli.InspectContainer(cont.ID)
	c.Assert(err, gocheck.IsNil)
	c.Assert(container.Path, gocheck.Equals, "docker")
	c.Assert(container.Args, gocheck.DeepEquals, []string{"run"})
	c.Assert(container.Config.User, gocheck.Equals, user)
	c.Assert(container.Config.Memory, gocheck.Equals, int64(app.Memory*1024*1024))
}
Esempio n. 5
0
func (s *S) TestExecuteCommandOnceWithoutContainers(c *gocheck.C) {
	app := testing.NewFakeApp("almah", "static", 2)
	p := dockerProvisioner{}
	var stdout, stderr bytes.Buffer
	err := p.ExecuteCommandOnce(&stdout, &stderr, app, "ls", "-lh")
	c.Assert(err, gocheck.Not(gocheck.IsNil))
}
Esempio n. 6
0
func (s *S) TestProvisionerExecuteCommand(c *gocheck.C) {
	var handler FakeSSHServer
	handler.output = ". .."
	server := httptest.NewServer(&handler)
	defer server.Close()
	host, port, _ := net.SplitHostPort(server.Listener.Addr().String())
	portNumber, _ := strconv.Atoi(port)
	config.Set("docker:ssh-agent-port", portNumber)
	defer config.Unset("docker:ssh-agent-port")
	app := testing.NewFakeApp("starbreaker", "python", 1)
	container, err := s.newContainer(&newContainerOpts{AppName: app.GetName()})
	c.Assert(err, gocheck.IsNil)
	defer s.removeTestContainer(container)
	container.HostAddr = host
	coll := collection()
	defer coll.Close()
	coll.Update(bson.M{"id": container.ID}, container)
	var stdout, stderr bytes.Buffer
	var p dockerProvisioner
	err = p.ExecuteCommand(&stdout, &stderr, app, "ls", "-ar")
	c.Assert(err, gocheck.IsNil)
	c.Assert(stderr.Bytes(), gocheck.IsNil)
	c.Assert(stdout.String(), gocheck.Equals, ". ..")
	body := handler.bodies[0]
	input := cmdInput{Cmd: "ls", Args: []string{"-ar"}}
	c.Assert(body, gocheck.DeepEquals, input)
	ip, _, _ := container.networkInfo()
	path := fmt.Sprintf("/container/%s/cmd", ip)
	c.Assert(handler.requests[0].URL.Path, gocheck.DeepEquals, path)
}
Esempio n. 7
0
func (s *S) TestProvisionerDestroy(c *gocheck.C) {
	err := newImage("tsuru/python", s.server.URL())
	c.Assert(err, gocheck.IsNil)
	cont, err := s.newContainer(nil)
	c.Assert(err, gocheck.IsNil)
	app := testing.NewFakeApp(cont.AppName, "python", 1)
	var p dockerProvisioner
	p.Provision(app)
	c.Assert(p.Destroy(app), gocheck.IsNil)
	ok := make(chan bool, 1)
	go func() {
		coll := collection()
		defer coll.Close()
		for {
			ct, err := coll.Find(bson.M{"appname": cont.AppName}).Count()
			if err != nil {
				c.Fatal(err)
			}
			if ct == 0 {
				ok <- true
				return
			}
			runtime.Gosched()
		}
	}()
	select {
	case <-ok:
	case <-time.After(10e9):
		c.Fatal("Timed out waiting for the container to be destroyed (10 seconds)")
	}
	c.Assert(rtesting.FakeRouter.HasBackend("myapp"), gocheck.Equals, false)
}
Esempio n. 8
0
func (s *S) TestStartedUnitsShouldReturnTrueForUnreachable(c *gocheck.C) {
	app := testing.NewFakeApp("almah", "static", 1)
	app.SetUnitStatus(provision.StatusUnreachable, 0)
	p := JujuProvisioner{}
	units := p.startedUnits(app)
	c.Assert(units, gocheck.HasLen, 1)
}
func (s *S) TestProvisionerRestart(c *gocheck.C) {
	var p dockerProvisioner
	app := testing.NewFakeApp("almah", "static", 1)
	container, err := s.newContainer(&newContainerOpts{AppName: app.GetName()})
	c.Assert(err, gocheck.IsNil)
	defer s.removeTestContainer(container)
	err = p.Start(app)
	c.Assert(err, gocheck.IsNil)
	dockerContainer, err := dCluster.InspectContainer(container.ID)
	c.Assert(err, gocheck.IsNil)
	c.Assert(dockerContainer.State.Running, gocheck.Equals, true)
	container.IP = ""
	container.HostPort = ""
	coll := collection()
	defer coll.Close()
	coll.Update(bson.M{"id": container.ID}, container)
	err = p.Restart(app)
	c.Assert(err, gocheck.IsNil)
	dockerContainer, err = dCluster.InspectContainer(container.ID)
	c.Assert(err, gocheck.IsNil)
	c.Assert(dockerContainer.State.Running, gocheck.Equals, true)
	container, err = getContainer(container.ID)
	c.Assert(err, gocheck.IsNil)
	expectedIP := dockerContainer.NetworkSettings.IPAddress
	expectedPort := dockerContainer.NetworkSettings.Ports["8888/tcp"][0].HostPort
	c.Assert(container.IP, gocheck.Equals, expectedIP)
	c.Assert(container.HostPort, gocheck.Equals, expectedPort)
	c.Assert(container.Status, gocheck.Equals, provision.StatusStarted.String())
	expectedSSHPort := dockerContainer.NetworkSettings.Ports["22/tcp"][0].HostPort
	c.Assert(container.SSHHostPort, gocheck.Equals, expectedSSHPort)
}
func (s *S) TestProvisionerRemoveUnits(c *gocheck.C) {
	err := newImage("tsuru/python", s.server.URL())
	c.Assert(err, gocheck.IsNil)
	container1, err := s.newContainer(&newContainerOpts{Status: "building"})
	c.Assert(err, gocheck.IsNil)
	defer rtesting.FakeRouter.RemoveBackend(container1.AppName)
	container2, err := s.newContainer(&newContainerOpts{Status: "building"})
	c.Assert(err, gocheck.IsNil)
	defer rtesting.FakeRouter.RemoveBackend(container2.AppName)
	container3, err := s.newContainer(&newContainerOpts{Status: "started"})
	c.Assert(err, gocheck.IsNil)
	defer s.removeTestContainer(container3)
	client, err := docker.NewClient(s.server.URL())
	c.Assert(err, gocheck.IsNil)
	err = client.StartContainer(container1.ID, nil)
	c.Assert(err, gocheck.IsNil)
	err = client.StartContainer(container2.ID, nil)
	c.Assert(err, gocheck.IsNil)
	app := testing.NewFakeApp(container1.AppName, "python", 0)
	var p dockerProvisioner
	err = p.RemoveUnits(app, 2)
	c.Assert(err, gocheck.IsNil)
	_, err = getContainer(container1.ID)
	c.Assert(err, gocheck.NotNil)
	_, err = getContainer(container2.ID)
	c.Assert(err, gocheck.NotNil)
}
func (s *S) TestProvisionerAddUnitsWithErrorDoesntLeaveLostUnits(c *gocheck.C) {
	callCount := 0
	s.server.CustomHandler("/containers/create", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		callCount++
		if callCount == 2 {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		s.server.DefaultHandler().ServeHTTP(w, r)
	}))
	defer s.server.CustomHandler("/containers/create", s.server.DefaultHandler())
	err := newImage("tsuru/python", s.server.URL())
	c.Assert(err, gocheck.IsNil)
	var p dockerProvisioner
	app := testing.NewFakeApp("myapp", "python", 0)
	p.Provision(app)
	defer p.Destroy(app)
	coll := collection()
	defer coll.Close()
	coll.Insert(container{ID: "c-89320", AppName: app.GetName(), Version: "a345fe", Image: "tsuru/python"})
	defer coll.RemoveId(bson.M{"id": "c-89320"})
	_, err = p.AddUnits(app, 3)
	c.Assert(err, gocheck.NotNil)
	count, err := coll.Find(bson.M{"appname": app.GetName()}).Count()
	c.Assert(err, gocheck.IsNil)
	c.Assert(count, gocheck.Equals, 1)
}
Esempio n. 12
0
func (s *ELBSuite) TestHandleMessagesWithPendingUnits(c *gocheck.C) {
	id := s.server.NewInstance()
	defer s.server.RemoveInstance(id)
	app := testing.NewFakeApp("2112", "python", 1)
	router, err := Router()
	c.Assert(err, gocheck.IsNil)
	err = router.AddBackend(app.GetName())
	defer router.RemoveBackend(app.GetName())
	output := strings.Replace(collectOutputNoInstanceID, "i-00004444", id, 1)
	tmpdir, err := commandmocker.Add("juju", output)
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	handle(&queue.Message{
		Action: addUnitToLoadBalancer,
		Args:   []string{"2112", "2112/0", "2112/1"},
	})
	resp, err := s.client.DescribeLoadBalancers(app.GetName())
	c.Assert(err, gocheck.IsNil)
	c.Assert(resp.LoadBalancerDescriptions, gocheck.HasLen, 1)
	instances := resp.LoadBalancerDescriptions[0].Instances
	c.Assert(instances, gocheck.HasLen, 1)
	c.Assert(instances[0].InstanceId, gocheck.Equals, id)
	msg, err := getQueue(queueName).Get(5e9)
	c.Assert(err, gocheck.IsNil)
	c.Assert(msg.Action, gocheck.Equals, addUnitToLoadBalancer)
	c.Assert(msg.Args, gocheck.DeepEquals, []string{"2112", "2112/1"})
}
Esempio n. 13
0
func (s *ELBSuite) TestHandleMessageWithUnits(c *gocheck.C) {
	id1 := s.server.NewInstance()
	id2 := s.server.NewInstance()
	defer s.server.RemoveInstance(id1)
	defer s.server.RemoveInstance(id2)
	app := testing.NewFakeApp("symfonia", "python", 1)
	router, err := Router()
	c.Assert(err, gocheck.IsNil)
	router.AddBackend(app.GetName())
	c.Assert(err, gocheck.IsNil)
	defer router.RemoveBackend(app.GetName())
	output := strings.Replace(simpleCollectOutput, "i-00004444", id1, -1)
	output = strings.Replace(output, "i-00004445", id2, -1)
	tmpdir, err := commandmocker.Add("juju", output)
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	msg := queue.Message{
		Action: addUnitToLoadBalancer,
		Args:   []string{"symfonia", "symfonia/0", "symfonia/1"},
	}
	handle(&msg)
	resp, err := s.client.DescribeLoadBalancers(app.GetName())
	c.Assert(err, gocheck.IsNil)
	c.Assert(resp.LoadBalancerDescriptions, gocheck.HasLen, 1)
	instances := resp.LoadBalancerDescriptions[0].Instances
	c.Assert(instances, gocheck.HasLen, 2)
	ids := []string{instances[0].InstanceId, instances[1].InstanceId}
	sort.Strings(ids)
	want := []string{id1, id2}
	sort.Strings(want)
	c.Assert(ids, gocheck.DeepEquals, want)
	c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true)
}
Esempio n. 14
0
func (s *ELBSuite) TestHandleMessageWithoutUnits(c *gocheck.C) {
	instIds := make([]string, 3)
	for i := 0; i < len(instIds); i++ {
		id := s.server.NewInstance()
		defer s.server.RemoveInstance(id)
		instIds[i] = id
	}
	replace := []string{"i-00004444", "i-00004445", "i-00004450"}
	output := simpleCollectOutput
	for i, r := range replace {
		output = strings.Replace(output, r, instIds[i], 1)
	}
	tmpdir, err := commandmocker.Add("juju", output)
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	app := testing.NewFakeApp("symfonia", "python", 1)
	router, err := Router()
	c.Assert(err, gocheck.IsNil)
	err = router.AddBackend(app.GetName())
	c.Assert(err, gocheck.IsNil)
	defer router.RemoveBackend(app.GetName())
	handle(&queue.Message{
		Action: addUnitToLoadBalancer,
		Args:   []string{"symfonia"},
	})
	resp, err := s.client.DescribeLoadBalancers(app.GetName())
	c.Assert(err, gocheck.IsNil)
	c.Assert(resp.LoadBalancerDescriptions, gocheck.HasLen, 1)
	instances := resp.LoadBalancerDescriptions[0].Instances
	c.Assert(instances, gocheck.HasLen, 3)
	ids := []string{instances[0].InstanceId, instances[1].InstanceId, instances[2].InstanceId}
	sort.Strings(ids)
	sort.Strings(instIds)
	c.Assert(ids, gocheck.DeepEquals, instIds)
}
Esempio n. 15
0
func (s *S) TestRunWithAgentCmds(c *gocheck.C) {
	app := testing.NewFakeApp("app-name", "python", 1)
	host_env := bind.EnvVar{
		Name:   "TSURU_HOST",
		Value:  "tsuru_host",
		Public: true,
	}
	token_env := bind.EnvVar{
		Name:   "TSURU_APP_TOKEN",
		Value:  "app_token",
		Public: true,
	}
	app.SetEnv(host_env)
	app.SetEnv(token_env)
	runCmd, err := config.GetString("docker:run-cmd:bin")
	c.Assert(err, gocheck.IsNil)
	unitAgentCmd := fmt.Sprintf("(tsuru_unit_agent tsuru_host app_token app-name %s || %s)", runCmd, runCmd)
	ssh, err := sshCmds()
	sshCmd := strings.Join(ssh, " && ")
	c.Assert(err, gocheck.IsNil)
	cmd := fmt.Sprintf("%s && %s", unitAgentCmd, sshCmd)
	expected := []string{"/bin/bash", "-c", cmd}
	cmds, err := runWithAgentCmds(app)
	c.Assert(err, gocheck.IsNil)
	c.Assert(cmds, gocheck.DeepEquals, expected)
}
func (s *S) TestProvisionerExecuteCommandOnceNoContainers(c *gocheck.C) {
	var p dockerProvisioner
	app := testing.NewFakeApp("almah", "static", 2)
	var buf bytes.Buffer
	err := p.ExecuteCommandOnce(&buf, &buf, app, "ls", "-lh")
	c.Assert(err, gocheck.Equals, provision.ErrEmptyApp)
}
Esempio n. 17
0
func (s *S) TestStartedUnits(c *gocheck.C) {
	app := testing.NewFakeApp("almah", "static", 2)
	app.SetUnitStatus(provision.StatusDown, 1)
	p := JujuProvisioner{}
	units := p.startedUnits(app)
	c.Assert(units, gocheck.HasLen, 1)
}
func (s *S) TestProvisionerStart(c *gocheck.C) {
	var p dockerProvisioner
	app := testing.NewFakeApp("almah", "static", 1)
	container, err := s.newContainer(&newContainerOpts{AppName: app.GetName()})
	c.Assert(err, gocheck.IsNil)
	defer s.removeTestContainer(container)
	dcli, err := docker.NewClient(s.server.URL())
	c.Assert(err, gocheck.IsNil)
	dockerContainer, err := dcli.InspectContainer(container.ID)
	c.Assert(err, gocheck.IsNil)
	c.Assert(dockerContainer.State.Running, gocheck.Equals, false)
	err = p.Start(app)
	c.Assert(err, gocheck.IsNil)
	dockerContainer, err = dcli.InspectContainer(container.ID)
	c.Assert(err, gocheck.IsNil)
	c.Assert(dockerContainer.State.Running, gocheck.Equals, true)
	container, err = getContainer(container.ID)
	c.Assert(err, gocheck.IsNil)
	expectedIP := dockerContainer.NetworkSettings.IPAddress
	expectedPort := dockerContainer.NetworkSettings.Ports["8888/tcp"][0].HostPort
	c.Assert(container.IP, gocheck.Equals, expectedIP)
	c.Assert(container.HostPort, gocheck.Equals, expectedPort)
	c.Assert(container.Status, gocheck.Equals, provision.StatusStarted.String())
	expectedSSHPort := dockerContainer.NetworkSettings.Ports["22/tcp"][0].HostPort
	c.Assert(container.SSHHostPort, gocheck.Equals, expectedSSHPort)
}
Esempio n. 19
0
func (s *S) TestProvisionUndefinedCharmsPath(c *gocheck.C) {
	config.Unset("juju:charms-path")
	p := JujuProvisioner{}
	err := p.Provision(testing.NewFakeApp("eternity", "sandman", 0))
	c.Assert(err, gocheck.NotNil)
	c.Assert(err.Error(), gocheck.Equals, `Setting "juju:charms-path" is not defined.`)
}
Esempio n. 20
0
func (s *S) TestDestroy(c *gocheck.C) {
	fexec := &etesting.FakeExecutor{}
	setExecut(fexec)
	defer setExecut(nil)
	app := testing.NewFakeApp("cribcaged", "python", 3)
	p := JujuProvisioner{}
	collection := p.unitsCollection()
	defer collection.Close()
	err := collection.Insert(
		instance{UnitName: "cribcaged/0"},
		instance{UnitName: "cribcaged/1"},
		instance{UnitName: "cribcaged/2"},
	)
	c.Assert(err, gocheck.IsNil)
	err = p.Destroy(app)
	c.Assert(err, gocheck.IsNil)
	args := []string{"destroy-service", "cribcaged"}
	c.Assert(fexec.ExecutedCmd("juju", args), gocheck.Equals, true)
	args = []string{"terminate-machine", "1"}
	c.Assert(fexec.ExecutedCmd("juju", args), gocheck.Equals, true)
	args = []string{"terminate-machine", "2"}
	c.Assert(fexec.ExecutedCmd("juju", args), gocheck.Equals, true)
	args = []string{"terminate-machine", "3"}
	c.Assert(fexec.ExecutedCmd("juju", args), gocheck.Equals, true)
	n, err := collection.Find(bson.M{
		"_id": bson.M{
			"$in": []string{"cribcaged/0", "cribcaged/1", "cribcaged/2"},
		},
	}).Count()
	c.Assert(err, gocheck.IsNil)
	c.Assert(n, gocheck.Equals, 0)
	c.Assert(fexec.ExecutedCmd("juju", args), gocheck.Equals, true)
}
Esempio n. 21
0
func (s *S) TestRemoveUnitInSameHostAsAnotherUnitShouldEnqueueAnotherBind(c *gocheck.C) {
	err := newImage("tsuru/python", s.server.URL())
	c.Assert(err, gocheck.IsNil)
	c1, err := s.newContainer(nil)
	c.Assert(err, gocheck.IsNil)
	c2, err := s.newContainer(nil)
	c.Assert(err, gocheck.IsNil)
	defer rtesting.FakeRouter.RemoveBackend(c1.AppName)
	client, err := docker.NewClient(s.server.URL())
	c.Assert(err, gocheck.IsNil)
	err = client.StartContainer(c1.ID, nil)
	c.Assert(err, gocheck.IsNil)
	a := testing.NewFakeApp(c1.AppName, "python", 0)
	var p dockerProvisioner
	err = p.RemoveUnit(a, c1.ID)
	c.Assert(err, gocheck.IsNil)
	_, err = getContainer(c1.ID)
	c.Assert(err, gocheck.NotNil)
	_, err = getContainer(c2.ID)
	c.Assert(err, gocheck.IsNil)
	q, err := getQueue()
	c.Assert(err, gocheck.IsNil)
	message, err := q.Get(1e6)
	c.Assert(err, gocheck.IsNil)
	expected := &queue.Message{Action: app.BindService, Args: []string{a.GetName(), c2.ID}}
	c.Assert(message, gocheck.DeepEquals, expected)
}
Esempio n. 22
0
func (s *S) TestRemoveUnit(c *gocheck.C) {
	fexec := &etesting.FakeExecutor{}
	setExecut(fexec)
	defer setExecut(nil)
	app := testing.NewFakeApp("two", "rush", 3)
	p := JujuProvisioner{}
	collection := p.unitsCollection()
	defer collection.Close()
	err := collection.Insert(instance{UnitName: "two/2", InstanceID: "i-00000439"})
	c.Assert(err, gocheck.IsNil)
	err = p.RemoveUnit(app, "two/2")
	c.Assert(err, gocheck.IsNil)
	ran := make(chan bool, 1)
	go func() {
		for {
			args1 := []string{"remove-unit", "two/2"}
			args2 := []string{"terminate-machine", "3"}
			if fexec.ExecutedCmd("juju", args1) && fexec.ExecutedCmd("juju", args2) {
				ran <- true
			}
			runtime.Gosched()
		}
	}()
	select {
	case <-ran:
	case <-time.After(2e9):
		c.Errorf("Did not run terminate-machine command after 2 seconds.")
	}
	n, err := collection.Find(bson.M{"_id": "two/2"}).Count()
	c.Assert(err, gocheck.IsNil)
	c.Assert(n, gocheck.Equals, 0)
}
Esempio n. 23
0
func (s *S) TestProvisionerRestartCallsTheRestartHook(c *gocheck.C) {
	var handler FakeSSHServer
	handler.output = "caad7bbd5411"
	server := httptest.NewServer(&handler)
	defer server.Close()
	host, port, _ := net.SplitHostPort(server.Listener.Addr().String())
	portNumber, _ := strconv.Atoi(port)
	config.Set("docker:ssh-agent-port", portNumber)
	defer config.Unset("docker:ssh-agent-port")
	var p dockerProvisioner
	app := testing.NewFakeApp("almah", "static", 1)
	newImage("tsuru/python", s.server.URL())
	cont, err := s.newContainer(&newContainerOpts{AppName: app.GetName()})
	c.Assert(err, gocheck.IsNil)
	defer s.removeTestContainer(cont)
	cont.HostAddr = host
	coll := collection()
	defer coll.Close()
	err = coll.Update(bson.M{"id": cont.ID}, cont)
	c.Assert(err, gocheck.IsNil)
	err = p.Restart(app)
	c.Assert(err, gocheck.IsNil)
	input := cmdInput{Cmd: "/var/lib/tsuru/restart"}
	body := handler.bodies[0]
	c.Assert(body, gocheck.DeepEquals, input)
	ip, _, _ := cont.networkInfo()
	path := fmt.Sprintf("/container/%s/cmd", ip)
	c.Assert(handler.requests[0].URL.Path, gocheck.DeepEquals, path)
}
Esempio n. 24
0
func (s *S) TestRemoveUnknownUnit(c *gocheck.C) {
	app := testing.NewFakeApp("tears", "rush", 2)
	p := JujuProvisioner{}
	err := p.RemoveUnit(app, "tears/2")
	c.Assert(err, gocheck.NotNil)
	c.Assert(err.Error(), gocheck.Equals, `App "tears" does not have a unit named "tears/2".`)
}
Esempio n. 25
0
func (s *S) TestExecuteCommandOnce(c *gocheck.C) {
	var handler FakeSSHServer
	handler.output = ". .."
	server := httptest.NewServer(&handler)
	defer server.Close()
	host, port, _ := net.SplitHostPort(server.Listener.Addr().String())
	portNumber, _ := strconv.Atoi(port)
	config.Set("docker:ssh-agent-port", portNumber)
	defer config.Unset("docker:ssh-agent-port")
	app := testing.NewFakeApp("almah", "static", 1)
	p := dockerProvisioner{}
	container, err := s.newContainer(&newContainerOpts{AppName: app.GetName()})
	c.Assert(err, gocheck.IsNil)
	defer s.removeTestContainer(container)
	container.HostAddr = host
	coll := collection()
	defer coll.Close()
	coll.Update(bson.M{"id": container.ID}, container)
	var stdout, stderr bytes.Buffer
	err = p.ExecuteCommandOnce(&stdout, &stderr, app, "ls", "-lh")
	c.Assert(err, gocheck.IsNil)
	c.Assert(stderr.Bytes(), gocheck.IsNil)
	c.Assert(stdout.String(), gocheck.Equals, ". ..")
	body := handler.bodies[0]
	input := cmdInput{Cmd: "ls", Args: []string{"-lh"}}
	c.Assert(body, gocheck.DeepEquals, input)
}
Esempio n. 26
0
func (s *S) TestAddr(c *gocheck.C) {
	app := testing.NewFakeApp("blue", "who", 1)
	p := JujuProvisioner{}
	addr, err := p.Addr(app)
	c.Assert(err, gocheck.IsNil)
	c.Assert(addr, gocheck.Equals, app.ProvisionedUnits()[0].GetIp())
}
Esempio n. 27
0
func (s *S) TestProvisionerStopSkipAlreadyStoppedContainers(c *gocheck.C) {
	dcli, _ := docker.NewClient(s.server.URL())
	app := testing.NewFakeApp("almah", "static", 2)
	p := dockerProvisioner{}
	container, err := s.newContainer(&newContainerOpts{AppName: app.GetName()})
	c.Assert(err, gocheck.IsNil)
	defer s.removeTestContainer(container)
	err = dcli.StartContainer(container.ID, nil)
	c.Assert(err, gocheck.IsNil)
	dockerContainer, err := dcli.InspectContainer(container.ID)
	c.Assert(err, gocheck.IsNil)
	c.Assert(dockerContainer.State.Running, gocheck.Equals, true)
	container2, err := s.newContainer(&newContainerOpts{AppName: app.GetName()})
	c.Assert(err, gocheck.IsNil)
	defer s.removeTestContainer(container2)
	err = dcli.StartContainer(container2.ID, nil)
	c.Assert(err, gocheck.IsNil)
	err = dcli.StopContainer(container2.ID, 1)
	c.Assert(err, gocheck.IsNil)
	container2.setStatus(provision.StatusStopped.String())
	dockerContainer2, err := dcli.InspectContainer(container2.ID)
	c.Assert(err, gocheck.IsNil)
	c.Assert(dockerContainer2.State.Running, gocheck.Equals, false)
	err = p.Stop(app)
	c.Assert(err, gocheck.IsNil)
	dockerContainer, err = dcli.InspectContainer(container.ID)
	c.Assert(err, gocheck.IsNil)
	c.Assert(dockerContainer.State.Running, gocheck.Equals, false)
	dockerContainer2, err = dcli.InspectContainer(container2.ID)
	c.Assert(err, gocheck.IsNil)
	c.Assert(dockerContainer2.State.Running, gocheck.Equals, false)
}
Esempio n. 28
0
func (s *ELBSuite) TestDestroyWithELB(c *gocheck.C) {
	h := &testing.TestHandler{}
	gandalfServer := testing.StartGandalfTestServer(h)
	defer gandalfServer.Close()
	config.Set("juju:charms-path", "/home/charms")
	defer config.Unset("juju:charms-path")
	fexec := &etesting.FakeExecutor{}
	setExecut(fexec)
	defer setExecut(nil)
	app := testing.NewFakeApp("jimmy", "who", 0)
	p := JujuProvisioner{}
	err := p.Provision(app)
	c.Assert(err, gocheck.IsNil)
	err = p.Destroy(app)
	c.Assert(err, gocheck.IsNil)
	router, err := Router()
	c.Assert(err, gocheck.IsNil)
	defer router.RemoveBackend(app.GetName())
	addr, err := router.Addr(app.GetName())
	c.Assert(addr, gocheck.Equals, "")
	c.Assert(err, gocheck.NotNil)
	c.Assert(err, gocheck.ErrorMatches, "not found")
	q := getQueue(queueName)
	msg, err := q.Get(1e9)
	c.Assert(err, gocheck.IsNil)
	if msg.Action != addUnitToLoadBalancer {
		q.Put(msg, 0)
	}
}
func (s *S) TestGetImageFromAppPlatform(c *gocheck.C) {
	app := testing.NewFakeApp("myapp", "python", 1)
	img := getImage(app)
	repoNamespace, err := config.GetString("docker:repository-namespace")
	c.Assert(err, gocheck.IsNil)
	c.Assert(img, gocheck.Equals, fmt.Sprintf("%s/python", repoNamespace))
}
Esempio n. 30
0
func (s *S) TestDeployLogsActions(c *gocheck.C) {
	h := &testing.TestHandler{}
	gandalfServer := testing.StartGandalfTestServer(h)
	defer gandalfServer.Close()
	provisioner := testing.NewFakeProvisioner()
	provisioner.PrepareOutput([]byte(""))
	provisioner.PrepareOutput([]byte("updated"))
	app := testing.NewFakeApp("cribcaged", "python", 1)
	provisioner.Provision(app)
	w := &bytes.Buffer{}
	err := Git(provisioner, app, "5734f0042844fdeb5bbc1b72b18f2dc1779cade7", w)
	c.Assert(err, gocheck.IsNil)
	logs := w.String()
	expected := `
 ---> tsuru receiving push

 ---> Replicating the application repository across units

 ---> Installing dependencies

 ---> Restarting application
Restarting app...
 ---> Deploy done!

`
	c.Assert(logs, gocheck.Equals, expected)
}