forked from lamoda/gonkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
250 lines (216 loc) · 6.17 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package main
import (
"database/sql"
"errors"
"flag"
"log"
"net/url"
"os"
"strconv"
"strings"
"github.com/aerospike/aerospike-client-go/v5"
"github.com/go-redis/redis/v9"
"github.com/joho/godotenv"
"github.com/lamoda/gonkey/checker/response_body"
"github.com/lamoda/gonkey/checker/response_db"
"github.com/lamoda/gonkey/fixtures"
redisLoader "github.com/lamoda/gonkey/fixtures/redis"
"github.com/lamoda/gonkey/output/allure_report"
"github.com/lamoda/gonkey/output/console_colored"
"github.com/lamoda/gonkey/runner"
aerospikeAdapter "github.com/lamoda/gonkey/storage/aerospike"
"github.com/lamoda/gonkey/testloader/yaml_file"
"github.com/lamoda/gonkey/variables"
)
type config struct {
Host string
TestsLocation string
DbDsn string
AerospikeHost string
RedisURL string
FixturesLocation string
EnvFile string
Allure bool
Verbose bool
Debug bool
DbType string
}
type storages struct {
db *sql.DB
aerospike *aerospikeAdapter.Client
}
func main() {
cfg := getConfig()
validateConfig(&cfg)
storages := initStorages(cfg)
testHandler := runner.NewConsoleHandler()
fixturesLoader := initLoaders(storages, cfg)
proxyURL, err := proxyURLFromEnv()
if err != nil {
log.Fatal(err)
}
testsRunner := initRunner(cfg, fixturesLoader, testHandler, proxyURL)
consoleOutput := console_colored.NewOutput(cfg.Verbose)
testsRunner.AddOutput(consoleOutput)
addCheckers(testsRunner, storages.db)
var allureOutput *allure_report.AllureReportOutput
if cfg.Allure {
allureOutput = allure_report.NewOutput("Gonkey", "./allure-results")
testsRunner.AddOutput(allureOutput)
}
err = testsRunner.Run()
if err != nil {
log.Fatal(err)
}
if allureOutput != nil {
allureOutput.Finalize()
}
summary := testHandler.Summary()
consoleOutput.ShowSummary(summary)
if !summary.Success {
os.Exit(1)
}
}
func initStorages(cfg config) storages {
db := initDB(cfg)
aerospikeClient := initAerospike(cfg)
return storages{
db: db,
aerospike: aerospikeClient,
}
}
func initLoaders(storages storages, cfg config) fixtures.Loader {
var fixturesLoader fixtures.Loader
if cfg.FixturesLocation != "" {
if storages.db != nil || storages.aerospike != nil {
fixturesLoader = fixtures.NewLoader(&fixtures.Config{
DB: storages.db,
Aerospike: storages.aerospike,
Location: cfg.FixturesLocation,
Debug: cfg.Debug,
DbType: fixtures.FetchDbType(cfg.DbType),
})
} else if cfg.DbType == fixtures.RedisParam {
redisOptions, err := redis.ParseURL(cfg.RedisURL)
if err != nil {
log.Panic("redis_url attribute is not a valid URL")
}
fixturesLoader = redisLoader.New(redisLoader.LoaderOptions{
FixtureDir: cfg.FixturesLocation,
Redis: redisOptions,
})
} else {
log.Fatal(errors.New("you should specify db_dsn to load fixtures"))
}
}
return fixturesLoader
}
func validateConfig(cfg *config) {
if cfg.Host == "" {
log.Fatal(errors.New("service hostname not provided"))
} else {
if !strings.HasPrefix(cfg.Host, "http://") && !strings.HasPrefix(cfg.Host, "https://") {
cfg.Host = "http://" + cfg.Host
}
cfg.Host = strings.TrimRight(cfg.Host, "/")
}
if cfg.TestsLocation == "" {
log.Fatal(errors.New("no tests location provided"))
}
if cfg.EnvFile != "" {
if err := godotenv.Load(cfg.EnvFile); err != nil {
log.Println(errors.New("can't load .env file"), err)
}
}
}
func addCheckers(r *runner.Runner, db *sql.DB) {
r.AddCheckers(response_body.NewChecker())
if db != nil {
r.AddCheckers(response_db.NewChecker(db))
}
}
func initRunner(
cfg config,
fixturesLoader fixtures.Loader,
handler *runner.ConsoleHandler,
proxyURL *url.URL,
) *runner.Runner {
return runner.New(
&runner.Config{
Host: cfg.Host,
FixturesLoader: fixturesLoader,
Variables: variables.New(),
HttpProxyURL: proxyURL,
},
yaml_file.NewLoader(cfg.TestsLocation),
handler.HandleTest,
)
}
func initAerospike(cfg config) *aerospikeAdapter.Client {
if cfg.AerospikeHost != "" {
address, port, namespace := parseAerospikeHost(cfg.AerospikeHost)
client, err := aerospike.NewClient(address, port)
if err != nil {
log.Fatal("Couldn't connect to aerospike: ", err)
}
return aerospikeAdapter.New(client, namespace)
}
return nil
}
func initDB(cfg config) *sql.DB {
if cfg.DbDsn != "" {
var err error
db, err := sql.Open("postgres", cfg.DbDsn)
if err != nil {
log.Fatal(err)
}
return db
}
return nil
}
func getConfig() config {
cfg := config{}
flag.StringVar(&cfg.Host, "host", "", "Target system hostname")
flag.StringVar(&cfg.TestsLocation, "tests", "", "Path to tests file or directory")
flag.StringVar(&cfg.DbDsn, "db_dsn", "", "DSN for the fixtures database (WARNING! Db tables will be truncated)")
flag.StringVar(&cfg.AerospikeHost, "aerospike_host", "", "Aerospike host for fixtures in form of 'host:port/namespace' (WARNING! Aerospike sets will be truncated)")
flag.StringVar(&cfg.RedisURL, "redis_url", "", "Redis server URL for fixture loading")
flag.StringVar(&cfg.FixturesLocation, "fixtures", "", "Path to fixtures directory")
flag.StringVar(&cfg.EnvFile, "env-file", "", "Path to env-file")
flag.BoolVar(&cfg.Allure, "allure", true, "Make Allure report")
flag.BoolVar(&cfg.Verbose, "v", false, "Verbose output")
flag.BoolVar(&cfg.Debug, "debug", false, "Debug output")
flag.StringVar(
&cfg.DbType,
"db-type",
fixtures.PostgresParam,
"Type of database (options: postgres, mysql, aerospike, redis)",
)
flag.Parse()
return cfg
}
func parseAerospikeHost(dsn string) (address string, port int, namespace string) {
parts := strings.Split(dsn, "/")
if len(parts) != 2 {
log.Fatalf("couldn't parse aerospike host %v, should be in form of host:port/namespace", dsn)
}
namespace = parts[1]
host := parts[0]
hostParts := strings.Split(host, ":")
address = hostParts[0]
port, err := strconv.Atoi(hostParts[1])
if err != nil {
log.Fatal("couldn't parse port: " + parts[1])
}
return
}
func proxyURLFromEnv() (*url.URL, error) {
if os.Getenv("HTTP_PROXY") != "" {
httpUrl, err := url.Parse(os.Getenv("HTTP_PROXY"))
if err != nil {
return nil, err
}
return httpUrl, nil
}
return nil, nil
}