-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #349 from anfredette/unit-test-containers
Add unit test for Uprobe in container
- Loading branch information
Showing
9 changed files
with
314 additions
and
65 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
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
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
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
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
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,84 @@ | ||
package bpfmanagent | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
bpfmaniov1alpha1 "github.com/bpfman/bpfman-operator/apis/v1alpha1" | ||
"github.com/go-logr/logr" | ||
|
||
"github.com/stretchr/testify/require" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
clientGoFake "k8s.io/client-go/kubernetes/fake" | ||
) | ||
|
||
type FakeContainerGetter struct { | ||
containerList *[]ContainerInfo | ||
} | ||
|
||
func (f *FakeContainerGetter) GetContainers(ctx context.Context, containerSelector *bpfmaniov1alpha1.ContainerSelector, | ||
logger logr.Logger) (*[]ContainerInfo, error) { | ||
return f.containerList, nil | ||
} | ||
|
||
func TestGetPods(t *testing.T) { | ||
ctx := context.TODO() | ||
|
||
// Create a fake clientset | ||
clientset := clientGoFake.NewSimpleClientset() | ||
|
||
// Create a ContainerSelector | ||
containerSelector := &bpfmaniov1alpha1.ContainerSelector{ | ||
Pods: metav1.LabelSelector{ | ||
MatchLabels: map[string]string{ | ||
"app": "test", | ||
}, | ||
}, | ||
Namespace: "default", | ||
} | ||
|
||
nodeName := "test-node" | ||
|
||
// Create a Pod that matches the label selector and is on the correct node | ||
pod := &v1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-pod", | ||
Namespace: "default", | ||
Labels: map[string]string{ | ||
"app": "test", | ||
}, | ||
}, | ||
Spec: v1.PodSpec{ | ||
NodeName: nodeName, | ||
}, | ||
} | ||
|
||
containerGetter := RealContainerGetter{ | ||
nodeName: nodeName, | ||
clientSet: clientset, | ||
} | ||
|
||
// Add the Pod to the fake clientset | ||
_, err := clientset.CoreV1().Pods("default").Create(ctx, pod, metav1.CreateOptions{}) | ||
require.NoError(t, err) | ||
|
||
// Call getPods and check the returned PodList | ||
podList, err := containerGetter.getPodsForNode(ctx, containerSelector) | ||
require.NoError(t, err) | ||
require.Len(t, podList.Items, 1) | ||
require.Equal(t, "test-pod", podList.Items[0].Name) | ||
|
||
// Try another selector | ||
// Create a ContainerSelector | ||
containerSelector = &bpfmaniov1alpha1.ContainerSelector{ | ||
Pods: metav1.LabelSelector{ | ||
MatchLabels: map[string]string{}, | ||
}, | ||
} | ||
|
||
podList, err = containerGetter.getPodsForNode(ctx, containerSelector) | ||
require.NoError(t, err) | ||
require.Len(t, podList.Items, 1) | ||
require.Equal(t, "test-pod", podList.Items[0].Name) | ||
} |
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
Oops, something went wrong.