private void encryptPasswords(File configFile) throws Exception { String currentContent = FileUtils.readFileToString(configFile, UTF_8); GoConfigHolder configHolder = magicalGoConfigXmlLoader.loadConfigHolder(currentContent); ByteArrayOutputStream stream = new ByteArrayOutputStream(); magicalGoConfigXmlWriter.write(configHolder.configForEdit, stream, true); String postEncryptContent = new String(stream.toByteArray()); if (!currentContent.equals(postEncryptContent)) { LOGGER.debug("[Encrypt] Writing config to file"); FileUtils.writeStringToFile(configFile, postEncryptContent); } }
private GoConfigHolder reloadConfig(String configXml, final List<PartialConfig> partials) { try { return loader.loadConfigHolder(configXml, cruiseConfig -> cruiseConfig.setPartials(partials)); } catch (Exception e) { LOGGER.debug("[CONFIG_MERGE] Post merge validation failed"); throw new ConfigMergePostValidationException(e.getMessage(), e); } }
private GoConfigHolder internalLoad(final String content, final ConfigModifyingUser configModifyingUser, final List<PartialConfig> partials) throws Exception { GoConfigHolder configHolder = magicalGoConfigXmlLoader.loadConfigHolder(content, cruiseConfig -> cruiseConfig.setPartials(partials)); CruiseConfig config = configHolder.config; checkinConfigToGitRepo(partials, config, content, configHolder.configForEdit.getMd5(), configModifyingUser.getUserName()); return configHolder; }
@Test public void shouldAllowOnlyThreeValuesForLockBehavior() throws Exception { xmlLoader.loadConfigHolder(pipelineWithAttributes("name=\"p1\" lockBehavior=\"" + LOCK_VALUE_LOCK_ON_FAILURE + "\"", CONFIG_SCHEMA_VERSION)); xmlLoader.loadConfigHolder(pipelineWithAttributes("name=\"p2\" lockBehavior=\"" + LOCK_VALUE_UNLOCK_WHEN_FINISHED + "\"", CONFIG_SCHEMA_VERSION)); xmlLoader.loadConfigHolder(pipelineWithAttributes("name=\"p3\" lockBehavior=\"" + LOCK_VALUE_NONE + "\"", CONFIG_SCHEMA_VERSION)); xmlLoader.loadConfigHolder(pipelineWithAttributes("name=\"pipelineWithNoLockBehaviorDefined\"", CONFIG_SCHEMA_VERSION)); assertXsdFailureDuringLoad(pipelineWithAttributes("name=\"pipelineWithWrongLockBehavior\" lockBehavior=\"some-random-value\"", CONFIG_SCHEMA_VERSION), "Value 'some-random-value' is not facet-valid with respect to enumeration '[lockOnFailure, unlockWhenFinished, none]'. It must be a value from the enumeration."); }
public static GoConfigHolder loadWithMigration(InputStream input, final ConfigElementImplementationRegistry registry) throws Exception { File tempFile = TestFileUtil.createTempFile("cruise-config.xml"); try { MagicalGoConfigXmlLoader xmlLoader = new MagicalGoConfigXmlLoader(new ConfigCache(), registry); FileUtils.copyInputStreamToFile(input, tempFile); migrate(tempFile); return xmlLoader.loadConfigHolder(FileUtils.readFileToString(tempFile, UTF_8)); } finally { FileUtils.deleteQuietly(tempFile); } }
@Test public void shouldGenerateSCMIdWhenMissing() throws Exception { String xml = "<cruise schemaVersion='" + GoConstants.CONFIG_SCHEMA_VERSION + "'><scms>\n" + SCM_WITH_MISSING_ID + " </scms></cruise>"; GoConfigHolder configHolder = xmlLoader.loadConfigHolder(xml); assertThat(configHolder.config.getSCMs().get(0).getId(), is(notNullValue())); }
@Test public void shouldThrowXsdValidationWhenSCMNameIsInvalid() throws Exception { String xml = "<cruise schemaVersion='" + GoConstants.CONFIG_SCHEMA_VERSION + "'><scms>\n" + SCM_WITH_INVALID_NAME + " </scms></cruise>"; try { xmlLoader.loadConfigHolder(xml); fail("should have thrown XsdValidationException"); } catch (XsdValidationException e) { assertThat(e.getMessage(), is("Name is invalid. \"name with space\" should conform to the pattern - [a-zA-Z0-9_\\-]{1}[a-zA-Z0-9_\\-.]*")); } }
@Test public void shouldThrowXsdValidationWhenSCMNameIsMissing() throws Exception { String xml = "<cruise schemaVersion='" + GoConstants.CONFIG_SCHEMA_VERSION + "'><scms>\n" + SCM_WITH_MISSING_NAME + " </scms></cruise>"; try { xmlLoader.loadConfigHolder(xml); fail("should have thrown XsdValidationException"); } catch (XsdValidationException e) { assertThat(e.getMessage(), is("\"Name\" is required for Scm")); } }
@Test public void shouldNotReturnNullWhenTrackingToolIsNotConfigured() throws Exception { CruiseConfig cruiseConfig = loader.loadConfigHolder(ONE_PIPELINE).config; PipelineConfig pipelineConfig = cruiseConfig.pipelineConfigByName(new CaseInsensitiveString("pipeline1")); TrackingTool trackingTool = pipelineConfig.trackingTool(); assertThat(trackingTool, is(not(nullValue()))); assertThat(trackingTool.getLink(), is("")); assertThat(trackingTool.getRegex(), is("")); } }
@Test public void shouldWriteConfigWithMultipleGroups() throws Exception { CruiseConfig cruiseConfig = ConfigMigrator.load(ConfigFileFixture.PIPELINE_GROUPS); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); xmlWriter.write(cruiseConfig, buffer, false); final ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer.toByteArray()); CruiseConfig config = xmlLoader.loadConfigHolder(IOUtils.toString(inputStream, UTF_8)).config; assertThat(config.getGroups().size(), is(2)); assertThat(config.getGroups().first().getGroup(), is("studios")); }
public static CruiseConfig load(String content) { try { ConfigElementImplementationRegistry registry = ConfigElementImplementationRegistryMother.withNoPlugins(); return new MagicalGoConfigXmlLoader(new ConfigCache(), registry).loadConfigHolder(migrate(content)).config; } catch (Exception e) { throw new RuntimeException(e); } } }
@Test public void shouldSetConfigOriginInCruiseConfig_AfterLoadingConfigFile() throws Exception { GoConfigHolder goConfigHolder = xmlLoader.loadConfigHolder(CONFIG, new MagicalGoConfigXmlLoader.Callback() { @Override public void call(CruiseConfig cruiseConfig) { cruiseConfig.setPartials(asList(new PartialConfig())); } }); assertThat(goConfigHolder.config.getOrigin(), is(new MergeConfigOrigin())); assertThat(goConfigHolder.configForEdit.getOrigin(), is(new FileConfigOrigin())); }
@Test public void shouldSetConfigOriginInPipeline_AfterLoadingConfigFile() throws Exception { CruiseConfig cruiseConfig = xmlLoader.loadConfigHolder(CONFIG).config; PipelineConfig pipelineConfig1 = cruiseConfig.pipelineConfigByName(new CaseInsensitiveString("pipeline1")); assertThat(pipelineConfig1.getOrigin(), is(new FileConfigOrigin())); }
@Test public void shouldLoadTrackingtool() throws Exception { CruiseConfig cruiseConfig = loader.loadConfigHolder(CONFIG_WITH_TRACKINGTOOL).config; PipelineConfig pipelineConfig = cruiseConfig.pipelineConfigByName(new CaseInsensitiveString("pipeline1")); TrackingTool trackingTool = pipelineConfig.trackingTool(); assertThat(trackingTool.getLink(), is("http://mingle05/projects/cce/cards/${ID}")); assertThat(trackingTool.getRegex(), is("(evo-\\d+)")); }
@Test public void shouldGenerateRepoAndPkgIdWhenMissing() throws Exception { String xml = "<cruise schemaVersion='" + CONFIG_SCHEMA_VERSION + "'><repositories>\n" + withPackages(REPO_WITH_MISSING_ID, PACKAGE_WITH_MISSING_ID) + " </repositories></cruise>"; GoConfigHolder configHolder = xmlLoader.loadConfigHolder(xml); assertThat(configHolder.config.getPackageRepositories().get(0).getId(), is(notNullValue())); assertThat(configHolder.config.getPackageRepositories().get(0).getPackages().get(0).getId(), is(notNullValue())); }
@Test public void shouldLoadConfigWithConfigRepo() throws Exception { CruiseConfig cruiseConfig = xmlLoader.loadConfigHolder(ONE_CONFIG_REPO).config; assertThat(cruiseConfig.getConfigRepos().size(), is(1)); ConfigRepoConfig configRepo = cruiseConfig.getConfigRepos().get(0); assertThat(configRepo.getMaterialConfig(), is(new GitMaterialConfig("https://github.com/tomzo/gocd-indep-config-part.git"))); }
@Test public void shouldDisallowModificationOfTokenGenerationKeyWhileTheServerIsOnline() throws Exception { xmlLoader.loadConfigHolder(configWithTokenGenerationKey("something")); systemEnvironment.setProperty("go.enforce.server.immutability", "Y"); assertFailureDuringLoad(configWithTokenGenerationKey("something-else"), RuntimeException.class, "The value of 'tokenGenerationKey' cannot be modified while the server is online. If you really want to make this change, you may do so while the server is offline. Please note: updating 'tokenGenerationKey' will invalidate all registration tokens issued to the agents so far."); }
private CruiseConfig cruiseConfigWithProperties(ArtifactPropertyConfig... artifactPropertyConfigs) throws Exception { CruiseConfig cruiseConfig = loader.loadConfigHolder(ONE_PIPELINE).configForEdit; JobConfig jobConfig = BuildPlanMother.withArtifactPropertiesGenerator(artifactPropertyConfigs); cruiseConfig.pipelineConfigByName(new CaseInsensitiveString("pipeline1")).first().allBuildPlans().add(jobConfig); return cruiseConfig; }
@Test public void shouldSupportEmptyPipelineGroup() throws Exception { PipelineConfigs group = new BasicPipelineConfigs("defaultGroup", new Authorization()); CruiseConfig config = new BasicCruiseConfig(group); ByteArrayOutputStream stream = new ByteArrayOutputStream(); new MagicalGoConfigXmlWriter(configCache, ConfigElementImplementationRegistryMother.withNoPlugins()).write(config, stream, true); GoConfigHolder configHolder = new MagicalGoConfigXmlLoader(new ConfigCache(), ConfigElementImplementationRegistryMother.withNoPlugins()) .loadConfigHolder(stream.toString()); assertThat(configHolder.config.findGroup("defaultGroup"), is(group)); }
@Test public void shouldAllowUnorderedRoleAndUserInXsd() throws Exception { CruiseConfig config = new MagicalGoConfigXmlLoader(new ConfigCache(), ConfigElementImplementationRegistryMother.withNoPlugins()).loadConfigHolder(ConfigMigrator.migrate( ConfigFileFixture.DEFAULT_XML_WITH_2_AGENTS)).configForEdit; config.setServerConfig(new ServerConfig("dir", security(null, admins(role("role2"), user("jez"), role("role1"))))); new MagicalGoConfigXmlWriter(new ConfigCache(), ConfigElementImplementationRegistryMother.withNoPlugins()).write(config, new ByteArrayOutputStream(), false); } }