func (s *CoreSuite) TestProject_ListContainers(c *C) { mA, _ := testing.NewServer("127.0.0.1:0", nil, nil) mB, _ := testing.NewServer("127.0.0.1:0", nil, nil) envs := map[string]*Environment{ "a": &Environment{Name: "a", DockerEndPoints: []string{mA.URL()}}, "b": &Environment{Name: "b", DockerEndPoints: []string{mB.URL()}}, } p := &Project{ Name: "foo", Repository: "[email protected]:foo/bar.git", Environments: envs, } input := bytes.NewBuffer(nil) da, _ := NewDocker(envs["a"].DockerEndPoints[0], nil) da.Deploy(p, Revision{}, &Dockerfile{}, input, false) db, _ := NewDocker(envs["b"].DockerEndPoints[0], nil) db.Deploy(p, Revision{}, &Dockerfile{}, input, false) time.Sleep(1 * time.Second) l, err := p.ListContainers() c.Assert(err, HasLen, 0) c.Assert(l, HasLen, 2) c.Assert(l[0].APIContainers.Names[0], Equals, "/foo") c.Assert(l[1].APIContainers.Names[0], Equals, "/foo") }
func TestListImagesErrors(t *testing.T) { server1, err := dtesting.NewServer("127.0.0.1:0", nil, nil) if err != nil { t.Fatal(err) } defer server1.Stop() server2, err := dtesting.NewServer("127.0.0.1:0", nil, nil) if err != nil { t.Fatal(err) } defer server2.Stop() cluster, err := New(nil, &MapStorage{}, Node{Address: server1.URL()}, Node{Address: server2.URL()}, ) if err != nil { t.Fatal(err) } opts := docker.PullImageOptions{Repository: "tsuru/python1"} err = cluster.PullImage(opts, docker.AuthConfiguration{}, server1.URL()) if err != nil { t.Error(err) } opts = docker.PullImageOptions{Repository: "tsuru/python2"} err = cluster.PullImage(opts, docker.AuthConfiguration{}, server2.URL()) if err != nil { t.Error(err) } server2.PrepareFailure("list-images-error", "/images/json") defer server2.ResetFailure("list-images-error") _, err = cluster.ListImages(docker.ListImagesOptions{All: true}) if err == nil { t.Fatal("Expected error to exist, got <nil>") } }
func (s *CoreSuite) TestProject_Status(c *C) { if !*githubFlag { c.Skip("--github not provided") } mA, _ := testing.NewServer("127.0.0.1:0", nil, nil) mB, _ := testing.NewServer("127.0.0.1:0", nil, nil) envs := map[string]*Environment{ "a": &Environment{Name: "a", DockerEndPoints: []string{mA.URL()}}, "b": &Environment{Name: "b", DockerEndPoints: []string{mB.URL()}}, } p := &Project{ Name: "foo", Repository: "[email protected]:mcuadros/cli-array-editor.git", GithubToken: githubToken, Environments: envs, Dockerfile: ".gitignore", } input := bytes.NewBuffer(nil) da, _ := NewDocker(envs["a"].DockerEndPoints[0], nil) da.Deploy(p, Revision{}, &Dockerfile{}, input, false) db, _ := NewDocker(envs["b"].DockerEndPoints[0], nil) db.Deploy(p, Revision{}, &Dockerfile{}, input, false) r, err := p.Status() c.Assert(err, HasLen, 0) c.Assert(r, HasLen, 2) c.Assert(r[0].Environment, Equals, envs["a"]) c.Assert(r[0].LastRevision.GetShort(), Equals, "a44ffbb10515") c.Assert(r[0].Containers, HasLen, 1) c.Assert(r[0].RunningContainers, HasLen, 1) c.Assert(r[0].Containers[0], Equals, r[0].RunningContainers[0]) }
func (s *SchedulerSuite) TestSchedulerSchedule(c *gocheck.C) { server0, err := testing.NewServer(nil) c.Assert(err, gocheck.IsNil) defer server0.Stop() server1, err := testing.NewServer(nil) c.Assert(err, gocheck.IsNil) defer server1.Stop() server2, err := testing.NewServer(nil) c.Assert(err, gocheck.IsNil) defer server2.Stop() var buf bytes.Buffer client, _ := docker.NewClient(server0.URL()) client.PullImage(docker.PullImageOptions{Repository: "tsuru/python", OutputStream: &buf}) client, _ = docker.NewClient(server1.URL()) client.PullImage(docker.PullImageOptions{Repository: "tsuru/python", OutputStream: &buf}) client, _ = docker.NewClient(server2.URL()) client.PullImage(docker.PullImageOptions{Repository: "tsuru/python", OutputStream: &buf}) a1 := app.App{Name: "impius", Teams: []string{"tsuruteam", "nodockerforme"}} a2 := app.App{Name: "mirror", Teams: []string{"tsuruteam"}} a3 := app.App{Name: "dedication", Teams: []string{"nodockerforme"}} cont1 := container{ID: "1", Name: "impius1", AppName: a1.Name} cont2 := container{ID: "2", Name: "mirror1", AppName: a2.Name} cont3 := container{ID: "3", Name: "dedication1", AppName: a3.Name} err = s.storage.Apps().Insert(a1, a2, a3) c.Assert(err, gocheck.IsNil) defer s.storage.Apps().RemoveAll(bson.M{"name": bson.M{"$in": []string{a1.Name, a2.Name, a3.Name}}}) coll := s.storage.Collection(schedulerCollection) err = coll.Insert( node{ID: "server0", Address: server0.URL(), Teams: []string{"tsuruteam"}}, node{ID: "server1", Address: server1.URL(), Teams: []string{"tsuruteam"}}, node{ID: "server2", Address: server2.URL()}, ) c.Assert(err, gocheck.IsNil) defer coll.RemoveAll(bson.M{"_id": bson.M{"$in": []string{"server0", "server1", "server2"}}}) contColl := collection() err = contColl.Insert( cont1, cont2, cont3, ) c.Assert(err, gocheck.IsNil) defer contColl.RemoveAll(bson.M{"name": bson.M{"$in": []string{cont1.Name, cont2.Name, cont3.Name}}}) var scheduler segregatedScheduler config := docker.Config{Cmd: []string{"/usr/sbin/sshd", "-D"}, Image: "tsuru/python"} opts := docker.CreateContainerOptions{Name: cont1.Name, Config: &config} node, _, err := scheduler.Schedule(opts) c.Assert(err, gocheck.IsNil) c.Check(node, gocheck.Equals, "server1") config = docker.Config{Cmd: []string{"/usr/sbin/sshd", "-D"}, Image: "tsuru/python"} opts = docker.CreateContainerOptions{Name: cont2.Name, Config: &config} node, _, err = scheduler.Schedule(opts) c.Assert(err, gocheck.IsNil) c.Check(node == "server0" || node == "server1", gocheck.Equals, true) config = docker.Config{Cmd: []string{"/usr/sbin/sshd", "-D"}, Image: "tsuru/python"} opts = docker.CreateContainerOptions{Name: cont3.Name, Config: &config} node, _, err = scheduler.Schedule(opts) c.Assert(err, gocheck.IsNil) c.Check(node, gocheck.Equals, "server2") }
func (s *S) TestSchedulerSchedule(c *check.C) { a1 := app.App{Name: "impius", Teams: []string{"tsuruteam", "nodockerforme"}, Pool: "pool1"} a2 := app.App{Name: "mirror", Teams: []string{"tsuruteam"}, Pool: "pool1"} a3 := app.App{Name: "dedication", Teams: []string{"nodockerforme"}, Pool: "pool1"} cont1 := container.Container{ID: "1", Name: "impius1", AppName: a1.Name} cont2 := container.Container{ID: "2", Name: "mirror1", AppName: a2.Name} cont3 := container.Container{ID: "3", Name: "dedication1", AppName: a3.Name} err := s.storage.Apps().Insert(a1, a2, a3) c.Assert(err, check.IsNil) defer s.storage.Apps().RemoveAll(bson.M{"name": bson.M{"$in": []string{a1.Name, a2.Name, a3.Name}}}) p := provision.Pool{Name: "pool1", Teams: []string{ "tsuruteam", "nodockerforme", }} o := provision.AddPoolOptions{Name: p.Name} err = provision.AddPool(o) c.Assert(err, check.IsNil) err = provision.AddTeamsToPool(p.Name, p.Teams) c.Assert(err, check.IsNil) defer provision.RemovePool(p.Name) contColl := s.p.Collection() defer contColl.Close() err = contColl.Insert( cont1, cont2, cont3, ) c.Assert(err, check.IsNil) defer contColl.RemoveAll(bson.M{"name": bson.M{"$in": []string{cont1.Name, cont2.Name, cont3.Name}}}) scheduler := segregatedScheduler{provisioner: s.p} clusterInstance, err := cluster.New(&scheduler, &cluster.MapStorage{}) s.p.cluster = clusterInstance c.Assert(err, check.IsNil) server1, err := testing.NewServer("127.0.0.1:0", nil, nil) c.Assert(err, check.IsNil) defer server1.Stop() server2, err := testing.NewServer("localhost:0", nil, nil) c.Assert(err, check.IsNil) defer server2.Stop() err = clusterInstance.Register(cluster.Node{ Address: server1.URL(), Metadata: map[string]string{"pool": "pool1"}, }) c.Assert(err, check.IsNil) localURL := strings.Replace(server2.URL(), "127.0.0.1", "localhost", -1) err = clusterInstance.Register(cluster.Node{ Address: localURL, Metadata: map[string]string{"pool": "pool1"}, }) c.Assert(err, check.IsNil) opts := docker.CreateContainerOptions{Name: cont1.Name} node, err := scheduler.Schedule(clusterInstance, opts, &container.SchedulerOpts{AppName: a1.Name, ProcessName: "web"}) c.Assert(err, check.IsNil) c.Check(node.Address, check.Equals, server1.URL()) opts = docker.CreateContainerOptions{Name: cont2.Name} node, err = scheduler.Schedule(clusterInstance, opts, &container.SchedulerOpts{AppName: a2.Name, ProcessName: "web"}) c.Assert(err, check.IsNil) c.Check(node.Address, check.Equals, localURL) }
func (s *S) TestHealerHealNodeWaitAndRegisterError(c *check.C) { defer func() { machines, _ := iaas.ListMachines() for _, m := range machines { m.Destroy() } }() iaas.RegisterIaasProvider("my-healer-iaas", newHealerIaaSConstructor("127.0.0.1", nil)) _, err := iaas.CreateMachineForIaaS("my-healer-iaas", map[string]string{}) c.Assert(err, check.IsNil) iaas.RegisterIaasProvider("my-healer-iaas", newHealerIaaSConstructor("localhost", nil)) node1, err := testing.NewServer("127.0.0.1:0", nil, nil) c.Assert(err, check.IsNil) node2, err := testing.NewServer("127.0.0.1:0", nil, nil) c.Assert(err, check.IsNil) node2.PrepareFailure("ping-failure", "/_ping") config.Set("iaas:node-protocol", "http") config.Set("iaas:node-port", urlPort(node2.URL())) defer config.Unset("iaas:node-protocol") defer config.Unset("iaas:node-port") cluster, err := cluster.New(nil, &cluster.MapStorage{}, cluster.Node{Address: node1.URL()}, ) c.Assert(err, check.IsNil) node1.PrepareFailure("pingErr", "/_ping") cluster.StartActiveMonitoring(100 * time.Millisecond) time.Sleep(300 * time.Millisecond) cluster.StopActiveMonitoring() var p dockerProvisioner err = p.Initialize() c.Assert(err, check.IsNil) p.cluster = cluster healer := nodeHealer{ locks: make(map[string]*sync.Mutex), provisioner: &p, disabledTime: 0, failuresBeforeHealing: 1, waitTimeNewMachine: 1 * time.Second, } nodes, err := p.getCluster().UnfilteredNodes() c.Assert(err, check.IsNil) c.Assert(nodes, check.HasLen, 1) c.Assert(urlPort(nodes[0].Address), check.Equals, urlPort(node1.URL())) c.Assert(urlToHost(nodes[0].Address), check.Equals, "127.0.0.1") c.Assert(nodes[0].FailureCount() > 0, check.Equals, true) nodes[0].Metadata["iaas"] = "my-healer-iaas" created, err := healer.healNode(&nodes[0]) c.Assert(err, check.ErrorMatches, ".*error registering new node.*") c.Assert(created.Address, check.Equals, "") c.Assert(nodes[0].FailureCount(), check.Equals, 0) nodes, err = p.getCluster().UnfilteredNodes() c.Assert(err, check.IsNil) c.Assert(nodes, check.HasLen, 1) c.Assert(urlPort(nodes[0].Address), check.Equals, urlPort(node1.URL())) c.Assert(urlToHost(nodes[0].Address), check.Equals, "127.0.0.1") }
func (s *S) SetUpTest(c *check.C) { config.Set("docker:api-timeout", 2) iaas.ResetAll() repositorytest.Reset() queue.ResetQueue() repository.Manager().CreateUser(s.user.Email) s.p = &dockerProvisioner{storage: &cluster.MapStorage{}} err := s.p.Initialize() c.Assert(err, check.IsNil) queue.ResetQueue() s.server, err = dtesting.NewServer("127.0.0.1:0", nil, nil) c.Assert(err, check.IsNil) s.p.cluster, err = cluster.New(nil, s.p.storage, cluster.Node{Address: s.server.URL(), Metadata: map[string]string{"pool": "test-default"}}, ) c.Assert(err, check.IsNil) mainDockerProvisioner = s.p err = dbtest.ClearAllCollectionsExcept(s.storage.Apps().Database, []string{"users", "tokens", "teams"}) c.Assert(err, check.IsNil) err = clearClusterStorage(s.clusterSess) c.Assert(err, check.IsNil) routertest.FakeRouter.Reset() opts := provision.AddPoolOptions{Name: "test-default", Default: true} err = provision.AddPool(opts) c.Assert(err, check.IsNil) s.storage.Tokens().Remove(bson.M{"appname": bson.M{"$ne": ""}}) s.logBuf = safe.NewBuffer(nil) log.SetLogger(log.NewWriterLogger(s.logBuf, true)) s.token = createTokenForUser(s.user, "*", string(permission.CtxGlobal), "", c) }
func (s *S) SetUpTest(c *check.C) { iaas.ResetAll() repositorytest.Reset() queue.ResetQueue() s.p = &dockerProvisioner{storage: &cluster.MapStorage{}} err := s.p.Initialize() c.Assert(err, check.IsNil) queue.ResetQueue() app.Provisioner = s.p s.server, err = dtesting.NewServer("127.0.0.1:0", nil, nil) c.Assert(err, check.IsNil) s.p.cluster, err = cluster.New(nil, s.p.storage, cluster.Node{Address: s.server.URL(), Metadata: map[string]string{"pool": "test-fallback"}}, ) c.Assert(err, check.IsNil) mainDockerProvisioner = s.p coll := s.p.collection() defer coll.Close() err = dbtest.ClearAllCollectionsExcept(coll.Database, []string{"users", "tokens", "teams"}) c.Assert(err, check.IsNil) err = clearClusterStorage(s.clusterSess) c.Assert(err, check.IsNil) routertest.FakeRouter.Reset() opts := provision.AddPoolOptions{Name: "test-fallback"} err = provision.AddPool(opts) c.Assert(err, check.IsNil) }
func (s *S) TestReplicateImage(c *gocheck.C) { var request *http.Request var requests int32 server, err := dtesting.NewServer(func(r *http.Request) { v := atomic.AddInt32(&requests, 1) if v == 2 { request = r } }) c.Assert(err, gocheck.IsNil) defer server.Stop() config.Set("docker:registry", "localhost:3030") defer config.Unset("docker:registry") cmutex.Lock() oldDockerCluster := dCluster dCluster, _ = cluster.New(nil, storage.Redis("localhost:6379", "tests"), cluster.Node{ID: "server0", Address: server.URL()}) cmutex.Unlock() defer func() { cmutex.Lock() defer cmutex.Unlock() dCluster = oldDockerCluster }() err = newImage("localhost:3030/base", "http://index.docker.io") c.Assert(err, gocheck.IsNil) cleanup := insertImage("localhost:3030/base", "server0", c) defer cleanup() err = replicateImage("localhost:3030/base") c.Assert(err, gocheck.IsNil) c.Assert(atomic.LoadInt32(&requests), gocheck.Equals, int32(3)) c.Assert(request.URL.Path, gocheck.Matches, ".*/images/localhost:3030/base/push$") }
func (s *S) TestProvisionerPlatformRemoveReturnsStorageError(c *gocheck.C) { registryServer := httptest.NewServer(nil) u, _ := url.Parse(registryServer.URL) config.Set("docker:registry", u.Host) defer config.Unset("docker:registry") var requests []*http.Request server, err := dtesting.NewServer("127.0.0.1:0", nil, func(r *http.Request) { requests = append(requests, r) }) c.Assert(err, gocheck.IsNil) defer server.Stop() var storage cluster.MapStorage cmutex.Lock() oldDockerCluster := dCluster dCluster, _ = cluster.New(nil, &storage, cluster.Node{Address: server.URL()}) cmutex.Unlock() defer func() { cmutex.Lock() dCluster = oldDockerCluster cmutex.Unlock() }() p := dockerProvisioner{} err = p.PlatformRemove("test") c.Assert(err, gocheck.NotNil) c.Assert(err, gocheck.DeepEquals, dstorage.ErrNoSuchImage) }
func (s *S) TestProvisionerPlatformRemove(c *gocheck.C) { registryServer := httptest.NewServer(nil) u, _ := url.Parse(registryServer.URL) config.Set("docker:registry", u.Host) defer config.Unset("docker:registry") var requests []*http.Request server, err := dtesting.NewServer("127.0.0.1:0", nil, func(r *http.Request) { requests = append(requests, r) }) c.Assert(err, gocheck.IsNil) defer server.Stop() var storage cluster.MapStorage imageName := assembleImageName("test") storage.StoreImage(imageName, server.URL()) cmutex.Lock() oldDockerCluster := dCluster dCluster, _ = cluster.New(nil, &storage, cluster.Node{Address: server.URL()}) cmutex.Unlock() defer func() { cmutex.Lock() dCluster = oldDockerCluster cmutex.Unlock() }() p := dockerProvisioner{} err = p.PlatformRemove("test") c.Assert(err, gocheck.IsNil) c.Assert(strings.Contains(requests[0].URL.RequestURI(), "tsuru/test"), gocheck.Equals, true) }
func (s *S) TestEnsureContainersStartedForceStopOnlyOnFailure(c *check.C) { config.Set("docker:bs:image", "myregistry/tsuru/bs") _, err := nodecontainer.InitializeBS() c.Assert(err, check.IsNil) var reqs []*http.Request server, err := testing.NewServer("127.0.0.1:0", nil, func(r *http.Request) { reqs = append(reqs, r) }) c.Assert(err, check.IsNil) defer server.Stop() errCount := 0 server.CustomHandler("/containers/big-sibling", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { reqs = append(reqs, r) if r.Method != "DELETE" { server.DefaultHandler().ServeHTTP(w, r) return } errCount++ if errCount > 2 { server.DefaultHandler().ServeHTTP(w, r) } else { w.WriteHeader(http.StatusInternalServerError) } })) p, err := dockertest.NewFakeDockerProvisioner(server.URL()) c.Assert(err, check.IsNil) defer p.Destroy() client, err := docker.NewClient(server.URL()) c.Assert(err, check.IsNil) buf := safe.NewBuffer(nil) err = ensureContainersStarted(p, buf, true, nil) c.Assert(err, check.IsNil) reqs = nil err = ensureContainersStarted(p, buf, true, nil) c.Assert(err, check.IsNil) var paths []string for _, r := range reqs { paths = append(paths, r.Method+" "+r.URL.Path) } c.Assert(paths, check.DeepEquals, []string{ "POST /images/create", "POST /containers/create", "POST /containers/big-sibling/stop", "DELETE /containers/big-sibling", "DELETE /containers/big-sibling", "DELETE /containers/big-sibling", "POST /containers/create", "GET /version", "POST /containers/big-sibling/start", }) c.Assert(reqs[3].URL.Query().Get("force"), check.Equals, "") c.Assert(reqs[4].URL.Query().Get("force"), check.Equals, "1") c.Assert(reqs[5].URL.Query().Get("force"), check.Equals, "1") containers, err := client.ListContainers(docker.ListContainersOptions{All: true}) c.Assert(err, check.IsNil) c.Assert(containers, check.HasLen, 1) container, err := client.InspectContainer(containers[0].ID) c.Assert(err, check.IsNil) c.Assert(container.Name, check.Equals, nodecontainer.BsDefaultName) }
func (s *CoreSuite) TestProject_Deploy(c *C) { if !*githubFlag { c.Skip("--github not provided") } m, _ := testing.NewServer("127.0.0.1:0", nil, nil) e := &Environment{Name: "a", DockerEndPoints: []string{m.URL()}} p := &Project{ Repository: "[email protected]:mcuadros/dockership.git", Environments: map[string]*Environment{"foo": e}, Dockerfile: "Dockerfile", GithubToken: githubToken, TaskStatus: TaskStatus{}, } input := bytes.NewBuffer(nil) err := p.Deploy("foo", input, false) c.Assert(err, HasLen, 0) l, err := p.ListContainers() c.Assert(err, HasLen, 0) c.Assert(l, HasLen, 1) c.Assert(l[0].DockerEndPoint, Equals, e.DockerEndPoints[0]) c.Assert(string(input.Bytes()), HasLen, 51) }
func (s *SchedulerSuite) TestSchedulerScheduleFallback(c *gocheck.C) { server0, err := testing.NewServer(nil) c.Assert(err, gocheck.IsNil) defer server0.Stop() var buf bytes.Buffer client, _ := docker.NewClient(server0.URL()) client.PullImage(docker.PullImageOptions{Repository: "tsuru/python", OutputStream: &buf}) a1 := app.App{Name: "impius", Teams: []string{"tsuruteam", "nodockerforme"}} cont1 := container{ID: "1", Name: "impius1", AppName: a1.Name} err = s.storage.Apps().Insert(a1) c.Assert(err, gocheck.IsNil) defer s.storage.Apps().RemoveAll(bson.M{"name": a1.Name}) coll := s.storage.Collection(schedulerCollection) err = coll.Insert( bson.M{"_id": "server0", "address": server0.URL()}, ) c.Assert(err, gocheck.IsNil) defer coll.RemoveAll(bson.M{"_id": "server0"}) contColl := collection() err = contColl.Insert(cont1) c.Assert(err, gocheck.IsNil) defer contColl.RemoveAll(bson.M{"name": cont1.Name}) var scheduler segregatedScheduler config := docker.Config{Cmd: []string{"/usr/sbin/sshd", "-D"}, Image: "tsuru/python"} opts := docker.CreateContainerOptions{Name: cont1.Name, Config: &config} node, _, err := scheduler.Schedule(opts) c.Assert(err, gocheck.IsNil) c.Check(node, gocheck.Equals, "server0") }
func (s *S) TestHealerHealNodeWithoutIaaS(c *check.C) { node1, err := testing.NewServer("127.0.0.1:0", nil, nil) c.Assert(err, check.IsNil) cluster, err := cluster.New(nil, &cluster.MapStorage{}, cluster.Node{Address: node1.URL()}, ) c.Assert(err, check.IsNil) var p dockerProvisioner err = p.Initialize() c.Assert(err, check.IsNil) p.cluster = cluster healer := nodeHealer{ locks: make(map[string]*sync.Mutex), provisioner: &p, disabledTime: 0, failuresBeforeHealing: 1, waitTimeNewMachine: 1 * time.Second, } nodes, err := p.getCluster().UnfilteredNodes() c.Assert(err, check.IsNil) c.Assert(nodes, check.HasLen, 1) created, err := healer.healNode(&nodes[0]) c.Assert(err, check.ErrorMatches, ".*error creating new machine.*") c.Assert(created.Address, check.Equals, "") nodes, err = p.getCluster().UnfilteredNodes() c.Assert(err, check.IsNil) c.Assert(nodes, check.HasLen, 1) c.Assert(urlPort(nodes[0].Address), check.Equals, urlPort(node1.URL())) c.Assert(urlToHost(nodes[0].Address), check.Equals, "127.0.0.1") }
func (s *S) startMultipleServersClusterSeggregated() (*dockerProvisioner, error) { var err error s.extraServer, err = dtesting.NewServer("localhost:0", nil, nil) if err != nil { return nil, err } otherURL := strings.Replace(s.extraServer.URL(), "127.0.0.1", "localhost", 1) var p dockerProvisioner err = p.Initialize() if err != nil { return nil, err } opts := provision.AddPoolOptions{Name: "pool1", Public: true} err = provision.AddPool(opts) if err != nil { return nil, err } opts = provision.AddPoolOptions{Name: "pool2", Public: true} err = provision.AddPool(opts) if err != nil { return nil, err } p.storage = &cluster.MapStorage{} sched := segregatedScheduler{provisioner: &p} p.cluster, err = cluster.New(&sched, p.storage, cluster.Node{Address: s.server.URL(), Metadata: map[string]string{"pool": "pool1"}}, cluster.Node{Address: otherURL, Metadata: map[string]string{"pool": "pool2"}}, ) if err != nil { return nil, err } return &p, nil }
func (s *S) SetUpSuite(c *gocheck.C) { s.collName = "docker_unit" s.imageCollName = "docker_image" s.gitHost = "my.gandalf.com" s.repoNamespace = "tsuru" s.sshUser = "******" config.Set("git:ro-host", s.gitHost) config.Set("database:url", "127.0.0.1:27017") config.Set("database:name", "docker_provision_tests_s") config.Set("docker:repository-namespace", s.repoNamespace) config.Set("docker:router", "fake") config.Set("docker:collection", s.collName) config.Set("docker:deploy-cmd", "/var/lib/tsuru/deploy") config.Set("docker:run-cmd:bin", "/usr/local/bin/circusd /etc/circus/circus.ini") config.Set("docker:run-cmd:port", "8888") config.Set("docker:ssh:add-key-cmd", "/var/lib/tsuru/add-key") config.Set("docker:ssh:user", s.sshUser) config.Set("queue", "fake") s.deployCmd = "/var/lib/tsuru/deploy" s.runBin = "/usr/local/bin/circusd" s.runArgs = "/etc/circus/circus.ini" s.port = "8888" fsystem = &ftesting.RecordingFs{} f, err := fsystem.Create(os.ExpandEnv("${HOME}/.ssh/id_rsa.pub")) c.Assert(err, gocheck.IsNil) f.Write([]byte("key-content")) f.Close() s.server, err = dtesting.NewServer("127.0.0.1:0", nil) c.Assert(err, gocheck.IsNil) s.targetRecover = tTesting.SetTargetFile(c) s.storage, err = db.Conn() c.Assert(err, gocheck.IsNil) }
func (s *S) TestPushImage(c *gocheck.C) { var request *http.Request server, err := dtesting.NewServer("127.0.0.1:0", nil, func(r *http.Request) { request = r }) c.Assert(err, gocheck.IsNil) defer server.Stop() config.Set("docker:registry", "localhost:3030") defer config.Unset("docker:registry") var storage cluster.MapStorage storage.StoreImage("localhost:3030/base", server.URL()) cmutex.Lock() oldDockerCluster := dCluster dCluster, _ = cluster.New(nil, &storage, cluster.Node{Address: server.URL()}) cmutex.Unlock() defer func() { cmutex.Lock() defer cmutex.Unlock() dCluster = oldDockerCluster }() err = newImage("localhost:3030/base", "http://index.docker.io") c.Assert(err, gocheck.IsNil) err = pushImage("localhost:3030/base") c.Assert(err, gocheck.IsNil) c.Assert(request.URL.Path, gocheck.Matches, ".*/images/localhost:3030/base/push$") }
func (s *S) TestMigrateImagesWithoutImageInStorage(c *check.C) { var requests []*http.Request server, err := testing.NewServer("127.0.0.1:0", nil, func(r *http.Request) { requests = append(requests, r) }) c.Assert(err, check.IsNil) defer server.Stop() var p dockerProvisioner err = p.Initialize() c.Assert(err, check.IsNil) p.cluster, _ = cluster.New(nil, &cluster.MapStorage{}, cluster.Node{Address: server.URL()}) app1 := app.App{Name: "app1"} conn, err := db.Conn() c.Assert(err, check.IsNil) defer conn.Close() conn.Apps().Insert(app1) defer conn.Apps().RemoveAll(bson.M{"name": bson.M{"$in": []string{app1.Name}}}) mainDockerProvisioner = &p err = MigrateImages() c.Assert(err, check.IsNil) client, err := docker.NewClient(server.URL()) c.Assert(err, check.IsNil) images, err := client.ListImages(docker.ListImagesOptions{All: true}) c.Assert(err, check.IsNil) c.Assert(images, check.HasLen, 0) }
func (s *S) SetUpSuite(c *gocheck.C) { s.collName = "docker_unit" s.imageCollName = "docker_image" s.gitHost = "my.gandalf.com" s.repoNamespace = "tsuru" s.sshUser = "******" config.Set("git:ro-host", s.gitHost) config.Set("database:url", "127.0.0.1:27017") config.Set("database:name", "docker_provision_tests_s") config.Set("docker:repository-namespace", s.repoNamespace) config.Set("docker:router", "fake") config.Set("docker:collection", s.collName) config.Set("docker:deploy-cmd", "/var/lib/tsuru/deploy") config.Set("docker:run-cmd:bin", "/usr/local/bin/circusd /etc/circus/circus.ini") config.Set("docker:run-cmd:port", "8888") config.Set("docker:ssh:add-key-cmd", "/var/lib/tsuru/add-key") config.Set("docker:ssh:user", s.sshUser) config.Set("docker:cluster:storage", "redis") config.Set("docker:cluster:redis-prefix", "redis-scheduler-storage-test") config.Set("queue", "fake") s.deployCmd = "/var/lib/tsuru/deploy" s.runBin = "/usr/local/bin/circusd" s.runArgs = "/etc/circus/circus.ini" s.port = "8888" var err error s.server, err = dtesting.NewServer("127.0.0.1:0", nil, nil) c.Assert(err, gocheck.IsNil) s.targetRecover = tTesting.SetTargetFile(c, []byte("http://localhost")) s.storage, err = db.Conn() c.Assert(err, gocheck.IsNil) s.oldProvisioner = app.Provisioner app.Provisioner = &dockerProvisioner{} }
func (s *S) TestProvisionerPlatformAdd(c *gocheck.C) { var requests []*http.Request server, err := dtesting.NewServer("127.0.0.1:0", func(r *http.Request) { requests = append(requests, r) }) c.Assert(err, gocheck.IsNil) defer server.Stop() config.Set("docker:registry", "localhost:3030") defer config.Unset("docker:registry") var storage mapStorage storage.StoreImage("localhost:3030/base", "server0") cmutex.Lock() oldDockerCluster := dCluster dCluster, _ = cluster.New(nil, &storage, cluster.Node{ID: "server0", Address: server.URL()}) cmutex.Unlock() defer func() { cmutex.Lock() dCluster = oldDockerCluster cmutex.Unlock() }() args := make(map[string]string) args["dockerfile"] = "http://localhost/Dockerfile" p := dockerProvisioner{} err = p.PlatformAdd("test", args, bytes.NewBuffer(nil)) c.Assert(err, gocheck.IsNil) c.Assert(requests, gocheck.HasLen, 2) queryString := requests[0].URL.Query() c.Assert(queryString.Get("t"), gocheck.Equals, assembleImageName("test")) c.Assert(queryString.Get("remote"), gocheck.Equals, "http://localhost/Dockerfile") }
func (s *CoreSuite) TestDockerGroup_DeployListContainersAndListImages(c *C) { dg := &DockerGroup{dockers: make(map[string]*Docker, 0)} for i := 0; i < 5; i++ { m, _ := testing.NewServer("127.0.0.1:0", nil, nil) defer m.Stop() dg.dockers[m.URL()], _ = NewDocker(m.URL(), nil) } p := &Project{Name: "foo", Repository: "[email protected]:foo/bar.git", UseShortRevisions: true} r := Revision{"foo/bar": Commit("qux")} input := bytes.NewBuffer(nil) errors := dg.Deploy(p, r, &Dockerfile{content: []byte("")}, input, true) c.Assert(errors, HasLen, 0) c.Assert(string(input.Bytes()), HasLen, 255) containers, errors := dg.ListContainers(p) c.Assert(errors, HasLen, 0) c.Assert(containers, HasLen, 5) for _, r := range containers { c.Assert(r.Image, Equals, ImageID("foo:qux")) } images, errors := dg.ListImages(p) c.Assert(errors, HasLen, 0) c.Assert(images, HasLen, 5) for _, r := range images { c.Assert(r.RepoTags, HasLen, 2) } }
func (s *S) TestReplicateImage(c *gocheck.C) { var requests int32 server, err := dtesting.NewServer(func(*http.Request) { atomic.AddInt32(&requests, 1) }) c.Assert(err, gocheck.IsNil) defer server.Stop() config.Set("docker:registry", "http://localhost:3030") defer config.Unset("docker:registry") cmutext.Lock() oldDockerCluster := dCluster dCluster, _ = cluster.New(cluster.Node{ID: "server0", Address: server.URL()}) cmutext.Unlock() defer func() { cmutext.Lock() defer cmutext.Unlock() dCluster = oldDockerCluster }() var buf bytes.Buffer err = dCluster.PullImage(dockerClient.PullImageOptions{Repository: "base", Registry: "http://index.docker.io"}, &buf) c.Assert(err, gocheck.IsNil) err = replicateImage("base") c.Assert(err, gocheck.IsNil) c.Assert(atomic.LoadInt32(&requests), gocheck.Equals, int32(3)) }
func (s *S) TestReplicateImageWithoutRegistryInTheImageName(c *gocheck.C) { var request *http.Request var requests int32 server, err := dtesting.NewServer(func(r *http.Request) { v := atomic.AddInt32(&requests, 1) if v == 2 { request = r } }) c.Assert(err, gocheck.IsNil) defer server.Stop() config.Set("docker:registry", "localhost:3030") defer config.Unset("docker:registry") cmutext.Lock() oldDockerCluster := dCluster dCluster, _ = cluster.New(nil, cluster.Node{ID: "server0", Address: server.URL()}) cmutext.Unlock() defer func() { cmutext.Lock() defer cmutext.Unlock() dCluster = oldDockerCluster }() var buf bytes.Buffer opts := dockerClient.PullImageOptions{ Repository: "localhost:3030/base", Registry: "http://index.docker.io", } err = dCluster.PullImage(opts, &buf) c.Assert(err, gocheck.IsNil) err = replicateImage("base") c.Assert(err, gocheck.IsNil) c.Assert(atomic.LoadInt32(&requests), gocheck.Equals, int32(3)) c.Assert(request.URL.Path, gocheck.Matches, ".*/images/localhost:3030/base/push$") }
func (s *S) TestPushImage(c *check.C) { var requests []*http.Request server, err := testing.NewServer("127.0.0.1:0", nil, func(r *http.Request) { requests = append(requests, r) }) c.Assert(err, check.IsNil) defer server.Stop() config.Set("docker:registry", "localhost:3030") defer config.Unset("docker:registry") var p dockerProvisioner err = p.Initialize() c.Assert(err, check.IsNil) p.cluster, err = cluster.New(nil, &cluster.MapStorage{}, cluster.Node{Address: server.URL()}) c.Assert(err, check.IsNil) err = s.newFakeImage(&p, "localhost:3030/base/img", nil) c.Assert(err, check.IsNil) err = p.PushImage("localhost:3030/base/img", "") c.Assert(err, check.IsNil) c.Assert(requests, check.HasLen, 3) c.Assert(requests[0].URL.Path, check.Equals, "/images/create") c.Assert(requests[1].URL.Path, check.Equals, "/images/localhost:3030/base/img/json") c.Assert(requests[2].URL.Path, check.Equals, "/images/localhost:3030/base/img/push") c.Assert(requests[2].URL.RawQuery, check.Equals, "") err = s.newFakeImage(&p, "localhost:3030/base/img:v2", nil) c.Assert(err, check.IsNil) err = p.PushImage("localhost:3030/base/img", "v2") c.Assert(err, check.IsNil) c.Assert(requests, check.HasLen, 6) c.Assert(requests[3].URL.Path, check.Equals, "/images/create") c.Assert(requests[4].URL.Path, check.Equals, "/images/localhost:3030/base/img:v2/json") c.Assert(requests[5].URL.Path, check.Equals, "/images/localhost:3030/base/img/push") c.Assert(requests[5].URL.RawQuery, check.Equals, "tag=v2") }
func (s *CoreSuite) TestDocker_Run(c *C) (p *Project, m *testing.DockerServer, rev Revision) { m, _ = testing.NewServer("127.0.0.1:0", nil, nil) d, _ := docker.NewClient(m.URL()) p = &Project{Name: "foo", Repository: "[email protected]:foo/bar.git", UseShortRevisions: true} buildImage(d, "foo:qux") rev = Revision{"foo/bar": "qux"} dc, err := NewDocker(m.URL(), nil) c.Assert(err, Equals, nil) err = dc.Run(p, rev) c.Assert(err, Equals, nil) l, err := dc.ListContainers(p) c.Assert(err, Equals, nil) c.Assert(l, HasLen, 1) c.Assert(l[0].Image.IsRevision(rev), Equals, true) c.Assert(l[0].IsRunning(), Equals, true) c.Assert(l[0].Names, HasLen, 1) c.Assert(l[0].Names[0], Equals, "/foo") return }
func (s *S) TestSwarmNodeUnits(c *check.C) { srv, err := testing.NewServer("127.0.0.1:0", nil, nil) c.Assert(err, check.IsNil) defer srv.Stop() opts := provision.AddNodeOptions{Address: srv.URL()} err = s.p.AddNode(opts) c.Assert(err, check.IsNil) nodes, err := s.p.ListNodes(nil) c.Assert(err, check.IsNil) c.Assert(nodes, check.HasLen, 1) units, err := nodes[0].Units() c.Assert(err, check.IsNil) c.Assert(units, check.HasLen, 0) a := &app.App{Name: "myapp", TeamOwner: s.team.Name, Deploys: 1} err = app.CreateApp(a, s.user) c.Assert(err, check.IsNil) imgName := "myapp:v1" err = image.SaveImageCustomData(imgName, map[string]interface{}{ "processes": map[string]interface{}{ "web": "python myapp.py", }, }) c.Assert(err, check.IsNil) err = image.AppendAppImageName(a.GetName(), imgName) c.Assert(err, check.IsNil) err = s.p.AddUnits(a, 1, "web", nil) c.Assert(err, check.IsNil) units, err = nodes[0].Units() c.Assert(err, check.IsNil) c.Assert(units, check.HasLen, 1) }
func StartMultipleServersCluster() (*FakeDockerProvisioner, error) { server1, err := testing.NewServer("127.0.0.1:0", nil, nil) if err != nil { return nil, err } server2, err := testing.NewServer("localhost:0", nil, nil) if err != nil { return nil, err } otherUrl := strings.Replace(server2.URL(), "127.0.0.1", "localhost", 1) p, err := NewFakeDockerProvisioner(server1.URL(), otherUrl) if err != nil { return nil, err } p.servers = []*testing.DockerServer{server1, server2} return p, nil }
func (s *S) TestServers(c *check.C) { server, err := dtesting.NewServer("127.0.0.1:0", nil, nil) c.Assert(err, check.IsNil) defer server.Stop() var p FakeDockerProvisioner p.servers = append(p.servers, server) c.Assert(p.Servers(), check.DeepEquals, p.servers) }
func (s *S) TestRebalanceContainersByHost(c *check.C) { otherServer, err := dtesting.NewServer("localhost:0", nil, nil) c.Assert(err, check.IsNil) defer otherServer.Stop() otherUrl := strings.Replace(otherServer.URL(), "127.0.0.1", "localhost", 1) p := &dockerProvisioner{} err = p.Initialize() c.Assert(err, check.IsNil) p.storage = &cluster.MapStorage{} p.scheduler = &segregatedScheduler{provisioner: p} p.cluster, err = cluster.New(p.scheduler, p.storage, cluster.Node{Address: s.server.URL(), Metadata: map[string]string{"pool": "pool1"}}, cluster.Node{Address: otherUrl, Metadata: map[string]string{"pool": "pool1"}}, ) c.Assert(err, check.IsNil) opts := provision.AddPoolOptions{Name: "pool1"} err = provision.AddPool(opts) c.Assert(err, check.IsNil) err = provision.AddTeamsToPool("pool1", []string{"team1"}) c.Assert(err, check.IsNil) err = s.newFakeImage(p, "tsuru/app-myapp", nil) c.Assert(err, check.IsNil) appInstance := provisiontest.NewFakeApp("myapp", "python", 0) defer p.Destroy(appInstance) p.Provision(appInstance) imageId, err := appCurrentImageName(appInstance.GetName()) c.Assert(err, check.IsNil) _, err = addContainersWithHost(&changeUnitsPipelineArgs{ toHost: "localhost", toAdd: map[string]*containersToAdd{"web": {Quantity: 5}}, app: appInstance, imageId: imageId, provisioner: p, }) c.Assert(err, check.IsNil) appStruct := &app.App{ Name: appInstance.GetName(), TeamOwner: "team1", Pool: "pool1", } err = s.storage.Apps().Insert(appStruct) c.Assert(err, check.IsNil) c1, err := p.listContainersByHost("localhost") c.Assert(err, check.IsNil) c.Assert(c1, check.HasLen, 5) c2, err := p.listContainersByHost("127.0.0.1") c.Assert(err, check.IsNil) c.Assert(c2, check.HasLen, 0) err = p.Cluster().Unregister(otherUrl) c.Assert(err, check.IsNil) buf := safe.NewBuffer(nil) err = p.rebalanceContainersByHost(net.URLToHost(otherUrl), buf) c.Assert(err, check.IsNil) c.Assert(p.scheduler.ignoredContainers, check.IsNil) c2, err = p.listContainersByHost("127.0.0.1") c.Assert(err, check.IsNil) c.Assert(c2, check.HasLen, 5) }