2
0

provider_test.go 2.1 KB

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