Example #1
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)
}
Example #2
0
func (s *S) TestExecuteCommandUnitDown(c *C) {
	var buf bytes.Buffer
	tmpdir, err := commandmocker.Add("juju", "$*")
	c.Assert(err, IsNil)
	defer commandmocker.Remove(tmpdir)
	app := NewFakeApp("almah", "static", 3)
	app.units[1].(*FakeUnit).status = provision.StatusDown
	p := JujuProvisioner{}
	err = p.ExecuteCommand(&buf, &buf, app, "ls", "-lha")
	c.Assert(err, IsNil)
	cmdOutput := "ssh -o StrictHostKeyChecking no -q 1 ls -lha"
	cmdOutput += "ssh -o StrictHostKeyChecking no -q 3 ls -lha"
	bufOutput := `Output from unit "almah/0":

ssh -o StrictHostKeyChecking no -q 1 ls -lha

Output from unit "almah/1":

Unit state is "down", it must be "started" for running commands.

Output from unit "almah/2":

ssh -o StrictHostKeyChecking no -q 3 ls -lha
`
	c.Assert(commandmocker.Ran(tmpdir), Equals, true)
	c.Assert(commandmocker.Output(tmpdir), Equals, cmdOutput)
	c.Assert(buf.String(), Equals, bufOutput)
}
Example #3
0
func (s *S) TestDestroy(c *C) {
	tmpdir, err := commandmocker.Add("juju", "$*")
	c.Assert(err, IsNil)
	defer commandmocker.Remove(tmpdir)
	app := NewFakeApp("cribcaged", "python", 3)
	p := JujuProvisioner{}
	err = p.Destroy(app)
	c.Assert(err, IsNil)
	c.Assert(commandmocker.Ran(tmpdir), Equals, true)
	expected := []string{
		"destroy-service", "cribcaged",
		"terminate-machine", "1",
		"terminate-machine", "2",
		"terminate-machine", "3",
	}
	ran := make(chan bool, 1)
	go func() {
		for {
			if reflect.DeepEqual(commandmocker.Parameters(tmpdir), expected) {
				ran <- true
			}
		}
	}()
	select {
	case <-ran:
	case <-time.After(2e9):
		c.Errorf("Did not run terminate-machine commands after 2 seconds.")
	}
	c.Assert(commandmocker.Parameters(tmpdir), DeepEquals, expected)
}
Example #4
0
func (s *S) TestCollectStatusDirtyOutput(c *C) {
	tmpdir, err := commandmocker.Add("juju", dirtyCollectOutput)
	c.Assert(err, IsNil)
	defer commandmocker.Remove(tmpdir)
	expected := []provision.Unit{
		{
			Name:    "as_i_rise/0",
			AppName: "as_i_rise",
			Type:    "django",
			Machine: 105,
			Ip:      "10.10.10.163",
			Status:  provision.StatusStarted,
		},
		{
			Name:    "the_infanta/1",
			AppName: "the_infanta",
			Type:    "gunicorn",
			Machine: 107,
			Ip:      "10.10.10.168",
			Status:  provision.StatusInstalling,
		},
	}
	p := JujuProvisioner{}
	units, err := p.CollectStatus()
	c.Assert(err, IsNil)
	if units[0].Type == "gunicorn" {
		units[0], units[1] = units[1], units[0]
	}
	c.Assert(units, DeepEquals, expected)
	c.Assert(commandmocker.Ran(tmpdir), Equals, true)
}
Example #5
0
func (s *S) TestAddRoute(c *gocheck.C) {
	tmpdir, err := commandmocker.Add("sudo", "$*")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	config.Set("nginx:domain", "andrewzito.com")
	config.Set("nginx:routes-path", "testdata")
	rfs := &testing.RecordingFs{}
	fsystem = rfs
	defer func() {
		fsystem = nil
	}()
	var r NginxRouter
	err = r.AddRoute("name", "127.0.0.1")
	c.Assert(err, gocheck.IsNil)
	file, err := rfs.Open("testdata/name")
	c.Assert(err, gocheck.IsNil)
	data, err := ioutil.ReadAll(file)
	c.Assert(err, gocheck.IsNil)
	expected := `server {
	listen 80;
	server_name name.andrewzito.com;
	location / {
		proxy_pass http://127.0.0.1;
	}
}`
	c.Assert(string(data), gocheck.Equals, expected)
	c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true)
	expected = "service nginx restart"
	c.Assert(commandmocker.Output(tmpdir), gocheck.Equals, expected)
}
Example #6
0
func (s *S) TestBootstrapProvisionHealerHeal(c *gocheck.C) {
	p := JujuProvisioner{}
	m := machine{
		AgentState:    "not-started",
		IpAddress:     "localhost",
		InstanceId:    "i-00000376",
		InstanceState: "running",
	}
	p.saveBootstrapMachine(m)
	conn, collection := p.bootstrapCollection()
	defer conn.Close()
	defer collection.Remove(m)
	sshTmpdir, err := commandmocker.Add("ssh", "$*")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(sshTmpdir)
	sshOutput := []string{
		"-o",
		"StrictHostKeyChecking no",
		"-q",
		"-l",
		"ubuntu",
		"localhost",
		"sudo",
		"start",
		"juju-provision-agent",
	}
	h := bootstrapProvisionHealer{}
	err = h.Heal()
	c.Assert(err, gocheck.IsNil)
	c.Assert(commandmocker.Ran(sshTmpdir), gocheck.Equals, true)
	c.Assert(commandmocker.Parameters(sshTmpdir), gocheck.DeepEquals, sshOutput)
}
Example #7
0
func (s *S) TestRemoveUnit(c *gocheck.C) {
	tmpdir, err := commandmocker.Add("juju", "removed")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	app := testing.NewFakeApp("two", "rush", 3)
	p := JujuProvisioner{}
	conn, collection := p.unitsCollection()
	defer conn.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)
	c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true)
	expected := []string{"remove-unit", "two/2", "terminate-machine", "3"}
	ran := make(chan bool, 1)
	go func() {
		for {
			if reflect.DeepEqual(commandmocker.Parameters(tmpdir), expected) {
				ran <- true
			}
			time.Sleep(1e3)
		}
	}()
	n, err := collection.Find(bson.M{"_id": "two/2"}).Count()
	c.Assert(err, gocheck.IsNil)
	c.Assert(n, gocheck.Equals, 0)
	select {
	case <-ran:
	case <-time.After(2e9):
		c.Errorf("Did not run terminate-machine command after 2 seconds.")
	}
}
Example #8
0
func (s *S) TestNewBareShouldCreateADir(c *gocheck.C) {
	dir, err := commandmocker.Add("git", "$*")
	c.Check(err, gocheck.IsNil)
	defer commandmocker.Remove(dir)
	err = newBare("myBare")
	c.Assert(err, gocheck.IsNil)
	c.Assert(commandmocker.Ran(dir), gocheck.Equals, true)
}
Example #9
0
func (s *S) TestInstanceMachineHealWhenEverythingIsOk(c *gocheck.C) {
	jujuTmpdir, err := commandmocker.Add("juju", collectOutput)
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(jujuTmpdir)
	sshTmpdir, err := commandmocker.Add("ssh", "$*")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(sshTmpdir)
	jujuOutput := []string{
		"status", // for juju status that gets the output
	}
	h := instanceMachineHealer{}
	err = h.Heal()
	c.Assert(err, gocheck.IsNil)
	c.Assert(commandmocker.Ran(jujuTmpdir), gocheck.Equals, true)
	c.Assert(commandmocker.Parameters(jujuTmpdir), gocheck.DeepEquals, jujuOutput)
	c.Assert(commandmocker.Ran(sshTmpdir), gocheck.Equals, false)
}
Example #10
0
func (s *S) TestProvisionerProvision(c *gocheck.C) {
	config.Set("local:authorized-key-path", "somepath")
	rfs := &fstesting.RecordingFs{}
	fsystem = rfs
	defer func() {
		fsystem = nil
	}()
	f, _ := os.Open("testdata/dnsmasq.leases")
	data, err := ioutil.ReadAll(f)
	c.Assert(err, gocheck.IsNil)
	file, err := rfs.Create("/var/lib/misc/dnsmasq.leases")
	c.Assert(err, gocheck.IsNil)
	_, err = file.Write(data)
	c.Assert(err, gocheck.IsNil)
	tmpdir, err := commandmocker.Add("sudo", "$*")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	sshTempDir, err := commandmocker.Add("ssh", "$*")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(sshTempDir)
	scpTempDir, err := commandmocker.Add("scp", "$*")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(scpTempDir)
	var p LocalProvisioner
	app := testing.NewFakeApp("myapp", "python", 0)
	defer p.collection().Remove(bson.M{"name": "myapp"})
	c.Assert(p.Provision(app), gocheck.IsNil)
	ok := make(chan bool, 1)
	go func() {
		for {
			coll := s.conn.Collection(s.collName)
			ct, err := coll.Find(bson.M{"name": "myapp", "status": provision.StatusStarted}).Count()
			if err != nil {
				c.Fatal(err)
			}
			if ct > 0 {
				ok <- true
				return
			}
			time.Sleep(1e3)
		}
	}()
	select {
	case <-ok:
	case <-time.After(10e9):
		c.Fatal("Timed out waiting for the container to be provisioned (10 seconds)")
	}
	c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true)
	expected := "lxc-create -t ubuntu -n myapp -- -S somepath"
	expected += "lxc-start --daemon -n myapp"
	expected += "service nginx restart"
	c.Assert(commandmocker.Output(tmpdir), gocheck.Equals, expected)
	var unit provision.Unit
	err = s.conn.Collection(s.collName).Find(bson.M{"name": "myapp"}).One(&unit)
	c.Assert(err, gocheck.IsNil)
	c.Assert(unit.Ip, gocheck.Equals, "10.10.10.15")
}
Example #11
0
func (s *S) TestNewShouldCreateNewGitBareRepository(c *C) {
	tmpdir, err := commandmocker.Add("git", "$*")
	c.Assert(err, IsNil)
	defer commandmocker.Remove(tmpdir)
	_, err = New("myRepo", []string{"pumpkin"}, true)
	c.Assert(err, IsNil)
	defer db.Session.Repository().Remove(bson.M{"_id": "myRepo"})
	c.Assert(commandmocker.Ran(tmpdir), Equals, true)
}
Example #12
0
func (s *S) TestRestartRouter(c *gocheck.C) {
	tmpdir, err := commandmocker.Add("sudo", "$*")
	c.Assert(err, gocheck.IsNil)
	err = RestartRouter()
	c.Assert(err, gocheck.IsNil)
	c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true)
	expected := "service nginx restart"
	c.Assert(commandmocker.Output(tmpdir), gocheck.Equals, expected)
}
Example #13
0
func (s *S) TestProvision(c *C) {
	tmpdir, err := commandmocker.Add("juju", "$*")
	c.Assert(err, IsNil)
	defer commandmocker.Remove(tmpdir)
	app := NewFakeApp("trace", "python", 0)
	p := JujuProvisioner{}
	err = p.Provision(app)
	c.Assert(err, IsNil)
	c.Assert(commandmocker.Ran(tmpdir), Equals, true)
	c.Assert(commandmocker.Output(tmpdir), Equals, "deploy --repository /home/charms local:python trace")
}
Example #14
0
func (s *S) TestLXCDestroy(c *gocheck.C) {
	tmpdir, err := commandmocker.Add("sudo", "$*")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	container := container{name: "container"}
	err = container.destroy()
	c.Assert(err, gocheck.IsNil)
	c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true)
	expected := "lxc-destroy -n container"
	c.Assert(commandmocker.Output(tmpdir), gocheck.Equals, expected)
}
Example #15
0
func (s *S) TestProvisionSetup(c *gocheck.C) {
	tmpdir, err := commandmocker.Add("scp", "$*")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	sshTempDir, err := commandmocker.Add("ssh", "$*")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(sshTempDir)
	p := LocalProvisioner{}
	formulasPath := "/home/ubuntu/formulas"
	config.Set("local:formulas-path", formulasPath)
	err = p.setup("10.10.10.10", "static")
	c.Assert(err, gocheck.IsNil)
	c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true)
	cmds := []string{
		"-q",
		"-o",
		"StrictHostKeyChecking no",
		"-r",
		formulasPath + "/static/hooks",
		"[email protected]:/var/lib/tsuru",
	}
	c.Assert(commandmocker.Parameters(tmpdir), gocheck.DeepEquals, cmds)
	c.Assert(commandmocker.Ran(sshTempDir), gocheck.Equals, true)
	cmds = []string{
		"-q",
		"-o",
		"StrictHostKeyChecking no",
		"-l",
		"ubuntu",
		"10.10.10.10",
		"sudo mkdir -p /var/lib/tsuru/hooks",
		"-q",
		"-o",
		"StrictHostKeyChecking no",
		"-l",
		"ubuntu",
		"10.10.10.10",
		"sudo chown -R ubuntu /var/lib/tsuru/hooks",
	}
	c.Assert(commandmocker.Parameters(sshTempDir), gocheck.DeepEquals, cmds)
}
Example #16
0
func (s *S) TestExecuteWithoutArgs(c *gocheck.C) {
	tmpdir, err := commandmocker.Add("ls", "ok")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	var e OsExecutor
	var b bytes.Buffer
	err = e.Execute("ls", nil, nil, &b, &b)
	c.Assert(err, gocheck.IsNil)
	c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true)
	c.Assert(commandmocker.Parameters(tmpdir), gocheck.IsNil)
	c.Assert(b.String(), gocheck.Equals, "ok")
}
Example #17
0
func (s *S) TestLXCCreate(c *gocheck.C) {
	config.Set("local:authorized-key-path", "somepath")
	tmpdir, err := commandmocker.Add("sudo", "$*")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	container := container{name: "container"}
	err = container.create()
	c.Assert(err, gocheck.IsNil)
	c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true)
	expected := "lxc-create -t ubuntu-cloud -n container -- -S somepath"
	c.Assert(commandmocker.Output(tmpdir), gocheck.Equals, expected)
}
func (s *S) TestCollectStatusDirtyOutput(c *gocheck.C) {
	server, url := badGatewayServer()
	defer server.Close()
	tmpdir, err := commandmocker.Add("juju", generateDirtyOutput(url))
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	expected := []provision.Unit{
		{
			Name:       "as_i_rise/0",
			AppName:    "as_i_rise",
			Type:       "django",
			Machine:    105,
			InstanceId: "i-00000439",
			Ip:         url,
			Status:     provision.StatusUnreachable,
		},
		{
			Name:       "the_infanta/1",
			AppName:    "the_infanta",
			Type:       "gunicorn",
			Machine:    107,
			InstanceId: "i-0000043e",
			Ip:         url,
			Status:     provision.StatusBuilding,
		},
	}
	p := JujuProvisioner{}
	units, err := p.CollectStatus()
	c.Assert(err, gocheck.IsNil)
	cp := make([]provision.Unit, len(units))
	copy(cp, units)
	if cp[0].Type == "gunicorn" {
		cp[0], cp[1] = cp[1], cp[0]
	}
	c.Assert(cp, gocheck.DeepEquals, expected)
	c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true)
	var wg sync.WaitGroup
	wg.Add(1)
	collection := p.unitsCollection()
	defer collection.Close()
	go func() {
		q := bson.M{"_id": bson.M{"$in": []string{"as_i_rise/0", "the_infanta/1"}}}
		for {
			if n, _ := collection.Find(q).Count(); n == 2 {
				break
			}
			runtime.Gosched()
		}
		collection.Remove(q)
		wg.Done()
	}()
	wg.Wait()
}
Example #19
0
func (s *S) TestProvisionerExecuteCommand(c *gocheck.C) {
	var p LocalProvisioner
	var buf bytes.Buffer
	tmpdir, err := commandmocker.Add("ssh", "$*")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	app := testing.NewFakeApp("almah", "static", 2)
	err = p.ExecuteCommand(&buf, &buf, app, "ls", "-lh")
	c.Assert(err, gocheck.IsNil)
	cmdOutput := fmt.Sprintf("-l ubuntu -q -o StrictHostKeyChecking no %s ls -lh", app.ProvisionUnits()[0].GetIp())
	c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true)
	c.Assert(commandmocker.Output(tmpdir), gocheck.Equals, cmdOutput)
}
func (s *S) TestCollectStatusFailure(c *gocheck.C) {
	tmpdir, err := commandmocker.Error("juju", "juju failed", 1)
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	p := JujuProvisioner{}
	_, err = p.CollectStatus()
	c.Assert(err, gocheck.NotNil)
	pErr, ok := err.(*provision.Error)
	c.Assert(ok, gocheck.Equals, true)
	c.Assert(pErr.Reason, gocheck.Equals, "juju failed")
	c.Assert(pErr.Err.Error(), gocheck.Equals, "exit status 1")
	c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true)
}
Example #21
0
func (s *S) TestExecute(c *gocheck.C) {
	tmpdir, err := commandmocker.Add("ls", "ok")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	var e OsExecutor
	var b bytes.Buffer
	err = e.Execute("ls", []string{"-lsa"}, nil, &b, &b)
	c.Assert(err, gocheck.IsNil)
	c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true)
	expected := []string{"-lsa"}
	c.Assert(commandmocker.Parameters(tmpdir), gocheck.DeepEquals, expected)
	c.Assert(b.String(), gocheck.Equals, "ok")
}
Example #22
0
func (s *S) TestRemoveUnitUnknownByJuju(c *gocheck.C) {
	output := `013-01-11 20:02:07,883 INFO Connecting to environment...
2013-01-11 20:02:10,147 INFO Connected to environment.
2013-01-11 20:02:10,160 ERROR Service unit 'two/2' was not found`
	tmpdir, err := commandmocker.Error("juju", output, 1)
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	app := testing.NewFakeApp("two", "rush", 3)
	p := JujuProvisioner{}
	err = p.RemoveUnit(app, "two/2")
	c.Assert(err, gocheck.IsNil)
	c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true)
}
Example #23
0
func (s *S) TestInstanceMachineHeal(c *gocheck.C) {
	jujuTmpdir, err := commandmocker.Add("juju", collectOutputInstanceDown)
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(jujuTmpdir)
	sshTmpdir, err := commandmocker.Add("ssh", "$*")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(sshTmpdir)
	jujuOutput := []string{
		"status", // for juju status that gets the output
	}
	sshOutput := []string{
		"-o",
		"StrictHostKeyChecking no",
		"-q",
		"-l",
		"ubuntu",
		"10.10.10.163",
		"sudo",
		"stop",
		"juju-machine-agent",
		"-o",
		"StrictHostKeyChecking no",
		"-q",
		"-l",
		"ubuntu",
		"10.10.10.163",
		"sudo",
		"start",
		"juju-machine-agent",
	}
	h := instanceMachineHealer{}
	err = h.Heal()
	c.Assert(err, gocheck.IsNil)
	c.Assert(commandmocker.Ran(jujuTmpdir), gocheck.Equals, true)
	c.Assert(commandmocker.Parameters(jujuTmpdir), gocheck.DeepEquals, jujuOutput)
	c.Assert(commandmocker.Ran(sshTmpdir), gocheck.Equals, true)
	c.Assert(commandmocker.Parameters(sshTmpdir), gocheck.DeepEquals, sshOutput)
}
Example #24
0
func (s *S) TestNewBareShouldNotPassTemplateOptionWhenItsNotSetInConfig(c *gocheck.C) {
	config.Unset("git:bare:template")
	bareLocation, err := config.GetString("git:bare:location")
	c.Assert(err, gocheck.IsNil)
	barePath := path.Join(bareLocation, "foo.git")
	dir, err := commandmocker.Add("git", "$*")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(dir)
	err = newBare("foo")
	c.Assert(err, gocheck.IsNil)
	c.Assert(commandmocker.Ran(dir), gocheck.Equals, true)
	expected := fmt.Sprintf("init %s --bare", barePath)
	c.Assert(commandmocker.Output(dir), gocheck.Equals, expected)
}
Example #25
0
func (s *S) TestRestart(c *gocheck.C) {
	tmpdir, err := commandmocker.Add("juju", "restart")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	app := testing.NewFakeApp("cribcaged", "python", 1)
	p := JujuProvisioner{}
	err = p.Restart(app)
	c.Assert(err, gocheck.IsNil)
	c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true)
	expected := []string{
		"ssh", "-o", "StrictHostKeyChecking no", "-q", "1", "/var/lib/tsuru/hooks/restart",
	}
	c.Assert(commandmocker.Parameters(tmpdir), gocheck.DeepEquals, expected)
}
Example #26
0
func (s *S) TestExecuteCommandOneUnit(c *C) {
	var buf bytes.Buffer
	tmpdir, err := commandmocker.Add("juju", "$*")
	c.Assert(err, IsNil)
	defer commandmocker.Remove(tmpdir)
	app := NewFakeApp("almah", "static", 1)
	p := JujuProvisioner{}
	err = p.ExecuteCommand(&buf, &buf, app, "ls", "-lh")
	c.Assert(err, IsNil)
	output := "ssh -o StrictHostKeyChecking no -q 1 ls -lh"
	c.Assert(commandmocker.Ran(tmpdir), Equals, true)
	c.Assert(commandmocker.Output(tmpdir), Equals, output)
	c.Assert(buf.String(), Equals, output+"\n")
}
Example #27
0
func (s *S) TestZookeeperHealerHeal(c *gocheck.C) {
	ln, err := net.Listen("tcp", ":2181")
	c.Assert(err, gocheck.IsNil)
	defer ln.Close()
	go func() {
		conn, _ := ln.Accept()
		fmt.Fprintln(conn, "notok")
		conn.Close()
	}()
	p := JujuProvisioner{}
	m := machine{
		AgentState:    "not-started",
		IpAddress:     "localhost",
		InstanceId:    "i-00000376",
		InstanceState: "running",
	}
	p.saveBootstrapMachine(m)
	conn, collection := p.bootstrapCollection()
	defer conn.Close()
	defer collection.Remove(m)
	sshTmpdir, err := commandmocker.Add("ssh", "$*")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(sshTmpdir)
	sshOutput := []string{
		"-o",
		"StrictHostKeyChecking no",
		"-q",
		"-l",
		"ubuntu",
		"localhost",
		"sudo",
		"stop",
		"zookeeper",
		"-o",
		"StrictHostKeyChecking no",
		"-q",
		"-l",
		"ubuntu",
		"localhost",
		"sudo",
		"start",
		"zookeeper",
	}
	h := zookeeperHealer{}
	err = h.Heal()
	c.Assert(err, gocheck.IsNil)
	c.Assert(commandmocker.Ran(sshTmpdir), gocheck.Equals, true)
	c.Assert(commandmocker.Parameters(sshTmpdir), gocheck.DeepEquals, sshOutput)
}
func (s *S) TestExecuteActionShouldNotCallSSH_ORIGINAL_COMMANDWhenRepositoryDoesNotExists(c *gocheck.C) {
	dir, err := commandmocker.Add("git-receive-pack", "$*")
	c.Check(err, gocheck.IsNil)
	defer commandmocker.Remove(dir)
	os.Args = []string{"gandalf", s.user.Name}
	os.Setenv("SSH_ORIGINAL_COMMAND", "git-receive-pack 'ghostapp.git'")
	defer func() {
		os.Args = []string{}
		os.Setenv("SSH_ORIGINAL_COMMAND", "")
	}()
	stdout := &bytes.Buffer{}
	errorMsg := "You don't have access to write in this repository."
	executeAction(hasWritePermission, errorMsg, stdout)
	c.Assert(commandmocker.Ran(dir), gocheck.Equals, false)
}
Example #29
0
func (s *S) TestProvisionerRestart(c *gocheck.C) {
	var p LocalProvisioner
	tmpdir, err := commandmocker.Add("ssh", "ok")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	app := testing.NewFakeApp("almah", "static", 1)
	err = p.Restart(app)
	c.Assert(err, gocheck.IsNil)
	ip := app.ProvisionUnits()[0].GetIp()
	expected := []string{
		"-l", "ubuntu", "-q", "-o", "StrictHostKeyChecking no", ip, "/var/lib/tsuru/hooks/restart",
	}
	c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true)
	c.Assert(commandmocker.Parameters(tmpdir), gocheck.DeepEquals, expected)
}
Example #30
0
func (s *S) TestInstaceUnitHealWhenEverythingIsOk(c *gocheck.C) {
	a := []app.App{
		{Name: "as_i_rise", Units: []app.Unit{{Name: "as_i_rise/0", State: "started", Ip: "server-1081.novalocal"}}},
		{Name: "the_infanta", Units: []app.Unit{{Name: "the_infanta/0", State: "started", Ip: "server-1086.novalocal"}}},
	}
	err := s.conn.Apps().Insert(&a)
	c.Assert(err, gocheck.IsNil)
	defer s.conn.Apps().RemoveAll(bson.M{"name": bson.M{"$in": []string{"as_i_rise", "the_infanta"}}})
	sshTmpdir, err := commandmocker.Add("ssh", "$*")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(sshTmpdir)
	h := instanceUnitHealer{}
	err = h.Heal()
	c.Assert(commandmocker.Ran(sshTmpdir), gocheck.Equals, false)
}