Skip to content

Commit

Permalink
adding UT for device plugin New function
Browse files Browse the repository at this point in the history
  • Loading branch information
enoodle committed Nov 26, 2024
1 parent 22dc131 commit f505cdd
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions internal/deviceplugin/device_plugin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package deviceplugin

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/spf13/viper"

"k8s.io/client-go/kubernetes/fake"

"github.com/run-ai/fake-gpu-operator/internal/common/constants"
"github.com/run-ai/fake-gpu-operator/internal/common/topology"
)

func TestDevicePlugin(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "DevicePlugin Suite")
}

var _ = Describe("NewDevicePlugins", func() {
Context("When the topology is nil", func() {
It("should panic", func() {
Expect(func() { NewDevicePlugins(nil, nil) }).To(Panic())
})
})

Context("When the fake node is enabled", Ordered, func() {
BeforeAll(func() {
viper.Set(constants.EnvFakeNode, true)
})

AfterAll(func() {
viper.Set(constants.EnvFakeNode, false)
})

It("should return a fake node device plugin", func() {
topology := &topology.NodeTopology{}
kubeClient := &fake.Clientset{}
devicePlugins := NewDevicePlugins(topology, kubeClient)
Expect(devicePlugins).To(HaveLen(1))
Expect(devicePlugins[0]).To(BeAssignableToTypeOf(&FakeNodeDevicePlugin{}))
})
})

Context("With normal node", func() {
It("should return a real node device plugin", func() {
topology := &topology.NodeTopology{}
kubeClient := &fake.Clientset{}
devicePlugins := NewDevicePlugins(topology, kubeClient)
Expect(devicePlugins).To(HaveLen(1))
Expect(devicePlugins[0]).To(BeAssignableToTypeOf(&RealNodeDevicePlugin{}))
})

It("should return a device plugin for each other device", func() {
topology := &topology.NodeTopology{
OtherDevices: []topology.GenericDevice{
{Name: "device1", Count: 1},
{Name: "device2", Count: 2},
},
}
kubeClient := &fake.Clientset{}
devicePlugins := NewDevicePlugins(topology, kubeClient)
Expect(devicePlugins).To(HaveLen(3))
Expect(devicePlugins[0]).To(BeAssignableToTypeOf(&RealNodeDevicePlugin{}))
})
})

})

0 comments on commit f505cdd

Please sign in to comment.