bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/bosun/conf/rule/rule_test.go (about) 1 package rule 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "bosun.org/cmd/bosun/conf" 10 ) 11 12 func TestPrint(t *testing.T) { 13 fname := "test.conf" 14 b, err := ioutil.ReadFile(fname) 15 if err != nil { 16 t.Fatal(err) 17 } 18 if err := os.Setenv("env", "1"); err != nil { 19 t.Fatal(err) 20 } 21 c, err := NewConf(fname, conf.EnabledBackends{OpenTSDB: true}, nil, string(b)) 22 if err != nil { 23 t.Fatal(err) 24 } 25 if w := c.Alerts["os.high_cpu"].Warn.Text; w != `avg(q("avg:rate:os.cpu{host=ny-nexpose01}", "2m", "")) > 80` { 26 t.Error("bad warn:", w) 27 } 28 if w := c.Alerts["m"].Crit.Text; w != `avg(q("avg:a", "", "")) > 1` { 29 t.Errorf("bad crit: %v", w) 30 } 31 if w := c.Alerts["braceTest"].Crit.Text; w != `avg(q("avg:o{t=m}", "", "")) > 1` { 32 t.Errorf("bad crit: %v", w) 33 } 34 if w := c.Alerts["macroBraceTest"].Crit.Text; w != `avg(q("avg:o{t=m}", "", "")) > 1` { 35 t.Errorf("bad crit: %v", w) 36 } 37 if w := c.Lookups["l"]; len(w.Entries) != 2 { 38 t.Errorf("bad lookup: %v", w) 39 } 40 checkMacroVarAlert(t, c.Alerts["macroVarAlert"]) 41 } 42 43 func checkMacroVarAlert(t *testing.T, a *conf.Alert) { 44 if a.Crit.String() != "3" { 45 t.Errorf("expected 'crit = 3'") 46 } 47 nots := map[string]bool{ 48 "default": true, 49 "nc1": true, 50 "nc2": true, 51 "nc3": true, 52 "nc4": true, 53 } 54 for _, n := range a.CritNotification.Notifications { 55 t.Log("found", n.Name) 56 delete(nots, n.Name) 57 } 58 if len(nots) > 0 { 59 t.Error("missing notifications", nots) 60 } 61 if a.Vars["a"] != "3" || a.Vars["$a"] != "3" { 62 t.Errorf("missing vars: %v", a.Vars) 63 } 64 } 65 66 func TestInvalid(t *testing.T) { 67 names := map[string]string{ 68 "lookup-key-pairs": "conf: lookup-key-pairs:3:1: at <entry a=3 { }>: lookup tags mismatch, expected {a=,b=}", 69 "number-func-args": `conf: number-func-args:2:1: at <warn = q("avg:o", ""...>: expr: parse: not enough arguments for q`, 70 "lookup-key-pairs-dup": `conf: lookup-key-pairs-dup:3:1: at <entry b=2,a=1 { }>: duplicate entry`, 71 "crit-warn-unmatching-tags": `conf: crit-warn-unmatching-tags:1:0: at <alert broken {\n cri...>: crit tags (a,c) and warn tags (c) must be equal`, 72 "depends-no-overlap": `conf: depends-no-overlap:1:0: at <alert broken {\n dep...>: Depends and crit/warn must share at least one tag.`, 73 "log-no-notification": `conf: log-no-notification:1:0: at <alert a {\n crit = 1...>: log specified but no notification`, 74 "crit-notification-no-template": `conf: crit-notification-no-template:5:0: at <alert a {\n crit = 1...>: notifications specified but no template`, 75 } 76 for fname, reason := range names { 77 path := filepath.Join("invalid", fname) 78 b, err := ioutil.ReadFile(path) 79 if err != nil { 80 t.Fatal(err) 81 } 82 _, err = NewConf(fname, conf.EnabledBackends{OpenTSDB: true}, nil, string(b)) 83 if err == nil { 84 t.Error("expected error in", path) 85 continue 86 } 87 if err.Error() != reason { 88 t.Errorf("got error `%s` in %s, expected `%s`", err, path, reason) 89 } 90 } 91 }