Esempio n. 1
0
func TestRemoveTask(t *testing.T) {
	s := newAgent("SID", nil)

	assert.Empty(t, s.tasks)

	t1, err := task.NewTask(cluster.BuildContainerConfig(dockerclient.ContainerConfig{}), "task1", 5*time.Second)
	assert.NoError(t, err)
	s.addTask(t1)

	t2, err := task.NewTask(cluster.BuildContainerConfig(dockerclient.ContainerConfig{}), "task1", 5*time.Second)
	assert.NoError(t, err)
	s.addTask(t2)
	assert.Equal(t, len(s.tasks), 2)
	assert.Equal(t, len(s.getTasks()), 2)

	assert.True(t, s.removeTask(t1.TaskId.GetValue()))
	assert.Equal(t, len(s.tasks), 1)
	assert.Equal(t, len(s.getTasks()), 1)

	assert.False(t, s.removeTask(t1.TaskId.GetValue()))
}
Esempio n. 2
0
func TestAddTask(t *testing.T) {
	s := newAgent("SID", nil)

	assert.Empty(t, s.tasks)
	assert.True(t, s.empty())

	t1, err := task.NewTask(cluster.BuildContainerConfig(containertypes.Config{}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), "task1", 5*time.Second)
	assert.NoError(t, err)
	s.addTask(t1)

	t2, err := task.NewTask(cluster.BuildContainerConfig(containertypes.Config{}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), "task1", 5*time.Second)
	assert.NoError(t, err)
	s.addTask(t2)
	assert.Equal(t, len(s.tasks), 2)
	assert.Equal(t, len(s.getTasks()), 2)
	assert.False(t, s.empty())

	s.addTask(t1)
	assert.Equal(t, len(s.tasks), 2)
	assert.Equal(t, len(s.getTasks()), 2)
}
Esempio n. 3
0
// CreateContainer for container creation in Mesos task
func (c *Cluster) CreateContainer(config *cluster.ContainerConfig, name string, authConfig *dockerclient.AuthConfig) (*cluster.Container, error) {
	if config.Memory == 0 && config.CpuShares == 0 {
		return nil, errResourcesNeeded
	}

	task, err := task.NewTask(config, name, c.taskCreationTimeout)
	if err != nil {
		return nil, err
	}

	go c.pendingTasks.Add(task)

	select {
	case container := <-task.GetContainer():
		return formatContainer(container), nil
	case err := <-task.Error:
		c.pendingTasks.Remove(task)
		return nil, err
	}
}
Esempio n. 4
0
// CreateContainer for container creation in Mesos task
func (c *Cluster) CreateContainer(config *cluster.ContainerConfig, name string, authConfig *types.AuthConfig) (*cluster.Container, error) {
	if config.HostConfig.Memory == 0 && config.HostConfig.CPUShares == 0 {
		return nil, errResourcesNeeded
	}

	if !c.checkNameUniqueness(name) {
		return nil, fmt.Errorf("Conflict: The name %s is already assigned or in pending tasks. You have to delete (or rename) that container to be able to assign %s to a container again.", name, name)
	}

	task, err := task.NewTask(config, name, c.taskCreationTimeout)
	if err != nil {
		return nil, err
	}

	go c.pendingTasks.Add(task)

	select {
	case container := <-task.GetContainer():
		return formatContainer(container), nil
	case err := <-task.Error:
		c.pendingTasks.Remove(task)
		return nil, err
	}
}