Search in sources :

Example 6 with JobStatusMessage

use of com.thoughtworks.go.server.messaging.JobStatusMessage in project gocd by gocd.

the class BuildAssignmentServiceTest method shouldFailTheJobWhenSecretResolutionErrorOccursForElasticConfiguration.

@Test
void shouldFailTheJobWhenSecretResolutionErrorOccursForElasticConfiguration() throws IOException, IllegalArtifactLocationException {
    PipelineConfig pipelineWithElasticJob = PipelineConfigMother.pipelineWithElasticJob(elasticProfileId1);
    JobPlan jobPlan = new InstanceFactory().createJobPlan(pipelineWithElasticJob.first().getJobs().first(), schedulingContext);
    jobPlans.add(jobPlan);
    JobInstance jobInstance = mock(JobInstance.class);
    doThrow(new SecretResolutionFailureException("some secret resolution related failure message")).when(elasticAgentPluginService).shouldAssignWork(elasticAgentInstance.elasticAgentMetadata(), null, jobPlan.getElasticProfile(), jobPlan.getClusterProfile(), jobPlan.getIdentifier());
    when(jobInstance.getState()).thenReturn(JobState.Scheduled);
    when(jobInstanceService.buildById(anyLong())).thenReturn(jobInstance);
    buildAssignmentService.onTimer();
    assertThatCode(() -> {
        JobPlan matchingJob = buildAssignmentService.findMatchingJob(elasticAgentInstance);
        assertThat(matchingJob).isNull();
        assertThat(buildAssignmentService.jobPlans()).containsExactly(jobPlan);
    }).doesNotThrowAnyException();
    InOrder inOrder = inOrder(jobInstanceService, scheduleService, consoleService, jobStatusTopic);
    inOrder.verify(jobInstanceService).buildById(jobPlan.getJobId());
    inOrder.verify(consoleService).appendToConsoleLog(jobPlan.getIdentifier(), "\nThis job was failed by GoCD. This job is configured to run on an elastic agent, there were errors while resolving secrets for the the associated elastic configurations.\nReasons: some secret resolution related failure message");
    inOrder.verify(scheduleService).failJob(jobInstance);
    inOrder.verify(jobStatusTopic).post(new JobStatusMessage(jobPlan.getIdentifier(), JobState.Scheduled, elasticAgentInstance.getUuid()));
}
Also used : InOrder(org.mockito.InOrder) JobStatusMessage(com.thoughtworks.go.server.messaging.JobStatusMessage) SecretResolutionFailureException(com.thoughtworks.go.plugin.access.exceptions.SecretResolutionFailureException) Test(org.junit.jupiter.api.Test)

Example 7 with JobStatusMessage

use of com.thoughtworks.go.server.messaging.JobStatusMessage in project gocd by gocd.

the class BuildRepositoryRemoteImplTest method shouldReportCurrentStatus.

@Test
void shouldReportCurrentStatus() throws Exception {
    JobIdentifier jobId = new JobIdentifier(new StageIdentifier("pipelineName", 1, "stageName", "1"), "job");
    buildRepository.reportCurrentStatus(info, jobId, JobState.Building);
    verify(agentService).updateRuntimeInfo(info);
    verify(repositoryService).updateStatusFromAgent(jobId, JobState.Building, info.getUUId());
    verify(jobStatusTopic).post(new JobStatusMessage(jobId, JobState.Building, info.getUUId()));
    assertThat(logFixture.getRawMessages()).contains(String.format("[%s] is reporting status [%s] for [%s]", info.agentInfoDebugString(), JobState.Building, jobId.toFullString()));
}
Also used : JobStatusMessage(com.thoughtworks.go.server.messaging.JobStatusMessage) Test(org.junit.jupiter.api.Test)

Example 8 with JobStatusMessage

use of com.thoughtworks.go.server.messaging.JobStatusMessage in project gocd by gocd.

the class ScheduleServiceIntegrationTest method shouldSendCreateAgentRequestToPluginForTheJobRequiringElasticAgentWhichHasBeenRescheduled.

