@@ -21,14 +21,18 @@ import (
2121
2222 . "github.com/onsi/ginkgo/v2"
2323 . "github.com/onsi/gomega"
24+ corev1 "k8s.io/api/core/v1"
2425 "k8s.io/apimachinery/pkg/api/errors"
26+ "k8s.io/apimachinery/pkg/api/resource"
2527 "k8s.io/apimachinery/pkg/types"
2628 "sigs.k8s.io/controller-runtime/pkg/reconcile"
2729
2830 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2931
3032 tfv1 "github.com/NexusGPU/tensor-fusion-operator/api/v1"
3133 "github.com/NexusGPU/tensor-fusion-operator/internal/config"
34+ "github.com/NexusGPU/tensor-fusion-operator/internal/constants"
35+ "github.com/NexusGPU/tensor-fusion-operator/internal/scheduler"
3236)
3337
3438var _ = Describe ("TensorFusionConnection Controller" , func () {
@@ -39,27 +43,61 @@ var _ = Describe("TensorFusionConnection Controller", func() {
3943
4044 typeNamespacedName := types.NamespacedName {
4145 Name : resourceName ,
42- Namespace : "default" , // TODO(user):Modify as needed
46+ Namespace : "default" ,
47+ }
48+ scheduler := scheduler .NewNaiveScheduler ()
49+ gpu := & tfv1.GPU {
50+ ObjectMeta : metav1.ObjectMeta {
51+ Name : "mock-gpu" ,
52+ },
4353 }
44- tensorfusionconnection := & tfv1.TensorFusionConnection {}
45-
4654 BeforeEach (func () {
55+ connection := & tfv1.TensorFusionConnection {}
4756 By ("creating the custom resource for the Kind TensorFusionConnection" )
48- err := k8sClient .Get (ctx , typeNamespacedName , tensorfusionconnection )
57+ err := k8sClient .Get (ctx , typeNamespacedName , connection )
4958 if err != nil && errors .IsNotFound (err ) {
5059 resource := & tfv1.TensorFusionConnection {
5160 ObjectMeta : metav1.ObjectMeta {
5261 Name : resourceName ,
5362 Namespace : "default" ,
5463 },
55- // TODO(user): Specify other spec details if needed.
64+ Spec : tfv1.TensorFusionConnectionSpec {
65+ PoolName : "mock" ,
66+ Resources : tfv1.Resources {
67+ Requests : tfv1.Resource {
68+ Tflops : resource .MustParse ("1" ),
69+ Vram : resource .MustParse ("1Gi" ),
70+ },
71+ Limits : tfv1.Resource {
72+ Tflops : resource .MustParse ("1" ),
73+ Vram : resource .MustParse ("1Gi" ),
74+ },
75+ },
76+ },
5677 }
5778 Expect (k8sClient .Create (ctx , resource )).To (Succeed ())
5879 }
80+
81+ scheduler .OnAdd (gpu )
82+ Expect (k8sClient .Create (ctx , gpu )).To (Succeed ())
83+ gpu .Status = tfv1.GPUStatus {
84+ UUID : "mock-gpu" ,
85+ NodeSelector : map [string ]string {
86+ "kubernetes.io/hostname" : "mock-node" ,
87+ },
88+ Capacity : tfv1.Resource {
89+ Tflops : resource .MustParse ("2" ),
90+ Vram : resource .MustParse ("2Gi" ),
91+ },
92+ Available : tfv1.Resource {
93+ Tflops : resource .MustParse ("2" ),
94+ Vram : resource .MustParse ("2Gi" ),
95+ },
96+ }
97+ Expect (k8sClient .Status ().Update (ctx , gpu )).To (Succeed ())
5998 })
6099
61100 AfterEach (func () {
62- // TODO(user): Cleanup logic after each test, like removing the resource instance.
63101 resource := & tfv1.TensorFusionConnection {}
64102 err := k8sClient .Get (ctx , typeNamespacedName , resource )
65103 Expect (err ).NotTo (HaveOccurred ())
@@ -74,13 +112,29 @@ var _ = Describe("TensorFusionConnection Controller", func() {
74112 Client : k8sClient ,
75113 Scheme : k8sClient .Scheme (),
76114 GpuPoolState : gpuPoolState ,
115+ Scheduler : scheduler ,
77116 }
78117 _ , err := controllerReconciler .Reconcile (ctx , reconcile.Request {
79118 NamespacedName : typeNamespacedName ,
80119 })
81120 Expect (err ).NotTo (HaveOccurred ())
82- // TODO(user): Add more specific assertions depending on your controller's reconciliation logic.
83- // Example: If you expect a certain status condition after reconciliation, verify it here.
121+ connection := & tfv1.TensorFusionConnection {}
122+ Expect (k8sClient .Get (ctx , typeNamespacedName , connection )).NotTo (HaveOccurred ())
123+ Expect (connection .Finalizers ).Should (ConsistOf (constants .Finalizer ))
124+ _ , err = controllerReconciler .Reconcile (ctx , reconcile.Request {
125+ NamespacedName : typeNamespacedName ,
126+ })
127+ Expect (err ).NotTo (HaveOccurred ())
128+ Expect (k8sClient .Get (ctx , typeNamespacedName , connection )).NotTo (HaveOccurred ())
129+ Expect (connection .Status .Phase ).To (Equal (tfv1 .TensorFusionConnectionStarting ))
130+
131+ workerPod := & corev1.Pod {}
132+ Expect (k8sClient .Get (ctx , typeNamespacedName , workerPod )).NotTo (HaveOccurred ())
133+ Expect (workerPod .Spec .NodeSelector ).To (Equal (gpu .Status .NodeSelector ))
134+
135+ Expect (k8sClient .Get (ctx , types.NamespacedName {Name : "mock-gpu" }, gpu )).NotTo (HaveOccurred ())
136+ Expect (gpu .Status .Available .Tflops ).To (Equal (resource .MustParse ("1" )))
137+ Expect (gpu .Status .Available .Vram ).To (Equal (resource .MustParse ("1Gi" )))
84138 })
85139 })
86140})
0 commit comments