public static ArtifactStore fromJSON(JsonReader jsonReader) { ArtifactStore artifactStore = new ArtifactStore( jsonReader.getString("id"), jsonReader.getString("plugin_id")); artifactStore.addConfigurations(ConfigurationPropertyRepresenter.fromJSONArray(jsonReader, "properties")); return artifactStore; } }
@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 shouldReturnFalseIfPluginInfoIsDefined() { final ArtifactStore artifactStore = new ArtifactStore("id", "plugin_id"); assertFalse(artifactStore.hasPluginInfo()); }
@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 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 addConfigurations_shouldAddConfigurationsWithEncryptedValue() { ConfigurationProperty property = create("username", true, "some_value"); ArtifactStore artifactStore = new ArtifactStore("id", "plugin_id"); artifactStore.addConfigurations(Arrays.asList(property)); assertThat(artifactStore.size(), is(1)); assertThat(artifactStore, contains(create("username", true, "some_value"))); }
@Test public void addConfigurations_shouldAddConfigurationsWithValue() { ConfigurationProperty property = create("username", false, "some_value"); ArtifactStore artifactStore = new ArtifactStore("id", "plugin_id"); artifactStore.addConfigurations(Arrays.asList(property)); assertThat(artifactStore.size(), is(1)); assertThat(artifactStore, contains(create("username", false, "some_value"))); }
@Test public void validate_shouldValidateArtifactPropertiesConfigurationKeyUniqueness() { final ValidationContext validationContext = mock(ValidationContext.class); final PluggableArtifactConfig artifactConfig = new PluggableArtifactConfig("Artifact-ID", "Store-ID", create("Foo", false, "Bar"), create("Foo", true, "Bar")); final ArtifactStores artifactStores = mock(ArtifactStores.class); assertFalse(artifactConfig.hasErrors()); when(validationContext.artifactStores()).thenReturn(artifactStores); when(artifactStores.find("Store-ID")).thenReturn(new ArtifactStore("Store-ID", "pluginId")); artifactConfig.validate(validationContext); Configuration configuration = artifactConfig.getConfiguration(); assertThat(configuration.get(0).errors().getAllOn("configurationKey"), is(Arrays.asList("Duplicate key 'Foo' found for Pluggable Artifact"))); assertThat(configuration.get(1).errors().getAllOn("configurationKey"), is(Arrays.asList("Duplicate key 'Foo' found for Pluggable Artifact"))); }
@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 shouldValidateArtifactStoreIdPattern() { ArtifactStore store = new ArtifactStore("!123", "docker"); store.validate(null); assertThat(store.errors().size(), is(1)); assertThat(store.errors().on(ArtifactStore.ID), is("Invalid id '!123'. 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 shouldNotAllowNullPluginIdOrArtifactStoreId() { ArtifactStore store = new ArtifactStore(); store.validate(null); assertThat(store.errors().size(), is(2)); assertThat(store.errors().on(ArtifactStore.PLUGIN_ID), is("Artifact store cannot have a blank plugin id.")); assertThat(store.errors().on(ArtifactStore.ID), is("Artifact store cannot have a blank id.")); }
@Test public void addConfigurations_shouldLeaveUserEnteredValuesAsIsIfPluginIsMissing() throws CryptoException { ArtifactMetadataStore.instance().remove("cd.go.s3"); PluggableArtifactConfig pluggableArtifactConfig = new PluggableArtifactConfig("id", "storeId"); ArrayList<ConfigurationProperty> configurationProperties = new ArrayList<>(); configurationProperties.add(ConfigurationPropertyMother.create("plain", false, "plain")); configurationProperties.add(ConfigurationPropertyMother.create("secure", true, new GoCipher().encrypt("password"))); BasicCruiseConfig cruiseConfig = GoConfigMother.defaultCruiseConfig(); cruiseConfig.getArtifactStores().add(new ArtifactStore("storeId", "cd.go.s3")); pluggableArtifactConfig.addConfigurations(configurationProperties); assertThat(pluggableArtifactConfig.getConfiguration(), is(configurationProperties)); }
@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 shouldReturnTrueIfPluginInfoIsDefined() { final ArtifactPluginInfo pluginInfo = new ArtifactPluginInfo(pluginDescriptor("plugin_id"), null, null, null, null, null); store.setPluginInfo(pluginInfo); final ArtifactStore artifactStore = new ArtifactStore("id", "plugin_id"); assertTrue(artifactStore.hasPluginInfo()); }
@Test public void shouldValidateConfigPropertyNameUniqueness() { ConfigurationProperty prop1 = ConfigurationPropertyMother.create("USERNAME"); ConfigurationProperty prop2 = ConfigurationPropertyMother.create("USERNAME"); ArtifactStore store = new ArtifactStore("s3.plugin", "cd.go.s3.plugin", prop1, prop2); store.validate(null); assertThat(store.errors().size(), is(0)); assertThat(prop1.errors().size(), is(1)); assertThat(prop2.errors().size(), is(1)); assertThat(prop1.errors().on(ConfigurationProperty.CONFIGURATION_KEY), is("Duplicate key 'USERNAME' found for Artifact store 's3.plugin'")); assertThat(prop2.errors().on(ConfigurationProperty.CONFIGURATION_KEY), is("Duplicate key 'USERNAME' found for Artifact store 's3.plugin'")); }
@Test public void postConstruct_shouldIgnoreEncryptionIfPluginInfoIsNotDefined() { ArtifactStore artifactStore = new ArtifactStore("id", "plugin_id", new ConfigurationProperty(new ConfigurationKey("password"), new ConfigurationValue("pass"))); artifactStore.encryptSecureConfigurations(); assertThat(artifactStore.size(), is(1)); assertFalse(artifactStore.first().isSecure()); }
@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)); }
@Test public void postConstruct_shouldEncryptSecureConfigurations() { final PluggableInstanceSettings storeConfig = new PluggableInstanceSettings( Arrays.asList(new PluginConfiguration("password", new Metadata(true, true))) ); final ArtifactPluginInfo pluginInfo = new ArtifactPluginInfo(pluginDescriptor("plugin_id"), storeConfig, null, null, null, null); store.setPluginInfo(pluginInfo); ArtifactStore artifactStore = new ArtifactStore("id", "plugin_id", new ConfigurationProperty(new ConfigurationKey("password"), new ConfigurationValue("pass"))); artifactStore.encryptSecureConfigurations(); assertThat(artifactStore.size(), is(1)); assertTrue(artifactStore.first().isSecure()); }