Search in sources :

Example 1 with EnvironmentVariablesConfig

use of com.thoughtworks.go.config.EnvironmentVariablesConfig in project gocd by gocd.

the class DefaultSchedulingContextTest method shouldCreatePermittedAgentContextCorrectly.

@Test
public void shouldCreatePermittedAgentContextCorrectly() throws Exception {
    AgentConfig linux = agent("uuid1", "linux");
    AgentConfig windows = agent("uuid2", "windows");
    windows.disable();
    Agents matchingAgents = new Agents(linux, windows);
    EnvironmentVariablesConfig existing = new EnvironmentVariablesConfig();
    existing.add("firstVar", "firstVal");
    existing.add("overriddenVar", "originalVal");
    SchedulingContext schedulingContext = new DefaultSchedulingContext("approver", matchingAgents);
    schedulingContext = schedulingContext.overrideEnvironmentVariables(existing);
    EnvironmentVariablesConfig stageLevel = new EnvironmentVariablesConfig();
    stageLevel.add("stageVar", "stageVal");
    stageLevel.add("overriddenVar", "overriddenVal");
    StageConfig config = StageConfigMother.custom("test", Approval.automaticApproval());
    config.setVariables(stageLevel);
    SchedulingContext context = schedulingContext.overrideEnvironmentVariables(config.getVariables());
    ReflectionUtil.setField(context, "rerun", true);
    SchedulingContext permittedAgentContext = context.permittedAgent("uuid1");
    Agents agents = (Agents) ReflectionUtil.getField(permittedAgentContext, "agents");
    assertThat(agents.size(), is(1));
    assertThat(agents.get(0).getAgentIdentifier().getUuid(), is("uuid1"));
    assertThat(permittedAgentContext.isRerun(), is(true));
    assertThat(permittedAgentContext.getApprovedBy(), is("approver"));
    EnvironmentVariablesConfig environmentVariablesUsed = permittedAgentContext.getEnvironmentVariablesConfig();
    assertThat(environmentVariablesUsed.size(), is(3));
    assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("firstVar", "firstVal")));
    assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("overriddenVar", "overriddenVal")));
    assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("stageVar", "stageVal")));
}
Also used : AgentConfig(com.thoughtworks.go.config.AgentConfig) EnvironmentVariableConfig(com.thoughtworks.go.config.EnvironmentVariableConfig) Agents(com.thoughtworks.go.config.Agents) EnvironmentVariablesConfig(com.thoughtworks.go.config.EnvironmentVariablesConfig) StageConfig(com.thoughtworks.go.config.StageConfig) Test(org.junit.Test)

Example 2 with EnvironmentVariablesConfig

use of com.thoughtworks.go.config.EnvironmentVariablesConfig in project gocd by gocd.

the class DefaultSchedulingContextTest method shouldCreateRerunSchedulingContextCorrectly.

@Test
public void shouldCreateRerunSchedulingContextCorrectly() throws Exception {
    AgentConfig linux = agent("uuid1", "linux");
    AgentConfig windows = agent("uuid2", "windows");
    windows.disable();
    Agents matchingAgents = new Agents(linux, windows);
    EnvironmentVariablesConfig existing = new EnvironmentVariablesConfig();
    existing.add("firstVar", "firstVal");
    existing.add("overriddenVar", "originalVal");
    SchedulingContext schedulingContext = new DefaultSchedulingContext("approver", matchingAgents);
    schedulingContext = schedulingContext.overrideEnvironmentVariables(existing);
    EnvironmentVariablesConfig stageLevel = new EnvironmentVariablesConfig();
    stageLevel.add("stageVar", "stageVal");
    stageLevel.add("overriddenVar", "overriddenVal");
    StageConfig config = StageConfigMother.custom("test", Approval.automaticApproval());
    config.setVariables(stageLevel);
    SchedulingContext context = schedulingContext.overrideEnvironmentVariables(config.getVariables());
    SchedulingContext rerunContext = context.rerunContext();
    assertThat(rerunContext.isRerun(), is(true));
    assertThat(rerunContext.getApprovedBy(), is("approver"));
    Agents agents = (Agents) ReflectionUtil.getField(rerunContext, "agents");
    assertThat(agents, is(matchingAgents));
    EnvironmentVariablesConfig environmentVariablesUsed = rerunContext.getEnvironmentVariablesConfig();
    assertThat(environmentVariablesUsed.size(), is(3));
    assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("firstVar", "firstVal")));
    assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("overriddenVar", "overriddenVal")));
    assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("stageVar", "stageVal")));
}
Also used : AgentConfig(com.thoughtworks.go.config.AgentConfig) EnvironmentVariableConfig(com.thoughtworks.go.config.EnvironmentVariableConfig) Agents(com.thoughtworks.go.config.Agents) EnvironmentVariablesConfig(com.thoughtworks.go.config.EnvironmentVariablesConfig) StageConfig(com.thoughtworks.go.config.StageConfig) Test(org.junit.Test)

