From 4343fc5952998f0b7b918d328a3a113cbb868e11 Mon Sep 17 00:00:00 2001 From: Frank Kloeker Date: Sun, 22 Dec 2024 15:13:40 +0100 Subject: [PATCH] add unit test from code copilot --- .github/workflows/unit.yaml | 33 +++++++++++++++ overlay_test.go | 82 +++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 .github/workflows/unit.yaml create mode 100644 overlay_test.go diff --git a/.github/workflows/unit.yaml b/.github/workflows/unit.yaml new file mode 100644 index 0000000..00590aa --- /dev/null +++ b/.github/workflows/unit.yaml @@ -0,0 +1,33 @@ +# .github/workflows/test.yml +name: Go Unit Tests + +on: + push: + branches: + - feat/unittests + pull_request: + branches: + - feat/unittests + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Setup Go + uses: actions/setup-go@v4 + with: + go-version: "1.19" + - name: Install dependencies + run: go mod tidy + - name: Run tests + run: go test -v ./... -coverprofile=coverage.out + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v3 + with: + files: coverage.out + flags: unittests + fail_ci_if_error: true + - name: Generate coverage badge + run: go tool cover -func coverage.out + diff --git a/overlay_test.go b/overlay_test.go new file mode 100644 index 0000000..5c8cd0e --- /dev/null +++ b/overlay_test.go @@ -0,0 +1,82 @@ +// overlay_test.go +package main + +import ( + "context" + "testing" + + apps "k8s.io/api/apps/v1" + core "k8s.io/api/core/v1" + meta "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes/fake" +) + +func TestCreateDaemonSet(t *testing.T) { + clientset := fake.NewSimpleClientset() + namespace := "kube-system" + app := "overlaytest" + + daemonset := &apps.DaemonSet{ + ObjectMeta: meta.ObjectMeta{ + Name: app, + }, + Spec: apps.DaemonSetSpec{ + Selector: &meta.LabelSelector{ + MatchLabels: map[string]string{ + "app": app, + }, + }, + Template: core.PodTemplateSpec{ + ObjectMeta: meta.ObjectMeta{ + Labels: map[string]string{ + "app": app, + }, + }, + Spec: core.PodSpec{ + Containers: []core.Container{ + { + Name: app, + Image: "test-image", + }, + }, + }, + }, + }, + } + + _, err := clientset.AppsV1().DaemonSets(namespace).Create(context.TODO(), daemonset, meta.CreateOptions{}) + if err != nil { + t.Fatalf("Failed to create DaemonSet: %v", err) + } + + _, err = clientset.AppsV1().DaemonSets(namespace).Get(context.TODO(), app, meta.GetOptions{}) + if err != nil { + t.Fatalf("Failed to get DaemonSet: %v", err) + } +} + +func TestListPods(t *testing.T) { + clientset := fake.NewSimpleClientset( + &core.Pod{ + ObjectMeta: meta.ObjectMeta{ + Name: "test-pod", + Namespace: "kube-system", + Labels: map[string]string{"app": "overlaytest"}, + }, + Status: core.PodStatus{ + PodIP: "192.168.1.1", + }, + }, + ) + + pods, err := clientset.CoreV1().Pods("kube-system").List(context.TODO(), meta.ListOptions{ + LabelSelector: "app=overlaytest", + }) + if err != nil { + t.Fatalf("Failed to list Pods: %v", err) + } + + if len(pods.Items) != 1 { + t.Fatalf("Expected 1 pod, got %d", len(pods.Items)) + } +}