You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

trigger_test.go 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. Copyright 2021 The KubeEdge Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package trigger
  14. import "testing"
  15. import "time"
  16. func TestTimerInRange(t *testing.T) {
  17. now := time.Now()
  18. hour, _ := time.ParseDuration("1h")
  19. var timerTest = []struct {
  20. start time.Time
  21. end time.Time
  22. expected bool
  23. }{
  24. {now.Add(-hour), now.Add(hour), true},
  25. {now.Add(-2 * hour), now.Add(-hour), false},
  26. {now.Add(hour), now.Add(2 * hour), false},
  27. {now.Add(hour), now.Add(-hour), false},
  28. {now.Add(2 * hour), now.Add(hour), true},
  29. }
  30. for _, tt := range timerTest {
  31. var timer = make(map[string]string)
  32. timer["start"] = tt.start.Format("15:04")
  33. timer["end"] = tt.end.Format("15:04")
  34. triggerExpr := map[string]interface{}{
  35. "timer": timer,
  36. }
  37. tg, _ := NewTrigger(triggerExpr)
  38. stats := make(map[string]interface{})
  39. if tg.Trigger(stats) != tt.expected {
  40. t.Errorf("failed to trigger timer %v, expected=%v", timer, tt.expected)
  41. }
  42. }
  43. }
  44. func TestCondition(t *testing.T) {
  45. metric := "numOfSamples"
  46. stats := map[string]interface{}{
  47. metric: 500,
  48. }
  49. var samplesTest = []struct {
  50. operator string
  51. threshold interface{}
  52. expected bool
  53. }{
  54. {"gt", 499, true},
  55. {">", 499, true},
  56. {"gt", 500, false},
  57. {">", 500, false},
  58. {"ge", 501, false},
  59. {">=", 501, false},
  60. {"ge", 500, true},
  61. {">=", 500, true},
  62. {"eq", 500, true},
  63. {"=", 500, true},
  64. {"=", 501, false},
  65. {"!=", 500, false},
  66. {"!=", 501, true},
  67. {"lt", 501, true},
  68. {"<", 501, true},
  69. {"lt", 500, false},
  70. {"<", 500, false},
  71. {"le", 501, true},
  72. {"<=", 501, true},
  73. {"le", 500, true},
  74. {"<=", 500, true},
  75. {"le", 499, false},
  76. {"<=", 499, false},
  77. }
  78. for _, st := range samplesTest {
  79. var condition = make(map[string]interface{})
  80. condition["operator"] = st.operator
  81. condition["metric"] = metric
  82. condition["threshold"] = st.threshold
  83. trigger := map[string]interface{}{
  84. "condition": condition,
  85. }
  86. tg, _ := NewTrigger(trigger)
  87. if st.expected != tg.Trigger(stats) {
  88. t.Errorf("failed to trigger condition:%+v, expected:%v", condition, st.expected)
  89. }
  90. }
  91. }
  92. func TestIndexCondition(t *testing.T) {
  93. metric := "pricision_delta"
  94. subMetric := metric + "[1]"
  95. stats := map[string]interface{}{
  96. metric: []float32{0.1, 0.2},
  97. }
  98. var samplesTest = []struct {
  99. operator string
  100. threshold float32
  101. expected bool
  102. }{
  103. {"gt", 0.3, false},
  104. {"gt", 0.2, false},
  105. {"gt", 0.1, true},
  106. {"ge", 0.2, true},
  107. }
  108. for _, st := range samplesTest {
  109. var condition = make(map[string]interface{})
  110. condition["operator"] = st.operator
  111. condition["metric"] = subMetric
  112. condition["threshold"] = st.threshold
  113. trigger := map[string]interface{}{
  114. "condition": condition,
  115. }
  116. tg, _ := NewTrigger(trigger)
  117. if st.expected != tg.Trigger(stats) {
  118. t.Errorf("failed to trigger condition:%+v, expected:%v", condition, st.expected)
  119. }
  120. }
  121. }
  122. func TestTimerAndCondition(t *testing.T) {
  123. now := time.Now()
  124. hour, _ := time.ParseDuration("1h")
  125. metric := "numOfSamples"
  126. stats := map[string]interface{}{
  127. metric: 500,
  128. }
  129. var samplesTest = []struct {
  130. start time.Time
  131. end time.Time
  132. operator string
  133. threshold int
  134. expected bool
  135. }{
  136. {now.Add(-hour), now.Add(hour), "ge", 500, true},
  137. {now.Add(-hour), now.Add(hour), "ge", 600, false},
  138. {now.Add(-2 * hour), now.Add(-hour), "ge", 500, false},
  139. }
  140. for _, st := range samplesTest {
  141. var timer = make(map[string]interface{})
  142. timer["start"] = st.start.Format("15:04")
  143. timer["end"] = st.end.Format("15:04")
  144. var condition = make(map[string]interface{})
  145. condition["operator"] = st.operator
  146. condition["metric"] = metric
  147. condition["threshold"] = st.threshold
  148. trigger := map[string]interface{}{
  149. "timer": timer,
  150. "condition": condition,
  151. }
  152. tg, _ := NewTrigger(trigger)
  153. if tg.Trigger(stats) != st.expected {
  154. t.Errorf("failed to trigger %v, expected=%v", trigger, st.expected)
  155. }
  156. }
  157. }