Search in sources :

Example 1 with InstanceSelectionStrategy

use of com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.InstanceSelectionStrategy in project gocd-ecs-elastic-agent by gocd.

the class TaskHelperTest method shouldCreateTaskAndScaleUpIfNoMatchingContainerInstanceFound.

@Test
void shouldCreateTaskAndScaleUpIfNoMatchingContainerInstanceFound() throws Exception {
    final InstanceSelectionStrategy instanceSelectionStrategy = mock(InstanceSelectionStrategy.class);
    final TaskDefinition taskDefinition = new TaskDefinition().withTaskDefinitionArn("task-definition-arn");
    final Task task = new Task().withTaskArn("task-arn").withTaskDefinitionArn(taskDefinition.getTaskDefinitionArn());
    final ContainerInstance containerInstance = new ContainerInstance().withContainerInstanceArn("container-instance-arn");
    RegisterTaskDefinitionRequest registerTaskDefinitionRequest = new RegisterTaskDefinitionRequest();
    when(pluginSettings.efsDnsOrIP()).thenReturn("efs-dns-name");
    when(elasticAgentProfileProperties.getImage()).thenReturn("alpine");
    when(ecsClient.registerTaskDefinition(any(RegisterTaskDefinitionRequest.class))).thenReturn(new RegisterTaskDefinitionResult().withTaskDefinition(taskDefinition));
    when(registerTaskDefinitionRequestBuilder.build(eq(pluginSettings), eq(elasticAgentProfileProperties), any(ContainerDefinition.class))).thenReturn(registerTaskDefinitionRequest);
    when(containerInstanceHelper.startOrCreateOneInstance(pluginSettings, elasticAgentProfileProperties, consoleLogAppender)).thenReturn(Optional.of(containerInstance));
    when(ecsClient.startTask(any(StartTaskRequest.class))).thenReturn(new StartTaskResult().withTasks(task));
    when(instanceSelectionStrategyFactory.strategyFor(any())).thenReturn(instanceSelectionStrategy);
    when(instanceSelectionStrategy.instanceForScheduling(eq(pluginSettings), eq(elasticAgentProfileProperties), any(ContainerDefinition.class))).thenReturn(Optional.empty());
    final Optional<ECSTask> ecsTask = taskHelper.create(createAgentRequest, pluginSettings, consoleLogAppender);
    assertThat(ecsTask.isPresent()).isTrue();
    assertThat(ecsTask.get().taskArn()).isEqualTo(task.getTaskArn());
    assertThat(ecsTask.get().taskDefinition()).isEqualTo(taskDefinition);
    verify(containerInstanceHelper).startOrCreateOneInstance(pluginSettings, elasticAgentProfileProperties, consoleLogAppender);
    verifyNoMoreInteractions(containerInstanceHelper);
    InOrder inOrder = inOrder(consoleLogAppender);
    inOrder.verify(consoleLogAppender, times(1)).accept("No running instance(s) found to build the ECS Task to perform current job.");
    inOrder.verify(consoleLogAppender, times(1)).accept("Registering ECS Task definition with cluster...");
    inOrder.verify(consoleLogAppender, times(1)).accept("Done registering ECS Task definition with cluster.");
    inOrder.verify(consoleLogAppender, times(1)).accept("Starting ECS Task to perform current job...");
    inOrder.verify(consoleLogAppender, times(1)).accept(String.format("ECS Task %s scheduled on container instance %s.", registerTaskDefinitionRequest.getFamily(), containerInstance.getEc2InstanceId()));
    verifyNoMoreInteractions(consoleLogAppender);
}
Also used : ECSTask(com.thoughtworks.gocd.elasticagent.ecs.ECSTask) ECSTask(com.thoughtworks.gocd.elasticagent.ecs.ECSTask) InOrder(org.mockito.InOrder) InstanceSelectionStrategy(com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.InstanceSelectionStrategy) Test(org.junit.jupiter.api.Test)

Example 2 with InstanceSelectionStrategy

use of com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.InstanceSelectionStrategy in project gocd-ecs-elastic-agent by gocd.

the class TaskHelperTest method shouldScaleUpASpotInstance_IfNotMatchingContainerInstanceFound_And_IfProfileRequiresASpotInstance.

