Esempio n. 1
0
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
}
Esempio n. 2
0
func (s *S) TestPushImage(c *gocheck.C) {
	var request *http.Request
	server, err := dtesting.NewServer(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 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()
		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) 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$")
}
Esempio n. 4
0
func (s *S) TestContainerHostPortNotFound(c *gocheck.C) {
	inspectOut := `{
	"NetworkSettings": {
		"IpAddress": "10.10.10.10",
		"IpPrefixLen": 8,
		"Gateway": "10.65.41.1",
		"PortMapping": {
			"8889": "59322"
		}
	}
}`
	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(
		cluster.Node{ID: "server", Address: server.URL},
	)
	c.Assert(err, gocheck.IsNil)
	defer func() {
		dCluster = oldCluster
	}()
	container := container{ID: "c-01", Port: "8888"}
	port, err := container.hostPort()
	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")
}
Esempio n. 5
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("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(nil)
	c.Assert(err, gocheck.IsNil)
	dCluster, _ = cluster.New(nil,
		cluster.Node{ID: "server", Address: s.server.URL()},
	)
}
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")
}
Esempio n. 7
0
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)
}
Esempio n. 8
0
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))
}
Esempio n. 9
0
func (s *HandlersSuite) TestListContainersHandler(c *gocheck.C) {
	var result []container
	coll := collection()
	dCluster, _ = cluster.New(segScheduler, nil)
	err := coll.Insert(container{ID: "blabla", Type: "python", HostAddr: "http://cittavld1182.globoi.com"})
	c.Assert(err, gocheck.IsNil)
	defer coll.Remove(bson.M{"id": "blabla"})
	err = coll.Insert(container{ID: "bleble", Type: "java", HostAddr: "http://cittavld1182.globoi.com"})
	c.Assert(err, gocheck.IsNil)
	defer coll.Remove(bson.M{"id": "bleble"})
	req, err := http.NewRequest("GET", "/node/cittavld1182.globoi.com/containers?:address=http://cittavld1182.globoi.com", nil)
	rec := httptest.NewRecorder()
	err = listContainersHandler(rec, req, nil)
	c.Assert(err, gocheck.IsNil)
	body, err := ioutil.ReadAll(rec.Body)
	c.Assert(err, gocheck.IsNil)
	err = json.Unmarshal(body, &result)
	c.Assert(err, gocheck.IsNil)
	c.Assert(result[0].ID, gocheck.DeepEquals, "blabla")
	c.Assert(result[0].Type, gocheck.DeepEquals, "python")
	c.Assert(result[0].HostAddr, gocheck.DeepEquals, "http://cittavld1182.globoi.com")
	c.Assert(result[1].ID, gocheck.DeepEquals, "bleble")
	c.Assert(result[1].Type, gocheck.DeepEquals, "java")
	c.Assert(result[1].HostAddr, gocheck.DeepEquals, "http://cittavld1182.globoi.com")
}
Esempio n. 10
0
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$")
}
Esempio n. 11
0
func (s *S) SetUpTest(c *gocheck.C) {
	var err error
	cmutex.Lock()
	defer cmutex.Unlock()
	dCluster, err = cluster.New(nil, &mapStorage{},
		cluster.Node{ID: "server", Address: s.server.URL()},
	)
	c.Assert(err, gocheck.IsNil)
}
Esempio n. 12
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()
		dCluster, err = cluster.New(nil, &mapStorage{}, nodes...)
		c.Assert(err, gocheck.IsNil)
	}()
	cluster := dockerCluster()
	c.Assert(cluster, gocheck.DeepEquals, expected)
}
Esempio n. 13
0
func (s *HandlersSuite) TestAddNodeHandler(c *gocheck.C) {
	dCluster, _ = cluster.New(segScheduler, nil)
	b := bytes.NewBufferString(`{"address": "host.com:4243", "ID": "server01", "teams": "myteam"}`)
	req, err := http.NewRequest("POST", "/node/add", b)
	c.Assert(err, gocheck.IsNil)
	rec := httptest.NewRecorder()
	err = addNodeHandler(rec, req)
	c.Assert(err, gocheck.IsNil)
	defer s.conn.Collection(schedulerCollection).RemoveId("server01")
	n, err := s.conn.Collection(schedulerCollection).FindId("server01").Count()
	c.Assert(err, gocheck.IsNil)
	c.Assert(n, gocheck.Equals, 1)
}
Esempio n. 14
0
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
}
Esempio n. 15
0
func (s *HealerSuite) SetUpSuite(c *gocheck.C) {
	var err error
	var server *httptest.Server
	s.healer = &ContainerHealer{}
	s.cleanup, server = startDockerTestServer("4567", &s.calls)
	storage := mapStorage{}
	storage.StoreContainer("8dfafdbc3a40", "server")
	storage.StoreContainer("dca19cd9bb9e", "server")
	storage.StoreContainer("3fd99cd9bb84", "server")
	cmutex.Lock()
	defer cmutex.Unlock()
	dCluster, err = cluster.New(nil, &storage,
		cluster.Node{ID: "server", Address: server.URL},
	)
	c.Assert(err, gocheck.IsNil)
}
Esempio n. 16
0
func (s *S) TestDockerClusterSegregated(c *gocheck.C) {
	config.Set("docker:segregate", true)
	defer config.Unset("docker:segregate")
	expected, _ := cluster.New(segScheduler, nil)
	oldDockerCluster := dCluster
	cmutex.Lock()
	dCluster = nil
	cmutex.Unlock()
	defer func() {
		cmutex.Lock()
		defer cmutex.Unlock()
		dCluster = oldDockerCluster
	}()
	cluster := dockerCluster()
	c.Assert(cluster, gocheck.DeepEquals, expected)
}
Esempio n. 17
0
func (s *S) TestCollectStatusFixContainer(c *gocheck.C) {
	coll := collection()
	defer coll.Close()
	err := coll.Insert(
		container{
			ID:       "9930c24f1c4x",
			AppName:  "makea",
			Type:     "python",
			Status:   "running",
			IP:       "127.0.0.4",
			HostPort: "9025",
			HostAddr: "127.0.0.1",
		},
	)
	c.Assert(err, gocheck.IsNil)
	defer coll.RemoveAll(bson.M{"appname": "makea"})
	expected := []provision.Unit{
		{
			Name:    "9930c24f1c4x",
			AppName: "makea",
			Type:    "python",
			Machine: 0,
			Ip:      "127.0.0.1",
			Status:  provision.StatusUnreachable,
		},
	}
	cleanup, server := startDocker()
	defer cleanup()
	var storage mapStorage
	storage.StoreContainer("9930c24f1c4x", "server0")
	cmutex.Lock()
	dCluster, err = cluster.New(nil, &storage,
		cluster.Node{ID: "server0", Address: server.URL},
	)
	cmutex.Unlock()
	c.Assert(err, gocheck.IsNil)
	var p dockerProvisioner
	units, err := p.CollectStatus()
	c.Assert(err, gocheck.IsNil)
	sortUnits(units)
	sortUnits(expected)
	c.Assert(units, gocheck.DeepEquals, expected)
	cont, err := getContainer("9930c24f1c4x")
	c.Assert(err, gocheck.IsNil)
	c.Assert(cont.IP, gocheck.Equals, "127.0.0.9")
	c.Assert(cont.HostPort, gocheck.Equals, "9999")
}
Esempio n. 18
0
func (s *S) TestDockerCluster(c *gocheck.C) {
	config.Set("docker:servers", []string{"http://localhost:4243", "http://10.10.10.10:4243"})
	expected, _ := cluster.New(
		cluster.Node{ID: "server0", Address: "http://localhost:4243"},
		cluster.Node{ID: "server1", Address: "http://10.10.10.10:4243"},
	)
	oldDockerCluster := dCluster
	cmutext.Lock()
	dCluster = nil
	cmutext.Unlock()
	defer func() {
		cmutext.Lock()
		defer cmutext.Unlock()
		dCluster = oldDockerCluster
	}()
	cluster := dockerCluster()
	c.Assert(cluster, gocheck.DeepEquals, expected)
}
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
}
Esempio n. 20
0
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()
	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
	}()
	err = replicateImage("tsuru/python")
	c.Assert(err, gocheck.IsNil)
	c.Assert(atomic.LoadInt32(&requests), gocheck.Equals, int32(0))
}
Esempio n. 21
0
func dockerCluster() *cluster.Cluster {
	cmutext.Lock()
	defer cmutext.Unlock()
	if dCluster == nil {
		servers, _ := config.GetList("docker:servers")
		if len(servers) < 1 {
			log.Fatal(`Tsuru is misconfigured. Setting "docker:servers" is mandatory`)
		}
		nodes := []cluster.Node{}
		for index, server := range servers {
			node := cluster.Node{
				ID:      fmt.Sprintf("server%d", index),
				Address: server,
			}
			nodes = append(nodes, node)
		}
		dCluster, _ = cluster.New(nodes...)
	}
	return dCluster
}
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))
}
Esempio n. 23
0
func (s *HandlersSuite) TestListNodeHandler(c *gocheck.C) {
	var result []node
	dCluster, _ = cluster.New(segScheduler, nil)
	err := s.conn.Collection(schedulerCollection).Insert(node{Address: "host.com:4243", ID: "server01"})
	c.Assert(err, gocheck.IsNil)
	defer s.conn.Collection(schedulerCollection).RemoveId("server01")
	err = s.conn.Collection(schedulerCollection).Insert(node{Address: "host.com:4243", ID: "server02"})
	c.Assert(err, gocheck.IsNil)
	defer s.conn.Collection(schedulerCollection).RemoveId("server02")
	req, err := http.NewRequest("GET", "/node/", nil)
	rec := httptest.NewRecorder()
	err = listNodeHandler(rec, req, nil)
	c.Assert(err, gocheck.IsNil)
	body, err := ioutil.ReadAll(rec.Body)
	c.Assert(err, gocheck.IsNil)
	err = json.Unmarshal(body, &result)
	c.Assert(err, gocheck.IsNil)
	c.Assert(result[0].ID, gocheck.Equals, "server01")
	c.Assert(result[0].Address, gocheck.DeepEquals, "host.com:4243")
	c.Assert(result[1].ID, gocheck.Equals, "server02")
	c.Assert(result[1].Address, gocheck.DeepEquals, "host.com:4243")
}
Esempio n. 24
0
func (s *S) SetUpTest(c *gocheck.C) {
	dCluster, _ = cluster.New(nil, nil,
		cluster.Node{ID: "server", Address: s.server.URL()},
	)
}
Esempio n. 25
0
func (s *FlattenSuite) setupDockerCluster() {
	node := cluster.Node{ID: "server", Address: s.server.URL}
	s.scheduler = &fakeScheduler{}
	dCluster, _ = cluster.New(s.scheduler, node)
	dCluster.SetStorage(&mapStorage{})
}
Esempio n. 26
0
func (s *S) TestCollectStatus(c *gocheck.C) {
	rtesting.FakeRouter.AddBackend("ashamed")
	defer rtesting.FakeRouter.RemoveBackend("ashamed")
	rtesting.FakeRouter.AddBackend("make-up")
	defer rtesting.FakeRouter.RemoveBackend("make-up")
	listener, err := net.Listen("tcp", "127.0.0.1:0")
	c.Assert(err, gocheck.IsNil)
	defer listener.Close()
	listenPort := strings.Split(listener.Addr().String(), ":")[1]
	var calls int
	c1Output := fmt.Sprintf(`{
	"NetworkSettings": {
		"IpAddress": "127.0.0.1",
		"IpPrefixLen": 8,
		"Gateway": "10.65.41.1",
		"PortMapping": {
			"%s": "90293"
		}
	}
}`, listenPort)
	c2Output := `{
	"NetworkSettings": {
		"IpAddress": "127.0.0.1",
		"IpPrefixLen": 8,
		"Gateway": "10.65.41.1",
		"PortMapping": {
			"8889": "90294"
		}
	}
}`
	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, "/commit") {
			w.Write([]byte(`{"Id":"i-1"}`))
		}
	}))
	defer server.Close()
	oldCluster := dockerCluster()
	dCluster, err = cluster.New(
		cluster.Node{ID: "server", Address: server.URL},
	)
	c.Assert(err, gocheck.IsNil)
	defer func() {
		dCluster = oldCluster
	}()
	err = collection().Insert(
		container{
			ID: "9930c24f1c5f", AppName: "ashamed", Type: "python",
			Port: listenPort, Status: "running", IP: "127.0.0.1",
			HostPort: "90293",
		},
		container{
			ID: "9930c24f1c4f", AppName: "make-up", Type: "python",
			Port: "8889", Status: "running", IP: "127.0.0.4",
			HostPort: "90295",
		},
		container{ID: "9930c24f1c6f", AppName: "make-up", Type: "python", Port: "9090", Status: "error"},
		container{ID: "9930c24f1c7f", AppName: "make-up", Type: "python", Port: "9090", Status: "created"},
	)
	rtesting.FakeRouter.AddRoute("ashamed", "http://"+s.hostAddr+":90293")
	rtesting.FakeRouter.AddRoute("make-up", "http://"+s.hostAddr+":90295")
	c.Assert(err, gocheck.IsNil)
	defer collection().RemoveAll(bson.M{"appname": "make-up"})
	defer collection().RemoveAll(bson.M{"appname": "ashamed"})
	psOutput := `9930c24f1c5f
9930c24f1c4f
9930c24f1c3f
9930c24f1c6f
9930c24f1c7f
`
	expected := []provision.Unit{
		{
			Name:    "9930c24f1c5f",
			AppName: "ashamed",
			Type:    "python",
			Machine: 0,
			Ip:      "127.0.0.1",
			Status:  provision.StatusStarted,
		},
		{
			Name:    "9930c24f1c4f",
			AppName: "make-up",
			Type:    "python",
			Machine: 0,
			Ip:      "127.0.0.1",
			Status:  provision.StatusInstalling,
		},
		{
			Name:    "9930c24f1c6f",
			AppName: "make-up",
			Type:    "python",
			Status:  provision.StatusError,
		},
	}
	output := map[string][][]byte{
		"ps -q": {[]byte(psOutput)},
	}
	fexec := &etesting.FakeExecutor{Output: output}
	setExecut(fexec)
	defer setExecut(nil)
	var p dockerProvisioner
	units, err := p.CollectStatus()
	c.Assert(err, gocheck.IsNil)
	sortUnits(units)
	sortUnits(expected)
	c.Assert(units, gocheck.DeepEquals, expected)
	cont, err := getContainer("9930c24f1c4f")
	c.Assert(err, gocheck.IsNil)
	c.Assert(cont.IP, gocheck.Equals, "127.0.0.1")
	c.Assert(cont.HostPort, gocheck.Equals, "90294")
	c.Assert(fexec.ExecutedCmd("ssh-keygen", []string{"-R", "127.0.0.4"}), gocheck.Equals, true)
	c.Assert(rtesting.FakeRouter.HasRoute("make-up", "http://"+s.hostAddr+":90295"), gocheck.Equals, false)
	c.Assert(rtesting.FakeRouter.HasRoute("make-up", "http://"+s.hostAddr+":90294"), gocheck.Equals, true)
	c.Assert(calls, gocheck.Equals, 4)
}
Esempio n. 27
0
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, nil,
		cluster.Node{ID: "server", Address: server.URL},
	)
	if err != nil {
		panic(err)
	}
	return func() {
		server.Close()
		dCluster = oldCluster
	}, server
}