public Approval toApproval(CRApproval crApproval) { if (crApproval == null) return Approval.automaticApproval(); Approval approval; if (crApproval.getType() == CRApprovalCondition.manual) approval = Approval.manualApproval(); else approval = Approval.automaticApproval(); AuthConfig authConfig = approval.getAuthConfig(); for (String user : crApproval.getAuthorizedUsers()) { authConfig.add(new AdminUser(new CaseInsensitiveString(user))); } for (String user : crApproval.getAuthorizedRoles()) { authConfig.add(new AdminRole(new CaseInsensitiveString(user))); } return approval; }
@Test public void shouldReturnDisplayNameForApprovalType() { Approval approval = Approval.automaticApproval(); assertThat(approval.getDisplayName(), is("On Success")); approval = Approval.manualApproval(); assertThat(approval.getDisplayName(), is("Manual")); }
@Test public void shouldAssignApprovalTypeOnFirstStageAsManualAndRestOfStagesAsUntouched() throws Exception { Map approvalAttributes = Collections.singletonMap(Approval.TYPE, Approval.MANUAL); Map<String, Map> map = Collections.singletonMap(StageConfig.APPROVAL, approvalAttributes); PipelineConfig pipelineConfig = PipelineConfigMother.pipelineConfig("p1", StageConfigMother.custom("s1", Approval.automaticApproval()), StageConfigMother.custom("s2", Approval.automaticApproval())); pipelineConfig.setConfigAttributes(map); assertThat(pipelineConfig.get(0).getApproval().getType(), is(Approval.MANUAL)); assertThat(pipelineConfig.get(1).getApproval().getType(), is(Approval.SUCCESS)); }
@Test public void shouldSetAutoApprovalOnStageInstance() { StageConfig stageConfig = StageConfigMother.custom("test", Approval.automaticApproval()); Stage instance = instanceFactory.createStageInstance(stageConfig, new DefaultSchedulingContext("anyone"), "md5", new TimeProvider()); assertThat(instance.getApprovalType(), is(GoConstants.APPROVAL_SUCCESS)); }
@Test public void shouldSetFetchMaterialsFlagOnStageInstance() throws Exception { StageConfig stageConfig = StageConfigMother.custom("test", Approval.automaticApproval()); stageConfig.setFetchMaterials(false); Stage instance = instanceFactory.createStageInstance(stageConfig, new DefaultSchedulingContext("anyone"), "md5", new TimeProvider()); assertThat(instance.shouldFetchMaterials(), is(false)); }
@Test public void shouldOverwriteExistingUsersWhileSettingNewUsers() { Approval approval = Approval.automaticApproval(); approval.getAuthConfig().add(new AdminUser(new CaseInsensitiveString("sachin"))); approval.getAuthConfig().add(new AdminRole(new CaseInsensitiveString("admin"))); List names = new ArrayList(); names.add(nameMap("awesome_shilpa")); names.add(nameMap("youth")); names.add(nameMap("")); List roles = new ArrayList(); roles.add(nameMap("role1")); roles.add(nameMap("role2")); roles.add(nameMap("")); approval.setOperatePermissions(names, roles); assertThat(approval.getAuthConfig().size(), is(4)); assertThat(approval.getAuthConfig(), hasItem((Admin) new AdminUser(new CaseInsensitiveString("awesome_shilpa")))); assertThat(approval.getAuthConfig(), hasItem((Admin) new AdminUser(new CaseInsensitiveString("youth")))); assertThat(approval.getAuthConfig(), hasItem((Admin) new AdminRole(new CaseInsensitiveString("role1")))); assertThat(approval.getAuthConfig(), hasItem((Admin) new AdminRole(new CaseInsensitiveString("role2")))); }
@Test public void shouldClearAllPermissions() { Approval approval = Approval.automaticApproval(); approval.getAuthConfig().add(new AdminUser(new CaseInsensitiveString("sachin"))); approval.getAuthConfig().add(new AdminRole(new CaseInsensitiveString("admin"))); approval.removeOperatePermissions(); assertThat(approval.getAuthConfig().isEmpty(), is(true)); }
@Test public void shouldClearAllPermissionsWhenTheAttributesAreNull() { Approval approval = Approval.automaticApproval(); approval.getAuthConfig().add(new AdminUser(new CaseInsensitiveString("sachin"))); approval.getAuthConfig().add(new AdminRole(new CaseInsensitiveString("admin"))); approval.setOperatePermissions(null, null); assertThat(approval.getAuthConfig().isEmpty(), is(true)); }
@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"))); }