@Test
void shouldScaleUpASpotInstance_IfNotMatchingContainerInstanceFound_And_IfProfileRequiresASpotInstance() throws LimitExceededException, ContainerFailedToRegisterException {
    final InstanceSelectionStrategy instanceSelectionStrategy = mock(InstanceSelectionStrategy.class);
    final TaskDefinition taskDefinition = new TaskDefinition().withTaskDefinitionArn("task-definition-arn");
    final Task task = new Task().withTaskArn("task-arn").withTaskDefinitionArn(taskDefinition.getTaskDefinitionArn());
    final ContainerInstance containerInstance = new ContainerInstance().withContainerInstanceArn("container-instance-arn");
    RegisterTaskDefinitionRequest registerTaskDefinitionRequest = new RegisterTaskDefinitionRequest();
    when(pluginSettings.efsDnsOrIP()).thenReturn("efs-dns-name");
    when(elasticAgentProfileProperties.getImage()).thenReturn("alpine");
    when(elasticAgentProfileProperties.runAsSpotInstance()).thenReturn(true);
    when(ecsClient.registerTaskDefinition(any(RegisterTaskDefinitionRequest.class))).thenReturn(new RegisterTaskDefinitionResult().withTaskDefinition(taskDefinition));
    when(registerTaskDefinitionRequestBuilder.build(eq(pluginSettings), eq(elasticAgentProfileProperties), any(ContainerDefinition.class))).thenReturn(registerTaskDefinitionRequest);
    when(containerInstanceHelper.startOrCreateOneInstance(pluginSettings, elasticAgentProfileProperties, consoleLogAppender)).thenReturn(Optional.of(containerInstance));
    when(ecsClient.startTask(any(StartTaskRequest.class))).thenReturn(new StartTaskResult().withTasks(task));
    when(instanceSelectionStrategyFactory.strategyFor(any())).thenReturn(instanceSelectionStrategy);
    when(instanceSelectionStrategy.instanceForScheduling(eq(pluginSettings), eq(elasticAgentProfileProperties), any(ContainerDefinition.class))).thenReturn(Optional.empty());
    final Optional<ECSTask> ecsTask = taskHelper.create(createAgentRequest, pluginSettings, consoleLogAppender);
    assertThat(ecsTask.isPresent()).isFalse();
    verify(spotInstanceService).create(pluginSettings, elasticAgentProfileProperties, consoleLogAppender);
    verifyNoMoreInteractions(containerInstanceHelper);
    InOrder inOrder = inOrder(consoleLogAppender);
    inOrder.verify(consoleLogAppender, times(1)).accept("No running instance(s) found to build the ECS Task to perform current job.");
    verifyNoMoreInteractions(consoleLogAppender);
}
Also used : ECSTask(com.thoughtworks.gocd.elasticagent.ecs.ECSTask) ECSTask(com.thoughtworks.gocd.elasticagent.ecs.ECSTask) InOrder(org.mockito.InOrder) InstanceSelectionStrategy(com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.InstanceSelectionStrategy) Test(org.junit.jupiter.api.Test)

Example 3 with InstanceSelectionStrategy

use of com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.InstanceSelectionStrategy in project gocd-ecs-elastic-agent by gocd.

the class TaskHelperTest method shouldCreateTaskForCreateAgentRequest.

