How to use K3sContainer class of org.testcontainers.k3s package

Best Testcontainers-java code snippet using org.testcontainers.k3s.K3sContainer

Source:K8sClusterTestResource.java Github

copy

Full Screen

...9import java.util.Collections;10import java.util.Map;11@Log12public class K8sClusterTestResource implements QuarkusTestResourceLifecycleManager {13 private K3sContainer k3sContainer;14 private ToStringConsumer logger;15 public KindContainer kindContainer;16 public static boolean k3s = true;17 public Map<String, String> startK3s() {18 try {19 k3sContainer.start();20 Thread.sleep(10000);21 return null;22 } catch (Exception ex) {23 System.out.println(logger.toUtf8String());24 return null;25 }26 }27 public Map<String, String> startKind() {28 kindContainer.start();29 return null;30 }31 @Override32 public Map<String, String> start() {33 return (k3s) ? startK3s() : startKind();34 }35 @Override36 public void stop() {37 if (k3s) {38 kindContainer.stop();39 } else k3sContainer.stop();40 }41 @Override42 public void inject(TestInjector testInjector) {43 if (!k3s) {44 testInjector.injectIntoFields(kindContainer.client(), (f) -> f.getType().isAssignableFrom(DefaultKubernetesClient.class));45 } else {46 Config config = Config.fromKubeconfig(k3sContainer.getKubeConfigYaml());47 // workaround for undiagnosed issue; fabric8 seems to not identify48 // the client key algorithm correctly, so fails to work with K3s49 // ECDSA keys unless configured explicitly50 config.setClientKeyAlgo("EC");51 DefaultKubernetesClient k8sclient = new DefaultKubernetesClient(config);52 testInjector.injectIntoFields(k8sclient, (f) -> f.getType().isAssignableFrom(DefaultKubernetesClient.class));53 }54 }55 @Override56 public void init(Map<String, String> initArgs) {57 QuarkusTestResourceLifecycleManager.super.init(initArgs);58 if (k3s) {59 logger = new ToStringConsumer();60 k3sContainer = new K3sContainer(DockerImageName.parse("rancher/k3s:v1.23.1-rc1-k3s1")).withLogConsumer(new CustomLogConsumer(log).withPrefix("KIND"));61 } else {62 kindContainer = new KindContainer().withLogConsumer(new CustomLogConsumer(log).withPrefix("KIND"));63 }64 }65}...

Full Screen

Full Screen

Source:LocalImageDeploymentIT.java Github

copy

Full Screen

...4import org.junit.jupiter.api.Timeout;5import org.testcontainers.containers.BindMode;6import org.testcontainers.junit.jupiter.Container;7import org.testcontainers.junit.jupiter.Testcontainers;8import org.testcontainers.k3s.K3sContainer;9import org.testcontainers.utility.DockerImageName;10import java.util.concurrent.TimeUnit;11import static org.junit.jupiter.api.Assertions.assertEquals;12import static org.junit.jupiter.api.Assertions.assertTrue;13/**14 * Test capability that allow us to import images properly15 */16@Testcontainers17public class LocalImageDeploymentIT {18 @Container19 private final @NotNull K3sContainer container = new K3sContainer(DockerImageName20 .parse("rancher/k3s:v1.21.3-k3s1"))21 .withFileSystemBind("./build/containers", "/build", BindMode.READ_ONLY);22 @Timeout(value = 2, unit = TimeUnit.MINUTES)23 @Test24 public void withLocalImages_mqttMessagePublishedReceived() throws Exception {25 var outLoadImage = container.execInContainer("/bin/ctr",26 "images",27 "import",28 "/build/" + "hivemq-k8s-image.tar");29 assertEquals(0, outLoadImage.getExitCode());30 final var outListImages = container.execInContainer("/bin/ctr", "images", "ls");31 assertTrue(outListImages.getStdout().contains("hivemq/hivemq4-test"));32 }33}...

Full Screen

Full Screen

Source:K3sEnvironment.java Github

copy

Full Screen

...3import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;4import org.testcontainers.containers.wait.strategy.Wait;5import org.testcontainers.containers.wait.strategy.WaitStrategy;6import org.testcontainers.junit.jupiter.Testcontainers;7import org.testcontainers.k3s.K3sContainer;8import org.testcontainers.utility.DockerImageName;9import lombok.extern.slf4j.Slf4j;10@Slf4j11@Testcontainers12public class K3sEnvironment {13 private K3sContainer k3s;14 public void start() {15 DockerImageName dockerImage = DockerImageName.parse("rancher/k3s:v1.21.3-k3s1");16 this.k3s = new K3sContainer(dockerImage)17 .withLogConsumer(new Slf4jLogConsumer(log));18 Runtime.getRuntime().addShutdownHook(new Thread(() -> {19 this.k3s.close();20 }));21 // this.k3s.waitingFor(Wait.defaultWaitStrategy().waitUntilReady(waitStrategyTarget));22 // System.out.println(this.k3s.isRunning());23 while (!this.k3s.isRunning()){24 25 };26 }27 /**28 * 29 * @return config for local Kubernetes cluster30 */...

Full Screen

Full Screen

K3sContainer

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.k3s;2import org.testcontainers.containers.GenericContainer;3import org.testcontainers.containers.wait.strategy.Wait;4import org.testcontainers.utility.DockerImageName;5public class K3sContainer<SELF extends K3sContainer<SELF>> extends GenericContainer<SELF> {6 private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("rancher/k3s");7 private static final String DEFAULT_TAG = "latest";8 private static final Integer K3S_PORT = 6443;9 private static final Integer K3S_API_PORT = 6444;10 public K3sContainer() {11 this(DEFAULT_IMAGE_NAME.withTag(DEFAULT_TAG));12 }13 public K3sContainer(final String dockerImageName) {14 this(DockerImageName.parse(dockerImageName));15 }16 public K3sContainer(final DockerImageName dockerImageName) {17 super(dockerImageName);18 dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);19 withExposedPorts(K3S_PORT, K3S_API_PORT);20 waitingFor(Wait.forHttp("/healthz").forStatusCode(200));21 }22 public Integer getK3sPort() {23 return getMappedPort(K3S_PORT);24 }25 public Integer getK3sApiPort() {26 return getMappedPort(K3S_API_PORT);27 }28 public String getK3sUrl() {29 }30}31package org.testcontainers.k3s;32import org.junit.jupiter.api.Test;33import org.testcontainers.junit.jupiter.Container;34import org.testcontainers.junit.jupiter.Testcontainers;35public class K3sContainerTest {36 private final K3sContainer k3sContainer = new K3sContainer();37 void test() {38 System.out.println("K3s port: " + k3sContainer.getK3sPort());39 System.out.println("K3s API port: " + k3sContainer.getK3sApiPort());40 System.out.println("K3s URL: " + k3sContainer.getK3sUrl());41 }42}

Full Screen

Full Screen

K3sContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.k3s.K3sContainer;2public class K3sTest {3 public static void main(String[] args) {4 K3sContainer k3s = new K3sContainer();5 k3s.start();6 System.out.println("K3s is running");7 k3s.stop();8 }9}

Full Screen

Full Screen

K3sContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.k3s.K3sContainer;2public class TestK3s {3 public static void main(String[] args) {4 K3sContainer k3s = new K3sContainer();5 k3s.start();6 System.out.println("K3s cluster is running");7 }8}

Full Screen

Full Screen

K3sContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.k3s.K3sContainer;2import org.testcontainers.k3s.K3sVersion;3import org.testcontainers.utility.MountableFile;4public class K3sContainerTest {5 public static void main(String[] args) {6 try (K3sContainer k3s = new K3sContainer(K3sVersion.v1_18_6_k3s1)) {7 k3s.withK3sImage("rancher/k3s:v1.18.6-k3s1");8 k3s.withCopyFileToContainer(MountableFile.forClasspathResource("k3s.yaml"), "/k3s.yaml");9 k3s.start();10 System.out.println("Kubectl version is: " + k3s.getKubectlVersion());11 System.out.println("K3s version is: " + k3s.getK3sVersion());12 System.out.println("K3s server URL is: " + k3s.getK3sServerUrl());13 System.out.println("K3s server token is: " + k3s.getK3sServerToken());14 System.out.println("K3s server certificate is: " + k3s.getK3sServerCertificate());15 }16 }17}18import org.testcontainers.k3s.K3sContainer;19import org.testcontainers.k3s.K3sVersion;20import org.testcontainers.utility.MountableFile;21public class K3sContainerTest {22 public static void main(String[] args) {23 try (K3sContainer k3s = new K3sContainer(K3sVersion.v1_18_6_k3s1)) {24 k3s.withK3sImage("rancher/k3s:v1.18.6-k3s1");25 k3s.withCopyFileToContainer(MountableFile.forClasspathResource("k3s.yaml"), "/k3s.yaml");26 k3s.start();27 System.out.println("Kubectl version is: " + k3s.getKubectlVersion());28 System.out.println("K3s version is: " + k3s.getK3sVersion());29 System.out.println("K3s server URL is: " + k3s.getK3

Full Screen

Full Screen

K3sContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.k3s.K3sContainer;2import org.testcontainers.k3s.K3sConfig;3import org.testcontainers.k3s.K3sConfigBuilder;4public class TestK3sContainer {5 public static void main(String[] args) {6 K3sConfig k3sConfig = new K3sConfigBuilder()7 .withK3sVersion("v1.19.5+k3s1")8 .withClusterSecret("abc123")9 .withK3sImage("rancher/k3s:v1.19.5-k3s1")10 .build();11 K3sContainer k3s = new K3sContainer(k3sConfig);12 k3s.start();13 System.out.println("K3s is running");14 k3s.stop();15 }16}

Full Screen

Full Screen

K3sContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.output.Slf4jLogConsumer;2import org.testcontainers.k3s.K3sContainer;3import org.testcontainers.utility.MountableFile;4import org.slf4j.LoggerFactory;5public class testK3sContainer {6 public static void main(String[] args) throws Exception {7 K3sContainer container = new K3sContainer("v1.17.2-k3s1")8 .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("K3s")));9 container.start();10 container.createPod(MountableFile.forClasspathResource("test-pod.yaml"));11 Thread.sleep(30000);12 container.stop();13 }14}

Full Screen

Full Screen

K3sContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.K3sContainer;2import org.testcontainers.containers.output.Slf4jLogConsumer;3import org.testcontainers.utility.DockerImageName;4import java.nio.file.Paths;5import static org.testcontainers.k3s.K3sContainer.K3S_VERSION;6public class 1 {7 public static void main(String[] args) {8 K3sContainer k3s = new K3sContainer(DockerImageName.parse("docker.io/rancher/k3s").withTag(K3S_VERSION))9 .withK3sVersion(K3S_VERSION)10 .withLogConsumer(new Slf4jLogConsumer(System.out));11 k3s.start();12 k3s.createDeployment(Paths.get("deployment.yaml"));13 k3s.deleteDeployment("hello-world");14 k3s.stop();15 }16}

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