@@ -23,8 +23,12 @@ import (
2323 . "github.com/onsi/ginkgo/v2"
2424 . "github.com/onsi/gomega"
2525 corev1 "k8s.io/api/core/v1"
26+ k8serrors "k8s.io/apimachinery/pkg/api/errors"
27+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+ "k8s.io/apimachinery/pkg/runtime/schema"
2629 virtv1 "kubevirt.io/api/core/v1"
2730 "sigs.k8s.io/controller-runtime/pkg/client"
31+ "sigs.k8s.io/controller-runtime/pkg/client/interceptor"
2832
2933 vmbuilder "github.com/deckhouse/virtualization-controller/pkg/builder/vm"
3034 "github.com/deckhouse/virtualization-controller/pkg/common/testutil"
@@ -88,4 +92,88 @@ var _ = Describe("TestNodePlacementHandler", func() {
8892 Entry ("Migration should be executed" , true ),
8993 Entry ("Migration not should be executed" , false ),
9094 )
95+
96+ It ("should return nil when vm is nil" , func () {
97+ h := NewNodePlacementHandler (nil , & OneShotMigrationMock {
98+ OnceMigrateFunc : func (ctx context.Context , vm * v1alpha2.VirtualMachine , annotationKey , annotationExpectedValue string ) (bool , error ) {
99+ return false , nil
100+ },
101+ })
102+
103+ _ , err := h .Handle (ctx , nil )
104+ Expect (err ).NotTo (HaveOccurred ())
105+ })
106+
107+ It ("should return nil when vm has deletion timestamp" , func () {
108+ vm := vmbuilder .NewEmpty (name , namespace )
109+ now := metav1 .Now ()
110+ vm .DeletionTimestamp = & now
111+
112+ h := NewNodePlacementHandler (nil , & OneShotMigrationMock {
113+ OnceMigrateFunc : func (ctx context.Context , vm * v1alpha2.VirtualMachine , annotationKey , annotationExpectedValue string ) (bool , error ) {
114+ return false , nil
115+ },
116+ })
117+
118+ _ , err := h .Handle (ctx , vm )
119+ Expect (err ).NotTo (HaveOccurred ())
120+ })
121+
122+ It ("should return error when kvvmi get fails" , func () {
123+ vm := vmbuilder .NewEmpty (name , namespace )
124+ getErr := errors .New ("get kvvmi failed" )
125+ interceptClient , err := testutil .NewFakeClientWithInterceptorWithObjects (interceptor.Funcs {
126+ Get : func (ctx context.Context , client client.WithWatch , key client.ObjectKey , obj client.Object , opts ... client.GetOption ) error {
127+ if _ , ok := obj .(* virtv1.VirtualMachineInstance ); ok {
128+ return getErr
129+ }
130+ return client .Get (ctx , key , obj , opts ... )
131+ },
132+ }, vm )
133+ Expect (err ).NotTo (HaveOccurred ())
134+
135+ h := NewNodePlacementHandler (interceptClient , & OneShotMigrationMock {
136+ OnceMigrateFunc : func (ctx context.Context , vm * v1alpha2.VirtualMachine , annotationKey , annotationExpectedValue string ) (bool , error ) {
137+ return false , nil
138+ },
139+ })
140+
141+ _ , err = h .Handle (ctx , vm )
142+ Expect (err ).To (MatchError (getErr ))
143+ })
144+
145+ It ("should ignore not found error when kvvmi get returns not found" , func () {
146+ vm := vmbuilder .NewEmpty (name , namespace )
147+ notFoundErr := k8serrors .NewNotFound (schema.GroupResource {Group : virtv1 .GroupVersion .Group , Resource : "virtualmachineinstances" }, name )
148+ interceptClient , err := testutil .NewFakeClientWithInterceptorWithObjects (interceptor.Funcs {
149+ Get : func (ctx context.Context , client client.WithWatch , key client.ObjectKey , obj client.Object , opts ... client.GetOption ) error {
150+ if _ , ok := obj .(* virtv1.VirtualMachineInstance ); ok {
151+ return notFoundErr
152+ }
153+ return client .Get (ctx , key , obj , opts ... )
154+ },
155+ }, vm )
156+ Expect (err ).NotTo (HaveOccurred ())
157+
158+ h := NewNodePlacementHandler (interceptClient , & OneShotMigrationMock {
159+ OnceMigrateFunc : func (ctx context.Context , vm * v1alpha2.VirtualMachine , annotationKey , annotationExpectedValue string ) (bool , error ) {
160+ return false , nil
161+ },
162+ })
163+
164+ _ , err = h .Handle (ctx , vm )
165+ Expect (err ).NotTo (HaveOccurred ())
166+ })
167+
168+ It ("should return node placement handler name" , func () {
169+ h := NewNodePlacementHandler (nil , nil )
170+ Expect (h .Name ()).To (Equal (nodePlacementHandler ))
171+ })
172+
173+ It ("should return error for nil kvvmi in genNodePlacementSum" , func () {
174+ sum , err := genNodePlacementSum (nil )
175+ Expect (err ).To (HaveOccurred ())
176+ Expect (err .Error ()).To (ContainSubstring ("kvvmi is nil" ))
177+ Expect (sum ).To (BeEmpty ())
178+ })
91179})
0 commit comments