@Test
void shouldCreateTaskForCreateAgentRequest() throws Exception {
    final InstanceSelectionStrategy instanceSelectionStrategy = mock(InstanceSelectionStrategy.class);
    final TaskDefinition taskDefinition = new TaskDefinition().withTaskDefinitionArn("task-definition-arn");
    final Task task = new Task().withTaskArn("task-arn").withTaskDefinitionArn(taskDefinition.getTaskDefinitionArn());
    final ContainerInstance containerInstance = new ContainerInstance().withContainerInstanceArn("container-instance-arn");
    final ArgumentCaptor<StartTaskRequest> startTaskRequestArgumentCaptor = ArgumentCaptor.forClass(StartTaskRequest.class);
    RegisterTaskDefinitionRequest registerTaskDefinitionRequest = new RegisterTaskDefinitionRequest();
    when(pluginSettings.efsDnsOrIP()).thenReturn("efs-dns-name");
    when(elasticAgentProfileProperties.isMountDockerSocket()).thenReturn(true);
    when(elasticAgentProfileProperties.getImage()).thenReturn("alpine");
    when(registerTaskDefinitionRequestBuilder.build(eq(pluginSettings), eq(elasticAgentProfileProperties), any(ContainerDefinition.class))).thenReturn(registerTaskDefinitionRequest);
    when(ecsClient.registerTaskDefinition(any())).thenReturn(new RegisterTaskDefinitionResult().withTaskDefinition(taskDefinition));
    when(ecsClient.startTask(startTaskRequestArgumentCaptor.capture())).thenReturn(new StartTaskResult().withTasks(task));
    when(instanceSelectionStrategyFactory.strategyFor(any())).thenReturn(instanceSelectionStrategy);
    when(instanceSelectionStrategy.instanceForScheduling(eq(pluginSettings), eq(elasticAgentProfileProperties), any(ContainerDefinition.class))).thenReturn(Optional.of(containerInstance));
    when(elasticAgentProfileProperties.runAsSpotInstance()).thenReturn(false);
    final Optional<ECSTask> ecsTask = taskHelper.create(createAgentRequest, pluginSettings, consoleLogAppender);
    assertThat(ecsTask.isPresent()).isTrue();
    assertThat(ecsTask.get().taskArn()).isEqualTo(task.getTaskArn());
    assertThat(ecsTask.get().taskDefinition()).isEqualTo(taskDefinition);
    final StartTaskRequest startTaskRequest = startTaskRequestArgumentCaptor.getValue();
    assertThat(startTaskRequest.getCluster()).isEqualTo(pluginSettings.getClusterName());
    assertThat(startTaskRequest.getTaskDefinition()).isEqualTo(taskDefinition.getTaskDefinitionArn());
    assertThat(startTaskRequest.getContainerInstances()).contains(containerInstance.getContainerInstanceArn());
    InOrder inOrder = inOrder(consoleLogAppender);
    inOrder.verify(consoleLogAppender, times(1)).accept("Found existing running container instance platform matching ECS Task instance configuration. Not starting a new EC2 instance...");
    inOrder.verify(consoleLogAppender, times(1)).accept("Registering ECS Task definition with cluster...");
    inOrder.verify(consoleLogAppender, times(1)).accept("Done registering ECS Task definition with cluster.");
    inOrder.verify(consoleLogAppender, times(1)).accept("Starting ECS Task to perform current job...");
    inOrder.verify(consoleLogAppender, times(1)).accept(String.format("ECS Task %s scheduled on container instance %s.", registerTaskDefinitionRequest.getFamily(), containerInstance.getEc2InstanceId()));
    verifyNoMoreInteractions(consoleLogAppender);
}
Also used : ECSTask(com.thoughtworks.gocd.elasticagent.ecs.ECSTask) ECSTask(com.thoughtworks.gocd.elasticagent.ecs.ECSTask) InOrder(org.mockito.InOrder) InstanceSelectionStrategy(com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.InstanceSelectionStrategy) Test(org.junit.jupiter.api.Test)

Example 4 with InstanceSelectionStrategy

use of com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.InstanceSelectionStrategy in project gocd-ecs-elastic-agent by gocd.

the class TaskHelperTest method shouldDeregisterTaskAndErrorOutIfFailsToCreateTask.

@Test
void shouldDeregisterTaskAndErrorOutIfFailsToCreateTask() throws Exception {
    final InstanceSelectionStrategy instanceSelectionStrategy = mock(InstanceSelectionStrategy.class);
    when(elasticAgentProfileProperties.getImage()).thenReturn("alpine");
    when(ecsClient.registerTaskDefinition(any(RegisterTaskDefinitionRequest.class))).thenReturn(new RegisterTaskDefinitionResult().withTaskDefinition(new TaskDefinition().withTaskDefinitionArn("task-definition-arn")));
    when(registerTaskDefinitionRequestBuilder.build(eq(pluginSettings), eq(elasticAgentProfileProperties), any(ContainerDefinition.class))).thenReturn(new RegisterTaskDefinitionRequest());
    when(instanceSelectionStrategyFactory.strategyFor(any())).thenReturn(instanceSelectionStrategy);
    when(instanceSelectionStrategy.instanceForScheduling(eq(pluginSettings), eq(elasticAgentProfileProperties), any(ContainerDefinition.class))).thenReturn(Optional.of(new ContainerInstance().withContainerInstanceArn("container-instance-arn")));
    when(ecsClient.startTask(any(StartTaskRequest.class))).thenReturn(new StartTaskResult().withFailures(new Failure().withReason("Failed to start task.").withArn("task-arn")));
    try {
        taskHelper.create(createAgentRequest, pluginSettings, consoleLogAppender);
        fail("Should fail");
    } catch (ContainerFailedToRegisterException e) {
        assertThat(e.getMessage()).contains("Failed to start task.");
    }
    verify(ecsClient).deregisterTaskDefinition(any(DeregisterTaskDefinitionRequest.class));
}
Also used : ContainerFailedToRegisterException(com.thoughtworks.gocd.elasticagent.ecs.exceptions.ContainerFailedToRegisterException) InstanceSelectionStrategy(com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.InstanceSelectionStrategy) Test(org.junit.jupiter.api.Test)

Aggregations

InstanceSelectionStrategy (com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.InstanceSelectionStrategy)4 Test (org.junit.jupiter.api.Test)4 ECSTask (com.thoughtworks.gocd.elasticagent.ecs.ECSTask)3 InOrder (org.mockito.InOrder)3 ContainerFailedToRegisterException (com.thoughtworks.gocd.elasticagent.ecs.exceptions.ContainerFailedToRegisterException)1