@Test public void findByArtifactId_shouldReturnPluggableArtifactConfigs() { ArtifactConfigs allConfigs = new ArtifactConfigs(); allConfigs.add(new PluggableArtifactConfig("s3", "cd.go.s3")); allConfigs.add(new PluggableArtifactConfig("docker", "cd.go.docker")); final PluggableArtifactConfig s3 = allConfigs.findByArtifactId("s3"); assertThat(s3, is(new PluggableArtifactConfig("s3", "cd.go.s3"))); }
@Test public void shouldSerializeToJson() { final PluggableArtifactConfig config = new PluggableArtifactConfig("id1", "Store-ID", create("Foo", false, "Bar")); final String actual = config.toJSON(); assertThat(actual, is("{\"configuration\":{\"Foo\":\"Bar\"},\"id\":\"id1\",\"storeId\":\"Store-ID\"}")); }
@Test public void findByArtifactId_shouldReturnNullWhenPluggableArtifactConfigNotExistWithGivenId() { ArtifactConfigs allConfigs = new ArtifactConfigs(); allConfigs.add(new PluggableArtifactConfig("s3", "cd.go.s3")); allConfigs.add(new PluggableArtifactConfig("docker", "cd.go.docker")); final PluggableArtifactConfig s3 = allConfigs.findByArtifactId("foo"); assertNull(s3); } }
@Test public void addConfigurations_shouldLeaveUserEnteredValuesAsIsIfArtifactStoreIsNull() throws CryptoException { PluggableArtifactConfig pluggableArtifactConfig = new PluggableArtifactConfig("id", "non-existent-store-id"); ArrayList<ConfigurationProperty> configurationProperties = new ArrayList<>(); configurationProperties.add(ConfigurationPropertyMother.create("plain", false, "plain")); configurationProperties.add(ConfigurationPropertyMother.create("secure", true, new GoCipher().encrypt("password"))); pluggableArtifactConfig.addConfigurations(configurationProperties); assertThat(pluggableArtifactConfig.getConfiguration(), is(configurationProperties)); }
@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 hasValidPluginAndStore_shouldReturnTrueIfPluginAndStoreExist() { PluggableArtifactConfig pluggableArtifactConfig = new PluggableArtifactConfig("dist", "s3"); assertTrue(pluggableArtifactConfig.hasValidPluginAndStore(new ArtifactStore("s3", "cd.go.s3"))); } }
@Test public void hasValidPluginAndStore_shouldReturnFalseIfStoreDoesNotExist() { PluggableArtifactConfig pluggableArtifactConfig = new PluggableArtifactConfig("dist", "s3"); assertFalse(pluggableArtifactConfig.hasValidPluginAndStore(new ArtifactStore("docker", "cd.go.docker"))); }
@Test public void shouldCreatePluggableArtifact() { final PluggableArtifactConfig artifactConfig = new PluggableArtifactConfig("Artifact-ID", "Store-ID", create("Foo", false, "Bar")); assertThat(artifactConfig.getId(), is("Artifact-ID")); assertThat(artifactConfig.getStoreId(), is("Store-ID")); assertThat(artifactConfig.getArtifactType(), is(ArtifactType.external)); assertThat(artifactConfig.getArtifactTypeValue(), is("Pluggable Artifact")); assertThat(artifactConfig.getConfiguration().get(0), is(create("Foo", false, "Bar"))); }
@Test public void validateTree_shouldValidatePresenceOfId() { final PluggableArtifactConfig artifactConfig = new PluggableArtifactConfig("", "s3"); final ArtifactStores artifactStores = new ArtifactStores(new ArtifactStore("s3", "cd.go.s3")); final boolean result = artifactConfig.validateTree(ValidationContextMother.validationContext(artifactStores)); assertFalse(result); assertThat(artifactConfig.errors().getAllOn("id"), is(Arrays.asList("\"Id\" is required for PluggableArtifact", "Invalid pluggable artifact id name ''. This must be alphanumeric and can contain underscores and periods (however, it cannot start with a period). The maximum allowed length is 255 characters."))); }
@Test public void validateTree_presenceOfStoreIdInArtifactStores() { PluggableArtifactConfig artifactConfig = new PluggableArtifactConfig("installer", ""); final ArtifactStores artifactStores = new ArtifactStores(new ArtifactStore("docker", "cd.go.docker")); final boolean result = artifactConfig.validateTree(ValidationContextMother.validationContext(artifactStores)); assertFalse(result); assertThat(artifactConfig.errors().getAllOn("storeId"), is(Arrays.asList("\"Store id\" is required for PluggableArtifact"))); }
@Test public void validate_shouldAddAnErrorIfArtifactIdIsInvalid() { PluggableArtifactConfig artifactConfig = new PluggableArtifactConfig("asf@%", "f"); final ArtifactStores artifactStores = new ArtifactStores(new ArtifactStore("docker", "cd.go.docker")); final boolean result = artifactConfig.validateTree(ValidationContextMother.validationContext(artifactStores)); assertFalse(result); assertThat(artifactConfig.errors().getAllOn("id"), is(Arrays.asList("Invalid pluggable artifact id name 'asf@%'. This must be alphanumeric and can contain underscores and periods (however, it cannot start with a period). The maximum allowed length is 255 characters."))); }
@Test public void validate_shouldNotErrorWhenArtifactPropertiesConfigurationIsSameForDifferentStores() { final PluggableArtifactConfig existingConfig = new PluggableArtifactConfig("id1", "storeId1", create("Foo", false, "Bar")); final List<ArtifactConfig> artifactConfigConfigs = Arrays.asList(existingConfig); final PluggableArtifactConfig newConfig = new PluggableArtifactConfig("id2", "storeId2", create("Foo", false, "Bar")); newConfig.validateUniqueness(artifactConfigConfigs); assertFalse(newConfig.hasErrors()); assertFalse(existingConfig.hasErrors()); assertNull(newConfig.errors().on("id")); assertNull(existingConfig.errors().on("id")); }
@Test public void hasValidPluginAndStore_shouldReturnFalseIfPluginDoesNotExist() { ArtifactMetadataStore.instance().remove("cd.go.s3"); PluggableArtifactConfig pluggableArtifactConfig = new PluggableArtifactConfig("dist", "s3"); assertFalse(pluggableArtifactConfig.hasValidPluginAndStore(new ArtifactStore("s3", "cd.go.s3"))); }
@Test public void validateTree_shouldValidateNullId() { PluggableArtifactConfig artifactConfig = new PluggableArtifactConfig(null, "s3"); final ArtifactStores artifactStores = new ArtifactStores(new ArtifactStore("s3", "cd.go.s3")); final boolean result = artifactConfig.validateTree(ValidationContextMother.validationContext(artifactStores)); assertFalse(result); }
@Test public void shouldBeValidIfFetchArtifactUsingAStageBeforeCurrentInTheSamePipeline() { upstream.getStage("stage").jobConfigByConfigName("job").artifactConfigs() .add(new PluggableArtifactConfig("s3", "cd.go.s3")); FetchPluggableArtifactTask task = new FetchPluggableArtifactTask(new CaseInsensitiveString("upstream"), new CaseInsensitiveString("stage"), new CaseInsensitiveString("job"), "s3"); task.validate(ConfigSaveValidationContext.forChain(config, upstream, upstream.getStage(new CaseInsensitiveString("up-stage1")))); assertThat(task.errors().isEmpty(), is(true)); }
@Test public void shouldBeValidIfFetchArtifactUsingADependantPipeline() { upstream.getStage("up-stage1").jobConfigByConfigName("up-job1").artifactConfigs() .add(new PluggableArtifactConfig("s3", "cd.go.s3")); FetchPluggableArtifactTask task = new FetchPluggableArtifactTask(new CaseInsensitiveString("upstream"), new CaseInsensitiveString("up-stage1"), new CaseInsensitiveString("up-job1"), "s3"); task.validate(ConfigSaveValidationContext.forChain(config, downstream, downstream.getStage(new CaseInsensitiveString("stage")))); assertThat(task.errors().isEmpty(), is(true)); }
@Test public void shouldBeValidWhenFetchArtifactIsFromAnyAncestorStage_onTheUpstreamPipeline() { uppestStream.getStage("uppest-stage2").jobConfigByConfigName("uppest-job2").artifactConfigs() .add(new PluggableArtifactConfig("s3", "cd.go.s3")); FetchPluggableArtifactTask task = new FetchPluggableArtifactTask(new CaseInsensitiveString("uppest_stream/upstream"), new CaseInsensitiveString("uppest-stage2"), new CaseInsensitiveString("uppest-job2"), "s3"); task.validate(ConfigSaveValidationContext.forChain(config, new BasicPipelineConfigs(), downstream, downstream.getStage(new CaseInsensitiveString("stage")))); assertThat(task.errors().isEmpty(), is(true)); }
@Test public void validate_shouldNotErrorWhenReferencingFilePipelineFromConfigRepositoryPipeline() { uppestStream.getStage("uppest-stage2").jobConfigByConfigName("uppest-job2").artifactConfigs() .add(new PluggableArtifactConfig("s3", "cd.go.s3")); uppestStream.setOrigin(new FileConfigOrigin()); downstream.setOrigin(new RepoConfigOrigin()); FetchPluggableArtifactTask task = new FetchPluggableArtifactTask(new CaseInsensitiveString("uppest_stream/upstream"), new CaseInsensitiveString("uppest-stage2"), new CaseInsensitiveString("uppest-job2"), "s3"); task.validate(ConfigSaveValidationContext.forChain(config, new BasicPipelineConfigs(), downstream, downstream.getStage(new CaseInsensitiveString("stage")))); assertThat(task.errors().isEmpty(), is(true)); }
@Test public void validate_shouldNotErrorWhenReferencingFilePipelineFromFilePipeline() { uppestStream.getStage("uppest-stage2").jobConfigByConfigName("uppest-job2").artifactConfigs() .add(new PluggableArtifactConfig("s3", "cd.go.s3")); uppestStream.setOrigin(new FileConfigOrigin()); downstream.setOrigin(new FileConfigOrigin()); FetchPluggableArtifactTask task = new FetchPluggableArtifactTask(new CaseInsensitiveString("uppest_stream/upstream"), new CaseInsensitiveString("uppest-stage2"), new CaseInsensitiveString("uppest-job2"), "s3"); task.validate(ConfigSaveValidationContext.forChain(config, new BasicPipelineConfigs(), downstream, downstream.getStage(new CaseInsensitiveString("stage")))); assertThat(task.errors().isEmpty(), is(true)); }
@Test public void validate_shouldNotErrorWhenReferencingConfigRepositoryPipelineFromConfigRepositoryPipeline() { uppestStream.getStage("uppest-stage2").jobConfigByConfigName("uppest-job2").artifactConfigs() .add(new PluggableArtifactConfig("s3", "cd.go.s3")); uppestStream.setOrigin(new RepoConfigOrigin()); downstream.setOrigin(new RepoConfigOrigin()); config.setArtifactStores(new ArtifactStores(new ArtifactStore("s3", "foo.plugin"))); FetchPluggableArtifactTask task = new FetchPluggableArtifactTask(new CaseInsensitiveString("uppest_stream/upstream"), new CaseInsensitiveString("uppest-stage2"), new CaseInsensitiveString("uppest-job2"), "s3"); task.validate(ConfigSaveValidationContext.forChain(config, new BasicPipelineConfigs(), downstream, downstream.getStage(new CaseInsensitiveString("stage")))); assertThat(task.errors().isEmpty(), is(true)); }