Search in sources :

Example 1 with MockContext

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

the class PuppetTaskTest method test503.

/**
 * 503 errors are retried. The resulting exception will be due to retry max
 * being reached, not the 503 error itself.
 */
@Test
public void test503() {
    stubFor503();
    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) {
        assertTrue(expected.getMessage().contains("Retry max reached"));
    } 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 2 with MockContext

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

the class PuppetTaskTest method testTokenCreateAllParams.

@Test
public void testTokenCreateAllParams() throws Exception {
    MockContext ctx = new MockContext(buildRbacCfg());
    UtilsTest.injectVariable(task, "action", "createApiToken");
    ctx.setVariable("tokenDescription", "token description");
    ctx.setVariable("tokenLabel", "token label");
    ctx.setVariable("tokenLife", "5h");
    task.execute(ctx);
    Map result = (Map) ctx.getVariable("result");
    assertTrue((Boolean) result.get("ok"));
    String token = (String) result.get("data");
    assertNotNull(token);
}
Also used : MockContext(com.walmartlabs.concord.sdk.MockContext) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 3 with MockContext

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

the class PuppetTaskTest method testTokenPayload.

@Test
public void testTokenPayload() throws Exception {
    MockContext ctx = new MockContext(buildRbacCfg());
    ctx.setVariable("tokenDescription", "token description");
    ctx.setVariable("tokenLabel", "token label");
    ctx.setVariable("tokenLife", "5h");
    RbacCfg cfg = Utils.createCfg(ctx, secretService, ctx.toMap(), RbacCfg.class);
    TokenPayload payload = new TokenPayload(cfg);
    assertEquals("token description", payload.getDescription());
    assertEquals("token label", payload.getLabel());
    assertEquals("5h", payload.getLifetime());
}
Also used : MockContext(com.walmartlabs.concord.sdk.MockContext) RbacCfg(com.walmartlabs.concord.plugins.puppet.model.cfg.RbacCfg) TokenPayload(com.walmartlabs.concord.plugins.puppet.model.token.TokenPayload) Test(org.junit.Test)

Example 4 with MockContext

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

the class PuppetTaskTest method testBadUrl.

@Test
public void testBadUrl() {
    MockContext ctx = new MockContext(buildDbQueryConfig());
    UtilsTest.injectVariable(task, "action", "pql");
    ctx.setVariable("queryString", "inventory[certname]{ limit 10 }");
    // Empty URL
    ctx.setVariable("databaseUrl", "");
    try {
        task.execute(ctx);
        fail("Bad url should cause an exception");
    } catch (MissingParameterException expected) {
        assert (expected.getMessage().contains("Cannot find value for databaseUrl"));
    } catch (Exception e) {
        fail("Unexpected exception with bad URL: " + e.getMessage());
    }
    // Invalid URL
    ctx.setVariable("databaseUrl", "notaurl");
    try {
        task.execute(ctx);
        fail("Bad url should cause an exception");
    } catch (IllegalArgumentException expected) {
        assert (expected.getMessage().contains("Invalid URL"));
    } catch (Exception e) {
        fail("Unexpected exception with bad URL: " + 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 5 with MockContext

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

the class PuppetTaskTest method testNoCertValidation.

@Test
public void testNoCertValidation() 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);
        fail("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;
    }
    ctx.setVariable(Constants.Keys.VALIDATE_CERTS_KEY, false);
    // 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) 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)

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