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) }
func (s *S) TestProvisionerStopSkipAlreadyStoppedContainers(c *gocheck.C) { dcli, _ := dockerClient.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) }
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) }
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) }
func (s *S) TestAddRouteForward(c *gocheck.C) { app := testing.NewFakeApp("myapp", "python", 1) rtesting.FakeRouter.AddBackend(app.GetName()) defer rtesting.FakeRouter.RemoveBackend(app.GetName()) cont := container{ID: "ble", AppName: app.GetName()} context := action.FWContext{Previous: cont} r, err := addRoute.Forward(context) c.Assert(err, gocheck.IsNil) cont = r.(container) hasRoute := rtesting.FakeRouter.HasRoute(app.GetName(), cont.getAddress()) c.Assert(hasRoute, gocheck.Equals, true) c.Assert(cont, gocheck.FitsTypeOf, container{}) }
func (s *S) TestContainerDeploy(c *gocheck.C) { h := &testing.TestHandler{} gandalfServer := testing.StartGandalfTestServer(h) defer gandalfServer.Close() go s.stopContainers(1) err := newImage("tsuru/python", s.server.URL()) c.Assert(err, gocheck.IsNil) app := testing.NewFakeApp("myapp", "python", 1) rtesting.FakeRouter.AddBackend(app.GetName()) defer rtesting.FakeRouter.RemoveBackend(app.GetName()) var buf bytes.Buffer _, err = deploy(app, "ff13e", &buf) c.Assert(err, gocheck.IsNil) }
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 := container{ID: "c-036", AppName: app.GetName(), Type: "python", IP: "10.10.10.1", HostAddr: host} err := s.conn.Collection(s.collName).Insert(container) c.Assert(err, gocheck.IsNil) defer s.conn.Collection(s.collName).Remove(bson.M{"_id": container.ID}) var stdout, stderr bytes.Buffer err = p.ExecuteCommandOnce(&stdout, &stderr, app, "ls", "-lh") c.Assert(err, gocheck.IsNil) 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) }
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) cont := container{ ID: handler.output, AppName: app.GetName(), Type: app.GetPlatform(), IP: "10.10.10.10", HostAddr: host, } err := collection().Insert(cont) c.Assert(err, gocheck.IsNil) defer collection().RemoveId(cont.ID) 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) path := "/container/10.10.10.10/cmd" c.Assert(handler.requests[0].URL.Path, gocheck.DeepEquals, path) }
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) }
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) }
func (s *S) TestCreateContainerForward(c *gocheck.C) { err := newImage("tsuru/python", s.server.URL()) c.Assert(err, gocheck.IsNil) client, err := dockerClient.NewClient(s.server.URL()) c.Assert(err, gocheck.IsNil) images, err := client.ListImages(true) c.Assert(err, gocheck.IsNil) cmds := []string{"ps", "-ef"} app := testing.NewFakeApp("myapp", "python", 1) context := action.FWContext{Params: []interface{}{app, images[0].ID, cmds}} r, err := createContainer.Forward(context) c.Assert(err, gocheck.IsNil) cont := r.(container) defer cont.remove() c.Assert(cont, gocheck.FitsTypeOf, container{}) c.Assert(cont.AppName, gocheck.Equals, app.GetName()) c.Assert(cont.Type, gocheck.Equals, app.GetPlatform()) port, err := getPort() c.Assert(err, gocheck.IsNil) c.Assert(cont.Port, gocheck.Equals, port) coll := collection() defer coll.Close() defer coll.Remove(bson.M{"id": cont.ID}) retrieved, err := getContainer(cont.ID) c.Assert(err, gocheck.IsNil) c.Assert(retrieved.ID, gocheck.Equals, cont.ID) c.Assert(retrieved.Status, gocheck.Equals, "created") }
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) }
func (s *S) TestStart(c *gocheck.C) { err := newImage("tsuru/python", s.server.URL()) c.Assert(err, gocheck.IsNil) app := testing.NewFakeApp("myapp", "python", 1) imageId := getImage(app) rtesting.FakeRouter.AddBackend(app.GetName()) defer rtesting.FakeRouter.RemoveBackend(app.GetName()) var buf bytes.Buffer cont, err := start(app, imageId, &buf) c.Assert(err, gocheck.IsNil) defer cont.remove() c.Assert(cont.ID, gocheck.Not(gocheck.Equals), "") cont2, err := getContainer(cont.ID) c.Assert(err, gocheck.IsNil) c.Assert(cont2.Image, gocheck.Equals, imageId) c.Assert(cont2.Status, gocheck.Equals, "running") }
func (s *S) TestProvisionerAddUnits(c *gocheck.C) { 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) s.conn.Collection(s.collName).Insert(container{ID: "c-89320", AppName: app.GetName(), Version: "a345fe"}) defer s.conn.Collection(s.collName).RemoveId("c-89320") units, err := p.AddUnits(app, 3) c.Assert(err, gocheck.IsNil) defer s.conn.Collection(s.collName).RemoveAll(bson.M{"appname": app.GetName()}) c.Assert(units, gocheck.HasLen, 3) count, err := s.conn.Collection(s.collName).Find(bson.M{"appname": app.GetName()}).Count() c.Assert(err, gocheck.IsNil) c.Assert(count, gocheck.Equals, 4) }
func (s *ELBSuite) TestDestroyELB(c *gocheck.C) { app := testing.NewFakeApp("blue", "who", 1) manager := ELBManager{} manager.e = s.client err := manager.Create(app) c.Assert(err, gocheck.IsNil) defer s.client.DeleteLoadBalancer(app.GetName()) // sanity defer manager.collection().Remove(bson.M{"name": app.GetName()}) // sanity err = manager.Destroy(app) c.Assert(err, gocheck.IsNil) _, err = s.client.DescribeLoadBalancers(app.GetName()) c.Assert(err, gocheck.NotNil) c.Assert(err, gocheck.ErrorMatches, `^.*\(LoadBalancerNotFound\)$`) n, err := manager.collection().Find(bson.M{"name": app.GetName()}).Count() c.Assert(err, gocheck.IsNil) c.Assert(n, gocheck.Equals, 0) }
func (s *S) TestNewContainerUndefinedUser(c *gocheck.C) { oldUser, _ := config.Get("docker:ssh:user") defer config.Set("docker:ssh:user", oldUser) config.Unset("docker:ssh:user") oldClusterNodes := clusterNodes clusterNodes = map[string]string{"server": s.server.URL()} defer func() { clusterNodes = oldClusterNodes }() err := newImage("tsuru/python", s.server.URL()) c.Assert(err, gocheck.IsNil) app := testing.NewFakeApp("app-name", "python", 1) rtesting.FakeRouter.AddBackend(app.GetName()) defer rtesting.FakeRouter.RemoveBackend(app.GetName()) cont, err := newContainer(app, getImage(app), []string{"docker", "run"}) c.Assert(err, gocheck.IsNil) defer s.removeTestContainer(&cont) dcli, _ := docker.NewClient(s.server.URL()) container, err := dcli.InspectContainer(cont.ID) c.Assert(err, gocheck.IsNil) c.Assert(container.Config.User, gocheck.Equals, "") }
func (s *ELBSuite) TestAddr(c *gocheck.C) { app := testing.NewFakeApp("enough", "who", 1) manager := ELBManager{} manager.e = s.client err := manager.Create(app) c.Assert(err, gocheck.IsNil) defer manager.Destroy(app) var lb loadBalancer err = manager.collection().Find(bson.M{"name": app.GetName()}).One(&lb) c.Assert(err, gocheck.IsNil) addr, err := manager.Addr(app) c.Assert(err, gocheck.IsNil) c.Assert(addr, gocheck.Equals, lb.DNSName) }
func (s *S) TestProvisionerAddr(c *gocheck.C) { err := newImage("tsuru/python", s.server.URL()) c.Assert(err, gocheck.IsNil) cont, err := s.newContainer() c.Assert(err, gocheck.IsNil) defer cont.remove() app := testing.NewFakeApp(cont.AppName, "python", 1) defer rtesting.FakeRouter.RemoveBackend(app.GetName()) var p dockerProvisioner addr, err := p.Addr(app) c.Assert(err, gocheck.IsNil) r, err := getRouter() c.Assert(err, gocheck.IsNil) expected, err := r.Addr(cont.AppName) c.Assert(err, gocheck.IsNil) c.Assert(addr, gocheck.Equals, expected) }
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 := dockerClient.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) }
func (s *ELBSuite) TestDeregisterUnit(c *gocheck.C) { id1 := s.server.NewInstance() defer s.server.RemoveInstance(id1) id2 := s.server.NewInstance() defer s.server.RemoveInstance(id2) unit1 := provision.Unit{InstanceId: id1} unit2 := provision.Unit{InstanceId: id2} app := testing.NewFakeApp("dirty", "who", 1) manager := ELBManager{} manager.e = s.client err := manager.Create(app) c.Assert(err, gocheck.IsNil) defer manager.Destroy(app) err = manager.Register(app, unit1, unit2) c.Assert(err, gocheck.IsNil) err = manager.Deregister(app, unit1, unit2) c.Assert(err, gocheck.IsNil) resp, err := s.client.DescribeLoadBalancers(app.GetName()) c.Assert(err, gocheck.IsNil) c.Assert(resp.LoadBalancerDescriptions, gocheck.HasLen, 1) c.Assert(resp.LoadBalancerDescriptions[0].Instances, gocheck.HasLen, 0) }
func (s *ELBSuite) TestRegisterUnit(c *gocheck.C) { id1 := s.server.NewInstance() defer s.server.RemoveInstance(id1) id2 := s.server.NewInstance() defer s.server.RemoveInstance(id2) app := testing.NewFakeApp("fooled", "who", 1) manager := ELBManager{} manager.e = s.client err := manager.Create(app) c.Assert(err, gocheck.IsNil) defer manager.Destroy(app) err = manager.Register(app, provision.Unit{InstanceId: id1}, provision.Unit{InstanceId: id2}) c.Assert(err, gocheck.IsNil) resp, err := s.client.DescribeLoadBalancers(app.GetName()) c.Assert(err, gocheck.IsNil) c.Assert(resp.LoadBalancerDescriptions, gocheck.HasLen, 1) c.Assert(resp.LoadBalancerDescriptions[0].Instances, gocheck.HasLen, 2) instances := resp.LoadBalancerDescriptions[0].Instances ids := []string{instances[0].InstanceId, instances[1].InstanceId} sort.Strings(ids) expected := []string{id1, id2} sort.Strings(expected) c.Assert(ids, gocheck.DeepEquals, expected) }
"github.com/globocom/tsuru/db" "github.com/globocom/tsuru/log" "github.com/globocom/tsuru/provision" "github.com/globocom/tsuru/queue" "labix.org/v2/mgo/bson" "net" "strings" ) var createContainer = action.Action{ Name: "create-container", Forward: func(ctx action.FWContext) (action.Result, error) { app := ctx.Params[0].(provision.App) imageId := ctx.Params[1].(string) cmds := ctx.Params[2].([]string) log.Debugf("create container for app %s, based on image %s, with cmds %s", app.GetName(), imageId, cmds) cont, err := newContainer(app, imageId, cmds) if err != nil { log.Errorf("error on create container for app %s - %s", app.GetName(), err) return nil, err } return cont, nil }, Backward: func(ctx action.BWContext) { c := ctx.FWResult.(container) dockerCluster().RemoveContainer(docker.RemoveContainerOptions{ID: c.ID}) coll := collection() defer coll.Close() coll.Remove(bson.M{"id": c.ID}) }, }