|
1 | 1 | import { K3sContainer } from "./k3s-container"; |
2 | 2 | import * as k8s from "@kubernetes/client-node"; |
3 | 3 | import { setTimeout } from "node:timers/promises"; |
| 4 | +import { GenericContainer, Network, Wait } from "testcontainers"; |
4 | 5 |
|
5 | 6 | describe("K3s", () => { |
6 | | - jest.setTimeout(150_000); |
| 7 | + jest.setTimeout(120_000); |
7 | 8 |
|
8 | | - it("should start and have listable node", async () => { |
9 | | - // starting_k3s { |
10 | | - const container = await new K3sContainer().start(); |
11 | | - // } |
| 9 | + it("should construct", () => { |
| 10 | + new K3sContainer("rancher/k3s:v1.31.2-k3s1"); |
| 11 | + }); |
12 | 12 |
|
13 | | - // connecting_with_client { |
14 | | - // obtain a kubeconfig file which allows us to connect to k3s |
15 | | - const kubeConfig = container.getKubeConfig(); |
| 13 | + // K3sContainer runs as a privileged container |
| 14 | + if (!process.env["CI_ROOTLESS"]) { |
| 15 | + it("should start and have listable node", async () => { |
| 16 | + // starting_k3s { |
| 17 | + const container = await new K3sContainer("rancher/k3s:v1.31.2-k3s1").start(); |
| 18 | + // } |
16 | 19 |
|
17 | | - const kc = new k8s.KubeConfig(); |
18 | | - kc.loadFromString(kubeConfig); |
| 20 | + // connecting_with_client { |
| 21 | + // obtain a kubeconfig file that allows us to connect to k3s |
| 22 | + const kubeConfig = container.getKubeConfig(); |
19 | 23 |
|
20 | | - const client = kc.makeApiClient(k8s.CoreV1Api); |
| 24 | + const kc = new k8s.KubeConfig(); |
| 25 | + kc.loadFromString(kubeConfig); |
21 | 26 |
|
22 | | - // interact with the running K3s server, e.g.: |
23 | | - const nodeList = await client.listNode(); |
24 | | - // } |
| 27 | + const client = kc.makeApiClient(k8s.CoreV1Api); |
25 | 28 |
|
26 | | - expect(nodeList.items).toHaveLength(1); |
| 29 | + // interact with the running K3s server, e.g.: |
| 30 | + const nodeList = await client.listNode(); |
| 31 | + // } |
27 | 32 |
|
28 | | - await container.stop(); |
29 | | - }); |
| 33 | + expect(nodeList.items).toHaveLength(1); |
30 | 34 |
|
31 | | - it("should start a pod", async () => { |
32 | | - const container = await new K3sContainer().start(); |
33 | | - const kc = new k8s.KubeConfig(); |
34 | | - kc.loadFromString(container.getKubeConfig()); |
35 | | - |
36 | | - const pod = { |
37 | | - metadata: { |
38 | | - name: "helloworld", |
39 | | - }, |
40 | | - spec: { |
41 | | - containers: [ |
42 | | - { |
43 | | - name: "helloworld", |
44 | | - image: "testcontainers/helloworld:1.1.0", |
45 | | - ports: [ |
46 | | - { |
47 | | - containerPort: 8080, |
48 | | - }, |
49 | | - ], |
50 | | - readinessProbe: { |
51 | | - tcpSocket: { |
52 | | - port: 8080, |
| 35 | + await container.stop(); |
| 36 | + }); |
| 37 | + |
| 38 | + it("should expose kubeconfig for a network alias", async () => { |
| 39 | + const network = await new Network().start(); |
| 40 | + const container = await new K3sContainer("rancher/k3s:v1.31.2-k3s1") |
| 41 | + .withNetwork(network) |
| 42 | + .withNetworkAliases("k3s") |
| 43 | + .start(); |
| 44 | + |
| 45 | + // obtain a kubeconfig that allows us to connect on the custom network |
| 46 | + const kubeConfig = container.getAliasedKubeConfig("k3s"); |
| 47 | + |
| 48 | + const kubectlContainer = await new GenericContainer("rancher/kubectl:v1.31.2") |
| 49 | + .withNetwork(network) |
| 50 | + .withCopyContentToContainer([{ content: kubeConfig, target: "/home/kubectl/.kube/config" }]) |
| 51 | + .withCommand(["get", "namespaces"]) |
| 52 | + .withWaitStrategy(Wait.forOneShotStartup()) |
| 53 | + .withStartupTimeout(30_000) |
| 54 | + .start(); |
| 55 | + |
| 56 | + const chunks = []; |
| 57 | + for await (const chunk of await kubectlContainer.logs()) { |
| 58 | + chunks.push(chunk); |
| 59 | + } |
| 60 | + expect(chunks).toEqual(expect.arrayContaining([expect.stringContaining("kube-system")])); |
| 61 | + |
| 62 | + await kubectlContainer.stop(); |
| 63 | + await container.stop(); |
| 64 | + await network.stop(); |
| 65 | + }); |
| 66 | + |
| 67 | + it("should start a pod", async () => { |
| 68 | + const container = await new K3sContainer("rancher/k3s:v1.31.2-k3s1").start(); |
| 69 | + const kc = new k8s.KubeConfig(); |
| 70 | + kc.loadFromString(container.getKubeConfig()); |
| 71 | + |
| 72 | + const pod = { |
| 73 | + metadata: { |
| 74 | + name: "helloworld", |
| 75 | + }, |
| 76 | + spec: { |
| 77 | + containers: [ |
| 78 | + { |
| 79 | + name: "helloworld", |
| 80 | + image: "testcontainers/helloworld:1.1.0", |
| 81 | + ports: [ |
| 82 | + { |
| 83 | + containerPort: 8080, |
| 84 | + }, |
| 85 | + ], |
| 86 | + readinessProbe: { |
| 87 | + tcpSocket: { |
| 88 | + port: 8080, |
| 89 | + }, |
53 | 90 | }, |
54 | 91 | }, |
55 | | - }, |
56 | | - ], |
57 | | - }, |
58 | | - }; |
59 | | - |
60 | | - const client = kc.makeApiClient(k8s.CoreV1Api); |
61 | | - await client.createNamespacedPod({ namespace: "default", body: pod }); |
62 | | - |
63 | | - // wait for pod to be ready |
64 | | - let ready = false; |
65 | | - for (const startTime = Date.now(); Date.now() - startTime < 60_000; ) { |
66 | | - const podList = await client.listNamespacedPod({ namespace: "default" }); |
67 | | - const pod = podList.items.find((pod) => pod.metadata?.name === "helloworld"); |
68 | | - const status = pod?.status; |
69 | | - ready = |
70 | | - status?.phase === "Running" && |
71 | | - !!status?.conditions?.some((cond) => cond.type === "Ready" && cond.status === "True"); |
72 | | - if (ready) break; |
73 | | - await setTimeout(3_000); |
74 | | - } |
75 | | - |
76 | | - expect(ready).toBe(true); |
77 | | - |
78 | | - await container.stop(); |
79 | | - }); |
| 92 | + ], |
| 93 | + }, |
| 94 | + }; |
| 95 | + |
| 96 | + const client = kc.makeApiClient(k8s.CoreV1Api); |
| 97 | + await client.createNamespacedPod({ namespace: "default", body: pod }); |
| 98 | + |
| 99 | + // wait for pod to be ready |
| 100 | + expect(await podIsReady(client, "default", "helloworld", 60_000)).toBe(true); |
| 101 | + |
| 102 | + await container.stop(); |
| 103 | + }); |
| 104 | + } |
80 | 105 | }); |
| 106 | + |
| 107 | +async function podIsReady(client: k8s.CoreV1Api, namespace: string, name: string, timeout: number): Promise<boolean> { |
| 108 | + for (const startTime = Date.now(); Date.now() - startTime < timeout; ) { |
| 109 | + const res = await client.readNamespacedPodStatus({ namespace, name }); |
| 110 | + const ready = |
| 111 | + res.status?.phase === "Running" && |
| 112 | + !!res.status?.conditions?.some((cond) => cond.type === "Ready" && cond.status === "True"); |
| 113 | + if (ready) return true; |
| 114 | + await setTimeout(3_000); |
| 115 | + } |
| 116 | + return false; |
| 117 | +} |
0 commit comments