@Test public void shouldGenerateAMapOfAllPipelinesAndTheirParentDependencies_WhenAllPipelinesInMapAreLocal() { /* * -----+ p2 --> p4 * p1 * -----+ p3 * * */ PipelineConfig p1 = createPipelineConfig("p1", "s1", "j1"); PipelineConfig p2 = createPipelineConfig("p2", "s2", "j1"); p2.addMaterialConfig( new DependencyMaterialConfig( new CaseInsensitiveString("p1"), new CaseInsensitiveString("s1"))); PipelineConfig p3 = createPipelineConfig("p3", "s3", "j1"); p3.addMaterialConfig( new DependencyMaterialConfig( new CaseInsensitiveString("p1"), new CaseInsensitiveString("s1"))); PipelineConfig p4 = createPipelineConfig("p4", "s4", "j1"); p4.addMaterialConfig( new DependencyMaterialConfig( new CaseInsensitiveString("p2"), new CaseInsensitiveString("s2"))); pipelines.addAll(Arrays.asList(p4, p2, p1, p3)); Map<String, List<PipelineConfig>> expectedPipelines = cruiseConfig.generatePipelineVsDownstreamMap(); assertThat(expectedPipelines.size(), is(5)); assertThat(expectedPipelines.get("p1"), hasItems(p2, p3)); assertThat(expectedPipelines.get("p2"), hasItems(p4)); assertThat(expectedPipelines.get("p3").isEmpty(), is(true)); assertThat(expectedPipelines.get("p4").isEmpty(), is(true)); assertThat(expectedPipelines.get("remote-pipe-1").isEmpty(), is(true)); }
@Test public void shouldNotIncludePackageMaterialsWithAutoUpdateFalse() { PipelineConfig pipeline1 = pipelineWithManyMaterials(false); pipeline1.addMaterialConfig(getPackageMaterialConfigWithAutoUpdateFalse()); pipeline1.addMaterialConfig(getPackageMaterialConfigWithAutoUpdateTrue()); pipeline1.add( new StageConfig( new CaseInsensitiveString("manual-stage"), new JobConfigs(), new Approval())); CruiseConfig config = new CruiseConfig(new PipelineConfigs(pipeline1)); assertThat(config.getAllUniqueMaterialsBelongingToAutoPipelines().size(), is(4)); }
public PipelineConfig addMaterialToPipeline(String pipelineName, MaterialConfig materialConfig) { CruiseConfig config = loadForEdit(); PipelineConfig pipelineConfig = config.pipelineConfigByName(new CaseInsensitiveString(pipelineName)); for (MaterialConfig materialConfig1 : new MaterialConfig[] {materialConfig}) { pipelineConfig.addMaterialConfig(materialConfig1); } writeConfigFile(config); return pipelineConfig; }
@Test public void shouldGenerateAMapOfAllPipelinesAndTheirParentDependencies_WhenThereAreRemotePipelinesInMap() { /* * -----+ p2 --> p4 * p1 * -----+ p3 --> remote-pipe-1 * * */ PipelineConfig p1 = createPipelineConfig("p1", "s1", "j1"); PipelineConfig p2 = createPipelineConfig("p2", "s2", "j1"); p2.addMaterialConfig( new DependencyMaterialConfig( new CaseInsensitiveString("p1"), new CaseInsensitiveString("s1"))); PipelineConfig p3 = createPipelineConfig("p3", "s3", "j1"); p3.addMaterialConfig( new DependencyMaterialConfig( new CaseInsensitiveString("p1"), new CaseInsensitiveString("s1"))); PipelineConfig p4 = createPipelineConfig("p4", "s4", "j1"); p4.addMaterialConfig( new DependencyMaterialConfig( new CaseInsensitiveString("p2"), new CaseInsensitiveString("s2"))); pipelines.addAll(Arrays.asList(p4, p2, p1, p3)); PipelineConfig remotePipe1 = createPipelineConfig("remote-pipe-1", "s5", "j1"); remotePipe1.addMaterialConfig( new DependencyMaterialConfig( new CaseInsensitiveString("p3"), new CaseInsensitiveString("s3"))); PartialConfig part = new PartialConfig(); part.getGroups().addPipeline("remoteGroup", remotePipe1); cruiseConfig = new BasicCruiseConfig(new BasicCruiseConfig(pipelines), part); Map<String, List<PipelineConfig>> expectedPipelines = cruiseConfig.generatePipelineVsDownstreamMap(); assertThat(expectedPipelines.size(), is(5)); assertThat(expectedPipelines.get("p1"), hasItems(p2, p3)); assertThat(expectedPipelines.get("p2"), hasItems(p4)); assertThat(expectedPipelines.get("p3"), hasItems(remotePipe1)); assertThat(expectedPipelines.get("remote-pipe-1").isEmpty(), is(true)); assertThat(expectedPipelines.get("p4").isEmpty(), is(true)); }
@Test public void shouldLoadShallowCloneFlagForGitMaterialsBaseOnTheirOwnPipelineConfig() throws IOException { GitTestRepo testRepo = new GitTestRepo(); PipelineConfig shallowPipeline = PipelineConfigMother.pipelineConfig( "shallowPipeline", new StageConfig( new CaseInsensitiveString("stage"), new JobConfigs(new JobConfig("job-one")))); shallowPipeline.materialConfigs().clear(); shallowPipeline.addMaterialConfig( new GitMaterialConfig(testRepo.projectRepositoryUrl(), null, true)); configHelper.addPipeline(shallowPipeline); PipelineConfig fullPipeline = PipelineConfigMother.pipelineConfig( "fullPipeline", new StageConfig( new CaseInsensitiveString("stage"), new JobConfigs(new JobConfig("job-one")))); fullPipeline.materialConfigs().clear(); fullPipeline.addMaterialConfig( new GitMaterialConfig(testRepo.projectRepositoryUrl(), null, false)); configHelper.addPipeline(fullPipeline); Pipeline shallowPipelineInstance = createAndLoadModifyOneFilePipeline(shallowPipeline); MaterialRevisions shallowRevisions = shallowPipelineInstance.getBuildCause().getMaterialRevisions(); assertThat( ((GitMaterial) shallowRevisions.getRevisions().get(0).getMaterial()).isShallowClone(), is(true)); Pipeline fullPipelineInstance = createAndLoadModifyOneFilePipeline(fullPipeline); MaterialRevisions fullRevisions = fullPipelineInstance.getBuildCause().getMaterialRevisions(); assertThat( ((GitMaterial) fullRevisions.getRevisions().get(0).getMaterial()).isShallowClone(), is(false)); }
@Test public void shouldLoadPipelineAlongwithBuildCauseHavingMaterialPasswordsPopulated() { PipelineConfig pipelineConfig = PipelineConfigMother.pipelineConfig( "last", new StageConfig( new CaseInsensitiveString("stage"), new JobConfigs(new JobConfig("job-one")))); pipelineConfig.materialConfigs().clear(); SvnMaterial onDirOne = MaterialsMother.svnMaterial("google.com", "dirOne", "loser", "boozer", false, "**/*.html"); P4Material onDirTwo = MaterialsMother.p4Material( "host:987654321", "zoozer", "secret", "through-the-window", true); onDirTwo.setFolder("dirTwo"); pipelineConfig.addMaterialConfig(onDirOne.config()); pipelineConfig.addMaterialConfig(onDirTwo.config()); configHelper.addPipeline(pipelineConfig); Pipeline building = PipelineMother.building(pipelineConfig); Pipeline pipeline = dbHelper.savePipelineWithMaterials(building); final long jobId = pipeline.getStages().get(0).getJobInstances().get(0).getId(); Pipeline loadedPipeline = (Pipeline) transactionTemplate.execute( new TransactionCallback() { public Object doInTransaction(TransactionStatus status) { return loader.pipelineWithPasswordAwareBuildCauseByBuildId(jobId); } }); MaterialRevisions revisions = loadedPipeline.getBuildCause().getMaterialRevisions(); assertThat( ((SvnMaterial) revisions.findRevisionFor(onDirOne).getMaterial()).getPassword(), is("boozer")); assertThat( ((P4Material) revisions.findRevisionFor(onDirTwo).getMaterial()).getPassword(), is("secret")); }
@Test // if other materials have expansion concept at some point, add more tests here public void shouldSetPasswordForExpandedSvnMaterial() { PipelineConfig pipelineConfig = PipelineConfigMother.pipelineConfig( "last", new StageConfig( new CaseInsensitiveString("stage"), new JobConfigs(new JobConfig("job-one")))); pipelineConfig.materialConfigs().clear(); SvnMaterialConfig materialConfig = svnRepo.materialConfig(); materialConfig.setConfigAttributes( Collections.singletonMap(SvnMaterialConfig.CHECK_EXTERNALS, String.valueOf(true))); materialConfig.setPassword("boozer"); pipelineConfig.addMaterialConfig(materialConfig); configHelper.addPipeline(pipelineConfig); Pipeline loadedPipeline = createAndLoadModifyOneFilePipeline(pipelineConfig); MaterialRevisions revisions = loadedPipeline.getBuildCause().getMaterialRevisions(); assertThat(revisions.getRevisions().size(), is(2)); assertThat( ((SvnMaterial) revisions.getRevisions().get(0).getMaterial()).getPassword(), is("boozer")); assertThat( ((SvnMaterial) revisions.getRevisions().get(1).getMaterial()).getPassword(), is("boozer")); }
@Test public void shouldSetAServerHealthMessageWhenMaterialForPipelineWithBuildCauseIsNotFound() throws IllegalArtifactLocationException, IOException { PipelineConfig pipelineConfig = PipelineConfigMother.pipelineConfig( "last", new StageConfig( new CaseInsensitiveString("stage"), new JobConfigs(new JobConfig("job-one")))); pipelineConfig.materialConfigs().clear(); SvnMaterialConfig onDirOne = MaterialConfigsMother.svnMaterialConfig( "google.com", "dirOne", "loser", "boozer", false, "**/*.html"); final P4MaterialConfig onDirTwo = MaterialConfigsMother.p4MaterialConfig( "host:987654321", "zoozer", "secret", "through-the-window", true); onDirTwo.setConfigAttributes(Collections.singletonMap(ScmMaterialConfig.FOLDER, "dirTwo")); pipelineConfig.addMaterialConfig(onDirOne); pipelineConfig.addMaterialConfig(onDirTwo); configHelper.addPipeline(pipelineConfig); Pipeline building = PipelineMother.building(pipelineConfig); final Pipeline pipeline = dbHelper.savePipelineWithMaterials(building); CruiseConfig cruiseConfig = configHelper.currentConfig(); PipelineConfig cfg = cruiseConfig.pipelineConfigByName(new CaseInsensitiveString("last")); cfg.removeMaterialConfig(cfg.materialConfigs().get(1)); configHelper.writeConfigFile(cruiseConfig); assertThat( serverHealthService.filterByScope(HealthStateScope.forPipeline("last")).size(), is(0)); final long jobId = pipeline.getStages().get(0).getJobInstances().get(0).getId(); Date currentTime = new Date(System.currentTimeMillis() - 1); Pipeline loadedPipeline = (Pipeline) transactionTemplate.execute( new TransactionCallback() { public Object doInTransaction(TransactionStatus status) { Pipeline loadedPipeline = null; try { loadedPipeline = loader.pipelineWithPasswordAwareBuildCauseByBuildId(jobId); fail( "should not have loaded pipeline with build-cause as one of the necessary materials was not found"); } catch (Exception e) { assertThat(e, is(instanceOf(StaleMaterialsOnBuildCause.class))); assertThat( e.getMessage(), is( "Cannot load job 'last/" + pipeline.getCounter() + "/stage/1/job-one' because material " + onDirTwo + " was not found in config.")); } return loadedPipeline; } }); assertThat(loadedPipeline, is(nullValue())); JobInstance reloadedJobInstance = jobInstanceService.buildById(jobId); assertThat(reloadedJobInstance.getState(), is(JobState.Completed)); assertThat(reloadedJobInstance.getResult(), is(JobResult.Failed)); assertThat( serverHealthService .filterByScope(HealthStateScope.forJob("last", "stage", "job-one")) .size(), is(1)); ServerHealthState error = serverHealthService .filterByScope(HealthStateScope.forJob("last", "stage", "job-one")) .get(0); assertThat( error, is( ServerHealthState.error( "Cannot load job 'last/" + pipeline.getCounter() + "/stage/1/job-one' because material " + onDirTwo + " was not found in config.", "Job for pipeline 'last/" + pipeline.getCounter() + "/stage/1/job-one' has been failed as one or more material configurations were either changed or removed.", HealthStateType.general(HealthStateScope.forJob("last", "stage", "job-one"))))); DateTime expiryTime = (DateTime) ReflectionUtil.getField(error, "expiryTime"); assertThat(expiryTime.toDate().after(currentTime), is(true)); assertThat( expiryTime.toDate().before(new Date(System.currentTimeMillis() + 5 * 60 * 1000 + 1)), is(true)); String logText = FileUtil.readToEnd(consoleService.findConsoleArtifact(reloadedJobInstance.getIdentifier())); assertThat( logText, containsString( "Cannot load job 'last/" + pipeline.getCounter() + "/stage/1/job-one' because material " + onDirTwo + " was not found in config.")); assertThat( logText, containsString( "Job for pipeline 'last/" + pipeline.getCounter() + "/stage/1/job-one' has been failed as one or more material configurations were either changed or removed.")); }