Search in sources :

Example 6 with Context

use of com.walmartlabs.concord.sdk.Context in project concord by walmartlabs.

the class SmtpTaskTest method testVariables.

@Test
public void testVariables() throws Exception {
    SmtpServer server = mail.getSmtp();
    Map<String, Object> smtpParams = new HashMap<>();
    smtpParams.put("host", "localhost");
    smtpParams.put("port", server.getPort());
    Map<String, Object> mailParams = new HashMap<>();
    mailParams.put("from", "my@mail.com");
    mailParams.put("to", "their@mail.com");
    mailParams.put("template", new File(ClassLoader.getSystemResource("test.mustache").toURI()).toString());
    Map<String, Object> m = new HashMap<>();
    m.put("name", "Concord");
    m.put("workDir", "/");
    m.put("smtp", smtpParams);
    m.put("mail", mailParams);
    Context ctx = new MockContext(m);
    SmtpTask t = new SmtpTask();
    t.execute(ctx);
    MimeMessage[] messages = mail.getReceivedMessages();
    assertEquals("Hello, Concord!\r\n", messages[0].getContent());
    mail.reset();
}
Also used : MockContext(com.walmartlabs.concord.sdk.MockContext) Context(com.walmartlabs.concord.sdk.Context) MockContext(com.walmartlabs.concord.sdk.MockContext) SmtpServer(com.icegreen.greenmail.smtp.SmtpServer) HashMap(java.util.HashMap) MimeMessage(javax.mail.internet.MimeMessage) File(java.io.File) Test(org.junit.jupiter.api.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 7 with Context

use of com.walmartlabs.concord.sdk.Context in project concord by walmartlabs.

the class TaskPolicy method paramMatches.

@SuppressWarnings("unchecked")
private static boolean paramMatches(String[] names, int nameIndex, List<Object> values, Object param, boolean isProtected) {
    if (param == null) {
        return values.contains(null);
    }
    if (param instanceof Map) {
        if (names == null) {
            return false;
        }
        Map<String, Object> m = (Map<String, Object>) param;
        String name = names[nameIndex];
        nameIndex += 1;
        return paramMatches(names, nameIndex, values, m.get(name), isProtected);
    } else if (param instanceof Context) {
        if (names == null) {
            return false;
        }
        Context ctx = (Context) param;
        String name = names[nameIndex];
        nameIndex += 1;
        Object v = isProtected ? ctx.getProtectedVariable(name) : ctx.getVariable(name);
        return paramMatches(names, nameIndex, values, v, isProtected);
    } else if (param instanceof Variables) {
        Variables vars = (Variables) param;
        String name = names[nameIndex];
        nameIndex += 1;
        Object v = vars.get(name);
        return paramMatches(names, nameIndex, values, v, isProtected);
    } else if (param instanceof com.walmartlabs.concord.runtime.v2.sdk.Context) {
        com.walmartlabs.concord.runtime.v2.sdk.Context ctx = (com.walmartlabs.concord.runtime.v2.sdk.Context) param;
        String name = names[nameIndex];
        nameIndex += 1;
        Object v = ctx.variables().get(name);
        return paramMatches(names, nameIndex, values, v, isProtected);
    } else if (param instanceof String) {
        return Utils.matchAny(values.stream().map(Object::toString).collect(Collectors.toList()), param.toString());
    } else {
        for (Object v : values) {
            if (v != null && v.equals(param)) {
                return true;
            }
        }
    }
    return false;
}
Also used : Context(com.walmartlabs.concord.sdk.Context) Variables(com.walmartlabs.concord.runtime.v2.sdk.Variables)

Example 8 with Context

use of com.walmartlabs.concord.sdk.Context in project concord by walmartlabs.

the class PolicyPreprocessorTest method testPreprocess.

@Test
public void testPreprocess() throws Exception {
    ExpressionManager expressionManager = new DefaultExpressionManager(new String[] { InternalConstants.Context.CONTEXT_KEY, InternalConstants.Context.EXECUTION_CONTEXT_KEY }, new InjectVariableELResolver());
    Context ctx = new ConcordExecutionContextFactory.ConcordExecutionContext(null, expressionManager, new Variables(), null, null);
    ctx.setVariable("gatekeeperArtifacts", Arrays.asList("a", "b", "c"));
    Path workDir = Files.createTempDirectory("concord-test");
    Files.createDirectories(workDir.resolve(InternalConstants.Files.CONCORD_SYSTEM_DIR_NAME));
    // -- read original policy
    String originalPolicy;
    try (InputStream is = PolicyPreprocessorTest.class.getResourceAsStream("/com/walmartlabs/concord/runner/policy.json")) {
        ObjectMapper om = new ObjectMapper();
        originalPolicy = om.writeValueAsString(om.readValue(is, Map.class));
    }
    // -- write tmp policy
    Path policyFile = workDir.resolve(InternalConstants.Files.CONCORD_SYSTEM_DIR_NAME).resolve(InternalConstants.Files.POLICY_FILE_NAME);
    try (InputStream is = PolicyPreprocessorTest.class.getResourceAsStream("/com/walmartlabs/concord/runner/policy.json")) {
        Files.copy(is, policyFile, StandardCopyOption.REPLACE_EXISTING);
    }
    // ---
    PolicyPreprocessor pp = new PolicyPreprocessor(workDir);
    pp.preTask("ansible", null, ctx);
    String processedPolicy = new String(Files.readAllBytes(policyFile));
    assertEquals(EXPECTED_PROCESSED_POLICY, processedPolicy);
    // ---
    pp.postTask("ansible", null, ctx);
    String restoredPolicy = new String(Files.readAllBytes(policyFile));
    assertEquals(originalPolicy, restoredPolicy);
}
Also used : Context(com.walmartlabs.concord.sdk.Context) Variables(io.takari.bpm.api.Variables) Path(java.nio.file.Path) DefaultExpressionManager(io.takari.bpm.el.DefaultExpressionManager) ExpressionManager(io.takari.bpm.el.ExpressionManager) DefaultExpressionManager(io.takari.bpm.el.DefaultExpressionManager) InputStream(java.io.InputStream) InjectVariableELResolver(com.walmartlabs.concord.runner.engine.el.InjectVariableELResolver) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 9 with Context

use of com.walmartlabs.concord.sdk.Context in project concord-plugins by walmartlabs.

the class PackerTaskExecutionTest method validateExecutingPacker.

@Test
public void validateExecutingPacker() throws Exception {
    ToolInitializer toolInitializer = new ToolInitializer(new OKHttpDownloadManager("packer"));
    Map<String, ToolCommand> commands = ImmutableMap.of("packer/build", new Build());
    PackerTask task = new PackerTask(commands, toolInitializer);
    Map<String, Object> args = Maps.newHashMap(mapBuilder().put("command", "build").put("saveOutput", true).put("template", packerTestFile().toString()).build());
    Context context = context(args);
    task.execute(context);
    /*

        Debug mode enabled. Builds will not be parallelized.
        ==> amazon-ebs: Prevalidating any provided VPC information
        ==> amazon-ebs: Prevalidating AMI Name: packer-example 1585436207
            amazon-ebs: Found Image ID: ami-04ac550b78324f651
        ==> amazon-ebs: Creating temporary keypair: packer_5e7fd62f-8128-badf-da32-4f8356ed603a
            amazon-ebs: Saving key for debug purposes: ec2_amazon-ebs.pem
        ==> amazon-ebs: Creating temporary security group for this instance: packer_5e7fd632-c20b-002b-ccb6-ea9e2adac21e
        ==> amazon-ebs: Authorizing access to port 22 from [0.0.0.0/0] in the temporary security groups...
        ==> amazon-ebs: Launching a source AWS instance...
        ==> amazon-ebs: Adding tags to source instance
            amazon-ebs: Adding tag: "Name": "Packer Builder"
            amazon-ebs: Instance ID: i-0d5e29779b4f2406d
        ==> amazon-ebs: Waiting for instance (i-0d5e29779b4f2406d) to become ready...
            amazon-ebs: Public DNS: ec2-34-238-135-221.compute-1.amazonaws.com
            amazon-ebs: Public IP: 34.238.135.221
            amazon-ebs: Private IP: 172.31.91.204
        ==> amazon-ebs: Using ssh communicator to connect: 34.238.135.221
        ==> amazon-ebs: Waiting for SSH to become available...
        ==> amazon-ebs: Connected to SSH!
        ==> amazon-ebs: Stopping the source instance...
            amazon-ebs: Stopping instance
        ==> amazon-ebs: Waiting for the instance to stop...
        ==> amazon-ebs: Creating AMI packer-example 1585436207 from instance i-0d5e29779b4f2406d
            amazon-ebs: AMI: ami-083dfd9b6f66d45b5
        ==> amazon-ebs: Waiting for AMI to become ready...
        ==> amazon-ebs: Terminating the source AWS instance...
        ==> amazon-ebs: Cleaning up any extra volumes...
        ==> amazon-ebs: No volumes to clean up, skipping
        ==> amazon-ebs: Deleting temporary security group...
        ==> amazon-ebs: Deleting temporary keypair...
        Build 'amazon-ebs' finished.

        ==> Builds finished. The artifacts of successful builds are:
        --> amazon-ebs: AMIs were created:
        us-east-1: ami-083dfd9b6f66d45b5

        */
    // Retrieve the logs from the context
    String logs = varAsString(context, "logs");
    assertThat(logs).contains("==> Builds finished. The artifacts of successful builds are:");
    assertThat(logs).contains("--> amazon-ebs: AMIs were created:");
}
Also used : Context(com.walmartlabs.concord.sdk.Context) OKHttpDownloadManager(com.walmartlabs.concord.plugins.OKHttpDownloadManager) Build(com.walmartlabs.concord.plugins.packer.commands.Build) ToolInitializer(ca.vanzyl.concord.plugins.tool.ToolInitializer) ToolCommand(ca.vanzyl.concord.plugins.tool.ToolCommand) Test(org.junit.Test)

Example 10 with Context

use of com.walmartlabs.concord.sdk.Context in project concord-plugins by walmartlabs.

the class TaurusTaskTest method test.

@Test
public void test() throws Exception {
    Path workDir = Files.createTempDirectory("test");
    Path scenarioFile = Paths.get(TaurusTaskTest.class.getResource("test.yml").toURI());
    Files.copy(scenarioFile, workDir.resolve("test.yml"));
    Map<String, Object> args = new HashMap<>();
    args.put("useFakeHome", false);
    args.put("action", "run");
    args.put(com.walmartlabs.concord.sdk.Constants.Context.WORK_DIR_KEY, workDir.toString());
    args.put("configs", Arrays.asList("test.yml", Collections.singletonMap("scenarios", Collections.singletonMap("quick-test", Collections.singletonMap("variables", Collections.singletonMap("endpoint", "/api/v1/server/ping"))))));
    Context ctx = mock(Context.class);
    when(ctx.getVariable(anyString())).then(i -> args.get(i.getArgument(0)));
    when(ctx.interpolate(any())).then(i -> i.getArgument(0));
    TaurusTask t = new TaurusTask();
    t.execute(ctx);
}
Also used : Path(java.nio.file.Path) Context(com.walmartlabs.concord.sdk.Context) HashMap(java.util.HashMap) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

Context (com.walmartlabs.concord.sdk.Context)10 Test (org.junit.Test)4 ToolCommand (ca.vanzyl.concord.plugins.tool.ToolCommand)3 ToolInitializer (ca.vanzyl.concord.plugins.tool.ToolInitializer)3 OKHttpDownloadManager (com.walmartlabs.concord.plugins.OKHttpDownloadManager)3 MockContext (com.walmartlabs.concord.sdk.MockContext)3 Path (java.nio.file.Path)3 HashMap (java.util.HashMap)3 Test (org.junit.jupiter.api.Test)3 SmtpServer (com.icegreen.greenmail.smtp.SmtpServer)2 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)2 Build (com.walmartlabs.concord.plugins.packer.commands.Build)2 File (java.io.File)2 MimeMessage (javax.mail.internet.MimeMessage)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ApiClient (com.walmartlabs.concord.ApiClient)1 ConcordApiClient (com.walmartlabs.concord.client.ConcordApiClient)1 Version (com.walmartlabs.concord.plugins.packer.commands.Version)1 InjectVariableELResolver (com.walmartlabs.concord.runner.engine.el.InjectVariableELResolver)1 Variables (com.walmartlabs.concord.runtime.v2.sdk.Variables)1