costintegration_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package stackit
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func TestParsePeriodDateOnly(t *testing.T) {
  7. defaultStart := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
  8. defaultEnd := time.Date(2026, 1, 31, 0, 0, 0, 0, time.UTC)
  9. start, end := parsePeriod("2026-01-05", "2026-01-10", defaultStart, defaultEnd)
  10. expectedStart := time.Date(2026, 1, 5, 0, 0, 0, 0, time.UTC)
  11. expectedEnd := time.Date(2026, 1, 11, 0, 0, 0, 0, time.UTC) // inclusive end + 1 day
  12. if !start.Equal(expectedStart) {
  13. t.Errorf("start = %v, want %v", start, expectedStart)
  14. }
  15. if !end.Equal(expectedEnd) {
  16. t.Errorf("end = %v, want %v", end, expectedEnd)
  17. }
  18. }
  19. func TestParsePeriodRFC3339(t *testing.T) {
  20. defaultStart := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
  21. defaultEnd := time.Date(2026, 1, 31, 0, 0, 0, 0, time.UTC)
  22. start, end := parsePeriod("2026-01-05T10:00:00Z", "2026-01-10T18:00:00Z", defaultStart, defaultEnd)
  23. expectedStart := time.Date(2026, 1, 5, 10, 0, 0, 0, time.UTC)
  24. expectedEnd := time.Date(2026, 1, 10, 18, 0, 0, 0, time.UTC) // RFC3339 used as-is
  25. if !start.Equal(expectedStart) {
  26. t.Errorf("start = %v, want %v", start, expectedStart)
  27. }
  28. if !end.Equal(expectedEnd) {
  29. t.Errorf("end = %v, want %v", end, expectedEnd)
  30. }
  31. }
  32. func TestParsePeriodEmpty(t *testing.T) {
  33. defaultStart := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
  34. defaultEnd := time.Date(2026, 1, 31, 0, 0, 0, 0, time.UTC)
  35. start, end := parsePeriod("", "", defaultStart, defaultEnd)
  36. if !start.Equal(defaultStart) {
  37. t.Errorf("start = %v, want default %v", start, defaultStart)
  38. }
  39. if !end.Equal(defaultEnd) {
  40. t.Errorf("end = %v, want default %v", end, defaultEnd)
  41. }
  42. }
  43. func TestParsePeriodInvalidFallsBack(t *testing.T) {
  44. defaultStart := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
  45. defaultEnd := time.Date(2026, 1, 31, 0, 0, 0, 0, time.UTC)
  46. start, end := parsePeriod("not-a-date", "also-not", defaultStart, defaultEnd)
  47. if !start.Equal(defaultStart) {
  48. t.Errorf("start = %v, want default %v", start, defaultStart)
  49. }
  50. if !end.Equal(defaultEnd) {
  51. t.Errorf("end = %v, want default %v", end, defaultEnd)
  52. }
  53. }
  54. func TestExtractRegionFromServiceName(t *testing.T) {
  55. tests := []struct {
  56. service string
  57. want string
  58. }{
  59. {"Tiny Server-t1.2-EU01", "eu01"},
  60. {"Object Storage Premium-EU02", "eu02"},
  61. {"GPU Server-n2.14d.g1-EU01", "eu01"},
  62. {"DNS-100-EU01", "eu01"},
  63. {"Some Future Service-US01", "us01"},
  64. {"NoRegionSuffix", "eu01"},
  65. }
  66. for _, tt := range tests {
  67. got := extractRegionFromServiceName(tt.service)
  68. if got != tt.want {
  69. t.Errorf("extractRegionFromServiceName(%q) = %q, want %q", tt.service, got, tt.want)
  70. }
  71. }
  72. }
  73. func TestSelectSTACKITCategory(t *testing.T) {
  74. tests := []struct {
  75. service string
  76. want string
  77. }{
  78. {"Compute Engine", "Compute"},
  79. {"SKE Cluster", "Compute"},
  80. {"Block Storage", "Storage"},
  81. {"Object Store", "Storage"},
  82. {"Load Balancer", "Network"},
  83. {"DNS Service", "Network"},
  84. {"Some Other Service", "Other"},
  85. }
  86. for _, tt := range tests {
  87. got := selectSTACKITCategory(tt.service)
  88. if got != tt.want {
  89. t.Errorf("selectSTACKITCategory(%q) = %q, want %q", tt.service, got, tt.want)
  90. }
  91. }
  92. }