Skip to content

Commit 47d29d6

Browse files
committed
add mock dgx server
1 parent ebecf8e commit 47d29d6

File tree

1 file changed

+93
-54
lines changed

1 file changed

+93
-54
lines changed

internal/controller/instaslice_daemonset_test.go

Lines changed: 93 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18,67 +18,106 @@ package controller
1818

1919
import (
2020
"context"
21+
"os"
22+
"testing"
2123

22-
. "github.com/onsi/ginkgo/v2"
23-
. "github.com/onsi/gomega"
24-
"k8s.io/apimachinery/pkg/api/errors"
25-
"k8s.io/apimachinery/pkg/types"
26-
"sigs.k8s.io/controller-runtime/pkg/reconcile"
24+
"github.com/NVIDIA/go-nvml/pkg/nvml"
25+
"github.com/NVIDIA/go-nvml/pkg/nvml/mock/dgxa100"
26+
"github.com/go-logr/logr/testr"
27+
"github.com/stretchr/testify/assert"
28+
v1 "k8s.io/api/core/v1"
29+
"k8s.io/client-go/kubernetes/scheme"
2730

2831
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32+
"k8s.io/apimachinery/pkg/types"
2933

3034
inferencev1 "codeflare.dev/instaslice/api/v1"
35+
runtimefake "sigs.k8s.io/controller-runtime/pkg/client/fake"
3136
)
3237

33-
var _ = Describe("Instaslice Daemonset", func() {
34-
Context("When reconciling a resource", func() {
35-
const resourceName = "test-resource"
38+
func TestCleanUp(t *testing.T) {
39+
// Set up the mock server
40+
server := dgxa100.New()
3641

37-
ctx := context.Background()
38-
39-
typeNamespacedName := types.NamespacedName{
40-
Name: resourceName,
41-
Namespace: "default", // TODO(user):Modify as needed
42-
}
43-
instaslice := &inferencev1.Instaslice{}
44-
45-
BeforeEach(func() {
46-
By("creating the custom resource for the Kind Instaslice")
47-
err := k8sClient.Get(ctx, typeNamespacedName, instaslice)
48-
if err != nil && errors.IsNotFound(err) {
49-
resource := &inferencev1.Instaslice{
50-
ObjectMeta: metav1.ObjectMeta{
51-
Name: resourceName,
52-
Namespace: "default",
53-
},
54-
// TODO(user): Specify other spec details if needed.
55-
}
56-
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
57-
}
58-
})
59-
60-
AfterEach(func() {
61-
// TODO(user): Cleanup logic after each test, like removing the resource instance.
62-
resource := &inferencev1.Instaslice{}
63-
err := k8sClient.Get(ctx, typeNamespacedName, resource)
64-
Expect(err).NotTo(HaveOccurred())
65-
66-
By("Cleanup the specific resource instance Instaslice")
67-
Expect(k8sClient.Delete(ctx, resource)).To(Succeed())
68-
})
69-
It("should successfully reconcile the resource", func() {
70-
By("Reconciling the created resource")
71-
controllerReconciler := &InstasliceReconciler{
72-
Client: k8sClient,
73-
Scheme: k8sClient.Scheme(),
42+
// Mock the NVML functions
43+
nvml.Init = func() nvml.Return {
44+
return nvml.SUCCESS
45+
}
46+
nvml.Shutdown = func() nvml.Return {
47+
return nvml.SUCCESS
48+
}
49+
nvml.DeviceGetHandleByUUID = func(uuid string) (nvml.Device, nvml.Return) {
50+
for _, dev := range server.Devices {
51+
device := dev.(*dgxa100.Device)
52+
if device.UUID == uuid {
53+
return device, nvml.SUCCESS
7454
}
55+
}
56+
return nil, nvml.ERROR_NOT_FOUND
57+
}
58+
59+
// Create a fake Kubernetes client
60+
s := scheme.Scheme
61+
_ = inferencev1.AddToScheme(s)
62+
fakeClient := runtimefake.NewClientBuilder().WithScheme(s).Build()
63+
64+
// Create a fake kubernetes clientset
65+
66+
//fakeKubeClient := fake.NewSimpleClientset()
67+
68+
// Create an InstaSliceDaemonsetReconciler
69+
reconciler := &InstaSliceDaemonsetReconciler{
70+
Client: fakeClient,
71+
Scheme: s,
72+
}
73+
// Create a fake Instaslice resource
74+
instaslice := &inferencev1.Instaslice{
75+
ObjectMeta: metav1.ObjectMeta{
76+
Name: "node-1",
77+
},
78+
Spec: inferencev1.InstasliceSpec{
79+
Prepared: map[string]inferencev1.PreparedDetails{
80+
"mig-uuid-1": {
81+
PodUUID: "pod-uid-1",
82+
Parent: "GPU-1",
83+
Giinfoid: 1,
84+
Ciinfoid: 1,
85+
},
86+
},
87+
Allocations: map[string]inferencev1.AllocationDetails{
88+
"allocation-1": {
89+
PodUUID: "pod-uid-1",
90+
PodName: "pod-name-1",
91+
Namespace: "default",
92+
},
93+
},
94+
},
95+
}
96+
fakeClient.Create(context.Background(), instaslice)
97+
98+
// Set the NODE_NAME environment variable
99+
os.Setenv("NODE_NAME", "node-1")
100+
defer os.Unsetenv("NODE_NAME")
101+
102+
// Create a fake Pod resource
103+
pod := &v1.Pod{
104+
ObjectMeta: metav1.ObjectMeta{
105+
UID: "pod-uid-1",
106+
Name: "pod-name-1",
107+
Namespace: "default",
108+
},
109+
}
110+
111+
// Create a logger
112+
logger := testr.New(t)
113+
114+
// Call the cleanUp function
115+
reconciler.cleanUp(context.Background(), pod, logger)
75116

76-
_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
77-
NamespacedName: typeNamespacedName,
78-
})
79-
Expect(err).NotTo(HaveOccurred())
80-
// TODO(user): Add more specific assertions depending on your controller's reconciliation logic.
81-
// Example: If you expect a certain status condition after reconciliation, verify it here.
82-
})
83-
})
84-
})
117+
// Verify the Instaslice resource was updated
118+
var updatedInstaslice inferencev1.Instaslice
119+
err := fakeClient.Get(context.Background(), types.NamespacedName{Name: "node-1"}, &updatedInstaslice)
120+
assert.NoError(t, err)
121+
assert.Empty(t, updatedInstaslice.Spec.Prepared)
122+
assert.Empty(t, updatedInstaslice.Spec.Allocations)
123+
}

0 commit comments

Comments
 (0)