provider_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package oracle
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. v1 "k8s.io/api/core/v1"
  9. "k8s.io/apimachinery/pkg/api/resource"
  10. )
  11. func TestGetKey(t *testing.T) {
  12. var testCases = map[string]struct {
  13. isVirtual bool
  14. gpus int
  15. }{
  16. "virtual-node": {
  17. true,
  18. 0,
  19. },
  20. "gpu": {
  21. false,
  22. 3,
  23. },
  24. "node": {
  25. false,
  26. 0,
  27. },
  28. }
  29. for instanceType, testCase := range testCases {
  30. t.Run(instanceType, func(t *testing.T) {
  31. labels := map[string]string{
  32. v1.LabelInstanceTypeStable: instanceType,
  33. }
  34. if testCase.isVirtual {
  35. labels[virtualNodeLabel] = ""
  36. }
  37. key := (&Oracle{}).GetKey(labels, testNode(testCase.gpus))
  38. assert.NotEmpty(t, key.ID())
  39. features := strings.Split(key.Features(), ",")
  40. assert.Len(t, features, 3)
  41. assert.Equal(t, instanceType, features[0])
  42. assert.Equal(t, strconv.FormatBool(testCase.isVirtual), features[1])
  43. assert.Equal(t, testCase.gpus, key.GPUCount())
  44. if testCase.gpus > 0 {
  45. assert.Equal(t, "nvidia.com/gpu", key.GPUType())
  46. } else {
  47. assert.Equal(t, "", key.GPUType())
  48. }
  49. })
  50. }
  51. }
  52. func TestGetPVKey(t *testing.T) {
  53. storageClass := "xyz"
  54. providerID := "ocid.abc"
  55. pv := &v1.PersistentVolume{
  56. Spec: v1.PersistentVolumeSpec{
  57. StorageClassName: storageClass,
  58. PersistentVolumeSource: v1.PersistentVolumeSource{
  59. CSI: &v1.CSIPersistentVolumeSource{
  60. VolumeHandle: providerID,
  61. Driver: driverOCIBV,
  62. },
  63. },
  64. },
  65. }
  66. pvkey := (&Oracle{}).GetPVKey(pv, map[string]string{}, "")
  67. assert.Equal(t, blockVolumePartNumber, pvkey.Features())
  68. assert.Equal(t, storageClass, pvkey.GetStorageClass())
  69. assert.Equal(t, providerID, pvkey.ID())
  70. }
  71. func testNode(gpus int) *v1.Node {
  72. capacity := map[v1.ResourceName]resource.Quantity{}
  73. if gpus > 0 {
  74. capacity["nvidia.com/gpu"] = resource.MustParse(fmt.Sprintf("%d", gpus))
  75. }
  76. return &v1.Node{
  77. Spec: v1.NodeSpec{
  78. ProviderID: "ocid.abc",
  79. },
  80. Status: v1.NodeStatus{
  81. Capacity: capacity,
  82. },
  83. }
  84. }