|
| 1 | +package dra |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + g "github.com/onsi/ginkgo/v2" |
| 8 | + o "github.com/onsi/gomega" |
| 9 | + |
| 10 | + drae2eutility "github.com/openshift/origin/test/extended/dra/utility" |
| 11 | + exutil "github.com/openshift/origin/test/extended/util" |
| 12 | + |
| 13 | + corev1 "k8s.io/api/core/v1" |
| 14 | + resourceapi "k8s.io/api/resource/v1beta1" |
| 15 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 16 | + "k8s.io/kubernetes/test/e2e/framework" |
| 17 | + e2epodutil "k8s.io/kubernetes/test/e2e/framework/pod" |
| 18 | + "k8s.io/utils/ptr" |
| 19 | +) |
| 20 | + |
| 21 | +type driver interface { |
| 22 | + DeviceClassName() string |
| 23 | +} |
| 24 | + |
| 25 | +type commonSpec struct { |
| 26 | + f *framework.Framework |
| 27 | + oc *exutil.CLI |
| 28 | + |
| 29 | + driver driver |
| 30 | + newContainer func(name string) corev1.Container |
| 31 | + // the node onto which the pod is expected to run |
| 32 | + node *corev1.Node |
| 33 | +} |
| 34 | + |
| 35 | +func (spec commonSpec) Test(t g.GinkgoTInterface) { |
| 36 | + // create a namespace |
| 37 | + ns, err := spec.f.CreateNamespace(context.TODO(), "dra", nil) |
| 38 | + framework.ExpectNoError(err) |
| 39 | + |
| 40 | + deviceclass := spec.driver.DeviceClassName() |
| 41 | + helper := drae2eutility.NewHelper(ns.Name, spec.driver.DeviceClassName()) |
| 42 | + |
| 43 | + claim := &resourceapi.ResourceClaimTemplate{ |
| 44 | + ObjectMeta: metav1.ObjectMeta{ |
| 45 | + Namespace: ns.Name, |
| 46 | + Name: "single-gpu", |
| 47 | + }, |
| 48 | + Spec: resourceapi.ResourceClaimTemplateSpec{ |
| 49 | + Spec: resourceapi.ResourceClaimSpec{ |
| 50 | + Devices: resourceapi.DeviceClaim{ |
| 51 | + Requests: []resourceapi.DeviceRequest{ |
| 52 | + { |
| 53 | + Name: "gpu", |
| 54 | + DeviceClassName: deviceclass, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + } |
| 61 | + |
| 62 | + // one pod, one container |
| 63 | + pod := helper.NewPod("pod") |
| 64 | + ctr := spec.newContainer("ctr") |
| 65 | + ctr.Resources.Claims = []corev1.ResourceClaim{{Name: "gpu"}} |
| 66 | + pod.Spec.Containers = append(pod.Spec.Containers, ctr) |
| 67 | + pod.Spec.ResourceClaims = []corev1.PodResourceClaim{ |
| 68 | + { |
| 69 | + Name: "gpu", |
| 70 | + ResourceClaimTemplateName: ptr.To(claim.Name), |
| 71 | + }, |
| 72 | + } |
| 73 | + pod.Spec.Tolerations = []corev1.Toleration{ |
| 74 | + { |
| 75 | + Key: "nvidia.com/gpu", |
| 76 | + Operator: corev1.TolerationOpExists, |
| 77 | + Effect: corev1.TaintEffectNoSchedule, |
| 78 | + }, |
| 79 | + } |
| 80 | + |
| 81 | + g.By("creating external claim and pod") |
| 82 | + resource := spec.f.ClientSet.ResourceV1beta1() |
| 83 | + claim, err = resource.ResourceClaimTemplates(ns.Name).Create(context.Background(), claim, metav1.CreateOptions{}) |
| 84 | + o.Expect(err).To(o.BeNil()) |
| 85 | + |
| 86 | + core := spec.f.ClientSet.CoreV1() |
| 87 | + pod, err = core.Pods(ns.Name).Create(context.Background(), pod, metav1.CreateOptions{}) |
| 88 | + o.Expect(err).To(o.BeNil()) |
| 89 | + |
| 90 | + g.By(fmt.Sprintf("waiting for pod %s/%s to be running", pod.Namespace, pod.Name)) |
| 91 | + err = e2epodutil.WaitForPodRunningInNamespace(context.Background(), spec.f.ClientSet, pod) |
| 92 | + o.Expect(err).To(o.BeNil()) |
| 93 | + |
| 94 | + pod, err = core.Pods(ns.Name).Get(context.Background(), pod.Name, metav1.GetOptions{}) |
| 95 | + o.Expect(err).To(o.BeNil()) |
| 96 | + o.Expect(pod.Spec.NodeName).To(o.Equal(spec.node.Name)) |
| 97 | + |
| 98 | + result, err := resource.ResourceClaims(ns.Name).List(context.Background(), metav1.ListOptions{}) |
| 99 | + o.Expect(err).To(o.BeNil()) |
| 100 | + o.Expect(len(result.Items)).To(o.Equal(1)) |
| 101 | + rc := result.Items[0] |
| 102 | + o.Expect(rc.Status.Allocation).NotTo(o.BeNil()) |
| 103 | + o.Expect(len(rc.Status.Allocation.Devices.Results)).To(o.Equal(1)) |
| 104 | + |
| 105 | + allocation := rc.Status.Allocation.Devices.Results[0] |
| 106 | + o.Expect(allocation.Request).To(o.Equal("gpu")) |
| 107 | + o.Expect(allocation.Driver).To(o.Equal(deviceclass)) |
| 108 | + o.Expect(allocation.Device).To(o.Equal("gpu-0")) |
| 109 | + |
| 110 | + g.By(fmt.Sprintf("retrieving logs for pod %s/%s", pod.Namespace, pod.Name)) |
| 111 | + args := []string{ |
| 112 | + "-n", pod.Namespace, pod.Name, "--all-containers", |
| 113 | + } |
| 114 | + logs, err := spec.oc.AsAdmin().Run("logs").Args(args...).Output() |
| 115 | + o.Expect(err).To(o.BeNil()) |
| 116 | + t.Logf("\n%s\n", logs) |
| 117 | +} |
0 commit comments