forked from cfpb/rhobot
/
rhobot_test.go
166 lines (138 loc) · 4.24 KB
/
rhobot_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"os"
"testing"
"time"
log "github.com/Sirupsen/logrus"
"github.com/cfpb/rhobot/config"
"github.com/cfpb/rhobot/database"
"github.com/cfpb/rhobot/healthcheck"
"github.com/cfpb/rhobot/report"
)
var conf *config.Config
func init() {
conf = config.NewConfig()
conf.SetLogLevel("debug")
}
func TestPostgresHealthCheckReporting(t *testing.T) {
cxn := database.GetPGConnection(conf.DBURI())
healthChecks, _ := healthcheck.ReadHealthCheckYAMLFromFile("healthcheck/healthchecksAll.yml")
results, _ := healthChecks.PreformHealthChecks(cxn)
var elements []report.Element
for _, val := range results {
elements = append(elements, val)
}
metadata := map[string]interface{}{
"name": healthChecks.Name,
"schema": "public",
"table": "healthchecks",
"timestamp": time.Now().Format(time.ANSIC),
}
rs := report.Set{Elements: elements, Metadata: metadata}
prr := report.NewPongo2ReportRunnerFromString(healthcheck.TemplateHealthcheckPostgres)
pgr := report.PGHandler{Cxn: cxn}
reader, err := prr.ReportReader(rs)
err = pgr.HandleReport(reader)
if err != nil {
t.Fatalf("error writing report to PG database\n%s", err)
}
}
func TestPostgresHealthCheckEscape(t *testing.T) {
cxn := database.GetPGConnection(conf.DBURI())
healthChecks, _ := healthcheck.ReadHealthCheckYAMLFromFile("healthcheck/healthchecksEscape.yml")
results, _ := healthChecks.PreformHealthChecks(cxn)
var elements []report.Element
for _, val := range results {
elements = append(elements, val)
}
metadata := map[string]interface{}{
"name": healthChecks.Name,
"schema": "public",
"table": "healthchecks",
"timestamp": time.Now().Format(time.ANSIC),
}
rs := report.Set{Elements: elements, Metadata: metadata}
prr := report.NewPongo2ReportRunnerFromString(healthcheck.TemplateHealthcheckPostgres)
pgr := report.PGHandler{Cxn: cxn}
reader, err := prr.ReportReader(rs)
err = pgr.HandleReport(reader)
if err != nil {
t.Fatalf("error writing report to PG database\n%s", err)
}
}
func TestLogLevelingFiltering(t *testing.T) {
cxn := database.GetPGConnection(conf.DBURI())
healthChecks, _ := healthcheck.ReadHealthCheckYAMLFromFile("healthcheck/healthchecksAll.yml")
results, _ := healthChecks.PreformHealthChecks(cxn)
var elements []report.Element
for _, val := range results {
elements = append(elements, val)
}
metadata := map[string]interface{}{
"name": healthChecks.Name,
}
originalReportSet := report.Set{Elements: elements, Metadata: metadata}
TestLogLevelResults := map[string]int{
"Debug": 6,
"Info": 5,
"Warn": 4,
"Error": 2,
"Fatal": 1,
}
for _, level := range report.LogLevelArray {
log.Infof("Report Filter LogLevel %v", level)
logFilteredSet := report.FilterReportSet(originalReportSet, level)
prr := report.JSONReportRunner{}
reader, _ := prr.ReportReader(logFilteredSet)
phr := report.PrintHandler{}
_ = phr.HandleReport(reader)
if len(logFilteredSet.GetElementArray()) != TestLogLevelResults[level] {
t.Fatalf("wrong number of healthchecks in report")
}
}
}
//The following are rhobot CLI test
func TestCLI_Healthchecks(t *testing.T) {
//TODO create fail verification test
args := []string{"rhobot", "healthchecks", "healthcheck/healthchecksTest.yml"}
os.Args = args
main()
}
func TestCLI_PG_Healthchecks(t *testing.T) {
//clear hctest
cxn := database.GetPGConnection(conf.DBURI())
result, err := cxn.Exec("DROP TABLE IF EXISTS public.hctest")
if err != nil {
t.Fatalf("error dropping public.hctest\n%s", err)
}
args := []string{"rhobot", "healthchecks", "healthcheck/healthchecksTest.yml",
"--schema", "public", "--table", "hctest"}
os.Args = args
main()
//make sure hctest only has 3 rows
row, err := cxn.Query("SELECT count(*) FROM public.hctest;")
defer row.Close()
if err != nil {
t.Fatalf("error selecting public.hctest\n%s", err)
} else {
if row.Next() {
var count int
err = row.Scan(&count)
if count != 3 {
t.Fatal("public.hctest count should have been 3")
}
} else {
t.Fatalf("no results in selecting public.hctest\n%s", err)
}
}
log.Info(result)
}
func TestCLI(t *testing.T) {
//All the following should exit with 0
os.Args = []string{"rhobot"}
main()
os.Args = []string{"rhobot", "-V"}
main()
os.Args = []string{"healthchecks"}
main()
}