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) }
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) }
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) }
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") }
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) }
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") }
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) 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) 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") }
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) }
func (s *S) TestNewBareShouldPassTemplateOptionWhenItExistsOnConfig(c *C) { bareTempl, err := config.GetString("git:bare:template") c.Assert(err, IsNil) bareLocation, err := config.GetString("git:bare:location") c.Assert(err, IsNil) barePath := path.Join(bareLocation, "foo.git") dir, err := commandmocker.Add("git", "$*") c.Assert(err, IsNil) defer commandmocker.Remove(dir) err = newBare("foo") c.Assert(err, IsNil) c.Assert(commandmocker.Ran(dir), Equals, true) expected := fmt.Sprintf("init %s --bare --template=%s", barePath, bareTempl) c.Assert(commandmocker.Output(dir), Equals, expected) }
func (s *S) TestRemoveBackend(c *gocheck.C) { tmpdir, err := commandmocker.Add("sudo", "$*") c.Assert(err, gocheck.IsNil) defer commandmocker.Remove(tmpdir) config.Set("nginx:routes-path", "testdata") rfs := &testing.RecordingFs{} fsystem = rfs defer func() { fsystem = nil }() var r NginxRouter err = r.RemoveBackend("name") c.Assert(err, gocheck.IsNil) c.Assert(rfs.HasAction("remove testdata/name"), gocheck.Equals, true) c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true) expected := "service nginx restart" c.Assert(commandmocker.Output(tmpdir), gocheck.Equals, expected) }
func (s *S) TestProvisionerDestroy(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) err = p.Provision(app) time.Sleep(5 * time.Second) c.Assert(err, gocheck.IsNil) c.Assert(p.Destroy(app), gocheck.IsNil) time.Sleep(5 * time.Second) 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" expected += "lxc-stop -n myapp" expected += "lxc-destroy -n myapp" c.Assert(commandmocker.Output(tmpdir), gocheck.Equals, expected) length, err := p.collection().Find(bson.M{"name": "myapp"}).Count() c.Assert(err, gocheck.IsNil) c.Assert(length, gocheck.Equals, 0) }
func (s *S) TestExecuteCommand(c *C) { var buf bytes.Buffer tmpdir, err := commandmocker.Add("juju", "$*") c.Assert(err, IsNil) defer commandmocker.Remove(tmpdir) app := NewFakeApp("almah", "static", 2) p := JujuProvisioner{} err = p.ExecuteCommand(&buf, &buf, app, "ls", "-lh") c.Assert(err, IsNil) bufOutput := `Output from unit "almah/0": ssh -o StrictHostKeyChecking no -q 1 ls -lh Output from unit "almah/1": ssh -o StrictHostKeyChecking no -q 2 ls -lh ` cmdOutput := "ssh -o StrictHostKeyChecking no -q 1 ls -lhssh -o StrictHostKeyChecking no -q 2 ls -lh" c.Assert(commandmocker.Ran(tmpdir), Equals, true) c.Assert(commandmocker.Output(tmpdir), Equals, cmdOutput) c.Assert(buf.String(), Equals, bufOutput) }