@Test
public // This test is written to verify fix for github issue https://github.com/gocd/gocd/issues/6615
void shouldSendCreateAgentRequestToPluginForTheJobRequiringElasticAgentWhichHasBeenRescheduled() {
    String pluginId = "ecs";
    ClusterProfile clusterProfile = new ClusterProfile("cluster_profile", pluginId);
    ElasticProfile elasticAgentProfile = new ElasticProfile("elastic_agent_profile", "cluster_profile");
    String ephemeralAutoRegisterKey = "auto_registry_key";
    // Mock elastic agent extension
    ElasticAgentPluginRegistry elasticAgentPluginRegistry = mock(ElasticAgentPluginRegistry.class);
    when(elasticAgentPluginRegistry.shouldAssignWork(any(), any(), any(), any(), any(), any())).thenReturn(true);
    when(elasticAgentPluginRegistry.has(any())).thenReturn(true);
    when(ephemeralAutoRegisterKeyService.autoRegisterKey()).thenReturn(ephemeralAutoRegisterKey);
    elasticAgentPluginService.setElasticAgentPluginRegistry(elasticAgentPluginRegistry);
    elasticAgentPluginService.setEphemeralAutoRegisterKeyService(ephemeralAutoRegisterKeyService);
    // Mock CreateAgentQueueHandler to verify create agent call was sent to the plugin
    CreateAgentQueueHandler createAgentQueueHandler = mock(CreateAgentQueueHandler.class);
    elasticAgentPluginService.setCreateAgentQueue(createAgentQueueHandler);
    // add a cluster profile and elastic agent profile to the config.
    GoConfigDao goConfigDao = configHelper.getGoConfigDao();
    goConfigDao.loadForEditing().getElasticConfig().getClusterProfiles().add(clusterProfile);
    goConfigDao.loadForEditing().getElasticConfig().getProfiles().add(elasticAgentProfile);
    // create 2 elastic agents for ecs plugin
    Agent agent = AgentMother.elasticAgent();
    agent.setElasticAgentId("elastic-agent-id-1");
    agent.setElasticPluginId(pluginId);
    Agent agentConfig2 = AgentMother.elasticAgent();
    agentConfig2.setElasticAgentId("elastic-agent-id-2");
    agentConfig2.setElasticPluginId(pluginId);
    dbHelper.addAgent(agent);
    dbHelper.addAgent(agentConfig2);
    // define a job in the config requiring elastic agent
    PipelineConfig pipelineToBeAdded = PipelineConfigMother.createPipelineConfigWithStages(UUID.randomUUID().toString(), "s1");
    pipelineToBeAdded.first().getJobs().first().setElasticProfileId("elastic_agent_profile");
    PipelineConfig pipelineConfig = configHelper.addPipeline(pipelineToBeAdded);
    // trigger the pipeline
    Pipeline pipeline = dbHelper.schedulePipeline(pipelineConfig, forceBuild(pipelineConfig), "Bob", new TimeProvider(), Collections.singletonMap("elastic_agent_profile", elasticAgentProfile), Collections.singletonMap("cluster_profile", clusterProfile));
    buildAssignmentService.onTimer();
    // verify no agents are building
    AgentInstance agent1 = agentService.findAgent(agent.getUuid());
    assertFalse(agent1.isBuilding());
    AgentInstance agent2 = agentService.findAgent(agentConfig2.getUuid());
    assertFalse(agent2.isBuilding());
    // assign the current job to elastic agent 1
    Work work = buildAssignmentService.assignWorkToAgent(agent(agent));
    assertThat(work, instanceOf(BuildWork.class));
    assertTrue(agent1.isBuilding());
    // reschedule the job
    scheduleService.rescheduleJob(pipeline.getFirstStage().getFirstJob());
    // when the job is rescheduled, the current job instance is marked as rescheduled and a job with new build id is created for it.
    long existingRescheduledBuildId = pipeline.getFirstStage().getFirstJob().getId();
    // newly scheduled build will have the next counter.
    long newlyScheduledBuildId = pipeline.getFirstStage().getFirstJob().getId() + 1;
    // existing job instance, which is rescheduled
    JobInstance existingRescheduledInstance = jobInstanceService.buildById(existingRescheduledBuildId);
    // newly created job instance
    JobInstance newlyScheduledInstance = jobInstanceService.buildById(newlyScheduledBuildId);
    assertThat(existingRescheduledInstance.getState(), is(JobState.Rescheduled));
    assertThat(newlyScheduledInstance.getState(), is(JobState.Scheduled));
    // verify the newly created instance's job plan include elastic profile and cluster profile
    assertThat(jobInstanceDao.loadPlan(newlyScheduledBuildId).getClusterProfile(), is(clusterProfile));
    assertThat(jobInstanceDao.loadPlan(newlyScheduledBuildId).getElasticProfile(), is(elasticAgentProfile));
    JobPlan jobPlanOfRescheduledInstance = jobInstanceDao.loadPlan(existingRescheduledBuildId);
    // invoke jobStatusListener to simulate first agent reporting job completed scenario
    jobStatusListener.onMessage(new JobStatusMessage(jobPlanOfRescheduledInstance.getIdentifier(), JobState.Completed, jobPlanOfRescheduledInstance.getAgentUuid()));
    // verify the newly created instance's job plan include elastic profile and cluster profile even after the first agent reports job completion
    assertThat(jobInstanceDao.loadPlan(newlyScheduledBuildId).getClusterProfile(), is(clusterProfile));
    assertThat(jobInstanceDao.loadPlan(newlyScheduledBuildId).getElasticProfile(), is(elasticAgentProfile));
    elasticAgentPluginService.createAgentsFor(Collections.emptyList(), Collections.singletonList(jobInstanceDao.loadPlan(newlyScheduledBuildId)));
    // verify create agent request was sent to the plugin
    CreateAgentMessage message = new CreateAgentMessage(ephemeralAutoRegisterKey, null, elasticAgentProfile, clusterProfile, jobPlanOfRescheduledInstance.getIdentifier());
    verify(createAgentQueueHandler, times(1)).post(message, 110000L);
}
Also used : CreateAgentMessage(com.thoughtworks.go.server.messaging.elasticagents.CreateAgentMessage) TimeProvider(com.thoughtworks.go.util.TimeProvider) BuildWork(com.thoughtworks.go.remote.work.BuildWork) JobStatusMessage(com.thoughtworks.go.server.messaging.JobStatusMessage) ElasticAgentPluginRegistry(com.thoughtworks.go.plugin.access.elastic.ElasticAgentPluginRegistry) BuildWork(com.thoughtworks.go.remote.work.BuildWork) Work(com.thoughtworks.go.remote.work.Work) ElasticProfile(com.thoughtworks.go.config.elastic.ElasticProfile) CreateAgentQueueHandler(com.thoughtworks.go.server.messaging.elasticagents.CreateAgentQueueHandler) ClusterProfile(com.thoughtworks.go.config.elastic.ClusterProfile) Test(org.junit.jupiter.api.Test)

