Exemple #1
0
func (p *FakeDockerProvisioner) moveOneContainer(cont container.Container, toHost string) (container.Container, error) {
	cont, index, err := p.findContainer(cont.ID)
	if err != nil {
		return cont, err
	}
	if cont.HostAddr == toHost {
		return cont, nil
	}
	if toHost == "" {
		for host := range p.containers {
			if host != cont.HostAddr {
				toHost = host
				break
			}
		}
	}
	originHost := cont.HostAddr
	moving := ContainerMoving{
		ContainerID: cont.ID,
		HostFrom:    originHost,
		HostTo:      toHost,
	}
	p.movings = append(p.movings, moving)
	if toHost == "" {
		cont.ID += "-recreated"
		p.containers[originHost][index] = cont
		return cont, nil
	}
	cont.HostAddr = toHost
	cont.ID += "-moved"
	last := len(p.containers[originHost]) - 1
	p.containers[originHost][index] = p.containers[originHost][last]
	p.containers[originHost] = p.containers[originHost][:last]
	p.containers[cont.HostAddr] = append(p.containers[cont.HostAddr], cont)
	return cont, nil
}
func (s *S) TestUpdateContainerInDBForward(c *check.C) {
	cont := container.Container{Name: "myName"}
	coll := s.p.Collection()
	defer coll.Close()
	err := coll.Insert(cont)
	c.Assert(err, check.IsNil)
	cont.ID = "myID"
	context := action.FWContext{Previous: cont, Params: []interface{}{runContainerActionsArgs{
		provisioner: s.p,
	}}}
	r, err := updateContainerInDB.Forward(context)
	c.Assert(r, check.FitsTypeOf, container.Container{})
	retrieved, err := s.p.GetContainer(cont.ID)
	c.Assert(err, check.IsNil)
	c.Assert(retrieved.ID, check.Equals, cont.ID)
}
Exemple #3
0
func (s *S) newContainer(opts *newContainerOpts, p *dockerProvisioner) (*container.Container, error) {
	container := container.Container{
		ID:          "id",
		IP:          "10.10.10.10",
		HostPort:    "3333",
		HostAddr:    "127.0.0.1",
		ProcessName: "web",
		ExposedPort: "8888/tcp",
	}
	if p == nil {
		p = s.p
	}
	image := "tsuru/python:latest"
	var customData map[string]interface{}
	if opts != nil {
		if opts.Image != "" {
			image = opts.Image
		}
		container.Status = opts.Status
		container.AppName = opts.AppName
		container.ProcessName = opts.ProcessName
		customData = opts.ImageCustomData
		if opts.Provisioner != nil {
			p = opts.Provisioner
		}
	}
	err := s.newFakeImage(p, image, customData)
	if err != nil {
		return nil, err
	}
	if container.AppName == "" {
		container.AppName = "container"
	}
	routertest.FakeRouter.AddBackend(container.AppName)
	routertest.FakeRouter.AddRoute(container.AppName, container.Address())
	ports := map[docker.Port]struct{}{
		docker.Port(s.port + "/tcp"): {},
	}
	config := docker.Config{
		Image:        image,
		Cmd:          []string{"ps"},
		ExposedPorts: ports,
	}
	createOptions := docker.CreateContainerOptions{Config: &config}
	createOptions.Name = randomString()
	_, c, err := p.Cluster().CreateContainer(createOptions)
	if err != nil {
		return nil, err
	}
	container.ID = c.ID
	container.Image = image
	container.Name = createOptions.Name
	conn, err := db.Conn()
	if err != nil {
		return nil, err
	}
	defer conn.Close()
	err = conn.Collection(s.collName).Insert(&container)
	if err != nil {
		return nil, err
	}
	imageId, err := appCurrentImageName(container.AppName)
	if err != nil {
		return nil, err
	}
	err = s.newFakeImage(p, imageId, nil)
	if err != nil {
		return nil, err
	}
	return &container, nil
}