public static StageConfig fromJSON(JsonReader jsonReader) { StageConfig stageConfig = new StageConfig(); jsonReader.readCaseInsensitiveStringIfPresent("name", stageConfig::setName); jsonReader.optBoolean("fetch_materials").ifPresent(stageConfig::setFetchMaterials); jsonReader.optBoolean("clean_working_directory").ifPresent(stageConfig::setCleanWorkingDir); jsonReader.optBoolean("never_cleanup_artifacts").ifPresent(stageConfig::setArtifactCleanupProhibited); stageConfig.setVariables(EnvironmentVariableRepresenter.fromJSONArray(jsonReader)); setJobs(jsonReader, stageConfig); jsonReader.optJsonObject("approval").ifPresent(approvalReader -> { stageConfig.setApproval(ApprovalRepresenter.fromJSON(approvalReader)); }); return stageConfig; }
public static StageConfig fromJSON(JsonReader jsonReader) { StageConfig stageConfig = new StageConfig(); jsonReader.readCaseInsensitiveStringIfPresent("name", stageConfig::setName); jsonReader.optBoolean("fetch_materials").ifPresent(stageConfig::setFetchMaterials); jsonReader.optBoolean("clean_working_directory").ifPresent(stageConfig::setCleanWorkingDir); jsonReader.optBoolean("never_cleanup_artifacts").ifPresent(stageConfig::setArtifactCleanupProhibited); stageConfig.setVariables(EnvironmentVariableRepresenter.fromJSONArray(jsonReader)); setJobs(jsonReader, stageConfig); jsonReader.optJsonObject("approval").ifPresent(approvalReader -> { stageConfig.setApproval(ApprovalRepresenter.fromJSON(approvalReader)); }); return stageConfig; }
@Test public void shouldPopulateEnvironmentVariablesFromAttributeMap() { StageConfig stageConfig = new StageConfig(); HashMap map = new HashMap(); HashMap valueHashMap = new HashMap(); valueHashMap.put("name", "FOO"); valueHashMap.put("value", "BAR"); map.put(StageConfig.ENVIRONMENT_VARIABLES, valueHashMap); EnvironmentVariablesConfig mockEnvironmentVariablesConfig = mock(EnvironmentVariablesConfig.class); stageConfig.setVariables(mockEnvironmentVariablesConfig); stageConfig.setConfigAttributes(map); verify(mockEnvironmentVariablesConfig).setConfigAttributes(valueHashMap); }
@Test public void shouldReturnFalseIfAnyDescendentIsInValid(){ EnvironmentVariablesConfig variables = mock(EnvironmentVariablesConfig.class); JobConfigs jobConfigs = mock(JobConfigs.class); Approval approval = mock(Approval.class); when(variables.validateTree(any(PipelineConfigSaveValidationContext.class))).thenReturn(false); when(jobConfigs.validateTree(any(PipelineConfigSaveValidationContext.class))).thenReturn(false); when(approval.validateTree(any(PipelineConfigSaveValidationContext.class))).thenReturn(false); StageConfig stageConfig = new StageConfig(new CaseInsensitiveString("p1"), jobConfigs); stageConfig.setVariables(variables); stageConfig.setApproval(approval); boolean isValid = stageConfig.validateTree(PipelineConfigSaveValidationContext.forChain(true, "group", new PipelineConfig(), stageConfig)); assertFalse(isValid); verify(jobConfigs).validateTree(any(PipelineConfigSaveValidationContext.class)); verify(variables).validateTree(any(PipelineConfigSaveValidationContext.class)); verify(approval).validateTree(any(PipelineConfigSaveValidationContext.class)); } }
@Test public void shouldReturnTrueIfAllDescendentsAreValid(){ EnvironmentVariablesConfig variables = mock(EnvironmentVariablesConfig.class); JobConfigs jobConfigs = mock(JobConfigs.class); Approval approval = mock(Approval.class); when(variables.validateTree(any(PipelineConfigSaveValidationContext.class))).thenReturn(true); when(jobConfigs.validateTree(any(PipelineConfigSaveValidationContext.class))).thenReturn(true); when(approval.validateTree(any(PipelineConfigSaveValidationContext.class))).thenReturn(true); StageConfig stageConfig = new StageConfig(new CaseInsensitiveString("p1"), jobConfigs); stageConfig.setVariables(variables); stageConfig.setApproval(approval); boolean isValid = stageConfig.validateTree(PipelineConfigSaveValidationContext.forChain(true, "group", new PipelineConfig(), stageConfig)); assertTrue(isValid); verify(jobConfigs).validateTree(any(PipelineConfigSaveValidationContext.class)); verify(variables).validateTree(any(PipelineConfigSaveValidationContext.class)); verify(approval).validateTree(any(PipelineConfigSaveValidationContext.class)); }
public static StageConfig stageConfigWithParams(String stageName, String paramName) { StageConfig stageConfig = StageConfigMother.stageConfig(stageName); ArrayList<EnvironmentVariableConfig> environmentVariableConfigs = new ArrayList<>(); environmentVariableConfigs.add(new EnvironmentVariableConfig("env1", "#{" + paramName + "}")); stageConfig.setVariables(new EnvironmentVariablesConfig(environmentVariableConfigs)); stageConfig.getJobs().add(JobConfigMother.jobConfig()); return stageConfig; }
@Test public void shouldValidateTree(){ EnvironmentVariablesConfig variables = mock(EnvironmentVariablesConfig.class); JobConfigs jobConfigs = mock(JobConfigs.class); Approval approval = mock(Approval.class); StageConfig stageConfig = new StageConfig(new CaseInsensitiveString("stage$"), jobConfigs, approval); stageConfig.setVariables(variables); stageConfig.validateTree(PipelineConfigSaveValidationContext.forChain(true, "group", new PipelineConfig(), stageConfig)); assertThat(stageConfig.errors().on(StageConfig.NAME), contains("Invalid stage name 'stage$'")); ArgumentCaptor<PipelineConfigSaveValidationContext> captor = ArgumentCaptor.forClass(PipelineConfigSaveValidationContext.class); verify(jobConfigs).validateTree(captor.capture()); PipelineConfigSaveValidationContext childContext = captor.getValue(); assertThat(childContext.getParent(), is(stageConfig)); verify(approval).validateTree(childContext); verify(variables).validateTree(childContext); }
public static StageConfig stageConfigWithEnvironmentVariable(String stageName) { StageConfig stageConfig = StageConfigMother.stageConfig(stageName); stageConfig.setVariables(EnvironmentVariablesConfigMother.environmentVariables()); stageConfig.getJobs().add(JobConfigMother.jobConfig()); return stageConfig; }
@Test public void shouldSetEnvironmentVariablesOnSchedulingContext() throws Exception { EnvironmentVariablesConfig existing = new EnvironmentVariablesConfig(); existing.add("firstVar", "firstVal"); existing.add("overriddenVar", "originalVal"); SchedulingContext schedulingContext = new DefaultSchedulingContext(); schedulingContext = schedulingContext.overrideEnvironmentVariables(existing); EnvironmentVariablesConfig stageLevel = new EnvironmentVariablesConfig(); stageLevel.add("stageVar", "stageVal"); stageLevel.add("overriddenVar", "overriddenVal"); StageConfig config = StageConfigMother.custom("test", Approval.automaticApproval()); config.setVariables(stageLevel); ReflectionUtil.setField(schedulingContext, "rerun", true); SchedulingContext context = schedulingContext.overrideEnvironmentVariables(config.getVariables()); assertThat(context.isRerun(), is(true)); EnvironmentVariablesConfig environmentVariablesUsed = context.getEnvironmentVariablesConfig(); assertThat(environmentVariablesUsed.size(), is(3)); assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("firstVar", "firstVal"))); assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("overriddenVar", "overriddenVal"))); assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("stageVar", "stageVal"))); }
@Test public void shouldCreateRerunSchedulingContextCorrectly() throws Exception { AgentConfig linux = agent("uuid1", "linux"); AgentConfig windows = agent("uuid2", "windows"); windows.disable(); Agents matchingAgents = new Agents(linux, windows); EnvironmentVariablesConfig existing = new EnvironmentVariablesConfig(); existing.add("firstVar", "firstVal"); existing.add("overriddenVar", "originalVal"); SchedulingContext schedulingContext = new DefaultSchedulingContext("approver", matchingAgents); schedulingContext = schedulingContext.overrideEnvironmentVariables(existing); EnvironmentVariablesConfig stageLevel = new EnvironmentVariablesConfig(); stageLevel.add("stageVar", "stageVal"); stageLevel.add("overriddenVar", "overriddenVal"); StageConfig config = StageConfigMother.custom("test", Approval.automaticApproval()); config.setVariables(stageLevel); SchedulingContext context = schedulingContext.overrideEnvironmentVariables(config.getVariables()); SchedulingContext rerunContext = context.rerunContext(); assertThat(rerunContext.isRerun(), is(true)); assertThat(rerunContext.getApprovedBy(), is("approver")); Agents agents = (Agents) ReflectionUtil.getField(rerunContext, "agents"); assertThat(agents, is(matchingAgents)); EnvironmentVariablesConfig environmentVariablesUsed = rerunContext.getEnvironmentVariablesConfig(); assertThat(environmentVariablesUsed.size(), is(3)); assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("firstVar", "firstVal"))); assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("overriddenVar", "overriddenVal"))); assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("stageVar", "stageVal"))); }
@Test public void shouldCreatePermittedAgentContextCorrectly() throws Exception { AgentConfig linux = agent("uuid1", "linux"); AgentConfig windows = agent("uuid2", "windows"); windows.disable(); Agents matchingAgents = new Agents(linux, windows); EnvironmentVariablesConfig existing = new EnvironmentVariablesConfig(); existing.add("firstVar", "firstVal"); existing.add("overriddenVar", "originalVal"); SchedulingContext schedulingContext = new DefaultSchedulingContext("approver", matchingAgents); schedulingContext = schedulingContext.overrideEnvironmentVariables(existing); EnvironmentVariablesConfig stageLevel = new EnvironmentVariablesConfig(); stageLevel.add("stageVar", "stageVal"); stageLevel.add("overriddenVar", "overriddenVal"); StageConfig config = StageConfigMother.custom("test", Approval.automaticApproval()); config.setVariables(stageLevel); SchedulingContext context = schedulingContext.overrideEnvironmentVariables(config.getVariables()); ReflectionUtil.setField(context, "rerun", true); SchedulingContext permittedAgentContext = context.permittedAgent("uuid1"); Agents agents = (Agents) ReflectionUtil.getField(permittedAgentContext, "agents"); assertThat(agents.size(), is(1)); assertThat(agents.get(0).getAgentIdentifier().getUuid(), is("uuid1")); assertThat(permittedAgentContext.isRerun(), is(true)); assertThat(permittedAgentContext.getApprovedBy(), is("approver")); EnvironmentVariablesConfig environmentVariablesUsed = permittedAgentContext.getEnvironmentVariablesConfig(); assertThat(environmentVariablesUsed.size(), is(3)); assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("firstVar", "firstVal"))); assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("overriddenVar", "overriddenVal"))); assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("stageVar", "stageVal"))); }
@Test public void shouldCloneStagesSoThatMutationDoesnotAffectTemplate() throws Exception { PipelineConfig pipelineConfig = new PipelineConfig(new CaseInsensitiveString("pipelineName"), new MaterialConfigs(MaterialConfigsMother.hgMaterialConfig("http://google.com"))); pipelineConfig.setTemplateName(new CaseInsensitiveString("templateName")); PipelineTemplateConfig template = new PipelineTemplateConfig(); JobConfig jobConfigFromTemplate = new JobConfig("job-1"); StageConfig stageConfigFromTemplate = new StageConfig(new CaseInsensitiveString("stage-1"), new JobConfigs(jobConfigFromTemplate)); template.add(stageConfigFromTemplate); pipelineConfig.usingTemplate(template); StageConfig stageConfigFromPipeline = pipelineConfig.get(0); EnvironmentVariablesConfig variablesConfig = new EnvironmentVariablesConfig(); variablesConfig.add("FOO", "BAR"); stageConfigFromPipeline.setVariables(variablesConfig); JobConfig jobConfigFromPipeline = stageConfigFromPipeline.jobConfigByConfigName(new CaseInsensitiveString("job-1")); EnvironmentVariablesConfig jobVariablesConfigFromPipeline = new EnvironmentVariablesConfig(); jobVariablesConfigFromPipeline.add("BAZ", "QUUX"); jobConfigFromPipeline.setVariables(jobVariablesConfigFromPipeline); assertThat(stageConfigFromPipeline.getVariables().isEmpty(), is(false)); assertThat(jobConfigFromPipeline.getVariables().isEmpty(), is(false)); assertThat(stageConfigFromTemplate.getVariables().isEmpty(), is(true)); assertThat(jobConfigFromTemplate.getVariables().isEmpty(), is(true)); }