-
Notifications
You must be signed in to change notification settings - Fork 19
/
suite.go
175 lines (140 loc) · 4.59 KB
/
suite.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
package suite
import (
"fmt"
"regexp"
"github.com/commander-cli/commander/v2/pkg/runtime"
)
// Suite represents the current tests, nodes and configs.
// It is used by the runtime to execute all tests and is an abstraction for the given source.
// In example it could be possible to add more formats like XML or a custom DSL implementation.
type Suite struct {
TestCases []runtime.TestCase
Config runtime.GlobalTestConfig
Nodes []runtime.Node
}
// NewSuite creates a suite structure from two byte slices,
// suiteContent is the file/suite that is under test
// overwriteConfigContent is an optional slice which overwrites the default configurations
// fileName is the file that is under test
func NewSuite(suiteContent, overwriteConfigContent []byte, fileName string) Suite {
overwriteConfig := ParseYAML(overwriteConfigContent, "default config")
s := ParseYAML(suiteContent, fileName)
s.mergeConfigs(overwriteConfig.Config, overwriteConfig.Nodes)
return s
}
// GetNodes returns all nodes defined in the suite
func (s Suite) GetNodes() []runtime.Node {
return s.Nodes
}
// GetNodeByName returns a node by the given name
func (s Suite) GetNodeByName(name string) (runtime.Node, error) {
for _, n := range s.Nodes {
if n.Name == name {
return n, nil
}
}
return runtime.Node{}, fmt.Errorf("could not find node with name %s", name)
}
// AddTest pushes a new test to the suite
// if the test was already added it will panic
func (s Suite) AddTest(t runtime.TestCase) {
if _, err := s.GetTestByTitle(t.Title); err == nil {
panic(fmt.Sprintf("Tests %s was already added to the suite", t.Title))
}
s.TestCases = append(s.TestCases, t)
}
// GetTests returns all tests of the test suite
func (s Suite) GetTests() []runtime.TestCase {
return s.TestCases
}
// GetTestByTitle returns a test by title, if the test was not found an error is returned
func (s Suite) GetTestByTitle(title string) (runtime.TestCase, error) {
for _, t := range s.GetTests() {
if t.Title == title {
return t, nil
}
}
return runtime.TestCase{}, fmt.Errorf("could not find test %s", title)
}
// FindTests returns a test by the given pattern, if the test was not found an error is returned
func (s Suite) FindTests(pattern string) ([]runtime.TestCase, error) {
var r []runtime.TestCase
for _, t := range s.GetTests() {
matched, err := regexp.Match(pattern, []byte(t.Title))
if err != nil {
panic(fmt.Sprintf("Regex error %s: %s", pattern, err.Error()))
}
if matched {
r = append(r, t)
}
}
if len(r) == 0 {
return []runtime.TestCase{}, fmt.Errorf("could not find test with pattern: %s", pattern)
}
return r, nil
}
// GetGlobalConfig returns the global configuration which applies to the complete suite
func (s Suite) GetGlobalConfig() runtime.GlobalTestConfig {
return s.Config
}
// MergeConfigs overwrites a global configuration over an entire suite.
// Config at the lowest level takes precedence
func (s Suite) mergeConfigs(config runtime.GlobalTestConfig, nodes []runtime.Node) {
s.Config.Env = mergeEnvironmentVariables(s.Config.Env, config.Env)
if s.Config.Dir == "" {
s.Config.Dir = config.Dir
}
if s.Config.Timeout == "" {
s.Config.Timeout = config.Timeout
}
if s.Config.Retries == 0 {
s.Config.Retries = config.Retries
}
if s.Config.Interval == "" {
s.Config.Interval = config.Interval
}
if !s.Config.InheritEnv {
s.Config.InheritEnv = config.InheritEnv
}
if len(s.Config.Nodes) == 0 {
s.Config.Nodes = config.Nodes
}
// append additional nodes
s.Nodes = append(s.Nodes, nodes...)
s.mergeTestConfigs()
}
// mergeConfigs will merge the suites runtime.GlobalTestConfig,
// with each runtime.TestCase in the suite
func (s Suite) mergeTestConfigs() {
for i := range s.TestCases {
s.TestCases[i].Command.Env = mergeEnvironmentVariables(s.Config.Env, s.TestCases[i].Command.Env)
if s.TestCases[i].Command.Dir == "" {
s.TestCases[i].Command.Dir = s.Config.Dir
}
if s.TestCases[i].Command.Timeout == "" {
s.TestCases[i].Command.Timeout = s.Config.Timeout
}
if s.TestCases[i].Command.Retries == 0 {
s.TestCases[i].Command.Retries = s.Config.Retries
}
if s.TestCases[i].Command.Interval == "" {
s.TestCases[i].Command.Interval = s.Config.Interval
}
if !s.TestCases[i].Command.InheritEnv {
s.TestCases[i].Command.InheritEnv = s.Config.InheritEnv
}
if len(s.TestCases[i].Nodes) == 0 {
s.TestCases[i].Nodes = s.Config.Nodes
}
}
}
func mergeEnvironmentVariables(global map[string]string, local map[string]string) map[string]string {
env := make(map[string]string)
for k, v := range global {
env[k] = v
}
for k, v := range local {
env[k] = v
}
return env
}