Skip to content

Commit

Permalink
Merge pull request #632 from sebrandon1/coverage
Browse files Browse the repository at this point in the history
Fix daemonset unit tests
  • Loading branch information
sebrandon1 authored Dec 14, 2023
2 parents 401046c + ac69a95 commit bcebb60
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pre-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ name: Test Incoming Changes

'on':
push:
branches: [main]
branches: [main, ginkgo_removal]
pull_request:
branches: [main]
branches: [main, ginkgo_removal]

jobs:
lint:
Expand Down
10 changes: 6 additions & 4 deletions tests/globalhelper/daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ import (
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
appsv1Typed "k8s.io/client-go/kubernetes/typed/apps/v1"
corev1Typed "k8s.io/client-go/kubernetes/typed/core/v1"

. "github.com/onsi/gomega"
)

func CreateAndWaitUntilDaemonSetIsReady(daemonSet *appsv1.DaemonSet, timeout time.Duration) error {
return createAndWaitUntilDaemonSetIsReady(GetAPIClient().K8sClient.AppsV1(), daemonSet, timeout)
return createAndWaitUntilDaemonSetIsReady(GetAPIClient().K8sClient.AppsV1(), GetAPIClient().K8sClient.CoreV1(), daemonSet, timeout)
}

// CreateAndWaitUntilDaemonSetIsReady creates daemonSet and waits until all pods are up and running.
func createAndWaitUntilDaemonSetIsReady(appsClient appsv1Typed.AppsV1Interface,
coreClient corev1Typed.CoreV1Interface,
daemonSet *appsv1.DaemonSet, timeout time.Duration) error {
runningDaemonSet, err := appsClient.DaemonSets(daemonSet.Namespace).Create(
context.TODO(), daemonSet, metav1.CreateOptions{})
Expand All @@ -33,7 +35,7 @@ func createAndWaitUntilDaemonSetIsReady(appsClient appsv1Typed.AppsV1Interface,
}

Eventually(func() bool {
status, err := isDaemonSetReady(appsClient, runningDaemonSet.Namespace, runningDaemonSet.Name)
status, err := isDaemonSetReady(appsClient, coreClient, runningDaemonSet.Namespace, runningDaemonSet.Name)
if err != nil {
glog.Errorf(
"daemonset %s is not ready, retry in 5 seconds", runningDaemonSet.Name)
Expand All @@ -47,7 +49,7 @@ func createAndWaitUntilDaemonSetIsReady(appsClient appsv1Typed.AppsV1Interface,
return nil
}

func isDaemonSetReady(client appsv1Typed.AppsV1Interface, namespace string, name string) (bool, error) {
func isDaemonSetReady(client appsv1Typed.AppsV1Interface, coreClient corev1Typed.CoreV1Interface, namespace string, name string) (bool, error) {
daemonSet, err := client.DaemonSets(namespace).Get(
context.TODO(),
name,
Expand All @@ -58,7 +60,7 @@ func isDaemonSetReady(client appsv1Typed.AppsV1Interface, namespace string, name
}

// Get number of nodes and compare with the number of scheduled pods
numNodes := GetNumberOfNodes()
numNodes := GetNumberOfNodes(coreClient)
if daemonSet.Status.DesiredNumberScheduled == int32(numNodes) &&
daemonSet.Status.NumberReady == daemonSet.Status.DesiredNumberScheduled {
return true, nil
Expand Down
11 changes: 10 additions & 1 deletion tests/globalhelper/daemonset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,19 @@ func TestIsDaemonsetReady(t *testing.T) {
var runtimeObjects []runtime.Object
runtimeObjects = append(runtimeObjects, generateDaemonset(testCase.numAvailable,
testCase.numScheduled, testCase.numUnavailable, testCase.numReady))
runtimeObjects = append(runtimeObjects, &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "test-node",
Labels: map[string]string{
"node-role.kubernetes.io/worker-cnf": "",
},
},
})

client := k8sfake.NewSimpleClientset(runtimeObjects...)

t.Run(testCase.name, func(t *testing.T) {
got, err := isDaemonSetReady(client.AppsV1(), "default", "test-daemonset")
got, err := isDaemonSetReady(client.AppsV1(), client.CoreV1(), "default", "test-daemonset")
if (err != nil) != testCase.wantErr {
t.Errorf("isDaemonsetReady() error = %v, wantErr %v", err, testCase.wantErr)

Expand Down
4 changes: 2 additions & 2 deletions tests/globalhelper/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func NodeHasHugePagesEnabled(node *corev1.Node, resourceName string) bool {
return hugepagesEnabled
}

func GetNumberOfNodes() int {
return getNumberOfNodes(GetAPIClient().K8sClient.CoreV1())
func GetNumberOfNodes(client corev1Typed.CoreV1Interface) int {
return getNumberOfNodes(client)
}

func getNumberOfNodes(client corev1Typed.CoreV1Interface) int {
Expand Down

0 comments on commit bcebb60

Please sign in to comment.