Search in sources :

Example 1 with Agents

use of com.thoughtworks.gocd.elasticagent.ecs.Agents in project gocd-ecs-elastic-agent by gocd.

the class JobCompletionRequestExecutorTest method shouldTerminateElasticAgentOnJobCompletion.

@Test
public void shouldTerminateElasticAgentOnJobCompletion() throws Exception {
    JobIdentifier jobIdentifier = new JobIdentifier("test", 1L, "test", "test_stage", "1", "test_job", 100L);
    String elasticAgentId = "agent-1";
    ClusterProfileProperties clusterProfileProperties = new ClusterProfileProperties();
    JobCompletionRequest request = new JobCompletionRequest(elasticAgentId, jobIdentifier, new ElasticAgentProfileProperties(), clusterProfileProperties);
    JobCompletionRequestExecutor executor = new JobCompletionRequestExecutor(request, mockAgentInstances, mockPluginRequest);
    Agents agents = new Agents();
    agents.add(new Agent(elasticAgentId));
    when(mockPluginRequest.listAgents()).thenReturn(agents);
    GoPluginApiResponse response = executor.execute();
    InOrder inOrder = inOrder(mockPluginRequest, mockAgentInstances);
    inOrder.verify(mockPluginRequest).disableAgents(agentsArgumentCaptor.capture());
    inOrder.verify(mockAgentInstances).terminate(elasticAgentId, clusterProfileProperties);
    inOrder.verify(mockPluginRequest).deleteAgents(agentsArgumentCaptor.capture());
    List<Agent> agentsToDisabled = agentsArgumentCaptor.getValue();
    assertThat(1).isEqualTo(agentsToDisabled.size());
    assertThat(elasticAgentId).isEqualTo(agentsToDisabled.get(0).elasticAgentId());
    List<Agent> agentsToDelete = agentsArgumentCaptor.getValue();
    assertThat(agentsToDisabled).isEqualTo(agentsToDelete);
    assertThat(200).isEqualTo(response.responseCode());
    assertThat(response.responseBody().isEmpty()).isTrue();
}
Also used : ClusterProfileProperties(com.thoughtworks.gocd.elasticagent.ecs.domain.ClusterProfileProperties) Agent(com.thoughtworks.gocd.elasticagent.ecs.domain.Agent) InOrder(org.mockito.InOrder) Agents(com.thoughtworks.gocd.elasticagent.ecs.Agents) JobCompletionRequest(com.thoughtworks.gocd.elasticagent.ecs.requests.JobCompletionRequest) JobIdentifier(com.thoughtworks.gocd.elasticagent.ecs.domain.JobIdentifier) GoPluginApiResponse(com.thoughtworks.go.plugin.api.response.GoPluginApiResponse) ElasticAgentProfileProperties(com.thoughtworks.gocd.elasticagent.ecs.domain.ElasticAgentProfileProperties) Test(org.junit.jupiter.api.Test)

Example 2 with Agents

use of com.thoughtworks.gocd.elasticagent.ecs.Agents in project gocd-ecs-elastic-agent by gocd.

the class JobCompletionRequestExecutorTest method shouldSkipTerminatingANonExistingAgent.

@Test
void shouldSkipTerminatingANonExistingAgent() throws Exception {
    JobIdentifier jobIdentifier = new JobIdentifier("test", 1L, "test", "test_stage", "1", "test_job", 100L);
    String elasticAgentId = "agent-1";
    JobCompletionRequest request = new JobCompletionRequest(elasticAgentId, jobIdentifier, new ElasticAgentProfileProperties(), new ClusterProfileProperties());
    JobCompletionRequestExecutor executor = new JobCompletionRequestExecutor(request, mockAgentInstances, mockPluginRequest);
    when(mockPluginRequest.listAgents()).thenReturn(new Agents());
    GoPluginApiResponse response = executor.execute();
    verify(mockPluginRequest, never()).disableAgents(anyCollection());
    verify(mockPluginRequest, never()).deleteAgents(anyCollection());
    verifyNoInteractions(mockAgentInstances);
    assertThat(200).isEqualTo(response.responseCode());
}
Also used : ClusterProfileProperties(com.thoughtworks.gocd.elasticagent.ecs.domain.ClusterProfileProperties) Agents(com.thoughtworks.gocd.elasticagent.ecs.Agents) JobCompletionRequest(com.thoughtworks.gocd.elasticagent.ecs.requests.JobCompletionRequest) JobIdentifier(com.thoughtworks.gocd.elasticagent.ecs.domain.JobIdentifier) GoPluginApiResponse(com.thoughtworks.go.plugin.api.response.GoPluginApiResponse) ElasticAgentProfileProperties(com.thoughtworks.gocd.elasticagent.ecs.domain.ElasticAgentProfileProperties) Test(org.junit.jupiter.api.Test)

Example 3 with Agents

use of com.thoughtworks.gocd.elasticagent.ecs.Agents in project gocd-ecs-elastic-agent by gocd.

the class JobCompletionRequestExecutor method execute.

@Override
public GoPluginApiResponse execute() throws Exception {
    PluginSettings clusterProfileProperties = jobCompletionRequest.clusterProfileProperties();
    String elasticAgentId = jobCompletionRequest.getElasticAgentId();
    Agents agents = pluginRequest.listAgents();
    if (!agents.agentIds().contains(elasticAgentId)) {
        LOG.debug("[Job Completion] Skipping request to delete agent with id '{}' as the agent does not exist on the server.", elasticAgentId);
        return DefaultGoPluginApiResponse.success("");
    }
    Agent agent = new Agent(elasticAgentId);
    LOG.debug("[Job Completion] Disabling elastic agent with id {} on job completion {}.", agent.elasticAgentId(), jobCompletionRequest.jobIdentifier());
    pluginRequest.disableAgents(Collections.singletonList(agent));
    LOG.debug("[Job Completion] Terminating elastic agent with id {} on job completion {}.", agent.elasticAgentId(), jobCompletionRequest.jobIdentifier());
    agentInstances.terminate(agent.elasticAgentId(), clusterProfileProperties);
    LOG.debug("[Job Completion] Deleting elastic agent with id {} on job completion {}.", agent.elasticAgentId(), jobCompletionRequest.jobIdentifier());
    pluginRequest.deleteAgents(Collections.singletonList(agent));
    return DefaultGoPluginApiResponse.success("");
}
Also used : Agent(com.thoughtworks.gocd.elasticagent.ecs.domain.Agent) PluginSettings(com.thoughtworks.gocd.elasticagent.ecs.domain.PluginSettings) Agents(com.thoughtworks.gocd.elasticagent.ecs.Agents)

Aggregations

Agents (com.thoughtworks.gocd.elasticagent.ecs.Agents)3 GoPluginApiResponse (com.thoughtworks.go.plugin.api.response.GoPluginApiResponse)2 Agent (com.thoughtworks.gocd.elasticagent.ecs.domain.Agent)2 ClusterProfileProperties (com.thoughtworks.gocd.elasticagent.ecs.domain.ClusterProfileProperties)2 ElasticAgentProfileProperties (com.thoughtworks.gocd.elasticagent.ecs.domain.ElasticAgentProfileProperties)2 JobIdentifier (com.thoughtworks.gocd.elasticagent.ecs.domain.JobIdentifier)2 JobCompletionRequest (com.thoughtworks.gocd.elasticagent.ecs.requests.JobCompletionRequest)2 Test (org.junit.jupiter.api.Test)2 PluginSettings (com.thoughtworks.gocd.elasticagent.ecs.domain.PluginSettings)1 InOrder (org.mockito.InOrder)1