gcpprovider_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package cloud
  2. import (
  3. "testing"
  4. )
  5. func TestParseGCPInstanceTypeLabel(t *testing.T) {
  6. cases := []struct {
  7. input string
  8. expected string
  9. }{
  10. {
  11. input: "n1-standard-2",
  12. expected: "n1standard",
  13. },
  14. {
  15. input: "e2-medium",
  16. expected: "e2medium",
  17. },
  18. {
  19. input: "k3s",
  20. expected: "unknown",
  21. },
  22. {
  23. input: "custom-n1-standard-2",
  24. expected: "custom",
  25. },
  26. {
  27. input: "n2d-highmem-8",
  28. expected: "n2dstandard",
  29. },
  30. }
  31. for _, test := range cases {
  32. result := parseGCPInstanceTypeLabel(test.input)
  33. if result != test.expected {
  34. t.Errorf("Input: %s, Expected: %s, Actual: %s", test.input, test.expected, result)
  35. }
  36. }
  37. }
  38. func TestParseGCPProjectID(t *testing.T) {
  39. cases := []struct {
  40. input string
  41. expected string
  42. }{
  43. {
  44. input: "gce://guestbook-12345/...",
  45. expected: "guestbook-12345",
  46. },
  47. {
  48. input: "gce:/guestbook-12345/...",
  49. expected: "",
  50. },
  51. {
  52. input: "asdfa",
  53. expected: "",
  54. },
  55. {
  56. input: "",
  57. expected: "",
  58. },
  59. }
  60. for _, test := range cases {
  61. result := parseGCPProjectID(test.input)
  62. if result != test.expected {
  63. t.Errorf("Input: %s, Expected: %s, Actual: %s", test.input, test.expected, result)
  64. }
  65. }
  66. }
  67. func TestGetUsageType(t *testing.T) {
  68. cases := []struct {
  69. input map[string]string
  70. expected string
  71. }{
  72. {
  73. input: map[string]string{
  74. GKEPreemptibleLabel: "true",
  75. },
  76. expected: "preemptible",
  77. },
  78. {
  79. input: map[string]string{
  80. GKESpotLabel: "true",
  81. },
  82. expected: "preemptible",
  83. },
  84. {
  85. input: map[string]string{
  86. KarpenterCapacityTypeLabel: KarpenterCapacitySpotTypeValue,
  87. },
  88. expected: "preemptible",
  89. },
  90. {
  91. input: map[string]string{
  92. "someotherlabel": "true",
  93. },
  94. expected: "ondemand",
  95. },
  96. {
  97. input: map[string]string{},
  98. expected: "ondemand",
  99. },
  100. }
  101. for _, test := range cases {
  102. result := getUsageType(test.input)
  103. if result != test.expected {
  104. t.Errorf("Input: %v, Expected: %s, Actual: %s", test.input, test.expected, result)
  105. }
  106. }
  107. }