Skip to content

Commit

Permalink
add unit test from code copilot
Browse files Browse the repository at this point in the history
  • Loading branch information
eumel8 committed Dec 22, 2024
1 parent 14b02e9 commit 4343fc5
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/unit.yaml
Original file line number Diff line number Diff line change
@@ -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

82 changes: 82 additions & 0 deletions overlay_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}

0 comments on commit 4343fc5

Please sign in to comment.