query_test.go 4.8 KB

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