provider_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 TestRegions(t *testing.T) {
  72. regions := (&Oracle{}).Regions()
  73. assert.Len(t, regions, 39)
  74. }
  75. func testNode(gpus int) *v1.Node {
  76. capacity := map[v1.ResourceName]resource.Quantity{}
  77. if gpus > 0 {
  78. capacity["nvidia.com/gpu"] = resource.MustParse(fmt.Sprintf("%d", gpus))
  79. }
  80. return &v1.Node{
  81. Spec: v1.NodeSpec{
  82. ProviderID: "ocid.abc",
  83. },
  84. Status: v1.NodeStatus{
  85. Capacity: capacity,
  86. },
  87. }
  88. }