Search in sources :

Example 1 with EnvironmentVariableConfig

use of com.thoughtworks.go.config.EnvironmentVariableConfig 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 EnvironmentVariableConfig

use of com.thoughtworks.go.config.EnvironmentVariableConfig 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 EnvironmentVariableConfig

use of com.thoughtworks.go.config.EnvironmentVariableConfig 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 EnvironmentVariableConfig

use of com.thoughtworks.go.config.EnvironmentVariableConfig 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 EnvironmentVariableConfig

use of com.thoughtworks.go.config.EnvironmentVariableConfig 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

EnvironmentVariableConfig (com.thoughtworks.go.config.EnvironmentVariableConfig)13 EnvironmentVariablesConfig (com.thoughtworks.go.config.EnvironmentVariablesConfig)11 Test (org.junit.Test)9 GoCipher (com.thoughtworks.go.security.GoCipher)5 StageConfig (com.thoughtworks.go.config.StageConfig)3 AgentConfig (com.thoughtworks.go.config.AgentConfig)2 Agents (com.thoughtworks.go.config.Agents)2 Resource (com.thoughtworks.go.config.Resource)2 JobPlan (com.thoughtworks.go.domain.JobPlan)2 WaitingJobPlan (com.thoughtworks.go.domain.WaitingJobPlan)2 Document (org.dom4j.Document)2 DOMDocument (org.dom4j.dom.DOMDocument)2 DOMElement (org.dom4j.dom.DOMElement)2 ArtifactPlan (com.thoughtworks.go.config.ArtifactPlan)1 DefaultJobPlan (com.thoughtworks.go.domain.DefaultJobPlan)1 JobIdentifier (com.thoughtworks.go.domain.JobIdentifier)1 Property (com.thoughtworks.go.domain.Property)1 StageIdentifier (com.thoughtworks.go.domain.StageIdentifier)1 XmlWriterContext (com.thoughtworks.go.domain.XmlWriterContext)1 BuildCause (com.thoughtworks.go.domain.buildcause.BuildCause)1