How to use VaultContainer class of org.testcontainers.vault package

Best Testcontainers-java code snippet using org.testcontainers.vault.VaultContainer

Source:AppTest.java Github

copy

Full Screen

...14import org.springframework.test.context.ActiveProfiles;15import org.springframework.test.context.junit4.SpringRunner;16import org.testcontainers.Testcontainers;17import org.testcontainers.containers.Container.ExecResult;18import org.testcontainers.vault.VaultContainer;19import com.ecwid.consul.v1.ConsulClient;20import com.ecwid.consul.v1.Response;21import example.testcontainers.consul.ConsulConfiguration;22import example.testcontainers.consul.ConsulConfiguration.ACL;23import example.testcontainers.consul.ConsulConfiguration.Ports;24import example.testcontainers.consul.ConsulConfiguration.Tokens;25import example.testcontainers.consul.ConsulContainer;26@RunWith(SpringRunner.class)27@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {})28@ActiveProfiles("test")29public class AppTest30{31 32 static ConsulContainer cc;33 static VaultContainer vaultContainer;34 static ConsulConfiguration config;35 36 @BeforeClass37 public static void init() throws IOException, InterruptedException {38 39 config = new ConsulConfiguration();40 Ports ports = new Ports();41 ports.setHttpPort(8501);42 config.setPorts(ports);43 config.setDatacenter("default");44 45 String consulMasterToken = UUID.randomUUID().toString();46 String consulDefaultToken = UUID.randomUUID().toString();47 String consulAgentToken = UUID.randomUUID().toString();48 String consulReplicationToken = UUID.randomUUID().toString();49 50 Tokens tokens = new Tokens();51 tokens.setMaster(consulMasterToken);52 tokens.setDefaultToken(consulDefaultToken);53 tokens.setAgent(consulAgentToken);54 tokens.setReplication(consulReplicationToken);55 56 ACL acl = new ACL();57 acl.setEnabled(true);58 acl.setDefaultPolicy("deny");59 acl.setTokens(tokens);60 61 config.setAcl(acl);62 63 cc = new ConsulContainer(config);64 cc.start();65 System.setProperty("spring.cloud.consul.host", "127.0.0.1");66 System.setProperty("spring.cloud.consul.port", String.valueOf(cc.getMappedPort(cc.getHttpPort())));67 ConsulClient client = new ConsulClient("127.0.0.1", cc.getMappedPort(cc.getHttpPort()));68 69 Testcontainers.exposeHostPorts(cc.getMappedPort(cc.getHttpPort()));70 71 // consul acl policy create -name read-only -rules key_prefix "" { policy = "read"}72 ExecResult consulresult = cc.execInContainer("consul", "acl", "policy", "create", "-name", 73 "read-only", "-rules", "key_prefix \"\" { policy = \"read\"}", "-token", consulMasterToken,74 "-http-addr=http://127.0.0.1:8501");75 assertTrue(consulresult.getExitCode() == 0);76 77 Exception actualException = null;78 Response<Boolean> savedWithToken = null;79 80 String testYaml = "db:\n" + 81 " port: \"3307\"";82 83 try {84 savedWithToken = client.setKVBinaryValue("test/spring-boot-example/application.yaml", testYaml.getBytes(),consulMasterToken,null,null);85 System.out.println("kv put value: "+savedWithToken.getValue());86 } catch (Exception e) {87 actualException = e;88 }89 90 // set master consul token for spring cloud temp testing91 //System.setProperty("spring.cloud.consul.config.acl-token",consulMasterToken);92 93 vaultContainer = new VaultContainer<>("vault:1.3.2")94 .withVaultToken("foo")95 .withSecretInVault("secret/test/spring-boot-example", "password=password1");96 vaultContainer.start();97 98 // enable vault consul backend99 ExecResult result = vaultContainer.execInContainer("vault", "secrets", "enable", "consul");100 assertTrue(result.getExitCode() == 0);101 102 // configure consul access103 result = vaultContainer.execInContainer("vault", "write", "consul/config/access", "address=host.testcontainers.internal:"+ cc.getMappedPort(cc.getHttpPort()), "token="+consulMasterToken);104 assertTrue(result.getExitCode() == 0);105 106 result = vaultContainer.execInContainer("vault", "write", "consul/roles/consul-read-only", "policies=read-only");107 assertTrue(result.getExitCode() == 0);...

Full Screen

Full Screen

Source:AbstractVaultTest.java Github

copy

Full Screen

...24import org.testcontainers.junit.jupiter.Container;25import org.testcontainers.junit.jupiter.Testcontainers;26import org.testcontainers.utility.DockerImageName;27import org.testcontainers.utility.MountableFile;28import org.testcontainers.vault.VaultContainer;29@Testcontainers30public abstract class AbstractVaultTest {31 private static final String TEST_VAULT_TOKEN = "my-test-token";32 private static final int VAULT_PORT = 8200;33 private static final int NGINX_SSL_PORT = 443;34 protected static final Network network = Network.newNetwork();35 @Container36 public final static VaultContainer<?> vaultContainer =37 new VaultContainer<>(DockerImageName38 // set env var to point to specific image/custom repo39 .parse(System.getenv("VAULT_IMAGE_VERSION"))40 .asCompatibleSubstituteFor("vault"))41 .withVaultToken(TEST_VAULT_TOKEN)42 .withNetwork(network)43 .withNetworkAliases("vault")44 .withSecretInVault("secret/testing", "top_secret=password1","db_password=dbpassword1")45 .withSecretInVault("cubbyhole/hello", "cubbyKey=cubbyVal")46 .waitingFor(Wait.forLogMessage(".*Vault server started.*", 1));47 @Container48 public static NginxContainer<?> nginxContainer =49 new NginxContainer<>(DockerImageName50 // set env var to point to specific image/custom repo51 .parse(System.getenv("NGINX_IMAGE_VERSION"))...

Full Screen

Full Screen

Source:VaultContainerTest.java Github

copy

Full Screen

...7import org.junit.ClassRule;8import org.junit.Test;9import org.testcontainers.containers.GenericContainer;10import org.testcontainers.containers.wait.Wait;11import org.testcontainers.vault.VaultContainer;12/**13 * This test shows the pattern to use the VaultContainer @ClassRule for a junit test. It also has tests that ensure14 * the secrets were added correctly by reading from Vault with the CLI and over HTTP.15 * NOTE This is from the official GitHub page and is intended to show how the vault works. It does not test the connector as such16 * for further info check it out https://github.com/testcontainers/testcontainers-java/blob/master/modules/vault/src/main/java/org/testcontainers/vault/VaultContainer.java17 */18public class VaultContainerTest {19 private static final int VAULT_PORT = 8201; //using non-default port to show other ports can be passed besides 820020 private static final String VAULT_TOKEN = "49VLojFvnnv5wnIY3GGW9i6x";21 @ClassRule22 public static VaultContainer vaultContainer = new VaultContainer<>()23 .withVaultToken(VAULT_TOKEN)24 .withVaultPort(VAULT_PORT)25 .withSecretInVault("secret/testing1", "top_secret=password123")26 .withSecretInVault("secret/testing2", "secret_one=password1",27 "secret_two=password2", "secret_three=password3", "secret_three=password3",28 "secret_four=password4")29 .waitingFor(Wait.forHttp("/v1/secret/testing1").forStatusCode(400));30 @Test31 public void readFirstSecretPathWithCli() throws IOException, InterruptedException {32 GenericContainer.ExecResult result = vaultContainer.execInContainer("vault",33 "read", "-field=top_secret", "secret/testing1");34 assertThat(result.getStdout(), containsString("password123"));35 }36 @Test...

Full Screen

Full Screen

VaultContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.containers.wait.strategy.Wait;3import org.testcontainers.utility.DockerImageName;4public class VaultContainer extends GenericContainer<VaultContainer> {5 private static final int VAULT_PORT = 8200;6 private static final String VAULT_IMAGE = "vault:1.7.0";7 public VaultContainer() {8 super(DockerImageName.parse(VAULT_IMAGE));9 withExposedPorts(VAULT_PORT);10 waitingFor(Wait.forHttp("/v1/sys/health").forStatusCode(200));11 }12 public String getVaultUrl() {13 }14}15import org.testcontainers.junit.jupiter.Container;16import org.testcontainers.junit.jupiter.Testcontainers;17public class VaultContainerTest {18 private static final VaultContainer vaultContainer = new VaultContainer();19 public void testVaultContainer() {20 String vaultUrl = vaultContainer.getVaultUrl();21 System.out.println(vaultUrl);22 }23}

Full Screen

Full Screen

VaultContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.VaultContainer;2public class 1 {3 public static void main(String[] args) {4 VaultContainer vault = new VaultContainer();5 vault.start();6 System.out.println("Vault is running!");7 System.out.println("Vault address: " + vault.getContainerIpAddress() + ":" + vault.getMappedPort(8200));8 }9}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testcontainers-java automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful