Skip to content

Commit dcb96ed

Browse files
committed
tests/ai-conformance: add DRA CUDA test
1 parent 58cdf8d commit dcb96ed

3 files changed

Lines changed: 125 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright The Kubernetes Authors.
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 dra_support
18+
19+
import (
20+
"fmt"
21+
"strings"
22+
"testing"
23+
24+
"k8s.io/apimachinery/pkg/runtime/schema"
25+
"k8s.io/kops/tests/e2e/scenarios/ai-conformance/validators"
26+
)
27+
28+
func TestDRAWorks(t *testing.T) {
29+
h := validators.NewValidatorHarness(t)
30+
31+
h.Logf("# DRA functional tests")
32+
33+
h.Logf("## Listing device classes")
34+
deviceClasses := h.ListDeviceClasses()
35+
for _, deviceClass := range deviceClasses {
36+
h.Logf("* %s", deviceClass.Name)
37+
}
38+
}

tests/e2e/scenarios/ai-conformance/validators/harness.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,22 @@ package validators
1818

1919
import (
2020
"context"
21+
"os"
22+
"path/filepath"
2123
"testing"
24+
25+
"k8s.io/client-go/dynamic"
26+
"k8s.io/client-go/rest"
27+
"k8s.io/client-go/tools/clientcmd"
2228
)
2329

2430
type ValidatorHarness struct {
2531
t *testing.T
2632

2733
output OutputSink
34+
35+
dynamicClient dynamic.Interface
36+
restConfig *rest.Config
2837
}
2938

3039
func NewValidatorHarness(t *testing.T) *ValidatorHarness {
@@ -36,6 +45,22 @@ func NewValidatorHarness(t *testing.T) *ValidatorHarness {
3645
h.t.Errorf("failed to close output: %v", err)
3746
}
3847
})
48+
49+
// use the current context in kubeconfig
50+
kubeconfig := os.Getenv("KUBECONFIG")
51+
if kubeconfig == "" {
52+
home, err := os.UserHomeDir()
53+
if err != nil {
54+
h.Fatalf("failed to get user home directory: %v", err)
55+
}
56+
kubeconfig = filepath.Join(home, ".kube", "config")
57+
}
58+
restConfig, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
59+
if err != nil {
60+
h.Fatalf("failed to build get kubeconfig: %v", err)
61+
}
62+
h.restConfig = restConfig
63+
3964
return h
4065
}
4166

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright The Kubernetes Authors.
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 validators
18+
19+
import (
20+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
21+
"k8s.io/apimachinery/pkg/runtime/schema"
22+
"k8s.io/client-go/dynamic"
23+
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
)
26+
27+
func (h *ValidatorHarness) DynamicClient() dynamic.Interface {
28+
if h.dynamicClient == nil {
29+
dynamicClient, err := dynamic.NewForConfig(h.restConfig)
30+
if err != nil {
31+
h.Fatalf("failed to create dynamic client: %v", err)
32+
}
33+
h.dynamicClient = dynamicClient
34+
}
35+
return h.dynamicClient
36+
}
37+
38+
type DeviceClass struct {
39+
u *unstructured.Unstructured
40+
}
41+
42+
func (d *DeviceClass) Name() string {
43+
return d.u.GetName()
44+
}
45+
46+
var deviceClassGVR = schema.GroupVersionResource{
47+
Group: "resource.k8s.io",
48+
Version: "v1",
49+
Resource: "deviceclasses",
50+
}
51+
52+
func (h *ValidatorHarness) ListDeviceClasses() []*DeviceClass {
53+
objectList, err := h.DynamicClient().Resource(deviceClassGVR).List(h.Context(), metav1.ListOptions{})
54+
if err != nil {
55+
h.Fatalf("failed to list device classes: %v", err)
56+
}
57+
var out []*DeviceClass
58+
for i := range objectList.Items {
59+
out = append(out, &DeviceClass{u: &objectList.Items[i]})
60+
}
61+
return out
62+
}

0 commit comments

Comments
 (0)