@Before public void setUp() { defaultServerConfig = new ServerConfig("artifactsDir", new SecurityConfig()); another = new ServerConfig("artifactsDir", new SecurityConfig()); }
@Test public void shouldNotThrowExceptionWhenUserProvidesValidPath() throws Exception { File file = new File(""); CruiseConfig cruiseConfig = new BasicCruiseConfig(); cruiseConfig.setServerConfig(new ServerConfig(file.getAbsolutePath() + "/logs", null)); ArtifactDirValidator dirValidator = new ArtifactDirValidator(); dirValidator.validate(cruiseConfig); } }
@Test public void shouldEnsureAgentAutoregisterKeyExists() throws Exception { ServerConfig serverConfig = new ServerConfig(); assertNull(serverConfig.getAgentAutoRegisterKey()); assertNotNull(serverConfig.getClass().getMethod("ensureAgentAutoregisterKeyExists").getAnnotation(PostConstruct.class)); serverConfig.ensureAgentAutoregisterKeyExists(); assertTrue(StringUtils.isNotBlank(serverConfig.getAgentAutoRegisterKey())); }
@Test public void shouldNotSaySecurityEnabledIfSecurityHasNoAuthenticatorsDefined() { ServerConfig serverConfig = new ServerConfig(); assertFalse("Security should not be enabled by default", serverConfig.isSecurityEnabled()); }
@Test public void shouldEnsureWebhookSecretExists() throws Exception { ServerConfig serverConfig = new ServerConfig(); assertNull(serverConfig.getWebhookSecret()); assertNotNull(serverConfig.getClass().getMethod("ensureWebhookSecretExists").getAnnotation(PostConstruct.class)); serverConfig.ensureWebhookSecretExists(); assertTrue(StringUtils.isNotBlank(serverConfig.getWebhookSecret())); }
@Test public void shouldEnsureTokenGenerationKeyExists() throws Exception { ServerConfig serverConfig = new ServerConfig(); assertNull(serverConfig.getTokenGenerationKey()); assertNotNull(serverConfig.getClass().getMethod("ensureTokenGenerationKeyExists").getAnnotation(PostConstruct.class)); serverConfig.ensureTokenGenerationKeyExists(); assertTrue(StringUtils.isNotBlank(serverConfig.getTokenGenerationKey())); } }
@Test public void shouldAllowArtifactPurgingIfPurgeParametersAreDefined() { another = new ServerConfig("artifacts", new SecurityConfig(), 10.0, 20.0); assertThat(another.isArtifactPurgingAllowed(), is(true)); another = new ServerConfig("artifacts", new SecurityConfig(), null, 20.0); assertThat(another.isArtifactPurgingAllowed(), is(false)); another = new ServerConfig("artifacts", new SecurityConfig(), 10.0, null); assertThat(another.isArtifactPurgingAllowed(), is(false)); another = new ServerConfig("artifacts", new SecurityConfig(), null, null); assertThat(another.isArtifactPurgingAllowed(), is(false)); }
@Test public void shouldGetTheDefaultJobTimeoutValue() { assertThat(new ServerConfig("artifacts", new SecurityConfig(), 10.0, 20.0).getJobTimeout(), is("0")); assertThat(new ServerConfig("artifacts", new SecurityConfig(), 10.0, 20.0, "30").getJobTimeout(), is("30")); }
@Test public void shouldThrowExceptionWhenUserProvidesPathPointToServerSandBox() { File file = new File(""); CruiseConfig cruiseConfig = new BasicCruiseConfig(); cruiseConfig.setServerConfig(new ServerConfig(file.getAbsolutePath(), null)); ArtifactDirValidator dirValidator = new ArtifactDirValidator(); try { dirValidator.validate(cruiseConfig); fail("should throw exception, see dot will make server check out the repository in the wrong place."); } catch (Exception e) { } }
@Test public void validate_shouldPassIfThePurgeStartAndPurgeUptoAreBothNotSet() { ServerConfig serverConfig = new ServerConfig("artifacts", new SecurityConfig()); serverConfig.validate(null); assertThat(serverConfig.errors().isEmpty(), is(true)); }
@Test public void validate_shouldFailIfThePurgeStartIs0SpecifiedButPurgeUptoIs() { ServerConfig serverConfig = new ServerConfig("artifacts", new SecurityConfig(), 0, 20.05, "30"); serverConfig.validate(null); assertThat(serverConfig.errors().isEmpty(), is(false)); assertThat(serverConfig.errors().on(ServerConfig.PURGE_START), is("Error in artifact cleanup values. The trigger value is has to be specified when a goal is set")); }
@Test public void shouldValidateThatTimeoutIsInvalidIfItsNotAValidNumber() { ServerConfig serverConfig = new ServerConfig("artifacts", new SecurityConfig(), 10, 20, "30M"); serverConfig.validate(null); assertThat(serverConfig.errors().isEmpty(), is(false)); assertThat(serverConfig.errors().on(ServerConfig.JOB_TIMEOUT), is("Timeout should be a valid number as it represents number of minutes")); }
@Test public void validate_shouldFailIfThePurgeStartIsBiggerThanPurgeUpto() { ServerConfig serverConfig = new ServerConfig("artifacts", new SecurityConfig(), 20.1, 20.05, "30"); serverConfig.validate(null); assertThat(serverConfig.errors().isEmpty(), is(false)); assertThat(serverConfig.errors().on(ServerConfig.PURGE_START), is("Error in artifact cleanup values. The trigger value (20.1GB) should be less than the goal (20.05GB)")); }
@Test public void shouldBeAbleToExplicitlyLockAPipeline() throws Exception { CruiseConfig config = GoConfigMother.configWithPipelines("pipeline1"); config.setServerConfig(new ServerConfig("foo", new SecurityConfig())); config.pipelineConfigByName(new CaseInsensitiveString("pipeline1")).lockExplicitly(); xmlWriter.write(config, output, false); assertThat(output.toString(), containsString("lockBehavior=\"" + PipelineConfig.LOCK_VALUE_LOCK_ON_FAILURE)); }
@Test public void shouldBeAbleToExplicitlyUnlockAPipeline() throws Exception { CruiseConfig config = GoConfigMother.configWithPipelines("pipeline1"); config.setServerConfig(new ServerConfig("foo", new SecurityConfig())); config.pipelineConfigByName(new CaseInsensitiveString("pipeline1")).unlockExplicitly(); xmlWriter.write(config, output, false); assertThat(output.toString(), containsString("lockBehavior=\"" + PipelineConfig.LOCK_VALUE_NONE)); }
@Test public void shouldReturnAnEmptyForSecureSiteUrlIfOnlySiteUrlIsConfigured() throws Exception { ServerConfig serverConfig = new ServerConfig(null,null, new ServerSiteUrlConfig("http://foo.bar:813"), new ServerSiteUrlConfig()); assertThat(serverConfig.getHttpsUrl(), is(new ServerSiteUrlConfig())); }
@Test public void shouldThrowExceptionWhenUserProvidesDot() throws Exception { CruiseConfig cruiseConfig = new BasicCruiseConfig(); cruiseConfig.setServerConfig(new ServerConfig(".", null)); ArtifactDirValidator dirValidator = new ArtifactDirValidator(); try { dirValidator.validate(cruiseConfig); fail("should throw exception, see dot will make server check out the repository in the wrong place."); } catch (Exception e) { } }
@Test public void shouldReturnTaskRepositoryLocation() { ServerConfig serverConfig = new ServerConfig(null,null, new ServerSiteUrlConfig("http://foo.bar:813"), new ServerSiteUrlConfig()); serverConfig.setCommandRepositoryLocation("foo"); assertThat(serverConfig.getCommandRepositoryLocation(), is("foo")); }
@Test public void shouldIgnoreErrorsFieldOnEquals() throws Exception { ServerConfig one = new ServerConfig(new SecurityConfig(), new MailHost(new GoCipher()), new ServerSiteUrlConfig("siteURL"), new ServerSiteUrlConfig("secureURL")); one.addError("siteUrl", "I dont like this url"); assertThat(one, is(new ServerConfig(new SecurityConfig(), new MailHost(new GoCipher()), new ServerSiteUrlConfig("siteURL"), new ServerSiteUrlConfig("secureURL")))); }
@Test public void shouldValidateRoleNamesInTemplateAdminAuthorization() { BasicCruiseConfig cruiseConfig = GoConfigMother.defaultCruiseConfig(); ServerConfig serverConfig = new ServerConfig(new SecurityConfig(new AdminsConfig(new AdminUser(new CaseInsensitiveString("admin")))), null); cruiseConfig.setServerConfig(serverConfig); GoConfigMother.enableSecurityWithPasswordFilePlugin(cruiseConfig); RoleConfig roleConfig = new RoleConfig(new CaseInsensitiveString("non-existent-role"), new RoleUser("non-existent-user")); PipelineTemplateConfig template = new PipelineTemplateConfig(new CaseInsensitiveString("template"), new Authorization(new AdminsConfig(new AdminRole(roleConfig))), StageConfigMother.manualStage("stage2"), StageConfigMother.manualStage("stage")); template.validate(ConfigSaveValidationContext.forChain(cruiseConfig)); assertThat(template.getAllErrors().get(0).getAllOn("name"), is(Arrays.asList("Role \"non-existent-role\" does not exist."))); }