Skip to content

Commit 0afb86b

Browse files
committed
Add unit tests for internal/validator Validate error paths
Cover the driver-list failure, an invalid nodeSelector on a listed driver, and the node-list failure, using fake-client interceptors. internal/validator reaches 100% statement coverage. Signed-off-by: Abrar Shivani <ashivani@nvidia.com>
1 parent 45c4ed6 commit 0afb86b

1 file changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
# Copyright (c) NVIDIA CORPORATION. All rights reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
**/
16+
17+
package validator
18+
19+
import (
20+
"context"
21+
"errors"
22+
"testing"
23+
24+
"github.com/stretchr/testify/require"
25+
corev1 "k8s.io/api/core/v1"
26+
"k8s.io/apimachinery/pkg/runtime"
27+
"sigs.k8s.io/controller-runtime/pkg/client"
28+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
29+
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
30+
31+
nvidiav1alpha1 "github.com/NVIDIA/gpu-operator/api/nvidia/v1alpha1"
32+
)
33+
34+
func newValidatorScheme(t *testing.T) *runtime.Scheme {
35+
t.Helper()
36+
s := runtime.NewScheme()
37+
require.NoError(t, nvidiav1alpha1.AddToScheme(s))
38+
require.NoError(t, corev1.AddToScheme(s))
39+
return s
40+
}
41+
42+
func TestValidateReturnsErrorWhenDriverListFails(t *testing.T) {
43+
s := newValidatorScheme(t)
44+
45+
requested := makeTestDriver("requested", nil, false)
46+
c := fake.NewClientBuilder().
47+
WithScheme(s).
48+
WithInterceptorFuncs(interceptor.Funcs{
49+
List: func(ctx context.Context, cl client.WithWatch, list client.ObjectList, opts ...client.ListOption) error {
50+
if _, ok := list.(*nvidiav1alpha1.NVIDIADriverList); ok {
51+
return errors.New("driver list boom")
52+
}
53+
return cl.List(ctx, list, opts...)
54+
},
55+
}).
56+
Build()
57+
nsv := NewNodeSelectorValidator(c)
58+
59+
err := nsv.Validate(context.Background(), requested)
60+
require.Error(t, err)
61+
require.Contains(t, err.Error(), "driver list boom")
62+
}
63+
64+
func TestValidateReturnsErrorWhenListedDriverHasInvalidSelector(t *testing.T) {
65+
s := newValidatorScheme(t)
66+
67+
requested := makeTestDriver("requested", nil, false)
68+
invalid := makeTestDriver("invalid-default", map[string]string{"nodepool": "b"}, true)
69+
70+
c := fake.NewClientBuilder().
71+
WithScheme(s).
72+
WithObjects(invalid).
73+
Build()
74+
nsv := NewNodeSelectorValidator(c)
75+
76+
err := nsv.Validate(context.Background(), requested)
77+
require.Error(t, err)
78+
require.Contains(t, err.Error(), "default NVIDIADriver")
79+
require.Contains(t, err.Error(), "cannot use nodeSelector")
80+
}
81+
82+
func TestValidateReturnsErrorWhenNodeListFails(t *testing.T) {
83+
s := newValidatorScheme(t)
84+
85+
requested := makeTestDriver("requested", map[string]string{"nodepool": "a"}, false)
86+
c := fake.NewClientBuilder().
87+
WithScheme(s).
88+
WithObjects(requested).
89+
WithInterceptorFuncs(interceptor.Funcs{
90+
List: func(ctx context.Context, cl client.WithWatch, list client.ObjectList, opts ...client.ListOption) error {
91+
if _, ok := list.(*corev1.NodeList); ok {
92+
return errors.New("node list boom")
93+
}
94+
return cl.List(ctx, list, opts...)
95+
},
96+
}).
97+
Build()
98+
nsv := NewNodeSelectorValidator(c)
99+
100+
err := nsv.Validate(context.Background(), requested)
101+
require.Error(t, err)
102+
require.Contains(t, err.Error(), "node list boom")
103+
}

0 commit comments

Comments
 (0)