@Override public void addEnvironmentVariable(String name, String value) { this.getFirstEditablePart().addEnvironmentVariable(name, value); }
@Override public void addEnvironmentVariable(EnvironmentVariableConfig variableConfig) { this.getFirstEditablePart().addEnvironmentVariable(variableConfig); }
@Test public void shouldReturnFalseThatNotEmptyWhenHasVariable() { environmentConfig.addEnvironmentVariable("k","v"); assertThat(environmentConfig.isEnvironmentEmpty(), is(false)); }
@Test public void shouldAddEnvironmentVariablesToEnvironmentVariableContextFrom2Parts() throws Exception { pairEnvironmentConfig.get(0).addEnvironmentVariable("variable-name1", "variable-value1"); pairEnvironmentConfig.get(1).addEnvironmentVariable("variable-name2", "variable-value2"); EnvironmentVariableContext context = pairEnvironmentConfig.createEnvironmentContext(); assertThat(context.getProperty("variable-name1"), is("variable-value1")); assertThat(context.getProperty("variable-name2"), is("variable-value2")); } @Test
@Test public void shouldAddDeduplicatedEnvironmentVariablesToEnvironmentVariableContextFrom2Parts() throws Exception { pairEnvironmentConfig.get(0).addEnvironmentVariable("variable-name1", "variable-value1"); pairEnvironmentConfig.get(1).addEnvironmentVariable("variable-name1", "variable-value1"); assertThat(pairEnvironmentConfig.getVariables().size(), is(1)); EnvironmentVariableContext context = pairEnvironmentConfig.createEnvironmentContext(); assertThat(context.getProperty("variable-name1"), is("variable-value1")); }
@Test public void shouldHaveVariablesFrom2Parts() throws Exception { pairEnvironmentConfig.get(0).addEnvironmentVariable("variable-name1", "variable-value1"); pairEnvironmentConfig.get(1).addEnvironmentVariable("variable-name2", "variable-value2"); assertTrue(pairEnvironmentConfig.hasVariable("variable-name1")); assertTrue(pairEnvironmentConfig.hasVariable("variable-name2")); } @Test
@Test public void shouldCreateErrorsForInconsistentEnvironmentVariables() throws Exception { pairEnvironmentConfig.get(0).addEnvironmentVariable("variable-name1", "variable-value1"); pairEnvironmentConfig.get(1).addEnvironmentVariable("variable-name1", "variable-value2"); pairEnvironmentConfig.validate(ConfigSaveValidationContext.forChain(pairEnvironmentConfig)); assertThat(pairEnvironmentConfig.errors().isEmpty(), is(false)); assertThat(pairEnvironmentConfig.errors().on(MergeEnvironmentConfig.CONSISTENT_KV), Matchers.is("Environment variable 'variable-name1' is defined more than once with different values")); }
@Test public void shouldUpdateEnvironmentVariables() { environmentConfig.addEnvironmentVariable("hello", "world"); environmentConfig.setConfigAttributes(Collections.singletonMap(BasicEnvironmentConfig.VARIABLES_FIELD, Arrays.asList(envVar("foo", "bar"), envVar("baz", "quux")))); assertThat(environmentConfig.getVariables(), hasItem(new EnvironmentVariableConfig("foo", "bar"))); assertThat(environmentConfig.getVariables(), hasItem(new EnvironmentVariableConfig("baz", "quux"))); assertThat(environmentConfig.getVariables().size(), is(2)); }
@Test public void shouldSupportEnvironmentVariablesInEnvironment() throws Exception { String content = configWithEnvironmentsAndAgents( "<environments>" + " <environment name='uat'>" + " <environmentvariables> " + " <variable name='VAR_NAME_1'><value>variable_name_value_1</value></variable>" + " <variable name='CRUISE_ENVIRONEMNT_NAME'><value>variable_name_value_2</value></variable>" + " </environmentvariables> " + " </environment>" + "</environments>", "<agents>" + " <agent uuid='1' hostname='test1.com' ipaddress='192.168.0.1' isDisabled='true' />" + "</agents>"); CruiseConfig config = ConfigMigrator.loadWithMigration(content).config; EnvironmentConfig element = new BasicEnvironmentConfig(new CaseInsensitiveString("uat")); element.addEnvironmentVariable("VAR_NAME_1", "variable_name_value_1"); element.addEnvironmentVariable("CRUISE_ENVIRONEMNT_NAME", "variable_name_value_2"); assertThat(config.getEnvironments(), hasItem(element)); }
@Test public void shouldAddEnvironmentVariablesToEnvironmentVariableContext() throws Exception { EnvironmentConfig another = new BasicEnvironmentConfig(new CaseInsensitiveString("other")); another.addEnvironmentVariable("variable-name", "variable-value"); EnvironmentVariableContext context = another.createEnvironmentContext(); assertThat(context.getProperty("variable-name"), is("variable-value")); }
@Override public void update(CruiseConfig configForEdit) throws Exception { EnvironmentConfig environmentConfig = configForEdit.getEnvironments().named(this.environmentConfig.name()); for (String uuid : agentsToAdd) { environmentConfig.addAgent(uuid); } for (String uuid : agentsToRemove) { environmentConfig.removeAgent(uuid); } for (String pipelineName : pipelinesToAdd) { environmentConfig.addPipeline(new CaseInsensitiveString(pipelineName)); } for (String pipelineName : pipelinesToRemove) { environmentConfig.removePipeline(new CaseInsensitiveString(pipelineName)); } for (EnvironmentVariableConfig variableConfig : envVarsToAdd) { environmentConfig.addEnvironmentVariable(variableConfig); } for (String variableName : envVarsToRemove) { environmentConfig.getVariables().removeIfExists(variableName); } }
@Test public void shouldAllowCDATAInEnvironmentVariableValues() throws Exception { //TODO : This should be fixed as part of #4865 //String multiLinedata = "\nsome data\nfoo bar"; String multiLinedata = "some data\nfoo bar"; String content = configWithEnvironmentsAndAgents( "<environments>" + " <environment name='uat'>" + " <environmentvariables> " + " <variable name='cdata'><value><![CDATA[" + multiLinedata + "]]></value></variable>" + " </environmentvariables> " + " </environment>" + "</environments>", "<agents>" + " <agent uuid='1' hostname='test1.com' ipaddress='192.168.0.1' isDisabled='true' />" + "</agents>"); CruiseConfig config = ConfigMigrator.loadWithMigration(content).config; EnvironmentConfig element = new BasicEnvironmentConfig(new CaseInsensitiveString("uat")); element.addEnvironmentVariable("cdata", multiLinedata); assertThat(config.getEnvironments().get(0), is(element)); }