Ejemplo n.º 1
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")
}
Ejemplo n.º 2
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")
}
Ejemplo n.º 3
0
func (s *S) TestDeploy(c *gocheck.C) {
	tmpdir, err := commandmocker.Add("juju", "")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	config.Set("git:unit-repo", "test/dir")
	defer func() {
		config.Unset("git:unit-repo")
	}()
	app := testing.NewFakeApp("cribcaged", "python", 1)
	w := &bytes.Buffer{}
	p := JujuProvisioner{}
	err = p.Deploy(app, "f83ac40", w)
	c.Assert(err, gocheck.IsNil)
	expected := []string{"set", app.GetName(), "app-version=f83ac40"}
	c.Assert(commandmocker.Parameters(tmpdir)[:3], gocheck.DeepEquals, expected)
}
Ejemplo n.º 4
0
func (s *S) TestInstanceMachineHealWhenEverythingIsOk(c *gocheck.C) {
	fexec := &etesting.FakeExecutor{}
	setExecut(fexec)
	defer setExecut(nil)
	jujuTmpdir, err := commandmocker.Add("juju", collectOutput)
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(jujuTmpdir)
	h := instanceMachineHealer{}
	err = h.Heal()
	c.Assert(err, gocheck.IsNil)
	args := []string{
		"status", // for juju status that gets the output
	}
	c.Assert(commandmocker.Ran(jujuTmpdir), gocheck.Equals, true)
	c.Assert(commandmocker.Parameters(jujuTmpdir), gocheck.DeepEquals, args)
}
Ejemplo n.º 5
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
	opts := ExecuteOptions{
		Cmd:    "ls",
		Stdout: &b,
		Stderr: &b,
	}
	err = e.Execute(opts)
	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")
}
Ejemplo n.º 6
0
func (s *S) TestExecute(c *check.C) {
	tmpdir, err := commandmocker.Add("ls", "ok")
	c.Assert(err, check.IsNil)
	defer commandmocker.Remove(tmpdir)
	var e OsExecutor
	var b bytes.Buffer
	opts := ExecuteOptions{
		Cmd:    "ls",
		Args:   []string{"-lsa"},
		Stdout: &b,
		Stderr: &b,
	}
	err = e.Execute(opts)
	c.Assert(err, check.IsNil)
	c.Assert(commandmocker.Ran(tmpdir), check.Equals, true)
	expected := []string{"-lsa"}
	c.Assert(commandmocker.Parameters(tmpdir), check.DeepEquals, expected)
	c.Assert(b.String(), check.Equals, "ok")
}
Ejemplo n.º 7
0
// This example demonstrates the mocking of the SSH command.
func ExampleAdd() {
	msg := "HELP!"
	path, err := commandmocker.Error("ssh", msg, 1)
	if err != nil {
		panic(err)
	}
	defer commandmocker.Remove(path)
	cmd := exec.Command("ssh", "-l", "root", "127.0.0.1")
	out, err := cmd.CombinedOutput()
	fmt.Println(err)
	fmt.Printf("%s\n", out)
	fmt.Println(commandmocker.Parameters(path))
	fmt.Println(commandmocker.Ran(path))
	// Output:
	// exit status 1
	// HELP!
	// [-l root 127.0.0.1]
	// true
}
Ejemplo n.º 8
0
func (s *S) TestInstanceMachineHeal(c *gocheck.C) {
	fexec := &etesting.FakeExecutor{}
	setExecut(fexec)
	defer setExecut(nil)
	jujuTmpdir, err := commandmocker.Add("juju", collectOutputInstanceDown)
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(jujuTmpdir)
	h := instanceMachineHealer{}
	err = h.Heal()
	c.Assert(err, gocheck.IsNil)
	args := []string{
		"-o",
		"StrictHostKeyChecking no",
		"-q",
		"-l",
		"ubuntu",
		"10.10.10.163",
		"sudo",
		"stop",
		"juju-machine-agent",
	}
	c.Assert(fexec.ExecutedCmd("ssh", args), gocheck.Equals, true)
	args = []string{
		"-o",
		"StrictHostKeyChecking no",
		"-q",
		"-l",
		"ubuntu",
		"10.10.10.163",
		"sudo",
		"start",
		"juju-machine-agent",
	}
	c.Assert(fexec.ExecutedCmd("ssh", args), gocheck.Equals, true)
	args = []string{
		"status", // for juju status that gets the output
	}
	c.Assert(commandmocker.Ran(jujuTmpdir), gocheck.Equals, true)
	c.Assert(commandmocker.Parameters(jujuTmpdir), gocheck.DeepEquals, args)
}
Ejemplo n.º 9
0
func (s *S) TestAddUnits(c *gocheck.C) {
	tmpdir, err := commandmocker.Add("juju", addUnitsOutput)
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	app := testing.NewFakeApp("resist", "rush", 0)
	p := JujuProvisioner{}
	units, err := p.AddUnits(app, 4)
	c.Assert(err, gocheck.IsNil)
	c.Assert(units, gocheck.HasLen, 4)
	names := make([]string, len(units))
	for i, unit := range units {
		names[i] = unit.Name
	}
	expected := []string{"resist/3", "resist/4", "resist/5", "resist/6"}
	c.Assert(names, gocheck.DeepEquals, expected)
	args := []string{
		"add-unit", "resist", "--num-units", "4",
	}
	c.Assert(commandmocker.Parameters(tmpdir), gocheck.DeepEquals, args)
	_, err = getQueue(queueName).Get(1e6)
	c.Assert(err, gocheck.NotNil)
}
Ejemplo n.º 10
0
func (s *S) TestInstanceAgenstConfigHealerHeal(c *gocheck.C) {
	server, err := ec2test.NewServer()
	c.Assert(err, gocheck.IsNil)
	defer server.Quit()
	id := server.NewInstances(1, "small", "ami-123", ec2.InstanceState{Code: 16, Name: "running"}, nil)[0]
	p := JujuProvisioner{}
	m := machine{
		AgentState:    "not-started",
		IPAddress:     "localhost",
		InstanceID:    id,
		InstanceState: "running",
	}
	p.saveBootstrapMachine(m)
	collection := p.bootstrapCollection()
	defer collection.Close()
	defer collection.Remove(m)
	a := app.App{
		Name:  "as_i_rise",
		Units: []app.Unit{{Name: "as_i_rise/0", State: "down", Ip: "server-1081.novalocal"}},
	}
	err = s.conn.Apps().Insert(&a)
	c.Assert(err, gocheck.IsNil)
	defer s.conn.Apps().Remove(bson.M{"name": "as_i_rise"})
	sshTmpdir, err := commandmocker.Error("ssh", "", 1)
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(sshTmpdir)
	h := instanceAgentsConfigHealer{}
	auth := aws.Auth{AccessKey: "access", SecretKey: "secret"}
	region := aws.SAEast
	region.EC2Endpoint = server.URL()
	h.e = ec2.New(auth, region)
	err = h.Heal()
	c.Assert(err, gocheck.IsNil)
	sshOutput := []string{
		"-o",
		"StrictHostKeyChecking no",
		"-q",
		"-l",
		"ubuntu",
		"server-1081.novalocal",
		"grep",
		"i-0.internal.invalid",
		"/etc/init/juju-machine-agent.conf",
		"-o",
		"StrictHostKeyChecking no",
		"-q",
		"-l",
		"ubuntu",
		"server-1081.novalocal",
		"sudo",
		"sed",
		"-i",
		"'s/env JUJU_ZOOKEEPER=.*/env JUJU_ZOOKEEPER=\"i-0.internal.invalid:2181\"/g'",
		"/etc/init/juju-machine-agent.conf",
		"-o",
		"StrictHostKeyChecking no",
		"-q",
		"-l",
		"ubuntu",
		"server-1081.novalocal",
		"grep",
		"i-0.internal.invalid",
		"/etc/init/juju-as_i_rise-0.conf",
		"-o",
		"StrictHostKeyChecking no",
		"-q",
		"-l",
		"ubuntu",
		"server-1081.novalocal",
		"sudo",
		"sed",
		"-i",
		"'s/env JUJU_ZOOKEEPER=.*/env JUJU_ZOOKEEPER=\"i-0.internal.invalid:2181\"/g'",
		"/etc/init/juju-as_i_rise-0.conf",
	}
	c.Assert(commandmocker.Ran(sshTmpdir), gocheck.Equals, true)
	c.Assert(commandmocker.Parameters(sshTmpdir), gocheck.DeepEquals, sshOutput)
}