Skip to content

Commit

Permalink
Pass along elastic profile and cluster profile properties to job comp…
Browse files Browse the repository at this point in the history
…letion request (gocd#5937)
  • Loading branch information
GaneshSPatil committed Mar 25, 2019
1 parent 4a338a8 commit f493bfb
Show file tree
Hide file tree
Showing 25 changed files with 183 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package com.thoughtworks.go.server.service;

import com.google.common.collect.ImmutableMap;
import com.thoughtworks.go.config.*;
import com.thoughtworks.go.config.elastic.ClusterProfile;
import com.thoughtworks.go.config.elastic.ElasticProfile;
import com.thoughtworks.go.config.exceptions.StageNotFoundException;
import com.thoughtworks.go.config.materials.MaterialConfigs;
import com.thoughtworks.go.domain.*;
Expand All @@ -43,8 +46,7 @@
import static com.thoughtworks.go.helper.ModificationsMother.modifyOneFile;
import static com.thoughtworks.go.util.DataStructureUtils.a;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -316,6 +318,35 @@ public void shouldCreateJobPlan() {
assertThat(plan, is(new DefaultJobPlan(new Resources(resourceConfigs), ArtifactPlan.toArtifactPlans(artifactConfigs), new ArrayList<>(), -1, new JobIdentifier(), null, new EnvironmentVariables(), new EnvironmentVariables(), null, null)));
}

@Test
public void shouldAddElasticProfileOnJobPlan() {
ElasticProfile elasticProfile = new ElasticProfile("id", "pluginId");
DefaultSchedulingContext context = new DefaultSchedulingContext("foo", new Agents(), ImmutableMap.of("id", elasticProfile));

ArtifactConfigs artifactConfigs = new ArtifactConfigs();
JobConfig jobConfig = new JobConfig(new CaseInsensitiveString("test"), null, artifactConfigs);
jobConfig.setElasticProfileId("id");
JobPlan plan = instanceFactory.createJobPlan(jobConfig, context);

assertThat(plan.getElasticProfile(), is(elasticProfile));
assertNull(plan.getClusterProfile());
}

@Test
public void shouldAddElasticProfileAndClusterProfileOnJobPlan() {
ElasticProfile elasticProfile = new ElasticProfile("id", "pluginId", "clusterId");
ClusterProfile clusterProfile = new ClusterProfile("clusterId", "pluginId");
DefaultSchedulingContext context = new DefaultSchedulingContext("foo", new Agents(), ImmutableMap.of("id", elasticProfile), ImmutableMap.of("clusterId", clusterProfile));

ArtifactConfigs artifactConfigs = new ArtifactConfigs();
JobConfig jobConfig = new JobConfig(new CaseInsensitiveString("test"), null, artifactConfigs);
jobConfig.setElasticProfileId("id");
JobPlan plan = instanceFactory.createJobPlan(jobConfig, context);

assertThat(plan.getElasticProfile(), is(elasticProfile));
assertThat(plan.getClusterProfile(), is(clusterProfile));
}

@Test
public void shouldReturnBuildInstance() {
ArtifactConfigs artifactConfigs = new ArtifactConfigs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.thoughtworks.go.config.Agents;
import com.thoughtworks.go.config.EnvironmentVariablesConfig;
import com.thoughtworks.go.config.ResourceConfigs;
import com.thoughtworks.go.config.elastic.ClusterProfile;
import com.thoughtworks.go.config.elastic.ElasticProfile;

import java.util.HashMap;
Expand All @@ -32,6 +33,7 @@ public class DefaultSchedulingContext implements SchedulingContext {
private final String approvedBy;
private final Agents agents;
private final Map<String, ElasticProfile> profiles;
private final Map<String, ClusterProfile> clusterProfiles;
private EnvironmentVariablesConfig variables = new EnvironmentVariablesConfig();
private boolean rerun;

Expand All @@ -48,9 +50,14 @@ public DefaultSchedulingContext(String approvedBy, Agents agents) {
}

public DefaultSchedulingContext(String approvedBy, Agents agents, Map<String, ElasticProfile> profiles) {
this(approvedBy, agents, profiles, new HashMap<>());
}

public DefaultSchedulingContext(String approvedBy, Agents agents, Map<String, ElasticProfile> profiles, Map<String, ClusterProfile> clusterProfiles) {
this.approvedBy = approvedBy;
this.agents = agents;
this.profiles = profiles;
this.clusterProfiles = clusterProfiles;
}

public String getApprovedBy() {
Expand All @@ -72,7 +79,7 @@ public EnvironmentVariablesConfig getEnvironmentVariablesConfig() {
}

public SchedulingContext overrideEnvironmentVariables(EnvironmentVariablesConfig environmentVariablesConfig) {
DefaultSchedulingContext context = new DefaultSchedulingContext(approvedBy, new Agents(agents), profiles);
DefaultSchedulingContext context = new DefaultSchedulingContext(approvedBy, new Agents(agents), profiles, clusterProfiles);
context.variables = variables.overrideWith(environmentVariablesConfig);
context.rerun = rerun;
return context;
Expand All @@ -85,7 +92,7 @@ public SchedulingContext permittedAgent(String permittedAgentUuid) {
permitted.add(agent);
}
}
DefaultSchedulingContext context = new DefaultSchedulingContext(approvedBy, permitted, profiles);
DefaultSchedulingContext context = new DefaultSchedulingContext(approvedBy, permitted, profiles, clusterProfiles);
context.variables = variables.overrideWith(new EnvironmentVariablesConfig());
context.rerun = rerun;
return context;
Expand All @@ -96,7 +103,7 @@ public boolean isRerun() {
}

public SchedulingContext rerunContext() {
DefaultSchedulingContext context = new DefaultSchedulingContext(approvedBy, agents, profiles);
DefaultSchedulingContext context = new DefaultSchedulingContext(approvedBy, agents, profiles, clusterProfiles);
context.variables = variables.overrideWith(new EnvironmentVariablesConfig());
context.rerun = true;
return context;
Expand All @@ -107,6 +114,11 @@ public ElasticProfile getElasticProfile(String profileId) {
return profiles.get(profileId);
}

@Override
public ClusterProfile getClusterProfile(String clusterProfileId) {
return clusterProfiles.get(clusterProfileId);
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.thoughtworks.go.config.AgentConfig;
import com.thoughtworks.go.config.EnvironmentVariablesConfig;
import com.thoughtworks.go.config.ResourceConfigs;
import com.thoughtworks.go.config.elastic.ClusterProfile;
import com.thoughtworks.go.config.elastic.ElasticProfile;

import java.util.Collection;
Expand All @@ -42,4 +43,6 @@ public interface SchedulingContext {
SchedulingContext rerunContext();

ElasticProfile getElasticProfile(String profileId);

ClusterProfile getClusterProfile(String clusterProfileId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

package com.thoughtworks.go.server.service;

import com.thoughtworks.go.config.*;
import com.thoughtworks.go.config.CaseInsensitiveString;
import com.thoughtworks.go.config.JobConfig;
import com.thoughtworks.go.config.PipelineConfig;
import com.thoughtworks.go.config.StageConfig;
import com.thoughtworks.go.config.elastic.ClusterProfile;
import com.thoughtworks.go.config.elastic.ElasticProfile;
import com.thoughtworks.go.config.exceptions.StageNotFoundException;
import com.thoughtworks.go.domain.*;
Expand Down Expand Up @@ -111,14 +115,23 @@ public void reallyCreateJobInstance(JobConfig config, JobInstances jobs, String
public JobPlan createJobPlan(JobConfig config, SchedulingContext context) {
JobIdentifier identifier = new JobIdentifier();
String elasticProfileId = config.getElasticProfileId();
String clusterProfileId = null;
ElasticProfile elasticProfile = null;
ClusterProfile clusterProfile = null;

if (elasticProfileId != null) {
elasticProfile = context.getElasticProfile(elasticProfileId);
clusterProfileId = elasticProfile.getClusterProfileId();
}

if (clusterProfileId != null) {
clusterProfile = context.getClusterProfile(clusterProfileId);
}

final EnvironmentVariables variables = EnvironmentVariables.toEnvironmentVariables(context.overrideEnvironmentVariables(config.getVariables()).getEnvironmentVariablesConfig());

return new DefaultJobPlan(new Resources(config.resourceConfigs()),
ArtifactPlan.toArtifactPlans(config.artifactConfigs()),
ArtifactPropertiesGenerator.toArtifactProperties(config.getProperties()), -1, identifier, null, variables, new EnvironmentVariables(), elasticProfile, null);
ArtifactPropertiesGenerator.toArtifactProperties(config.getProperties()), -1, identifier, null, variables, new EnvironmentVariables(), elasticProfile, clusterProfile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,8 @@ public Capabilities getCapabilities(String pluginId) {
return getVersionedElasticAgentExtension(pluginId).getCapabilities(pluginId);
}

public void reportJobCompletion(String pluginId, String elasticAgentId, JobIdentifier jobIdentifier) {
VersionedElasticAgentExtension elasticAgentExtension = getVersionedElasticAgentExtension(pluginId);
elasticAgentExtension.jobCompletion(pluginId, elasticAgentId, jobIdentifier);
public void reportJobCompletion(String pluginId, String elasticAgentId, JobIdentifier jobIdentifier, Map<String, String> elasticProfileConfiguration, Map<String, String> clusterProfileConfiguration) {
getVersionedElasticAgentExtension(pluginId).jobCompletion(pluginId, elasticAgentId, jobIdentifier, elasticProfileConfiguration, clusterProfileConfiguration);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public boolean has(String pluginId) {
return findPlugin(pluginId) != null;
}

public void reportJobCompletion(String pluginId, String elasticAgentId, JobIdentifier jobIdentifier) {
LOGGER.debug("Processing report job completion for plugin: {} for elasticAgentId: {} for job: {}", pluginId, elasticAgentId, jobIdentifier);
extension.reportJobCompletion(pluginId, elasticAgentId, jobIdentifier);
LOGGER.debug("Done processing report job completion for plugin: {} for elasticAgentId: {} for job: {}", pluginId, elasticAgentId, jobIdentifier);
public void reportJobCompletion(String pluginId, String elasticAgentId, JobIdentifier jobIdentifier, Map<String, String> elasticProfileConfiguration, Map<String, String> clusterProfileConfiguration) {
LOGGER.debug("Processing report job completion for plugin: {} for elasticAgentId: {} for job: {} with configuration: {} in cluster: {}", pluginId, elasticAgentId, jobIdentifier, elasticProfileConfiguration, clusterProfileConfiguration);
extension.reportJobCompletion(pluginId, elasticAgentId, jobIdentifier, elasticProfileConfiguration, clusterProfileConfiguration);
LOGGER.debug("Done processing report job completion for plugin: {} for elasticAgentId: {} for job: {} with configuration: {} in cluster: {}", pluginId, elasticAgentId, jobIdentifier, elasticProfileConfiguration, clusterProfileConfiguration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ public interface VersionedElasticAgentExtension {

String getAgentStatusReport(String pluginId, JobIdentifier identifier, String elasticAgentId);

void jobCompletion(String pluginId, String elasticAgentId, JobIdentifier jobIdentifier);
void jobCompletion(String pluginId, String elasticAgentId, JobIdentifier jobIdentifier, Map<String, String> elasticProfileConfiguration, Map<String, String> clusterProfileConfiguration);
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public String onSuccess(String responseBody, Map<String, String> responseHeaders
}

@Override
public void jobCompletion(String pluginId, String elasticAgentId, JobIdentifier jobIdentifier) {
public void jobCompletion(String pluginId, String elasticAgentId, JobIdentifier jobIdentifier, Map<String, String> elasticProfileConfiguration, Map<String, String> clusterProfileConfiguration) {
pluginRequestHelper.submitRequest(pluginId, REQUEST_JOB_COMPLETION, new DefaultPluginInteractionCallback<String>() {
@Override
public String requestBody(String resolvedExtensionVersion) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,14 @@ private JsonObject jobIdentifierJson(JobIdentifier jobIdentifier) {
return jobIdentifierJson;
}

public String getJobCompletionRequestBody(String elasticAgentId, JobIdentifier jobIdentifier) {
public String getJobCompletionRequestBody(String elasticAgentId, JobIdentifier jobIdentifier, Map<String, String> elasticProfileConfiguration, Map<String, String> clusterProfileConfiguration) {
JsonObject jsonObject = new JsonObject();

jsonObject.addProperty("elastic_agent_id", elasticAgentId);
jsonObject.add("job_identifier", jobIdentifierJson(jobIdentifier));
jsonObject.add("elastic_agent_profile_properties", mapToJsonObject(elasticProfileConfiguration));
jsonObject.add("cluster_profile_properties", mapToJsonObject(clusterProfileConfiguration));

return GSON.toJson(jsonObject);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ public String onSuccess(String responseBody, Map<String, String> responseHeaders
}

@Override
public void jobCompletion(String pluginId, String elasticAgentId, JobIdentifier jobIdentifier) {
public void jobCompletion(String pluginId, String elasticAgentId, JobIdentifier jobIdentifier, Map<String, String> elasticProfileConfiguration, Map<String, String> clusterProfileConfiguration) {
pluginRequestHelper.submitRequest(pluginId, REQUEST_JOB_COMPLETION, new DefaultPluginInteractionCallback<String>() {
@Override
public String requestBody(String resolvedExtensionVersion) {
return elasticAgentExtensionConverterV5.getJobCompletionRequestBody(elasticAgentId, jobIdentifier);
return elasticAgentExtensionConverterV5.getJobCompletionRequestBody(elasticAgentId, jobIdentifier, elasticProfileConfiguration, clusterProfileConfiguration);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
import org.junit.rules.ExpectedException;
import org.mockito.ArgumentCaptor;

import java.util.Collections;
import java.util.Map;

import static com.thoughtworks.go.plugin.access.elastic.ElasticAgentExtension.SUPPORTED_VERSIONS;
import static com.thoughtworks.go.plugin.access.elastic.v4.ElasticAgentPluginConstantsV4.REQUEST_GET_PLUGIN_SETTINGS_ICON;
import static com.thoughtworks.go.plugin.domain.common.PluginConstants.ELASTIC_AGENT_EXTENSION;
Expand Down Expand Up @@ -103,9 +106,11 @@ public void shouldMakeJobCompletionCall() {

final String elasticAgentId = "ea1";
final JobIdentifier jobIdentifier = new JobIdentifier("up42", 2, "Test", "up42_stage", "10", "up42_job");
final Map<String, String> elasticProfileConfiguration = Collections.singletonMap("Image", "alpine:latest");
final Map<String, String> clusterProfileConfiguration = Collections.singletonMap("ServerURL", "https://example.com/go");
when(pluginManager.submitTo(eq(PLUGIN_ID), eq(ELASTIC_AGENT_EXTENSION), requestArgumentCaptor.capture())).thenReturn(DefaultGoPluginApiResponse.success(null));

extension.reportJobCompletion(PLUGIN_ID, elasticAgentId, jobIdentifier);
extension.reportJobCompletion(PLUGIN_ID, elasticAgentId, jobIdentifier, elasticProfileConfiguration, clusterProfileConfiguration);

verify(pluginManager, times(1)).submitTo(eq(PLUGIN_ID), eq(ELASTIC_AGENT_EXTENSION), any(GoPluginApiRequest.class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public void shouldMakeJobCompletionCall() {
final JobIdentifier jobIdentifier = new JobIdentifier("up42", 2, "Test", "up42_stage", "10", "up42_job");
when(pluginManager.submitTo(eq(PLUGIN_ID), eq(ELASTIC_AGENT_EXTENSION), requestArgumentCaptor.capture())).thenReturn(DefaultGoPluginApiResponse.success(null));

extensionV4.jobCompletion(PLUGIN_ID, elasticAgentId, jobIdentifier);
extensionV4.jobCompletion(PLUGIN_ID, elasticAgentId, jobIdentifier, Collections.EMPTY_MAP, Collections.EMPTY_MAP);

String expectedRequestBody = "{\n" +
" \"elastic_agent_id\": \"ea1\",\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,20 @@ public void shouldMakeCreateAgentCall() {
public void shouldMakeJobCompletionCall() {
final String elasticAgentId = "ea1";
final JobIdentifier jobIdentifier = new JobIdentifier("up42", 2, "Test", "up42_stage", "10", "up42_job");
final Map<String, String> profile = Collections.singletonMap("Image", "alpine:latest");
final Map<String, String> clusterProfile = Collections.singletonMap("ServerURL", "https://example.com/go");
when(pluginManager.submitTo(eq(PLUGIN_ID), eq(ELASTIC_AGENT_EXTENSION), requestArgumentCaptor.capture())).thenReturn(DefaultGoPluginApiResponse.success(null));

extensionV5.jobCompletion(PLUGIN_ID, elasticAgentId, jobIdentifier);
extensionV5.jobCompletion(PLUGIN_ID, elasticAgentId, jobIdentifier, profile, clusterProfile);

String expectedRequestBody = "{\n" +
" \"elastic_agent_id\": \"ea1\",\n" +
" \"elastic_agent_profile_properties\": {\n" +
" \"Image\": \"alpine:latest\"\n" +
" },\n" +
" \"cluster_profile_properties\": {\n" +
" \"ServerURL\": \"https://example.com/go\"\n" +
" },\n" +
" \"job_identifier\": {\n" +
" \"pipeline_name\": \"up42\",\n" +
" \"pipeline_label\": \"Test\",\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@ public void shouldTalkToExtensionToGetAgentStatusReport() {
public void shouldTalkToExtensionToReportJobCompletion() {
final JobIdentifier jobIdentifier = new JobIdentifier();
final String elasticAgentId = "ea_1";
final Map<String, String> elasticProfileConfiguration = Collections.singletonMap("Image", "alpine:latest");
final Map<String, String> clusterProfileConfiguration = Collections.singletonMap("ServerURL", "https://example.com/go");

elasticAgentPluginRegistry.reportJobCompletion(PLUGIN_ID, elasticAgentId, jobIdentifier);
elasticAgentPluginRegistry.reportJobCompletion(PLUGIN_ID, elasticAgentId, jobIdentifier, elasticProfileConfiguration, clusterProfileConfiguration);

verify(elasticAgentExtension, times(1)).reportJobCompletion(PLUGIN_ID, elasticAgentId, jobIdentifier);
verify(elasticAgentExtension, times(1)).reportJobCompletion(PLUGIN_ID, elasticAgentId, jobIdentifier, elasticProfileConfiguration, clusterProfileConfiguration);
verifyNoMoreInteractions(elasticAgentExtension);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,20 @@ public void shouldJSONizeShouldAssignWorkRequestBody() throws Exception {

@Test
public void shouldJSONizeJobCompletionRequestBody() throws Exception {
String actual = new ElasticAgentExtensionConverterV5().getJobCompletionRequestBody("ea1", jobIdentifier);
HashMap<String, String> elasticProfileConfiguration = new HashMap<>();
elasticProfileConfiguration.put("property_name", "property_value");
HashMap<String, String> clusterProfileConfiguration = new HashMap<>();
clusterProfileConfiguration.put("property_name", "property_value");
String actual = new ElasticAgentExtensionConverterV5().getJobCompletionRequestBody("ea1", jobIdentifier, elasticProfileConfiguration, clusterProfileConfiguration);

String expected = "{" +
" \"elastic_agent_id\":\"ea1\"," +
" \"elastic_agent_profile_properties\":{" +
" \"property_name\":\"property_value\"" +
" }," +
" \"cluster_profile_properties\":{" +
" \"property_name\":\"property_value\"" +
" }," +
" \"job_identifier\": {\n" +
" \"pipeline_name\": \"test-pipeline\",\n" +
" \"pipeline_counter\": 1,\n" +
Expand Down
Loading

0 comments on commit f493bfb

Please sign in to comment.