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$")
}
Example #2
0
func (s *S) TestContainerNetworkInfo(c *check.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()
	var storage cluster.MapStorage
	storage.StoreContainer("c-01", server.URL)
	p, err := newFakeDockerProvisioner(server.URL)
	c.Assert(err, check.IsNil)
	p.cluster, err = cluster.New(nil, &storage,
		cluster.Node{Address: server.URL},
	)
	c.Assert(err, check.IsNil)
	container := Container{ID: "c-01"}
	info, err := container.NetworkInfo(p)
	c.Assert(err, check.IsNil)
	c.Assert(info.IP, check.Equals, "10.10.10.10")
	c.Assert(info.HTTPHostPort, check.Equals, "")
}
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()
	var storage cluster.MapStorage
	storage.StoreContainer("c-01", server.URL)
	oldCluster := dockerCluster()
	var err error
	dCluster, err = cluster.New(nil, &storage,
		cluster.Node{Address: server.URL},
	)
	c.Assert(err, gocheck.IsNil)
	defer func() {
		dCluster = oldCluster
	}()
	container := container{ID: "c-01"}
	info, err := container.networkInfo()
	c.Assert(info.IP, gocheck.Equals, "10.10.10.10")
	c.Assert(info.SSHHostPort, gocheck.Equals, "")
	c.Assert(info.HTTPHostPort, gocheck.Equals, "")
	c.Assert(err, gocheck.NotNil)
	c.Assert(err.Error(), gocheck.Equals, "Container port 8888 is not mapped to any host port")
}
func (s *HandlersSuite) TestFixContainerHandler(c *gocheck.C) {
	coll := collection()
	defer coll.Close()
	err := coll.Insert(
		container{
			ID:       "9930c24f1c4x",
			AppName:  "makea",
			Type:     "python",
			Status:   provision.StatusStarted.String(),
			IP:       "127.0.0.4",
			HostPort: "9025",
			HostAddr: "127.0.0.1",
		},
	)
	c.Assert(err, gocheck.IsNil)
	defer coll.RemoveAll(bson.M{"appname": "makea"})
	cleanup, server := startDocker()
	defer cleanup()
	var storage cluster.MapStorage
	storage.StoreContainer("9930c24f1c4x", server.URL)
	cmutex.Lock()
	dCluster, err = cluster.New(nil, &storage,
		cluster.Node{Address: server.URL},
	)
	cmutex.Unlock()
	request, err := http.NewRequest("POST", "/fix-containers", nil)
	c.Assert(err, gocheck.IsNil)
	recorder := httptest.NewRecorder()
	err = fixContainersHandler(recorder, request, nil)
	c.Assert(err, gocheck.IsNil)
	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")
}
func (s *S) TestProvisionerPlatformAdd(c *gocheck.C) {
	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()
	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()
		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")
}
Example #6
0
func (s *S) TestFixContainersEmptyPortDoesNothing(c *check.C) {
	cleanup, server, p := startDocker("")
	defer cleanup()
	coll := p.collection()
	defer coll.Close()
	err := coll.Insert(
		container{
			ID:       "9930c24f1c4x",
			AppName:  "makea",
			Type:     "python",
			Status:   provision.StatusStarted.String(),
			IP:       "",
			HostPort: "",
			HostAddr: "127.0.0.1",
		},
	)
	c.Assert(err, check.IsNil)
	defer coll.RemoveAll(bson.M{"appname": "makea"})
	var storage cluster.MapStorage
	storage.StoreContainer("9930c24f1c4x", server.URL)
	p.cluster, err = cluster.New(nil, &storage,
		cluster.Node{Address: server.URL},
	)
	c.Assert(err, check.IsNil)
	err = p.fixContainers()
	c.Assert(err, check.IsNil)
	cont, err := p.getContainer("9930c24f1c4x")
	c.Assert(err, check.IsNil)
	c.Assert(cont.IP, check.Equals, "")
	c.Assert(cont.HostPort, check.Equals, "")
}
Example #7
0
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)
}
Example #8
0
func (s *S) TestCheckContainer(c *check.C) {
	cleanup, server, p := startDocker("9999")
	defer cleanup()
	coll := p.Collection()
	defer coll.Close()
	cont := container.Container{
		ID:       "9930c24f1c4x",
		AppName:  "makea",
		Type:     "python",
		Status:   provision.StatusStarted.String(),
		IP:       "127.0.0.9",
		HostPort: "9999",
		HostAddr: "127.0.0.1",
	}
	err := coll.Insert(cont)
	c.Assert(err, check.IsNil)
	defer coll.RemoveAll(bson.M{"appname": cont.AppName})
	var storage cluster.MapStorage
	storage.StoreContainer(cont.ID, server.URL)
	p.cluster, err = cluster.New(nil, &storage,
		cluster.Node{Address: server.URL},
	)
	c.Assert(err, check.IsNil)
	err = p.checkContainer(&cont)
	c.Assert(err, check.IsNil)
}
Example #9
0
func (s *HandlersSuite) TestFixContainerHandler(c *check.C) {
	queue.ResetQueue()
	cleanup, server, p := startDocker("9999")
	defer cleanup()
	coll := p.collection()
	defer coll.Close()
	conn, err := db.Conn()
	c.Assert(err, check.IsNil)
	defer conn.Close()
	err = conn.Apps().Insert(&app.App{Name: "makea"})
	c.Assert(err, check.IsNil)
	defer conn.Apps().RemoveAll(bson.M{"name": "makea"})
	err = coll.Insert(
		container{
			ID:       "9930c24f1c4x",
			AppName:  "makea",
			Type:     "python",
			Status:   provision.StatusStarted.String(),
			IP:       "127.0.0.4",
			HostPort: "9025",
			HostAddr: "127.0.0.1",
		},
	)
	c.Assert(err, check.IsNil)
	defer coll.RemoveAll(bson.M{"appname": "makea"})
	var storage cluster.MapStorage
	storage.StoreContainer("9930c24f1c4x", server.URL)
	mainDockerProvisioner = p
	mainDockerProvisioner.cluster, err = cluster.New(nil, &storage,
		cluster.Node{Address: server.URL},
	)
	c.Assert(err, check.IsNil)
	request, err := http.NewRequest("POST", "/fix-containers", nil)
	c.Assert(err, check.IsNil)
	recorder := httptest.NewRecorder()
	err = fixContainersHandler(recorder, request, nil)
	c.Assert(err, check.IsNil)
	cont, err := p.getContainer("9930c24f1c4x")
	c.Assert(err, check.IsNil)
	c.Assert(cont.IP, check.Equals, "127.0.0.9")
	c.Assert(cont.HostPort, check.Equals, "9999")
}
Example #10
0
func (s *S) TestFixContainer(c *check.C) {
	cleanup, server, p := startDocker("9999")
	defer cleanup()
	coll := p.Collection()
	defer coll.Close()
	cont := container.Container{
		ID:       "9930c24f1c4x",
		AppName:  "makea",
		Type:     "python",
		Status:   provision.StatusStarted.String(),
		IP:       "127.0.0.4",
		HostPort: "9025",
		HostAddr: "127.0.0.1",
	}
	err := coll.Insert(cont)
	c.Assert(err, check.IsNil)
	defer coll.RemoveAll(bson.M{"appname": cont.AppName})
	err = s.storage.Apps().Insert(&app.App{Name: cont.AppName})
	c.Assert(err, check.IsNil)
	appInstance := provisiontest.NewFakeApp(cont.AppName, cont.Type, 0)
	defer p.Destroy(appInstance)
	p.Provision(appInstance)
	var storage cluster.MapStorage
	storage.StoreContainer(cont.ID, server.URL)
	p.cluster, err = cluster.New(nil, &storage,
		cluster.Node{Address: server.URL},
	)
	c.Assert(err, check.IsNil)
	info, err := cont.NetworkInfo(p)
	c.Assert(err, check.IsNil)
	err = p.fixContainer(&cont, info)
	c.Assert(err, check.IsNil)
	conta, err := p.GetContainer("9930c24f1c4x")
	c.Assert(err, check.IsNil)
	c.Assert(conta.IP, check.Equals, "127.0.0.9")
	c.Assert(conta.HostPort, check.Equals, "9999")
}