func (s *S) SetUpTest(c *gocheck.C) { var err error dCluster, err = cluster.New(nil, storage.Redis("localhost:6379", "tests"), cluster.Node{ID: "server", Address: s.server.URL()}, ) c.Assert(err, gocheck.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 dockerCluster() *cluster.Cluster { cmutex.Lock() defer cmutex.Unlock() if dCluster == nil { if segregate, _ := config.GetBool("docker:segregate"); segregate { dCluster, _ = cluster.New(segScheduler) } else { clusterNodes = make(map[string]string) servers, _ := config.GetList("docker:servers") if len(servers) < 1 { log.Fatal(`Tsuru is misconfigured. Setting "docker:servers" is mandatory`) } nodes := make([]cluster.Node, len(servers)) for index, server := range servers { id := fmt.Sprintf("server%d", index) node := cluster.Node{ ID: id, Address: server, } nodes[index] = node clusterNodes[id] = server } dCluster, _ = cluster.New(nil, nodes...) } if redisServer, err := config.GetString("docker:scheduler:redis-server"); err == nil { prefix, _ := config.GetString("docker:scheduler:redis-prefix") if password, err := config.GetString("docker:scheduler:redis-password"); err == nil { dCluster.SetStorage(storage.AuthenticatedRedis(redisServer, password, prefix)) } else { dCluster.SetStorage(storage.Redis(redisServer, prefix)) } } } return dCluster }
func (s *S) TestContainerNetworkInfoNotFound(c *gocheck.C) { inspectOut := `{ "NetworkSettings": { "IpAddress": "10.10.10.10", "IpPrefixLen": 8, "Gateway": "10.65.41.1", "Ports": {} } }` server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if strings.Contains(r.URL.Path, "/containers/") { w.Write([]byte(inspectOut)) } })) defer server.Close() oldCluster := dockerCluster() var err error dCluster, err = cluster.New(nil, storage.Redis("localhost:6379", "tests"), cluster.Node{ID: "server", Address: server.URL}, ) c.Assert(err, gocheck.IsNil) defer func() { dCluster = oldCluster removeClusterNodes([]string{"server"}, c) }() container := container{ID: "c-01"} clean := createFakeContainers([]string{"c-01"}, c) defer clean() ip, port, err := container.networkInfo() c.Assert(ip, gocheck.Equals, "") c.Assert(port, gocheck.Equals, "") c.Assert(err, gocheck.NotNil) c.Assert(err.Error(), gocheck.Equals, "Container port 8888 is not mapped to any host port") }
func startDocker() (func(), *httptest.Server) { output := `{ "State": { "Running": true, "Pid": 2785, "ExitCode": 0, "StartedAt": "2013-08-15T03:38:45.709874216-03:00", "Ghost": false }, "Image": "b750fe79269d2ec9a3c593ef05b4332b1d1a02a62b4accb2c21d589ff2f5f2dc", "NetworkSettings": { "IpAddress": "127.0.0.9", "IpPrefixLen": 8, "Gateway": "10.65.41.1", "Ports": { "8888/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "9999" } ] } } }` server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if strings.Contains(r.URL.Path, "/containers/9930c24f1c4x") { w.Write([]byte(output)) } })) var err error oldCluster := dockerCluster() dCluster, err = cluster.New(nil, storage.Redis("localhost:6379", "tests"), cluster.Node{ID: "server", Address: server.URL}, ) if err != nil { panic(err) } return func() { server.Close() dCluster = oldCluster }, server }
func (s *S) TestReplicateImageNoRegistry(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() cmutex.Lock() oldDockerCluster := dCluster dCluster, _ = cluster.New(nil, storage.Redis("localhost:6379", "tests"), cluster.Node{ID: "server111", Address: server.URL()}) cmutex.Unlock() defer func() { cmutex.Lock() defer cmutex.Unlock() removeClusterNodes([]string{"server111"}, c) dCluster = oldDockerCluster }() err = replicateImage("tsuru/python") c.Assert(err, gocheck.IsNil) c.Assert(atomic.LoadInt32(&requests), gocheck.Equals, int32(0)) }
func (s *S) TestDockerCluster(c *gocheck.C) { config.Set("docker:servers", []string{"http://localhost:4243", "http://10.10.10.10:4243"}) defer config.Unset("docker:servers") expected, _ := cluster.New(nil, nil, cluster.Node{ID: "server0", Address: "http://localhost:4243"}, cluster.Node{ID: "server1", Address: "http://10.10.10.10:4243"}, ) nodes, err := dCluster.Nodes() c.Assert(err, gocheck.IsNil) cmutex.Lock() dCluster = nil cmutex.Unlock() defer func() { cmutex.Lock() defer cmutex.Unlock() removeClusterNodes([]string{"server0", "server1"}, c) dCluster, err = cluster.New(nil, storage.Redis("localhost:6379", "tests"), nodes...) c.Assert(err, gocheck.IsNil) }() cluster := dockerCluster() c.Assert(cluster, gocheck.DeepEquals, expected) }
func dockerCluster() *cluster.Cluster { cmutex.Lock() defer cmutex.Unlock() var clusterStorage cluster.Storage if dCluster == nil { if redisServer, err := config.GetString("docker:scheduler:redis-server"); err == nil { prefix, _ := config.GetString("docker:scheduler:redis-prefix") if password, err := config.GetString("docker:scheduler:redis-password"); err == nil { clusterStorage = storage.AuthenticatedRedis(redisServer, password, prefix) } else { clusterStorage = storage.Redis(redisServer, prefix) } } var nodes []cluster.Node if segregate, _ := config.GetBool("docker:segregate"); segregate { dCluster, _ = cluster.New(segScheduler, clusterStorage) } else { nodes = getDockerServers() dCluster, _ = cluster.New(nil, clusterStorage, nodes...) } } return dCluster }
func startDockerTestServer(containerPort string, calls *int) (func(), *httptest.Server) { listAllOutput := `[ { "Id": "8dfafdbc3a40", "Image": "base:latest", "Command": "echo 1", "Created": 1367854155, "Status": "Ghost", "Ports": null, "SizeRw":12288, "SizeRootFs": 0 }, { "Id": "dca19cd9bb9e", "Image": "tsuru/python:latest", "Command": "echo 1", "Created": 1376319760, "Status": "Exit 0", "Ports": null, "SizeRw": 0, "SizeRootFs": 0 }, { "Id": "3fd99cd9bb84", "Image": "tsuru/python:latest", "Command": "echo 1", "Created": 1376319760, "Status": "Up 7 seconds", "Ports": null, "SizeRw": 0, "SizeRootFs": 0 } ]` c1Output := fmt.Sprintf(`{ "State": { "Running": true, "Pid": 2785, "ExitCode": 0, "StartedAt": "2013-08-15T03:38:45.709874216-03:00", "Ghost": false }, "NetworkSettings": { "IpAddress": "127.0.0.4", "IpPrefixLen": 8, "Gateway": "10.65.41.1", "Ports": { "8888/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "%s" } ] } } }`, containerPort) c2Output := `{ "State": { "Running": true, "Pid": 2785, "ExitCode": 0, "StartedAt": "2013-08-15T03:38:45.709874216-03:00", "Ghost": false }, "Image": "b750fe79269d2ec9a3c593ef05b4332b1d1a02a62b4accb2c21d589ff2f5f2dc", "NetworkSettings": { "IpAddress": "127.0.0.1", "IpPrefixLen": 8, "Gateway": "10.65.41.1", "Ports": { "8889/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "9024" } ] } } }` server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { (*calls)++ if strings.Contains(r.URL.Path, "/containers/") { if strings.Contains(r.URL.Path, "/containers/9930c24f1c4f") { w.Write([]byte(c2Output)) } if strings.Contains(r.URL.Path, "/containers/9930c24f1c5f") { w.Write([]byte(c1Output)) } if strings.Contains(r.URL.Path, "/containers/json") { w.Write([]byte(listAllOutput)) } if strings.Contains(r.URL.Path, "/export") { w.Write([]byte("tar stream data")) } } if strings.Contains(r.URL.Path, "/commit") { w.Write([]byte(`{"Id":"i-1"}`)) } })) var err error oldCluster := dockerCluster() dCluster, err = cluster.New(nil, storage.Redis("localhost:6379", "tests"), cluster.Node{ID: "server", Address: server.URL}, ) if err != nil { panic(err) } return func() { server.Close() dCluster = oldCluster }, server }