-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (85 loc) · 2.28 KB
/
main.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
package main
import (
"flag"
"fmt"
"github.com/fatih/color"
"github.com/jinzhu/configor"
"github.com/rodrigodiez/smocha/types"
"github.com/rodrigodiez/smocha/validate"
"log"
"net/http"
"os"
"time"
)
var testbook types.Testbook
var green = color.New(color.FgGreen).SprintFunc()
var red = color.New(color.FgRed).SprintFunc()
var yellow = color.New(color.FgYellow).SprintFunc()
func main() {
var failedTests = 0
filename := flag.String("testbook", "testbook.yml", "a testbook file")
flag.Parse()
if _, err := os.Stat(*filename); err != nil {
defer os.Exit(1)
}
configor.New(&configor.Config{ENVPrefix: "SMOCHA"}).Load(&testbook, *filename)
ch := make(chan bool, len(testbook.Tests))
throttle := time.Tick(time.Second / time.Duration(testbook.Rate))
fmt.Printf("Starting %s tests on %s at %s requests per second\n", yellow(testbook.Schema), yellow(testbook.Host), yellow(testbook.Rate))
for i := range testbook.Tests {
<-throttle
go test(testbook.Tests[i], testbook.Host, testbook.Schema, ch)
}
for _ = range testbook.Tests {
if ok := <-ch; ok == false {
failedTests++
defer os.Exit(1)
}
}
fmt.Printf("%d tests (%s, %s)\n", len(testbook.Tests), green(fmt.Sprintf("%d passed", len(testbook.Tests)-failedTests)), red(fmt.Sprintf("%d failed", failedTests)))
}
func test(test types.Test, host string, schema string, ch chan bool) {
res, err := http.Get(fmt.Sprintf("%s://%s%s", schema, host, test.Url))
if err != nil {
printErr(test, err)
ch <- false
return
}
if test.Should.HaveStatus != 0 {
result, err := validate.Status(res, test)
if result == false {
printErr(test, err)
ch <- false
return
}
}
if test.Should.MatchJsonSchema != "" {
result, err := validate.MatchJsonSchema(res, test)
if result == false {
printErr(test, err)
ch <- false
return
}
}
if test.Should.Contain != "" {
result, err := validate.Contain(res, test)
if result == false {
printErr(test, err)
ch <- false
return
}
}
if len(test.Should.HaveHeaders) > 0 {
result, err := validate.HaveHeaders(res, test)
if result == false {
printErr(test, err)
ch <- false
return
}
}
fmt.Printf("%s %s\n", green("✔"), green(test.Url))
ch <- true
}
func printErr(test types.Test, err error) {
log.Printf("%s%s %s", red("✗"), red(test.Url), err)
}