Example 3 with EnvironmentVariablesConfig

use of com.thoughtworks.go.config.EnvironmentVariablesConfig in project gocd by gocd.

the class EnvironmentVariableSqlMapDao method load.

public EnvironmentVariablesConfig load(final Long entityId, final EnvironmentVariableType type) {
    List<EnvironmentVariableConfig> result = (List<EnvironmentVariableConfig>) transactionTemplate.execute(new TransactionCallback() {

        @Override
        public Object doInTransaction(TransactionStatus transactionStatus) {
            Criteria criteria = sessionFactory.getCurrentSession().createCriteria(EnvironmentVariableConfig.class).add(Restrictions.eq("entityId", entityId)).add(Restrictions.eq("entityType", type.toString())).addOrder(Order.asc("id"));
            criteria.setCacheable(true);
            return criteria.list();
        }
    });
    for (EnvironmentVariableConfig var : result) {
        var.ensureEncrypted();
    }
    return new EnvironmentVariablesConfig(result);
}
Also used : EnvironmentVariableConfig(com.thoughtworks.go.config.EnvironmentVariableConfig) TransactionCallback(org.springframework.transaction.support.TransactionCallback) EnvironmentVariablesConfig(com.thoughtworks.go.config.EnvironmentVariablesConfig) TransactionStatus(org.springframework.transaction.TransactionStatus) List(java.util.List) Criteria(org.hibernate.Criteria)

Example 4 with EnvironmentVariablesConfig

use of com.thoughtworks.go.config.EnvironmentVariablesConfig in project gocd by gocd.

the class JobPlanXmlViewModelTest method shouldConvertJobPlanToXmlDocument.

@Test
public void shouldConvertJobPlanToXmlDocument() throws IOException, DocumentException {
    EnvironmentVariableConfig secureEnvVariable = new EnvironmentVariableConfig(new GoCipher(), "secureVariable", "value2", true);
    DefaultJobPlan jobPlan1 = JobInstanceMother.jobPlan("job-1", 1);
    jobPlan1.setJobId(10);
    EnvironmentVariablesConfig variables = new EnvironmentVariablesConfig();
    variables.add("some_var", "blah");
    variables.add(secureEnvVariable);
    jobPlan1.setVariables(variables);
    DefaultJobPlan jobPlan2 = JobInstanceMother.jobPlan("job-2", 1);
    jobPlan2.setJobId(11);
    JobPlanXmlViewModel jobPlanXmlViewModel = new JobPlanXmlViewModel(ArrayUtil.asList(new WaitingJobPlan(jobPlan1, "envName"), new WaitingJobPlan(jobPlan2, null)));
    String expectedXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<scheduledJobs>" + "<job name=\"job-1\" id=\"10\">" + "<link rel=\"self\" href=\"http://baseurl/go/tab/build/detail/pipeline/1/stage/1/job-1\"/>" + "<buildLocator>pipeline/1/stage/1/job-1</buildLocator>" + "<environment>envName</environment>" + "<resources><resource><![CDATA[foo]]></resource><resource><![CDATA[bar]]></resource></resources>" + "<environmentVariables><variable name=\"some_var\">blah</variable><variable name=\"secureVariable\">****</variable></environmentVariables>" + "</job>" + "<job name=\"job-2\" id=\"11\">" + "<link rel=\"self\" href=\"http://baseurl/go/tab/build/detail/pipeline/1/stage/1/job-2\"/>" + "<buildLocator>pipeline/1/stage/1/job-2</buildLocator>" + "<resources><resource><![CDATA[foo]]></resource><resource><![CDATA[bar]]></resource></resources>" + "</job>" + "</scheduledJobs>";
    Document document = jobPlanXmlViewModel.toXml(new XmlWriterContext("http://baseurl/go", null, null, null, null));
    assertEquals(expectedXml, document.asXML());
}
Also used : EnvironmentVariableConfig(com.thoughtworks.go.config.EnvironmentVariableConfig) DefaultJobPlan(com.thoughtworks.go.domain.DefaultJobPlan) GoCipher(com.thoughtworks.go.security.GoCipher) EnvironmentVariablesConfig(com.thoughtworks.go.config.EnvironmentVariablesConfig) XmlWriterContext(com.thoughtworks.go.domain.XmlWriterContext) WaitingJobPlan(com.thoughtworks.go.domain.WaitingJobPlan) Document(org.dom4j.Document) Test(org.junit.Test)

