helper_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package monitor
  15. import (
  16. "testing"
  17. . "github.com/smartystreets/goconvey/convey"
  18. "yunion.io/x/onecloud/pkg/apis/monitor"
  19. )
  20. func TestHelperWhere(t *testing.T) {
  21. Convey("Alert query where", t, func() {
  22. q := NewAlertCondition("", "").Query()
  23. w1 := q.Where()
  24. w1.Equal("hostname", "server1").NotEqual("hypervisor", "kvm").
  25. OR().GT("key", "val").LT("key1", "val2")
  26. So(w1.ToTags(), ShouldResemble, []monitor.MetricQueryTag{
  27. {
  28. Operator: "=",
  29. Key: "hostname",
  30. Value: "server1",
  31. },
  32. {
  33. Condition: "AND",
  34. Operator: "!=",
  35. Key: "hypervisor",
  36. Value: "kvm",
  37. },
  38. {
  39. Condition: "OR",
  40. Operator: ">",
  41. Key: "key",
  42. Value: "val",
  43. },
  44. {
  45. Condition: "OR",
  46. Operator: "<",
  47. Key: "key1",
  48. Value: "val2",
  49. },
  50. })
  51. })
  52. }
  53. func TestHelperSelects(t *testing.T) {
  54. Convey("Alert query selects", t, func() {
  55. sels := NewAlertCondition("", "").Query().Selects()
  56. sels.Select("name").COUNT().MATH("/", "100")
  57. sels.Select("io_util")
  58. So(sels.parts[0].MetricQuerySelect, ShouldResemble, monitor.MetricQuerySelect{
  59. {
  60. Type: "field",
  61. Params: []string{"name"},
  62. },
  63. {
  64. Type: "count",
  65. },
  66. {
  67. Type: "math",
  68. Params: []string{"/ 100"},
  69. },
  70. })
  71. So(sels.parts[1].MetricQuerySelect, ShouldResemble, monitor.MetricQuerySelect{
  72. {
  73. Type: "field",
  74. Params: []string{"io_util"},
  75. },
  76. })
  77. })
  78. }
  79. func TestAlertConfig(t *testing.T) {
  80. Convey("Alert config test", t, func() {
  81. enabled := true
  82. // disabled := false
  83. conf, err := NewAlertConfig("alert1", "5s", true)
  84. So(err, ShouldBeNil)
  85. q := conf.Condition("telegraf", "cpu").Avg().LT(50).Query()
  86. sels := q.Selects()
  87. sels.Select("usage_active").MEAN()
  88. sels.Select("usage_irq").COUNT()
  89. q.Where().Equal("host_ip", "10.168.222.231")
  90. So(conf.ToAlertCreateInput(), ShouldResemble, monitor.AlertCreateInput{
  91. Name: "alert1",
  92. Frequency: 5,
  93. Settings: monitor.AlertSetting{
  94. Conditions: []monitor.AlertCondition{
  95. {
  96. Type: "query",
  97. Query: monitor.AlertQuery{
  98. Model: monitor.MetricQuery{
  99. Database: "telegraf",
  100. Measurement: "cpu",
  101. Selects: []monitor.MetricQuerySelect{
  102. {
  103. {
  104. Type: "field",
  105. Params: []string{"usage_active"},
  106. },
  107. {
  108. Type: "mean",
  109. },
  110. },
  111. {
  112. {
  113. Type: "field",
  114. Params: []string{"usage_irq"},
  115. },
  116. {
  117. Type: "count",
  118. },
  119. },
  120. },
  121. Tags: []monitor.MetricQueryTag{
  122. {
  123. Key: "host_ip",
  124. Operator: "=",
  125. Value: "10.168.222.231",
  126. },
  127. },
  128. },
  129. },
  130. Reducer: monitor.Condition{
  131. Type: "avg",
  132. },
  133. Evaluator: monitor.Condition{
  134. Type: "lt",
  135. Params: []float64{50},
  136. },
  137. },
  138. },
  139. },
  140. Enabled: &enabled,
  141. Level: "",
  142. })
  143. })
  144. }