gcpprovider_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. for _, test := range cases {
  28. result := parseGCPInstanceTypeLabel(test.input)
  29. if result != test.expected {
  30. t.Errorf("Input: %s, Expected: %s, Actual: %s", test.input, test.expected, result)
  31. }
  32. }
  33. }
  34. func TestParseGCPProjectID(t *testing.T) {
  35. cases := []struct {
  36. input string
  37. expected string
  38. }{
  39. {
  40. input: "gce://guestbook-12345/...",
  41. expected: "guestbook-12345",
  42. },
  43. {
  44. input: "gce:/guestbook-12345/...",
  45. expected: "",
  46. },
  47. {
  48. input: "asdfa",
  49. expected: "",
  50. },
  51. {
  52. input: "",
  53. expected: "",
  54. },
  55. }
  56. for _, test := range cases {
  57. result := parseGCPProjectID(test.input)
  58. if result != test.expected {
  59. t.Errorf("Input: %s, Expected: %s, Actual: %s", test.input, test.expected, result)
  60. }
  61. }
  62. }
  63. func TestGetUsageType(t *testing.T) {
  64. cases := []struct {
  65. input map[string]string
  66. expected string
  67. }{
  68. {
  69. input: map[string]string{
  70. GKEPreemptibleLabel: "true",
  71. },
  72. expected: "preemptible",
  73. },
  74. {
  75. input: map[string]string{
  76. GKESpotLabel: "true",
  77. },
  78. expected: "preemptible",
  79. },
  80. {
  81. input: map[string]string{
  82. KarpenterCapacityTypeLabel: KarpenterCapacitySpotTypeValue,
  83. },
  84. expected: "preemptible",
  85. },
  86. {
  87. input: map[string]string{
  88. "someotherlabel": "true",
  89. },
  90. expected: "ondemand",
  91. },
  92. {
  93. input: map[string]string{},
  94. expected: "ondemand",
  95. },
  96. }
  97. for _, test := range cases {
  98. result := getUsageType(test.input)
  99. if result != test.expected {
  100. t.Errorf("Input: %v, Expected: %s, Actual: %s", test.input, test.expected, result)
  101. }
  102. }
  103. }