Ejemplo n.º 1
0
func (s *S) TestProvisionerRemoveUnitNotInApp(c *gocheck.C) {
	out := `{
	"NetworkSettings": {
		"IpAddress": "127.0.0.1",
		"IpPrefixLen": 8,
		"Gateway": "10.65.41.1",
		"PortMapping": {
			"8888": "90293"
		}
	}
}`
	app := testing.NewFakeApp("myapp", "python", 0)
	fexec := &etesting.FakeExecutor{
		Output: map[string][][]byte{
			"*":            {[]byte("c-10")},
			"inspect c-10": {[]byte(out)},
		},
	}
	setExecut(fexec)
	defer setExecut(nil)
	container, err := newContainer(app)
	c.Assert(err, gocheck.IsNil)
	defer container.remove()
	var p dockerProvisioner
	err = p.RemoveUnit(testing.NewFakeApp("hisapp", "python", 1), container.Id)
	c.Assert(err, gocheck.NotNil)
	c.Assert(err.Error(), gocheck.Equals, "Unit does not belong to this app")
	_, err = getContainer(container.Id)
	c.Assert(err, gocheck.IsNil)
}
Ejemplo n.º 2
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")
}
Ejemplo n.º 3
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)
}
Ejemplo n.º 4
0
func (s *S) TestDeployFailureSecondStep(c *gocheck.C) {
	var (
		p   dockerProvisioner
		buf bytes.Buffer
	)
	app := testing.NewFakeApp("app", "python", 0)
	app.AddUnit(&testing.FakeUnit{Name: "app/0"})
	p.Provision(app)
	defer p.Destroy(app)
	defer s.conn.Collection(s.collName).RemoveAll(bson.M{"appname": app.GetName()})
	output := `{
	"NetworkSettings": {
		"IpAddress": "10.10.10.%d",
		"IpPrefixLen": 8,
		"Gateway": "10.65.41.1",
		"PortMapping": {"8888": "37574"}
	}
}`
	fexec := etesting.FailLaterExecutor{
		Succeeds: 3,
		FakeExecutor: etesting.FakeExecutor{
			Output: map[string][][]byte{
				"*":              {[]byte("c-0955")},
				"inspect c-0955": {[]byte(output)},
			},
		},
	}
	setExecut(&fexec)
	defer setExecut(nil)
	err := p.Deploy(app, &buf)
	c.Assert(err, gocheck.NotNil)
	c.Assert(buf.String(), gocheck.Equals, "c-0955")
	c.Assert(fexec.ExecutedCmd("docker", []string{"rm", "c-0955"}), gocheck.Equals, true)
	c.Assert(app.ProvisionUnits(), gocheck.HasLen, 1)
}
Ejemplo n.º 5
0
func (s *ELBSuite) TestCreateELBUsingVPC(c *gocheck.C) {
	old, _ := config.Get("juju:elb-avail-zones")
	config.Unset("juju:elb-avail-zones")
	config.Set("juju:elb-use-vpc", true)
	config.Set("juju:elb-vpc-subnets", []string{"subnet-a4a3a2a1", "subnet-002200"})
	config.Set("juju:elb-vpc-secgroups", []string{"sg-0900"})
	defer func() {
		config.Set("juju:elb-avail-zones", old)
		config.Unset("juju:elb-use-vpc")
		config.Unset("juju:elb-vpc-subnets")
		config.Unset("juju:elb-vpc-secgroups")
	}()
	app := testing.NewFakeApp("relax", "who", 1)
	manager := ELBManager{}
	err := manager.Create(app)
	c.Assert(err, gocheck.IsNil)
	defer s.client.DeleteLoadBalancer(app.GetName())
	conn, coll := manager.collection()
	defer conn.Close()
	defer coll.Remove(bson.M{"name": app.GetName()})
	resp, err := s.client.DescribeLoadBalancers(app.GetName())
	c.Assert(err, gocheck.IsNil)
	c.Assert(resp.LoadBalancerDescriptions, gocheck.HasLen, 1)
	lbd := resp.LoadBalancerDescriptions[0]
	c.Assert(lbd.Subnets, gocheck.DeepEquals, []string{"subnet-a4a3a2a1", "subnet-002200"})
	c.Assert(lbd.SecurityGroups, gocheck.DeepEquals, []string{"sg-0900"})
	c.Assert(lbd.Scheme, gocheck.Equals, "internal")
	c.Assert(lbd.AvailZones, gocheck.HasLen, 0)
}
Ejemplo n.º 6
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))
}
Ejemplo n.º 7
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))
}
Ejemplo n.º 8
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)
}
Ejemplo n.º 9
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)
}
Ejemplo n.º 10
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"})
}
Ejemplo n.º 11
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)
}
Ejemplo n.º 12
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)
}
Ejemplo n.º 13
0
func (s *ELBSuite) TestDestroyWithELB(c *gocheck.C) {
	config.Set("juju:charms-path", "/home/charms")
	defer config.Unset("juju:charms-path")
	fexec := &etesting.FakeExecutor{}
	execut = fexec
	defer func() {
		execut = 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)
	lb := p.LoadBalancer()
	defer lb.Destroy(app) // sanity
	addr, err := lb.Addr(app)
	c.Assert(addr, gocheck.Equals, "")
	c.Assert(err, gocheck.NotNil)
	c.Assert(err.Error(), gocheck.Equals, "not found")
	q := getQueue(queueName)
	msg, err := q.Get(1e9)
	c.Assert(err, gocheck.IsNil)
	if msg.Action == addUnitToLoadBalancer && msg.Args[0] == "jimmy" {
		msg.Delete()
	} else {
		q.Release(msg, 0)
	}
}
Ejemplo n.º 14
0
func (s *S) TestProvisionerExecuteCommandMultipleContainers(c *gocheck.C) {
	fexec := &etesting.FakeExecutor{
		Output: map[string][][]byte{"*": {[]byte(". ..")}},
	}
	setExecut(fexec)
	defer setExecut(nil)
	app := testing.NewFakeApp("starbreaker", "python", 1)
	err := s.conn.Collection(s.collName).Insert(
		container{Id: "c-036", AppName: "starbreaker", Type: "python", Ip: "10.10.10.1"},
		container{Id: "c-037", AppName: "starbreaker", Type: "python", Ip: "10.10.10.2"},
	)
	c.Assert(err, gocheck.IsNil)
	defer s.conn.Collection(s.collName).RemoveAll(bson.M{"_id": bson.M{"$in": []string{"c-036", "c-037"}}})
	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)
	args1 := []string{
		"10.10.10.1", "-l", s.sshUser,
		"-o", "StrictHostKeyChecking no",
		"--", "ls", "-ar",
	}
	args2 := []string{
		"10.10.10.1", "-l", s.sshUser,
		"-o", "StrictHostKeyChecking no",
		"--", "ls", "-ar",
	}
	c.Assert(fexec.ExecutedCmd("ssh", args1), gocheck.Equals, true)
	c.Assert(fexec.ExecutedCmd("ssh", args2), gocheck.Equals, true)
}
Ejemplo n.º 15
0
func (s *S) TestProvisionerAddr(c *gocheck.C) {
	out := `{
	"NetworkSettings": {
		"IpAddress": "10.10.10.10",
		"IpPrefixLen": 8,
		"Gateway": "10.65.41.1",
		"PortMapping": {"8888": "37574"}
	}
}`
	id := "123"
	runCmd := "run -d -t -p 8888 tsuru/python /bin/bash -c /var/lib/tsuru/add-key key-content && /usr/sbin/sshd -D"
	fexec := &etesting.FakeExecutor{Output: map[string][][]byte{runCmd: {[]byte(id)}, "inspect " + id: {[]byte(out)}}}
	setExecut(fexec)
	defer setExecut(nil)
	var p dockerProvisioner
	app := testing.NewFakeApp("myapp", "python", 1)
	p.Provision(app)
	defer p.Destroy(app)
	w := &bytes.Buffer{}
	err := p.Deploy(app, w)
	c.Assert(err, gocheck.IsNil)
	defer p.Destroy(app)
	defer s.conn.Collection(s.collName).RemoveId(id)
	addr, err := p.Addr(app)
	c.Assert(err, gocheck.IsNil)
	r, err := getRouter()
	c.Assert(err, gocheck.IsNil)
	expected, err := r.Addr("myapp")
	c.Assert(err, gocheck.IsNil)
	c.Assert(addr, gocheck.Equals, expected)
}
Ejemplo n.º 16
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)
}
Ejemplo n.º 17
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.`)
}
Ejemplo n.º 18
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)
}
Ejemplo n.º 19
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 := dockerClient.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)
}
Ejemplo n.º 20
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)
}
Ejemplo n.º 21
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)
}
Ejemplo n.º 22
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".`)
}
Ejemplo n.º 23
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)
}
Ejemplo n.º 24
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())
}
Ejemplo n.º 25
0
func (s *S) TestNewContainer(c *gocheck.C) {
	oldClusterNodes := clusterNodes
	clusterNodes = map[string]string{"server": s.server.URL()}
	defer func() { clusterNodes = oldClusterNodes }()
	app := testing.NewFakeApp("app-name", "brainfuck", 1)
	rtesting.FakeRouter.AddBackend(app.GetName())
	defer rtesting.FakeRouter.RemoveBackend(app.GetName())
	dockerCluster().PullImage(
		docker.PullImageOptions{Repository: "tsuru/brainfuck"},
		docker.AuthConfiguration{},
	)
	cont, err := newContainer(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())
	c.Assert(cont.Name, gocheck.Not(gocheck.Equals), "")
	c.Assert(cont.Name, gocheck.HasLen, 20)
	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)
	dcli, _ := docker.NewClient(s.server.URL())
	container, err := dcli.InspectContainer(cont.ID)
	c.Assert(err, gocheck.IsNil)
	c.Assert(container.Config.User, gocheck.Equals, user)
}
Ejemplo n.º 26
0
func (s *ELBSuite) TestDestroyWithELB(c *gocheck.C) {
	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 && msg.Args[0] == "jimmy" {
		msg.Delete()
	} else {
		q.Release(msg, 0)
	}
}
Ejemplo n.º 27
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)
}
Ejemplo n.º 28
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)
}
Ejemplo n.º 29
0
func (s *ELBSuite) TestCreateELB(c *gocheck.C) {
	app := testing.NewFakeApp("together", "gotthard", 1)
	manager := ELBManager{}
	manager.e = s.client
	err := manager.Create(app)
	c.Assert(err, gocheck.IsNil)
	defer s.client.DeleteLoadBalancer(app.GetName())
	conn, coll := manager.collection()
	defer conn.Close()
	defer coll.Remove(bson.M{"name": app.GetName()})
	resp, err := s.client.DescribeLoadBalancers("together")
	c.Assert(err, gocheck.IsNil)
	c.Assert(resp.LoadBalancerDescriptions, gocheck.HasLen, 1)
	c.Assert(resp.LoadBalancerDescriptions[0].ListenerDescriptions, gocheck.HasLen, 1)
	listener := resp.LoadBalancerDescriptions[0].ListenerDescriptions[0].Listener
	c.Assert(listener.InstancePort, gocheck.Equals, 80)
	c.Assert(listener.LoadBalancerPort, gocheck.Equals, 80)
	c.Assert(listener.InstanceProtocol, gocheck.Equals, "HTTP")
	c.Assert(listener.Protocol, gocheck.Equals, "HTTP")
	c.Assert(listener.SSLCertificateId, gocheck.Equals, "")
	dnsName := resp.LoadBalancerDescriptions[0].DNSName
	var lb loadBalancer
	err = s.conn.Collection(s.cName).Find(bson.M{"name": app.GetName()}).One(&lb)
	c.Assert(err, gocheck.IsNil)
	c.Assert(lb.DNSName, gocheck.Equals, dnsName)
}
Ejemplo n.º 30
0
func (s *S) TestDeployShouldRestart(c *gocheck.C) {
	out := `{
	"NetworkSettings": {
		"IpAddress": "10.10.10.10",
		"IpPrefixLen": 8,
		"Gateway": "10.65.41.1",
		"PortMapping": {"8888": "37574"}
	}
}`
	fexec := &etesting.FakeExecutor{
		Output: map[string][][]byte{
			"*": {[]byte("8yasfiajfias")},
			"inspect 8yasfiajfias": {[]byte(out)},
		},
	}
	setExecut(fexec)
	defer setExecut(nil)
	p := dockerProvisioner{}
	app := testing.NewFakeApp("cribcaged", "python", 1)
	p.Provision(app)
	defer p.Destroy(app)
	w := &bytes.Buffer{}
	err := p.Deploy(app, w)
	defer p.Destroy(app)
	defer s.conn.Collection(s.collName).RemoveId(out)
	c.Assert(err, gocheck.IsNil)
	c.Assert(app.Commands, gocheck.DeepEquals, []string{"restart"})
}