Example 5 with EnvironmentVariablesConfig

use of com.thoughtworks.go.config.EnvironmentVariablesConfig in project gocd by gocd.

the class JobXmlViewModelTest method shouldMaskSecureVariables.

@Test
public void shouldMaskSecureVariables() throws IOException, DocumentException {
    EnvironmentVariableConfig envVariable = new EnvironmentVariableConfig(null, "stdVariable", "value1", false);
    EnvironmentVariableConfig secureEnvVariable = new EnvironmentVariableConfig(new GoCipher(), "secureVariable", "value2", true);
    EnvironmentVariablesConfig environmentVariablesConfig = new EnvironmentVariablesConfig();
    environmentVariablesConfig.add(envVariable);
    environmentVariablesConfig.add(secureEnvVariable);
    when(jobPlan.getVariables()).thenReturn(environmentVariablesConfig);
    DOMDocument document = (DOMDocument) jobXmlViewModel.toXml(xmlWriterContext);
    Assert.assertThat(document.asXML(), containsString("<environmentvariables><variable name=\"stdVariable\"><![CDATA[value1]]></variable><variable name=\"secureVariable\"><![CDATA[****]]></variable></environmentvariables>"));
}
Also used : EnvironmentVariableConfig(com.thoughtworks.go.config.EnvironmentVariableConfig) GoCipher(com.thoughtworks.go.security.GoCipher) EnvironmentVariablesConfig(com.thoughtworks.go.config.EnvironmentVariablesConfig) DOMDocument(org.dom4j.dom.DOMDocument) Test(org.junit.Test)

Aggregations

EnvironmentVariablesConfig (com.thoughtworks.go.config.EnvironmentVariablesConfig)16 EnvironmentVariableConfig (com.thoughtworks.go.config.EnvironmentVariableConfig)11 Test (org.junit.Test)11 GoCipher (com.thoughtworks.go.security.GoCipher)5 AgentConfig (com.thoughtworks.go.config.AgentConfig)3 Agents (com.thoughtworks.go.config.Agents)3 StageConfig (com.thoughtworks.go.config.StageConfig)3 DefaultJobPlan (com.thoughtworks.go.domain.DefaultJobPlan)2 JobInstances (com.thoughtworks.go.domain.JobInstances)2 Pipeline (com.thoughtworks.go.domain.Pipeline)2 Stage (com.thoughtworks.go.domain.Stage)2 ArtifactPlans (com.thoughtworks.go.config.ArtifactPlans)1 ElasticProfile (com.thoughtworks.go.config.elastic.ElasticProfile)1 JobIdentifier (com.thoughtworks.go.domain.JobIdentifier)1 WaitingJobPlan (com.thoughtworks.go.domain.WaitingJobPlan)1 XmlWriterContext (com.thoughtworks.go.domain.XmlWriterContext)1 BuildCause (com.thoughtworks.go.domain.buildcause.BuildCause)1 HttpOperationResult (com.thoughtworks.go.server.service.result.HttpOperationResult)1 List (java.util.List)1 Document (org.dom4j.Document)1