Skip to content

Commit

Permalink
Add k3s WithManifest option
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Chacin <pablochacin@gmail.com>
  • Loading branch information
pablochacin committed Nov 16, 2023
1 parent 0aab31f commit f5f1859
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
20 changes: 20 additions & 0 deletions modules/k3s/k3s.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ type K3sContainer struct {
testcontainers.Container
}

// WithManifest loads the manifest into the cluster and applies it
func WithManifest(manifestPath string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) {
manifest := filepath.Base(manifestPath)
target := "/var/lib/rancher/k3s/server/manifests/" + manifest

// Add a post start hook to copy the manifest.
// We cannot use the Files option in the GenericRequest because the target
// path is created when the container starts
manifestHook := testcontainers.ContainerLifecycleHooks{
PostStarts: []testcontainers.ContainerHook{
func(ctx context.Context, c testcontainers.Container) error {
return c.CopyFileToContainer(ctx, manifest, target, 0x644)
},
},
}
req.LifecycleHooks = append(req.LifecycleHooks, manifestHook)
}
}

// RunContainer creates an instance of the K3s container type
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*K3sContainer, error) {
host, err := getContainerHost(ctx, opts...)
Expand Down
44 changes: 44 additions & 0 deletions modules/k3s/k3s_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package k3s_test

import (
"bytes"
"context"
"testing"
"time"
Expand Down Expand Up @@ -161,3 +162,46 @@ func Test_APIServerReady(t *testing.T) {
t.Fatalf("failed to create pod %v", err)
}
}

func Test_WithManifestOption(t *testing.T) {
ctx := context.Background()

k3sContainer, err := k3s.RunContainer(ctx,
testcontainers.WithImage("docker.io/rancher/k3s:v1.27.1-k3s1"),
k3s.WithManifest("nginx-manifest.yaml"),
)
if err != nil {
t.Fatal(err)
}

// Clean up the container
defer func() {
if err := k3sContainer.Terminate(ctx); err != nil {
t.Fatal(err)
}
}()

// FIXME: while we don't have a WaitFor condition for the pods to get ready
// give some time for the pod to be created
time.Sleep(10 * time.Second)

rc, stdout, err := k3sContainer.Exec(
ctx,
[]string{
"kubectl",
"wait",
"pod",
"nginx",
"--for=condition=Ready",
"--timeout=90s",
},
)
if err != nil {
t.Fatalf("waiting for pods ready %v", err)
}
if rc != 0 {
output := bytes.Buffer{}
output.ReadFrom(stdout)
t.Fatalf("pods not ready \n%s\n", output.String())
}
}
14 changes: 14 additions & 0 deletions modules/k3s/nginx-manifest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Pod
metadata:
labels:
run: pod
name: nginx
namespace: default
spec:
containers:
- name: pod
image: nginx
imagePullPolicy: Always


0 comments on commit f5f1859

Please sign in to comment.