Example 9 with JobStatusMessage

use of com.thoughtworks.go.server.messaging.JobStatusMessage in project gocd by gocd.

the class ScheduleServiceIntegrationTest method shouldUseEnvVariableFromNewConfigWhenAJobIsRerunAfterChangingTheConfig.

// #6815
@Test
public void shouldUseEnvVariableFromNewConfigWhenAJobIsRerunAfterChangingTheConfig() {
    String pipelineName = "p1";
    String stage = "s1";
    String job = "j1";
    PipelineConfig pipelineConfig = configHelper.addPipeline(pipelineName, stage, job);
    configHelper.addEnvironmentVariableToPipeline(pipelineName, new EnvironmentVariablesConfig(Arrays.asList(new EnvironmentVariableConfig("K1", "V1"))));
    configHelper.addEnvironmentVariableToStage(pipelineName, stage, new EnvironmentVariablesConfig(Arrays.asList(new EnvironmentVariableConfig("K2", "V2"))));
    configHelper.addEnvironmentVariableToJob(pipelineName, stage, job, new EnvironmentVariablesConfig(Arrays.asList(new EnvironmentVariableConfig("K3", "V3"))));
    Pipeline pipeline = runAndPass(pipelineConfig, 1);
    long jobId = pipeline.getFirstStage().getFirstJob().getId();
    JobPlan jobPlan = jobInstanceDao.loadPlan(jobId);
    EnvironmentVariables variables = jobPlan.getVariables();
    assertThat(variables, hasItems(new EnvironmentVariable("K1", "V1"), new EnvironmentVariable("K2", "V2"), new EnvironmentVariable("K3", "V3")));
    Pipeline successfulPipeline = pass(pipeline);
    JobInstance jobInstance = successfulPipeline.getFirstStage().getFirstJob();
    jobStatusListener.onMessage(new JobStatusMessage(jobInstance.getIdentifier(), jobInstance.getState(), jobInstance.getAgentUuid()));
    jobPlan = jobInstanceDao.loadPlan(jobId);
    assertThat(jobPlan.getVariables().size(), is(0));
    configHelper.addEnvironmentVariableToPipeline(pipelineName, new EnvironmentVariablesConfig(Arrays.asList(new EnvironmentVariableConfig("K1_updated", "V1_updated"))));
    Stage rerunStage = scheduleService.rerunJobs(pipeline.getFirstStage(), Arrays.asList(job), new HttpOperationResult());
    assertThat(rerunStage.getFirstJob().getPlan().getVariables().size(), is(3));
    assertThat(rerunStage.getFirstJob().getPlan().getVariables(), hasItems(new EnvironmentVariable("K1_updated", "V1_updated"), new EnvironmentVariable("K2", "V2"), new EnvironmentVariable("K3", "V3")));
}
Also used : HttpOperationResult(com.thoughtworks.go.server.service.result.HttpOperationResult) JobStatusMessage(com.thoughtworks.go.server.messaging.JobStatusMessage) Test(org.junit.jupiter.api.Test)

