-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.clj
97 lines (89 loc) · 3.73 KB
/
core.clj
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
(ns rester.core
(:gen-class)
(:require [clj-http.cookies :as cookies]
[clj-http.core :as http]
[clojure.string :as str]
[clojure.tools.cli :as cli]
[clojure.tools.logging :as log]
[rester.impl
:refer
[exec-in-order load-config junit-report print-test-results summarise-results]]
[rester.utils :as utils
:refer
[deep-merge process-tests]]
[rester.loader :as loader])
(:import java.io.File
java.lang.Integer
java.util.Date))
(defn exec-tests [tests opts]
(let [test-groups (process-tests tests opts)
executed (exec-in-order (:runnable test-groups) opts)]
(apply concat executed (-> test-groups (dissoc :runnable) vals))))
(defn load-tests [file opts]
(loader/load-tests-from file opts))
(defn run-tests [file opts]
(let [[tests-file work-sheet] (str/split file #":" 2)]
(binding [http/*cookie-store* (when (:use-cookies opts) (cookies/cookie-store))]
(-> tests-file
(load-tests work-sheet)
(exec-tests opts)
(summarise-results)))))
(defn load-profiles [file]
(load-config file))
(def cli-options
[["-c" "--config CONFIG" "configuration file"
:validate [#(.exists (File. %)) "config file not found"]
:assoc-fn #(assoc %1 %2 (load-config %3))]
["-p" "--profiles PROFILES" "comma separated list of profiles"
:default ["main"]
:parse-fn #(some-> % not-empty (str/split #","))]
["-t" "--concurrency CONCURRENCY" "concurrency level (max number of concurrent test execution)"
:parse-fn #(Integer/parseInt %)
:validate [#(< 0 % 1000) "Must be a number between 1 and 1000"] ]
["-b" "--binding BINDING" "placeholder value as \"SOME_VAR=SOME VALUE\"" :id :bindings :default {}
:assoc-fn #(update %1 %2 conj (str/split %3 #"=" 2))]
["-H" "--header HEADER" "common headers as \"SOME_HEADER=SOME VALUE\"" :id :headers :default {}
:assoc-fn #(update %1 %2 conj (str/split %3 #"=" 2))]
["-s" "--skip SKIP" "skip executing tests with this skip flag"]
["-r" "--report REPORT" "report file (defaults to target/<test-file-name>-results.xml)"]
["-k" "--use-cookies" "Save and send cookies automatically"]
["-v" "--verbose" "output HTTP logs"]
["-h" "--help"]])
(defn usage [opts]
(->> ["Usage:"
"rester [options] <rester-test-cases>.[csv|xls:sheetName|yaml]"
""
"Options:"
(:summary opts)]
(str/join \newline )))
(defn -main
"Executes HTTP(S) requests specified in the argument provided spreadsheet."
[& args]
(let [opts (cli/parse-opts args cli-options)]
(when (-> opts :options :help)
(println (usage opts))
(System/exit 0))
(when (:errors opts)
(println (:errors opts))
(println (usage opts))
(System/exit 1))
(when (empty? (:arguments opts))
(println "Missing test case file")
(println (usage opts))
(System/exit 1))
(when (-> opts :options :verbose)
(println "Parsed Options:")
(println (pr-str (:options opts))))
(try
(doseq [p (-> opts :options :profiles)
f (:arguments opts)
:let [options (deep-merge (get-in opts [:options :config (keyword p)])
(-> opts :options (dissoc :config)))
start (System/currentTimeMillis)
_ (log/infof "Executing %s with profile %s : %s" f p (Date.))
results (run-tests f options)]]
(log/infof "Completed in %f secs" (/ (- (System/currentTimeMillis) start) 1000.0))
((juxt #(junit-report (:options opts) f %) print-test-results) results))
(catch Exception e
(log/fatalf e "Failed executing tests!")
(System/exit 1)))))