|
| 1 | +// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package controllers |
| 5 | + |
| 6 | +import ( |
| 7 | + "github.com/ironcore-dev/libvirt-provider/api" |
| 8 | + "github.com/ironcore-dev/provider-utils/claimutils/pci" |
| 9 | + . "github.com/onsi/ginkgo/v2" |
| 10 | + . "github.com/onsi/gomega" |
| 11 | + "libvirt.org/go/libvirtxml" |
| 12 | +) |
| 13 | + |
| 14 | +var _ = Describe("MachineController with GPUs", func() { |
| 15 | + It("should convert claimed GPUs to libvirt host devices", func(ctx SpecContext) { |
| 16 | + By("creating a machine with claimed GPUs") |
| 17 | + machine := &api.Machine{ |
| 18 | + Spec: api.MachineSpec{ |
| 19 | + Gpu: []pci.Address{ |
| 20 | + {Domain: 0, Bus: 1, Slot: 0, Function: 0}, |
| 21 | + {Domain: 0, Bus: 2, Slot: 0, Function: 1}, |
| 22 | + }, |
| 23 | + }, |
| 24 | + } |
| 25 | + |
| 26 | + By("converting claimed GPUs to host devices") |
| 27 | + hostDevs := clamedGPUsToHostDevs(machine) |
| 28 | + |
| 29 | + By("ensuring the correct host devices are returned") |
| 30 | + Expect(hostDevs).To(HaveLen(2)) |
| 31 | + Expect(hostDevs).To(ContainElements( |
| 32 | + libvirtxml.DomainHostdev{ |
| 33 | + Alias: &libvirtxml.DomainAlias{ |
| 34 | + Name: "gpu0", |
| 35 | + }, |
| 36 | + Managed: "yes", |
| 37 | + SubsysPCI: &libvirtxml.DomainHostdevSubsysPCI{ |
| 38 | + Source: &libvirtxml.DomainHostdevSubsysPCISource{ |
| 39 | + Address: &libvirtxml.DomainAddressPCI{ |
| 40 | + Domain: &machine.Spec.Gpu[0].Domain, |
| 41 | + Bus: &machine.Spec.Gpu[0].Bus, |
| 42 | + Slot: &machine.Spec.Gpu[0].Slot, |
| 43 | + Function: &machine.Spec.Gpu[0].Function, |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + Address: &libvirtxml.DomainAddress{ |
| 48 | + PCI: &libvirtxml.DomainAddressPCI{}, |
| 49 | + }, |
| 50 | + }, |
| 51 | + libvirtxml.DomainHostdev{ |
| 52 | + Alias: &libvirtxml.DomainAlias{ |
| 53 | + Name: "gpu1", |
| 54 | + }, |
| 55 | + Managed: "yes", |
| 56 | + SubsysPCI: &libvirtxml.DomainHostdevSubsysPCI{ |
| 57 | + Source: &libvirtxml.DomainHostdevSubsysPCISource{ |
| 58 | + Address: &libvirtxml.DomainAddressPCI{ |
| 59 | + Domain: &machine.Spec.Gpu[1].Domain, |
| 60 | + Bus: &machine.Spec.Gpu[1].Bus, |
| 61 | + Slot: &machine.Spec.Gpu[1].Slot, |
| 62 | + Function: &machine.Spec.Gpu[1].Function, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + Address: &libvirtxml.DomainAddress{ |
| 67 | + PCI: &libvirtxml.DomainAddressPCI{}, |
| 68 | + }, |
| 69 | + }, |
| 70 | + )) |
| 71 | + }) |
| 72 | + |
| 73 | + It("should convert claimed GPUs to libvirt host devices", func(ctx SpecContext) { |
| 74 | + By("creating a machine with 0 claimed GPUs") |
| 75 | + machine := &api.Machine{ |
| 76 | + Spec: api.MachineSpec{ |
| 77 | + Gpu: []pci.Address{}, |
| 78 | + }, |
| 79 | + } |
| 80 | + |
| 81 | + By("converting claimed GPUs to host devices") |
| 82 | + hostDevs := clamedGPUsToHostDevs(machine) |
| 83 | + |
| 84 | + By("ensuring no host devices are returned") |
| 85 | + Expect(hostDevs).To(HaveLen(0)) |
| 86 | + }) |
| 87 | + |
| 88 | + It("should convert claimed GPUs to libvirt host devices", func(ctx SpecContext) { |
| 89 | + By("creating a machine with nil Gu field") |
| 90 | + machine := &api.Machine{ |
| 91 | + Spec: api.MachineSpec{}, |
| 92 | + } |
| 93 | + |
| 94 | + By("converting claimed GPUs to host devices") |
| 95 | + hostDevs := clamedGPUsToHostDevs(machine) |
| 96 | + |
| 97 | + By("ensuring no host devices are returned") |
| 98 | + Expect(hostDevs).To(HaveLen(0)) |
| 99 | + }) |
| 100 | +}) |
0 commit comments