@Test public void shouldPopulateArtifactPlansFromAttributeMap() { HashMap map = new HashMap(); HashMap valueHashMap = new HashMap(); valueHashMap.put("src", "dest"); valueHashMap.put("src1", "dest1"); map.put(JobConfig.ARTIFACT_CONFIGS, valueHashMap); ArtifactConfigs mockArtifactConfigs = mock(ArtifactConfigs.class); JobConfig jobConfig = new JobConfig(new CaseInsensitiveString("job-name"), new ResourceConfigs(), mockArtifactConfigs); jobConfig.setConfigAttributes(map); verify(mockArtifactConfigs).setConfigAttributes(valueHashMap); }
@Test public void shouldEncryptSecurePropertiesForOnlyFetchExternalArtifactTask() { JobConfig jobConfig = new JobConfig(new CaseInsensitiveString("job")); FetchPluggableArtifactTask mockFetchExternalArtifactTask = mock(FetchPluggableArtifactTask.class); jobConfig.addTask(mockFetchExternalArtifactTask); jobConfig.encryptSecureProperties(new BasicCruiseConfig(), new PipelineConfig(), jobConfig); verify(mockFetchExternalArtifactTask).encryptSecureProperties(any(CruiseConfig.class), any(PipelineConfig.class), any(FetchPluggableArtifactTask.class)); }
@Test public void shouldNotAttemptToEncryptPropertiesIfThereAreNoPluginConfigs() { PipelineConfig pipelineConfig = new PipelineConfig(); StageConfig mockStageConfig = mock(StageConfig.class); pipelineConfig.add(mockStageConfig); JobConfig jobConfig = new JobConfig(new CaseInsensitiveString("job")); when(mockStageConfig.getJobs()).thenReturn(new JobConfigs(jobConfig)); when(mockStageConfig.name()).thenReturn(new CaseInsensitiveString("stage")); pipelineConfig.encryptSecureProperties(new BasicCruiseConfig(), pipelineConfig); verify(mockStageConfig, never()).encryptSecureProperties(eq(new BasicCruiseConfig()), eq(pipelineConfig), ArgumentMatchers.any(StageConfig.class)); }
@Test public void shouldNotValidateJobNameUniquenessInAbsenceOfName(){ JobConfig job = new JobConfig(); job.validateNameUniqueness(new HashMap<>()); assertTrue(job.errors().isEmpty()); }
@Test public void shouldSupportRunMultipleInstance() throws Exception { CruiseConfig cruiseConfig = loadJobWithRunMultipleInstance("10"); JobConfig job = cruiseConfig.findJob("pipeline1", "mingle", "do-something"); JobConfig jobConfig = new JobConfig("do-something"); jobConfig.setRunInstanceCount(10); assertThat(job, is(jobConfig)); }
@Test public void shouldEncryptSecurePropertiesInStagesIfPipelineHasStagesDefined() { PipelineConfig pipelineConfig = new PipelineConfig(); StageConfig mockStageConfig = mock(StageConfig.class); pipelineConfig.add(mockStageConfig); JobConfig jobConfig = new JobConfig(new CaseInsensitiveString("job")); jobConfig.artifactConfigs().add(new PluggableArtifactConfig("foo", "bar")); when(mockStageConfig.getJobs()).thenReturn(new JobConfigs(jobConfig)); when(mockStageConfig.name()).thenReturn(new CaseInsensitiveString("stage")); pipelineConfig.encryptSecureProperties(new BasicCruiseConfig(), pipelineConfig); verify(mockStageConfig).encryptSecureProperties(eq(new BasicCruiseConfig()), eq(pipelineConfig), ArgumentMatchers.any(StageConfig.class)); }
@Test public void shouldReturnTimeoutType() { JobConfig job = new JobConfig("job"); assertThat(job.getTimeoutType(), is(JobConfig.DEFAULT_TIMEOUT)); job.setTimeout("0"); assertThat(job.getTimeoutType(), is(JobConfig.NEVER_TIMEOUT)); job.setTimeout("10"); assertThat(job.getTimeoutType(), is(JobConfig.OVERRIDE_TIMEOUT)); }
@Test public void shouldPopulateResourcesFromAttributeMap() { HashMap map = new HashMap(); String value = "a, b,c ,d,e"; map.put(JobConfig.RESOURCES, value); ResourceConfigs resourceConfigs = new ResourceConfigs(); resourceConfigs.add(new ResourceConfig("z")); JobConfig jobConfig = new JobConfig(new CaseInsensitiveString("job-name"), resourceConfigs, null); jobConfig.setConfigAttributes(map); assertThat(jobConfig.resourceConfigs().size(), is(5)); }
@Test public void shouldNotSetJobNameIfNotGiven() throws Exception { JobConfig config = new JobConfig("some-job-name"); config.setConfigAttributes(m()); assertThat(config.name(), is(new CaseInsensitiveString("some-job-name"))); config.setConfigAttributes(m(JobConfig.NAME, null)); assertThat(config.name(), is(nullValue())); }
@Test public void shouldReturnRunTypeCorrectly() { JobConfig job = new JobConfig("job"); assertThat(job.getRunType(), is(JobConfig.RUN_SINGLE_INSTANCE)); job.setRunOnAllAgents(true); assertThat(job.getRunType(), is(JobConfig.RUN_ON_ALL_AGENTS)); job.setRunOnAllAgents(false); job.setRunInstanceCount(10); assertThat(job.getRunType(), is(JobConfig.RUN_MULTIPLE_INSTANCE)); }
@Test public void shouldFindCorrectJobIfJobIsOnAllAgentsAndAmbiguousName() throws Exception { JobConfig allAgentsJob = new JobConfig("job-for-all-agents"); JobConfig ambiguousJob = new JobConfig("job-for-all"); allAgentsJob.setRunOnAllAgents(true); ambiguousJob.setRunOnAllAgents(true); JobConfigs jobs = new JobConfigs(); jobs.add(ambiguousJob); jobs.add(allAgentsJob); StageConfig stage = new StageConfig(new CaseInsensitiveString("stage-name"), jobs); JobConfig found = stage.jobConfigByInstanceName(RunOnAllAgents.CounterBasedJobNameGenerator.appendMarker("job-for-all-agents", 1), true); assertThat(found, is(allAgentsJob)); }
@Test public void shouldCopyAttributeValuesFromAttributeMap() throws Exception { config = new JobConfig();//override the setup mock TaskFactory taskFactory = mock(TaskFactory.class); ExecTask emptyExecTask = new ExecTask(); when(taskFactory.taskInstanceFor(emptyExecTask.getTaskType())).thenReturn(emptyExecTask); config.setConfigAttributes(DataStructureUtils.m(JobConfig.NAME, "foo-job", JobConfig.TASKS, DataStructureUtils.m(Tasks.TASK_OPTIONS, "exec", "exec", DataStructureUtils.m(Task.TASK_TYPE, "exec", ExecTask.COMMAND, "ls", ExecTask.ARGS, "-la", ExecTask.WORKING_DIR, "/tmp"))), taskFactory); assertThat(config.name(), is(new CaseInsensitiveString("foo-job"))); assertThat(config.getTasks().get(0), is(new ExecTask("ls", "-la", "/tmp"))); assertThat(config.getTasks().size(), is(1)); }
@Test public void shouldValidateThatTheTimeoutIsAValidNumber() { JobConfig job = new JobConfig("job"); job.setTimeout("5.5"); job.validate(ConfigSaveValidationContext.forChain(new BasicCruiseConfig())); assertThat(job.errors().isEmpty(), is(true)); }
@Test public void shouldErrorOutWhenTimeoutIsANegativeNumber() { JobConfig jobConfig = new JobConfig("job"); jobConfig.setTimeout("-1"); jobConfig.validate(ConfigSaveValidationContext.forChain(new BasicCruiseConfig())); assertThat(jobConfig.errors().isEmpty(), is(false)); assertThat(jobConfig.errors().on(JobConfig.TIMEOUT), is("Timeout cannot be a negative number as it represents number of minutes")); }
@Test public void shouldMarkJobInvalidIfTimeoutIsNotAValidNumber() { JobConfig job = new JobConfig("job"); job.setTimeout("5.5MN"); job.validate(ConfigSaveValidationContext.forChain(new BasicCruiseConfig())); assertThat(job.errors().isEmpty(), is(false)); assertThat(job.errors().on(JobConfig.TIMEOUT), is("Timeout should be a valid number as it represents number of minutes")); }
@Test public void shouldAddEnvironmentVariablesToJobPlan() { EnvironmentVariablesConfig variablesConfig = new EnvironmentVariablesConfig(); variablesConfig.add("blahVar", "blahVal"); JobConfig jobConfig = new JobConfig("foo"); jobConfig.setVariables(variablesConfig); SchedulingContext context = new DefaultSchedulingContext(); JobPlan plan = instanceFactory.createJobPlan(jobConfig, context); assertThat(plan.getVariables(), hasItem(new EnvironmentVariable("blahVar", "blahVal"))); }
@Test public void shouldReturnBuildInstance() { ArtifactConfigs artifactConfigs = new ArtifactConfigs(); JobConfig jobConfig = new JobConfig(new CaseInsensitiveString("test"), null, artifactConfigs); RunOnAllAgents.CounterBasedJobNameGenerator jobNameGenerator = new RunOnAllAgents.CounterBasedJobNameGenerator(CaseInsensitiveString.str(jobConfig.name())); JobInstances jobs = instanceFactory.createJobInstance(new CaseInsensitiveString("stage_foo"), jobConfig, new DefaultSchedulingContext(), new TimeProvider(), jobNameGenerator); JobInstance jobInstance = jobs.first(); assertThat(jobConfig.name(), is(new CaseInsensitiveString(jobInstance.getName()))); assertThat(jobInstance.getState(), is(JobState.Scheduled)); assertThat(jobInstance.getScheduledDate(), is(notNullValue())); }
@Test public void shouldAddJobsGivenInTheAttributesMapAfterClearingExistingJobs() throws Exception{ JobConfigs jobs = new JobConfigs(); jobs.add(new JobConfig("quux")); jobs.setConfigAttributes(a(m(JobConfig.NAME, "foo"), m(JobConfig.NAME, "bar"))); assertThat(jobs.get(0).name(), is(new CaseInsensitiveString("foo"))); assertThat(jobs.get(1).name(), is(new CaseInsensitiveString("bar"))); assertThat(jobs.size(), is(2)); }
public static StageConfig stageWithTasks(String stageName) { JobConfig job = new JobConfig("job"); job.addTask(new ExecTask("ls", "-la", "pwd")); job.addTask(new AntTask()); return new StageConfig(new CaseInsensitiveString(stageName), new JobConfigs(job)); }
@Test public void shouldNotThrowExceptionIfNoRoleUsed() { StageConfig stage = new StageConfig(new CaseInsensitiveString("stage-foo"), new JobConfigs(new JobConfig(new CaseInsensitiveString("build-1"), new ResourceConfigs(), new ArtifactConfigs(), new Tasks(new ExecTask("ls", "-la", "work")) )) ); PipelineConfigs pipelinesConfig = new BasicPipelineConfigs("group", new Authorization(), new PipelineConfig(new CaseInsensitiveString("pipeline"), new MaterialConfigs(), stage)); CruiseConfig config = new BasicCruiseConfig(pipelinesConfig); config.server().security().addRole(new RoleConfig(new CaseInsensitiveString("role1"))); pipelinesConfig.validate(ConfigSaveValidationContext.forChain(config)); assertThat(pipelinesConfig.errors().isEmpty(), is(true)); }