Skip to content

Commit 00b810a

Browse files
committed
[no-relnote] Minor cleanups
Signed-off-by: Evan Lezar <[email protected]>
1 parent 56fcb4a commit 00b810a

File tree

3 files changed

+87
-137
lines changed

3 files changed

+87
-137
lines changed

tests/e2e/device-plugin_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package e2e
1919

2020
import (
2121
"fmt"
22+
"path/filepath"
2223
"strings"
2324
"time"
2425

@@ -28,6 +29,7 @@ import (
2829
helm "github.com/mittwald/go-helm-client"
2930
helmValues "github.com/mittwald/go-helm-client/values"
3031
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32+
"k8s.io/apimachinery/pkg/util/rand"
3133

3234
"github.com/NVIDIA/k8s-device-plugin/tests/e2e/common/diagnostics"
3335
"github.com/NVIDIA/k8s-device-plugin/tests/e2e/internal"
@@ -39,7 +41,7 @@ const (
3941

4042
// Actual test suite
4143
var _ = Describe("GPU Device Plugin", Ordered, Label("gpu", "e2e", "device-plugin"), func() {
42-
// Init global suite vars vars
44+
// Init global suite vars
4345
var (
4446
helmReleaseName string
4547
chartSpec helm.ChartSpec
@@ -74,7 +76,7 @@ var _ = Describe("GPU Device Plugin", Ordered, Label("gpu", "e2e", "device-plugi
7476

7577
BeforeAll(func(ctx SpecContext) {
7678
// Create clients for apiextensions and our CRD api
77-
helmReleaseName = "nvdp-e2e-test-" + randomSuffix()
79+
helmReleaseName = "nvdp-e2e-test-" + rand.String(5)
7880

7981
chartSpec = helm.ChartSpec{
8082
ReleaseName: helmReleaseName,
@@ -145,9 +147,9 @@ var _ = Describe("GPU Device Plugin", Ordered, Label("gpu", "e2e", "device-plugi
145147
})
146148
It("it should run GPU jobs", Label("gpu-job"), func(ctx SpecContext) {
147149
By("Creating a GPU job")
148-
jobNames, err := CreateOrUpdateJobsFromFile(ctx, clientSet, "job-1.yaml", testNamespace.Name)
150+
jobNames, err := CreateOrUpdateJobsFromFile(ctx, clientSet, testNamespace.Name, filepath.Join(projectRoot, "testdata", "job-1.yaml"))
149151
Expect(err).NotTo(HaveOccurred())
150-
Expect(jobNames).NotTo(BeEmpty())
152+
Expect(jobNames).NotTo(HaveLen(1))
151153

152154
// Defer cleanup for the job
153155
DeferCleanup(func(ctx SpecContext) {

tests/e2e/e2e_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ var (
9393

9494
ctx context.Context
9595
packagePath string
96+
projectRoot string
9697
)
9798

9899
func TestMain(t *testing.T) {
@@ -103,6 +104,7 @@ func TestMain(t *testing.T) {
103104
// get the package path
104105
_, thisFile, _, _ := runtime.Caller(0)
105106
packagePath = filepath.Dir(thisFile)
107+
projectRoot = filepath.Join(packagePath, "..", "..")
106108

107109
ctx = context.Background()
108110
getTestEnv()
@@ -285,8 +287,6 @@ func CreateTestingNS(baseName string, c clientset.Interface, labels map[string]s
285287
type k8sLabels map[string]string
286288

287289
// eventuallyNonControlPlaneNodes is a helper for asserting node properties
288-
//
289-
//nolint:unused
290290
func eventuallyNonControlPlaneNodes(ctx context.Context, cli clientset.Interface) AsyncAssertion {
291291
return Eventually(func(g Gomega) ([]corev1.Node, error) {
292292
return getNonControlPlaneNodes(ctx, cli)
@@ -497,8 +497,8 @@ func getNode(nodes []corev1.Node, nodeName string) corev1.Node {
497497
}
498498

499499
// CreateOrUpdateJobsFromFile creates or updates jobs from a file
500-
func CreateOrUpdateJobsFromFile(ctx context.Context, cli clientset.Interface, filename, namespace string) ([]string, error) {
501-
jobs, err := newJobFromfile(filepath.Join(packagePath, "..", "..", "testdata", filename))
500+
func CreateOrUpdateJobsFromFile(ctx context.Context, cli clientset.Interface, namespace string, filename string) ([]string, error) {
501+
jobs, err := newJobFromfile(filename)
502502
if err != nil {
503503
return nil, fmt.Errorf("failed to create Job from file: %w", err)
504504
}

tests/e2e/gpu-feature-discovery_test.go

Lines changed: 77 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -35,37 +35,28 @@ import (
3535
"github.com/NVIDIA/k8s-device-plugin/tests/e2e/internal"
3636
)
3737

38+
var expectedLabelPatterns = k8sLabels{
39+
"nvidia.com/gfd.timestamp": "[0-9]{10}",
40+
"nvidia.com/cuda.driver.major": "[0-9]+",
41+
"nvidia.com/cuda.driver.minor": "[0-9]+",
42+
"nvidia.com/cuda.driver.rev": "[0-9]*",
43+
"nvidia.com/cuda.runtime.major": "[0-9]+",
44+
"nvidia.com/cuda.runtime.minor": "[0-9]+",
45+
"nvidia.com/gpu.machine": ".*",
46+
"nvidia.com/gpu.count": "[0-9]+",
47+
"nvidia.com/gpu.replicas": "[0-9]+",
48+
"nvidia.com/gpu.sharing-strategy": "[none|mps|time-slicing]",
49+
"nvidia.com/gpu.product": "[A-Za-z_-]+",
50+
"nvidia.com/gpu.memory": "[0-9]+",
51+
"nvidia.com/gpu.family": "[a-z]+",
52+
"nvidia.com/mig.capable": "[true|false]",
53+
"nvidia.com/gpu.compute.major": "[0-9]+",
54+
"nvidia.com/gpu.compute.minor": "[0-9]+",
55+
"nvidia.com/mps.capable": "[true|false]",
56+
}
57+
3858
// Actual test suite
3959
var _ = Describe("GPU Feature Discovery", Ordered, Label("gfd", "gpu", "e2e"), func() {
40-
expectedLabelPatterns := k8sLabels{
41-
"nvidia.com/gfd.timestamp": "[0-9]{10}",
42-
"nvidia.com/cuda.driver.major": "[0-9]+",
43-
"nvidia.com/cuda.driver.minor": "[0-9]+",
44-
"nvidia.com/cuda.driver.rev": "[0-9]*",
45-
"nvidia.com/cuda.runtime.major": "[0-9]+",
46-
"nvidia.com/cuda.runtime.minor": "[0-9]+",
47-
"nvidia.com/gpu.machine": ".*",
48-
"nvidia.com/gpu.count": "[0-9]+",
49-
"nvidia.com/gpu.replicas": "[0-9]+",
50-
"nvidia.com/gpu.sharing-strategy": "[none|mps|time-slicing]",
51-
"nvidia.com/gpu.product": "[A-Za-z_-]+",
52-
"nvidia.com/gpu.memory": "[0-9]+",
53-
"nvidia.com/gpu.family": "[a-z]+",
54-
"nvidia.com/mig.capable": "[true|false]",
55-
"nvidia.com/gpu.compute.major": "[0-9]+",
56-
"nvidia.com/gpu.compute.minor": "[0-9]+",
57-
"nvidia.com/mps.capable": "[true|false]",
58-
}
59-
60-
defaultCollectorObjects := []string{
61-
"pods",
62-
"nodes",
63-
"namespaces",
64-
"deployments",
65-
"daemonsets",
66-
"nodeFeature",
67-
}
68-
6960
// Init global suite vars
7061
var (
7162
helmReleaseName string
@@ -75,6 +66,18 @@ var _ = Describe("GPU Feature Discovery", Ordered, Label("gfd", "gpu", "e2e"), f
7566
diagnosticsCollector diagnostics.Collector
7667
)
7768

69+
collectLogsFrom = []string{
70+
"pods",
71+
"nodes",
72+
"namespaces",
73+
"deployments",
74+
"daemonsets",
75+
"nodeFeature",
76+
}
77+
if CollectLogsFrom != "" && CollectLogsFrom != "default" {
78+
collectLogsFrom = strings.Split(CollectLogsFrom, ",")
79+
}
80+
7881
values := helmValues.Options{
7982
Values: []string{
8083
fmt.Sprintf("image.repository=%s", ImageRepo),
@@ -92,12 +95,6 @@ var _ = Describe("GPU Feature Discovery", Ordered, Label("gfd", "gpu", "e2e"), f
9295
return err == nil
9396
}
9497

95-
// check Collector objects
96-
collectLogsFrom = defaultCollectorObjects
97-
if CollectLogsFrom != "" && CollectLogsFrom != "default" {
98-
collectLogsFrom = strings.Split(CollectLogsFrom, ",")
99-
}
100-
10198
BeforeAll(func(ctx SpecContext) {
10299
helmReleaseName = "gfd-e2e-test" + rand.String(5)
103100

@@ -152,115 +149,66 @@ var _ = Describe("GPU Feature Discovery", Ordered, Label("gfd", "gpu", "e2e"), f
152149
})
153150

154151
When("When deploying GFD", Ordered, Label("serial"), func() {
155-
Context("NV Driver is not installed", func() {
156-
BeforeEach(func() {
157-
// Skip this context when driver is enabled since "NV Driver is installed"
158-
// context provides more comprehensive testing
159-
if NVIDIA_DRIVER_ENABLED {
160-
Skip("Skipping driver-not-installed tests when NVIDIA_DRIVER_ENABLED is true")
161-
}
162-
})
163-
164-
It("it should create nvidia.com timestamp label", Label("timestamp"), func(ctx SpecContext) {
165-
nodeList, err := clientSet.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
166-
Expect(err).NotTo(HaveOccurred())
167-
Expect(len(nodeList.Items)).ToNot(BeZero())
168-
169-
// We pick one node targeted for our NodeFeature objects
170-
nodes, err := getNonControlPlaneNodes(ctx, clientSet)
171-
Expect(err).NotTo(HaveOccurred())
172-
173-
targetNodeName := nodes[0].Name
174-
Expect(targetNodeName).ToNot(BeEmpty())
152+
It("it should create nvidia.com labels", func(ctx SpecContext) {
153+
nodeList, err := clientSet.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
154+
Expect(err).NotTo(HaveOccurred())
155+
Expect(len(nodeList.Items)).ToNot(BeZero())
175156

176-
By("Checking the node labels")
177-
labelChecker := map[string]k8sLabels{
178-
targetNodeName: {
179-
"nvidia.com/gfd.timestamp": "[0-9]{10}",
180-
}}
181-
eventuallyNonControlPlaneNodes(ctx, clientSet).Should(MatchLabels(labelChecker, nodes))
182-
})
183-
Context("and the NodeFeature API is enabled", func() {
184-
It("gfd should create node feature object", Label("nodefeature"), func(ctx SpecContext) {
185-
By("Updating GFD Helm chart values")
186-
newValues := values
187-
newValues.Values = append(newValues.Values, "nfd.enableNodeFeatureApi=true")
188-
chartSpec.ValuesOptions = newValues
189-
chartSpec.Replace = true
190-
_, err := helmClient.UpgradeChart(ctx, &chartSpec, nil)
191-
Expect(err).NotTo(HaveOccurred())
157+
// We pick one node targeted for our NodeFeature objects
158+
nodes, err := getNonControlPlaneNodes(ctx, clientSet)
159+
Expect(err).NotTo(HaveOccurred())
192160

193-
By("Checking if NodeFeature CR object is created")
194-
nodes, err := getNonControlPlaneNodes(ctx, clientSet)
195-
Expect(err).NotTo(HaveOccurred())
161+
targetNodeName := nodes[0].Name
162+
Expect(targetNodeName).ToNot(BeEmpty())
196163

197-
targetNodeName := nodes[0].Name
198-
Expect(targetNodeName).ToNot(BeEmpty())
199-
Eventually(func(g Gomega) bool {
200-
return checkNodeFeatureObject(ctx, targetNodeName)
201-
}).WithContext(ctx).WithPolling(5 * time.Second).WithTimeout(2 * time.Minute).Should(BeTrue())
164+
By("Checking the node labels")
202165

203-
By("Checking that node labels are created from NodeFeature object")
204-
labelChecker := map[string]k8sLabels{
205-
targetNodeName: {
206-
"nvidia.com/gfd.timestamp": "[0-9]{10}",
207-
}}
208-
eventuallyNonControlPlaneNodes(ctx, clientSet).Should(MatchLabels(labelChecker, nodes))
209-
})
210-
})
211-
})
212-
213-
When("NV Driver is installed", func() {
214-
It("it should create nvidia.com labels", Label("driver", "labels"), func(ctx SpecContext) {
215-
if !NVIDIA_DRIVER_ENABLED {
216-
Skip("NVIDIA_DRIVER_ENABLED is not set")
166+
labelChecker := map[string]k8sLabels{
167+
targetNodeName: expectedLabelPatterns,
168+
}
169+
if !NVIDIA_DRIVER_ENABLED {
170+
// If the NVIDIA driver is not installed, we only check the
171+
// timestamp label to allow for local testing on non-GPU
172+
// systems.
173+
labelChecker[targetNodeName] = k8sLabels{
174+
"nvidia.com/gfd.timestamp": "[0-9]{10}",
217175
}
218-
219-
By("Checking the node labels")
220-
nodeList, err := clientSet.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
176+
}
177+
eventuallyNonControlPlaneNodes(ctx, clientSet).Should(MatchLabels(labelChecker, nodes))
178+
})
179+
Context("and the NodeFeature API is enabled", func() {
180+
It("gfd should create node feature object", Label("nodefeature"), func(ctx SpecContext) {
181+
By("Updating GFD Helm chart values")
182+
newValues := values
183+
newValues.Values = append(newValues.Values, "nfd.enableNodeFeatureApi=true")
184+
chartSpec.ValuesOptions = newValues
185+
chartSpec.Replace = true
186+
_, err := helmClient.UpgradeChart(ctx, &chartSpec, nil)
221187
Expect(err).NotTo(HaveOccurred())
222-
Expect(len(nodeList.Items)).ToNot(BeZero())
223188

224-
// We pick one node targeted for our NodeFeature objects
189+
By("Checking if NodeFeature CR object is created")
225190
nodes, err := getNonControlPlaneNodes(ctx, clientSet)
226191
Expect(err).NotTo(HaveOccurred())
227192

228193
targetNodeName := nodes[0].Name
229194
Expect(targetNodeName).ToNot(BeEmpty())
195+
Eventually(func(g Gomega) bool {
196+
return checkNodeFeatureObject(ctx, targetNodeName)
197+
}).WithContext(ctx).WithPolling(5 * time.Second).WithTimeout(2 * time.Minute).Should(BeTrue())
230198

231-
By("Checking the node labels")
199+
By("Checking that node labels are created from NodeFeature object")
232200
labelChecker := map[string]k8sLabels{
233-
targetNodeName: expectedLabelPatterns}
234-
eventuallyNonControlPlaneNodes(ctx, clientSet).Should(MatchLabels(labelChecker, nodes))
235-
})
236-
Context("and the NodeFeature API is enabled", func() {
237-
It("gfd should create node feature object", Label("driver", "nodefeature"), func(ctx SpecContext) {
238-
if !NVIDIA_DRIVER_ENABLED {
239-
Skip("NVIDIA_DRIVER_ENABLED is not set")
201+
targetNodeName: expectedLabelPatterns,
202+
}
203+
if !NVIDIA_DRIVER_ENABLED {
204+
// If the NVIDIA driver is not installed, we only check the
205+
// timestamp label to allow for local testing on non-GPU
206+
// systems.
207+
labelChecker[targetNodeName] = k8sLabels{
208+
"nvidia.com/gfd.timestamp": "[0-9]{10}",
240209
}
241-
By("Updating GFD Helm chart values")
242-
newValues := values
243-
newValues.Values = append(newValues.Values, "nfd.enableNodeFeatureApi=true")
244-
chartSpec.ValuesOptions = newValues
245-
chartSpec.Replace = true
246-
_, err := helmClient.UpgradeChart(ctx, &chartSpec, nil)
247-
Expect(err).NotTo(HaveOccurred())
248-
249-
By("Checking if NodeFeature CR object is created")
250-
nodes, err := getNonControlPlaneNodes(ctx, clientSet)
251-
Expect(err).NotTo(HaveOccurred())
252-
253-
targetNodeName := nodes[0].Name
254-
Expect(targetNodeName).ToNot(BeEmpty())
255-
Eventually(func(g Gomega) bool {
256-
return checkNodeFeatureObject(ctx, targetNodeName)
257-
}).WithContext(ctx).WithPolling(5 * time.Second).WithTimeout(2 * time.Minute).Should(BeTrue())
258-
259-
By("Checking that node labels are created from NodeFeature CR object")
260-
checkForLabels := map[string]k8sLabels{
261-
targetNodeName: expectedLabelPatterns}
262-
eventuallyNonControlPlaneNodes(ctx, clientSet).Should(MatchLabels(checkForLabels, nodes))
263-
})
210+
}
211+
eventuallyNonControlPlaneNodes(ctx, clientSet).Should(MatchLabels(labelChecker, nodes))
264212
})
265213
})
266214
})

0 commit comments

Comments
 (0)