forked from lamoda/gonkey
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
137 lines (117 loc) · 3.5 KB
/
Copy pathmain.go
File metadata and controls
137 lines (117 loc) · 3.5 KB
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
package main
import (
"database/sql"
"errors"
"flag"
"log"
"os"
"strings"
"github.com/joho/godotenv"
"github.com/lamoda/gonkey/checker/response_body"
"github.com/lamoda/gonkey/checker/response_db"
"github.com/lamoda/gonkey/checker/response_schema"
"github.com/lamoda/gonkey/fixtures"
"github.com/lamoda/gonkey/output/allure_report"
"github.com/lamoda/gonkey/output/console_colored"
"github.com/lamoda/gonkey/runner"
"github.com/lamoda/gonkey/testloader/yaml_file"
"github.com/lamoda/gonkey/variables"
)
func main() {
var config struct {
Host string
SpecPath string
TestsLocation string
DbDsn string
FixturesLocation string
EnvFile string
Allure bool
Verbose bool
Debug bool
DbType string
}
flag.StringVar(&config.Host, "host", "", "Target system hostname")
flag.StringVar(&config.SpecPath, "spec", "", "Path or URL to swagger specification")
flag.StringVar(&config.TestsLocation, "tests", "", "Path to tests file or directory")
flag.StringVar(&config.DbDsn, "db_dsn", "", "DSN for the fixtures database (WARNING! Db tables will be truncated)")
flag.StringVar(&config.FixturesLocation, "fixtures", "", "Path to fixtures directory")
flag.StringVar(&config.EnvFile, "env-file", "", "Path to env-file")
flag.BoolVar(&config.Allure, "allure", true, "Make Allure report")
flag.BoolVar(&config.Verbose, "v", false, "Verbose output")
flag.BoolVar(&config.Debug, "debug", false, "Debug output")
flag.StringVar(
&config.EnvFile,
"db-type",
fixtures.PostgresParam,
"Type of database (options: postgres, mysql)",
)
flag.Parse()
if config.Host == "" {
log.Fatal(errors.New("service hostname not provided"))
} else {
if !strings.HasPrefix(config.Host, "http://") && !strings.HasPrefix(config.Host, "https://") {
config.Host = "http://" + config.Host
}
config.Host = strings.TrimRight(config.Host, "/")
}
if config.TestsLocation == "" {
log.Fatal(errors.New("no tests location provided"))
}
var db *sql.DB
if config.DbDsn != "" {
var err error
db, err = sql.Open("postgres", config.DbDsn)
if err != nil {
log.Fatal(err)
}
}
var fixturesLoader fixtures.Loader
if db != nil && config.FixturesLocation != "" {
fixturesLoader = fixtures.NewLoader(&fixtures.Config{
DB: db,
Location: config.FixturesLocation,
Debug: config.Debug,
DbType: fixtures.FetchDbType(config.DbType),
})
} else if config.FixturesLocation != "" {
log.Fatal(errors.New("you should specify db_dsn to load fixtures"))
}
if config.EnvFile != "" {
if err := godotenv.Load(config.EnvFile); err != nil {
log.Println(errors.New("can't load .env file"), err)
}
}
r := runner.New(
&runner.Config{
Host: config.Host,
FixturesLoader: fixturesLoader,
Variables: variables.New(),
},
yaml_file.NewLoader(config.TestsLocation),
)
consoleOutput := console_colored.NewOutput(config.Verbose)
r.AddOutput(consoleOutput)
var allureOutput *allure_report.AllureReportOutput
if config.Allure {
allureOutput = allure_report.NewOutput("Gonkey", "./allure-results")
r.AddOutput(allureOutput)
}
r.AddCheckers(response_body.NewChecker())
if config.SpecPath != "" {
r.AddCheckers(response_schema.NewChecker(config.SpecPath))
}
if db != nil {
r.AddCheckers(response_db.NewChecker(db))
}
summary, err := r.Run()
if err != nil {
log.Fatal(err)
}
consoleOutput.ShowSummary(summary)
if allureOutput != nil {
allureOutput.Finalize()
}
if !summary.Success {
os.Exit(1)
}
}