query_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package prom
  2. import (
  3. "reflect"
  4. "testing"
  5. "time"
  6. "github.com/opencost/opencost/core/pkg/source"
  7. "github.com/prometheus/client_golang/api"
  8. )
  9. func TestWarningsFrom(t *testing.T) {
  10. var results any = map[string]interface{}{
  11. "status": "success",
  12. "warnings": []string{
  13. "Warning #1",
  14. "Warning #2",
  15. },
  16. }
  17. warnings := warningsFrom(results)
  18. if len(warnings) != 2 {
  19. t.Errorf("Unexpected warnings length: %d, Expected 2.", len(warnings))
  20. }
  21. if warnings[0] != "Warning #1" {
  22. t.Errorf("Unexpected first warning: %s", warnings[0])
  23. }
  24. if warnings[1] != "Warning #2" {
  25. t.Errorf("Unexpected second warning: %s", warnings[1])
  26. }
  27. }
  28. func TestContext_isRequestStepAligned(t *testing.T) {
  29. type fields struct {
  30. Client api.Client
  31. name string
  32. errorCollector *source.QueryErrorCollector
  33. }
  34. type args struct {
  35. start time.Time
  36. end time.Time
  37. step time.Duration
  38. }
  39. tests := []struct {
  40. name string
  41. fields fields
  42. args args
  43. want bool
  44. }{
  45. {
  46. name: "Test with times that are not step aligned to the hour",
  47. fields: fields{},
  48. args: args{
  49. start: time.Date(2022, 11, 7, 4, 59, 30, 0, time.UTC),
  50. end: time.Date(2022, 11, 8, 4, 59, 30, 0, time.UTC),
  51. step: time.Hour,
  52. },
  53. want: false,
  54. },
  55. {
  56. name: "Test with times that are step aligned to the hour",
  57. fields: fields{},
  58. args: args{
  59. start: time.Date(2022, 11, 7, 4, 0, 0, 0, time.UTC),
  60. end: time.Date(2022, 11, 8, 4, 0, 0, 0, time.UTC),
  61. step: time.Hour,
  62. },
  63. want: true,
  64. },
  65. {
  66. name: "Test with times where start is aligned to the hour but end is not",
  67. fields: fields{},
  68. args: args{
  69. start: time.Date(2022, 11, 7, 4, 0, 0, 0, time.UTC),
  70. end: time.Date(2022, 11, 8, 4, 59, 0, 0, time.UTC),
  71. step: time.Hour,
  72. },
  73. want: false,
  74. },
  75. {
  76. name: "Test with times where end is aligned to the hour but start is not",
  77. fields: fields{},
  78. args: args{
  79. start: time.Date(2022, 11, 7, 4, 59, 0, 0, time.UTC),
  80. end: time.Date(2022, 11, 8, 4, 0, 0, 0, time.UTC),
  81. step: time.Hour,
  82. },
  83. want: false,
  84. },
  85. }
  86. for _, tt := range tests {
  87. t.Run(tt.name, func(t *testing.T) {
  88. ctx := &Context{
  89. Client: tt.fields.Client,
  90. name: tt.fields.name,
  91. errorCollector: tt.fields.errorCollector,
  92. }
  93. if got := ctx.isRequestStepAligned(tt.args.start, tt.args.end, tt.args.step); got != tt.want {
  94. t.Errorf("isRequestStepAligned() = %v, want %v", got, tt.want)
  95. }
  96. })
  97. }
  98. }
  99. func TestContext_alignWindow(t *testing.T) {
  100. type fields struct {
  101. Client api.Client
  102. name string
  103. errorCollector *source.QueryErrorCollector
  104. }
  105. type args struct {
  106. start time.Time
  107. end time.Time
  108. step time.Duration
  109. }
  110. tests := []struct {
  111. name string
  112. fields fields
  113. args args
  114. wantStart time.Time
  115. wantEnd time.Time
  116. }{
  117. {
  118. name: "Do not update the start and end when step-aligned",
  119. fields: fields{},
  120. args: args{
  121. start: time.Date(2022, 11, 7, 4, 0, 0, 0, time.UTC),
  122. end: time.Date(2022, 11, 8, 4, 0, 0, 0, time.UTC),
  123. step: time.Hour,
  124. },
  125. wantStart: time.Date(2022, 11, 7, 4, 0, 0, 0, time.UTC),
  126. wantEnd: time.Date(2022, 11, 8, 4, 0, 0, 0, time.UTC),
  127. },
  128. {
  129. name: "Update start to be step-aligned and leave end the same",
  130. fields: fields{},
  131. args: args{
  132. start: time.Date(2022, 11, 7, 4, 59, 0, 0, time.UTC),
  133. end: time.Date(2022, 11, 8, 4, 0, 0, 0, time.UTC),
  134. step: time.Hour,
  135. },
  136. wantStart: time.Date(2022, 11, 7, 4, 0, 0, 0, time.UTC),
  137. wantEnd: time.Date(2022, 11, 8, 4, 0, 0, 0, time.UTC),
  138. },
  139. {
  140. name: "Update end to be step-aligned and leave start the same",
  141. fields: fields{},
  142. args: args{
  143. start: time.Date(2022, 11, 7, 4, 0, 0, 0, time.UTC),
  144. end: time.Date(2022, 11, 8, 4, 59, 0, 0, time.UTC),
  145. step: time.Hour,
  146. },
  147. wantStart: time.Date(2022, 11, 7, 4, 0, 0, 0, time.UTC),
  148. wantEnd: time.Date(2022, 11, 8, 4, 0, 0, 0, time.UTC),
  149. },
  150. {
  151. name: "Update start and end to be step-aligned",
  152. fields: fields{},
  153. args: args{
  154. start: time.Date(2022, 11, 7, 4, 59, 0, 0, time.UTC),
  155. end: time.Date(2022, 11, 8, 4, 59, 0, 0, time.UTC),
  156. step: time.Hour,
  157. },
  158. wantStart: time.Date(2022, 11, 7, 4, 0, 0, 0, time.UTC),
  159. wantEnd: time.Date(2022, 11, 8, 4, 0, 0, 0, time.UTC),
  160. },
  161. }
  162. for _, tt := range tests {
  163. t.Run(tt.name, func(t *testing.T) {
  164. ctx := &Context{
  165. Client: tt.fields.Client,
  166. name: tt.fields.name,
  167. errorCollector: tt.fields.errorCollector,
  168. }
  169. got, got1 := ctx.alignWindow(tt.args.start, tt.args.end, tt.args.step)
  170. if !reflect.DeepEqual(got, tt.wantStart) {
  171. t.Errorf("alignWindow() got = %v, want %v", got, tt.wantStart)
  172. }
  173. if !reflect.DeepEqual(got1, tt.wantEnd) {
  174. t.Errorf("alignWindow() got1 = %v, want %v", got1, tt.wantEnd)
  175. }
  176. })
  177. }
  178. }