Example 10 with JobStatusMessage

use of com.thoughtworks.go.server.messaging.JobStatusMessage in project gocd by gocd.

the class ScheduleServiceIntegrationTest method shouldUseEnvVariableFromNewConfigWhenAStageIsRerunAfterChangingTheConfig.

// #6815
@Test
public void shouldUseEnvVariableFromNewConfigWhenAStageIsRerunAfterChangingTheConfig() {
    String pipelineName = "p1";
    String stageName = "s1";
    String jobName = "j1";
    PipelineConfig pipelineConfig = configHelper.addPipeline(pipelineName, stageName, jobName);
    configHelper.addEnvironmentVariableToPipeline(pipelineName, new EnvironmentVariablesConfig(Arrays.asList(new EnvironmentVariableConfig("K1", "V1"))));
    configHelper.addEnvironmentVariableToStage(pipelineName, stageName, new EnvironmentVariablesConfig(Arrays.asList(new EnvironmentVariableConfig("K2", "V2"))));
    configHelper.addEnvironmentVariableToJob(pipelineName, stageName, jobName, new EnvironmentVariablesConfig(Arrays.asList(new EnvironmentVariableConfig("K3", "V3"))));
    Pipeline pipeline = runAndPass(pipelineConfig, 1);
    long jobId = pipeline.getFirstStage().getFirstJob().getId();
    JobPlan jobPlan = jobInstanceDao.loadPlan(jobId);
    EnvironmentVariables variables = jobPlan.getVariables();
    assertThat(variables, hasItems(new EnvironmentVariable("K1", "V1"), new EnvironmentVariable("K2", "V2"), new EnvironmentVariable("K3", "V3")));
    Pipeline successfulPipeline = pass(pipeline);
    JobInstance jobInstance = successfulPipeline.getFirstStage().getFirstJob();
    jobStatusListener.onMessage(new JobStatusMessage(jobInstance.getIdentifier(), jobInstance.getState(), jobInstance.getAgentUuid()));
    jobPlan = jobInstanceDao.loadPlan(jobId);
    assertThat(jobPlan.getVariables().size(), is(0));
    configHelper.addEnvironmentVariableToPipeline(pipelineName, new EnvironmentVariablesConfig(Arrays.asList(new EnvironmentVariableConfig("K1_updated", "V1_updated"))));
    Stage rerunStage = scheduleService.rerunStage(pipelineConfig.name().toString(), 1, stageName);
    assertThat(rerunStage.getFirstJob().getPlan().getVariables().size(), is(3));
    assertThat(rerunStage.getFirstJob().getPlan().getVariables(), hasItems(new EnvironmentVariable("K1_updated", "V1_updated"), new EnvironmentVariable("K2", "V2"), new EnvironmentVariable("K3", "V3")));
}
Also used : JobStatusMessage(com.thoughtworks.go.server.messaging.JobStatusMessage) Test(org.junit.jupiter.api.Test)

Aggregations

JobStatusMessage (com.thoughtworks.go.server.messaging.JobStatusMessage)10 Test (org.junit.jupiter.api.Test)7 ClusterProfile (com.thoughtworks.go.config.elastic.ClusterProfile)2 ElasticProfile (com.thoughtworks.go.config.elastic.ElasticProfile)2 CreateAgentMessage (com.thoughtworks.go.server.messaging.elasticagents.CreateAgentMessage)2 InOrder (org.mockito.InOrder)2 JobIdentifier (com.thoughtworks.go.domain.JobIdentifier)1 JobInstance (com.thoughtworks.go.domain.JobInstance)1 JobPlan (com.thoughtworks.go.domain.JobPlan)1 JobState (com.thoughtworks.go.domain.JobState)1 ElasticAgentPluginRegistry (com.thoughtworks.go.plugin.access.elastic.ElasticAgentPluginRegistry)1 SecretResolutionFailureException (com.thoughtworks.go.plugin.access.exceptions.SecretResolutionFailureException)1 BuildWork (com.thoughtworks.go.remote.work.BuildWork)1 Work (com.thoughtworks.go.remote.work.Work)1 RulesViolationException (com.thoughtworks.go.server.exceptions.RulesViolationException)1 CreateAgentQueueHandler (com.thoughtworks.go.server.messaging.elasticagents.CreateAgentQueueHandler)1 HttpOperationResult (com.thoughtworks.go.server.service.result.HttpOperationResult)1 TimeProvider (com.thoughtworks.go.util.TimeProvider)1