Search in sources :

Example 16 with MockContext

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

the class ConfigurationTest method setup.

@Before
public void setup() {
    Map<String, Object> cfg = new HashMap<>();
    cfg.put(Constants.Keys.DATABASE_URL_KEY, "https://example.com");
    cfg.put(Constants.Keys.API_TOKEN_KEY, "23w4der5f6tg7yh8uj9iko");
    cfg.put("txId", "cd729813-e93f-4e0a-9856-dc8e65bdd9df");
    cfg.put("workDir", System.getProperty("user.dir"));
    ctx = new MockContext(cfg);
}
Also used : MockContext(com.walmartlabs.concord.sdk.MockContext) HashMap(java.util.HashMap) Before(org.junit.Before)

Example 17 with MockContext

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

the class PuppetTaskTest method testSelfSignedCertWithPath.

@Test
public void testSelfSignedCertWithPath() throws Exception {
    MockContext ctx = new MockContext(buildDbQueryConfig());
    UtilsTest.injectVariable(task, "action", "pql");
    ctx.setVariable("queryString", "inventory[certname]{ limit 10 }");
    ctx.setVariable(Constants.Keys.DATABASE_URL_KEY, httpsRule.baseUrl());
    // Self-signed cert will fail unless we provide a cert to trust
    try {
        task.execute(ctx);
        throw new Exception("Task should fail when self-signed cert is used without a provided certificate to trust.");
    } catch (SSLHandshakeException expected) {
        // that's fine
        log.info("hit expected ssl exception. Not a problem");
    } catch (Exception e) {
        log.error(e.getMessage());
        throw e;
    }
    Map<String, Object> certificate = new HashMap<>();
    certificate.put("path", getWiremockCertFile().toString());
    ctx.setVariable("certificate", certificate);
    // now it should work
    task.execute(ctx);
    Map result = (Map) ctx.getVariable("result");
    assertNotNull(result);
    assertTrue((boolean) result.get("ok"));
    List data = (List) result.get("data");
    assertEquals(10, data.size());
}
Also used : MockContext(com.walmartlabs.concord.sdk.MockContext) HashMap(java.util.HashMap) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ApiException(com.walmartlabs.concord.plugins.puppet.model.exception.ApiException) CertificateException(java.security.cert.CertificateException) MissingParameterException(com.walmartlabs.concord.plugins.puppet.model.exception.MissingParameterException) Test(org.junit.Test)

Example 18 with MockContext

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

the class PuppetTaskTest method test404.

@Test
public void test404() {
    stubFor404();
    MockContext ctx = new MockContext(buildDbQueryConfig());
    UtilsTest.injectVariable(task, "action", "pql");
    ctx.setVariable("queryString", "inventory[certname]{ limit 10 }");
    try {
        task.execute(ctx);
        throw new Exception("No exception was thrown in task.");
    } catch (ApiException expected) {
        assertEquals(404, expected.getCode());
    } catch (Exception e) {
        fail("404 error should cause API exception");
    }
}
Also used : MockContext(com.walmartlabs.concord.sdk.MockContext) ApiException(com.walmartlabs.concord.plugins.puppet.model.exception.ApiException) CertificateException(java.security.cert.CertificateException) MissingParameterException(com.walmartlabs.concord.plugins.puppet.model.exception.MissingParameterException) ApiException(com.walmartlabs.concord.plugins.puppet.model.exception.ApiException) Test(org.junit.Test)

Example 19 with MockContext

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

the class PuppetTaskTest method testMissingAction.

@Test
public void testMissingAction() {
    MockContext ctx = new MockContext(buildDbQueryConfig());
    try {
        UtilsTest.injectVariable(task, "action", null);
        task.execute(ctx);
        fail("Missing action value should cause exception");
    } catch (MissingParameterException expected) {
        // ok
        assertTrue(expected.getMessage().contains("action"));
    } catch (Exception e) {
        fail("Unexpected exception: " + e.getMessage());
    }
}
Also used : MockContext(com.walmartlabs.concord.sdk.MockContext) MissingParameterException(com.walmartlabs.concord.plugins.puppet.model.exception.MissingParameterException) ApiException(com.walmartlabs.concord.plugins.puppet.model.exception.ApiException) CertificateException(java.security.cert.CertificateException) MissingParameterException(com.walmartlabs.concord.plugins.puppet.model.exception.MissingParameterException) Test(org.junit.Test)

Example 20 with MockContext

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

the class PuppetTaskTest method testIgnoreErrors.

@Test
public void testIgnoreErrors() throws Exception {
    MockContext ctx = new MockContext(buildDbQueryConfig());
    // Set ignoreErrors = true
    UtilsTest.injectVariable(task, "action", "bad_action");
    UtilsTest.injectVariable(task, "ignoreErrors", true);
    // Normally, this should throw an exception
    task.execute(ctx);
    // Make sure it failed gracefully and has an error message
    Map result = (Map) ctx.getVariable("result");
    assertNotNull(result);
    assertFalse((Boolean) result.get("ok"));
    String error = (String) result.get("error");
    assertTrue(error.contains("Not a supported action"));
}
Also used : MockContext(com.walmartlabs.concord.sdk.MockContext) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

MockContext (com.walmartlabs.concord.sdk.MockContext)25 HashMap (java.util.HashMap)18 Test (org.junit.Test)17 Map (java.util.Map)11 ApiException (com.walmartlabs.concord.plugins.puppet.model.exception.ApiException)8 MissingParameterException (com.walmartlabs.concord.plugins.puppet.model.exception.MissingParameterException)8 CertificateException (java.security.cert.CertificateException)8 Test (org.junit.jupiter.api.Test)6 List (java.util.List)5 Context (com.walmartlabs.concord.sdk.Context)3 SmtpServer (com.icegreen.greenmail.smtp.SmtpServer)2 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)2 File (java.io.File)2 Path (java.nio.file.Path)2 MimeMessage (javax.mail.internet.MimeMessage)2 Before (org.junit.Before)2 RbacCfg (com.walmartlabs.concord.plugins.puppet.model.cfg.RbacCfg)1 TokenPayload (com.walmartlabs.concord.plugins.puppet.model.token.TokenPayload)1 LockService (com.walmartlabs.concord.sdk.LockService)1 ObjectStorage (com.walmartlabs.concord.sdk.ObjectStorage)1