-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |