You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| main | The composition root of the commander, initializes the `urfave/cli` framework. | /cmd/commander |
33
+
| app | Contains all commands for commander, i.e. add or test. Will only be used by the `cmd` package located under `/cmd`| /pkg/app |
34
+
| matcher | Implements the matching logic for the different assertions types in the suite like `equals, `contains`, `json` or `xml`. | /pkg/matcher |
35
+
| output | Output is a package which allows to add different output types. For example it could be possible to add a new output format in `json` or for a health check. | /pkg/output |
36
+
| runtime | Runtime controls the execution of the test suite. It will be initialized by the `main` package and will be a given a `Suite` which contains all tests to be executed. The runtime also contains all `executors` which are different types of ways to execute a test, i.e. on a local machine or a node viá ssh. | /pkg/runtime |
37
+
| suite | Suite is responsible for parsing the defined formats of different test suites. At the moment only `yaml` is supported but it would be possible to add support for a custom DSL or formats like `toml` and `json`. The `suite.Suite` struct will be used by the `runtime.Runtime` for executing the tests. | /pkg/suite |
38
+
39
+
40
+
### Build targets
41
+
42
+
```
43
+
# Initialise dev environment
44
+
$ make init
45
+
46
+
# Build the project binary
47
+
$ make build
48
+
49
+
# Unit tests
50
+
$ make test
51
+
52
+
# Coverage
53
+
$ make test-coverage
54
+
55
+
# Coverage with more complex tests like ssh execution
56
+
$ make test-coverage-all
57
+
58
+
# Integration tests for linux and macos
59
+
$ make integration-unix
60
+
61
+
# Integration on linux
62
+
$ make integration-linux
63
+
64
+
# Integration windows
65
+
$ make integration-windows
66
+
67
+
# Add depdencies to vendor
68
+
$ make deps
69
+
```
70
+
71
+
### Unit tests
72
+
73
+
`COMMANDER_TEST_ALL` will enable all tests which are depending on external systems like docker or databases.
74
+
Enables ssh tests in unit test suite and sets the credentials for the target host.
75
+
`COMMANDER_SSH_TEST` must be set to `1` to enable ssh tests.
76
+
77
+
**Note:** I am aware that unit tests should not test external systems nor libraries, but in favour of simplicity and laziness
### Add a new field to the `YAML` suite - with a leaning-by-doing task
92
+
93
+
It is a little bit annoying to add fields because it will be converted multiple times to keep the
94
+
suite format abstracted from the runtime package.
95
+
The idea behind this it to add support for other formats like `json`, `toml` or maybe a custom DSL.
96
+
97
+
**Definition of done**:
98
+
99
+
- Add a property `message` which always display a message while executing a test
100
+
```yaml
101
+
tests:
102
+
echo hello:
103
+
exit-code: 0
104
+
config:
105
+
message: this is a very special test
106
+
```
107
+
- Support global configurations
108
+
- Create a simple test case
109
+
110
+
**1. Extend the conversion struct `suite.YAMLTestConfigConf` in [yaml_suite.go](../pkg/suite/yaml_suite.go) with the `Message` property.**
111
+
112
+
The structs types with a `Conf` suffix represent the configuration type, the `YAML` prefix the format of the suite.
113
+
The naming makes it a little bit clearer in the code which type is used, i.e. a `runtime.CommandUnderTest` or a `suite.YAMLTestConfigConf`.
114
+
115
+
```go
116
+
Message string `yaml:"message,omitempty"`
117
+
```
118
+
119
+
**2. Add the `Message` property as a `string` to the `runtime.CommandUnderTest` struct**
120
+
121
+
The `runtime.CommandUnderTest` struct in [runtime.go](../pkg/runtime/runtime.go) will be used by the runtime to
122
+
create the command with all its configs like `env` variables and is used for the test execution.
123
+
124
+
**3. Add a simple test case**
125
+
126
+
Open [yaml_suite_test.go](../pkg/suite/yaml_suite_test.go) and look for an existing test to add this property or create a new one.
127
+
I recommend to create a simple test case before adding the properties because it is easier to debug and test.
128
+
In our example we could extend the `TestYAMLSuite_ShouldPreferLocalTestConfigs` test but will add a new `TestYAMLSuite_Message` test for the simplicity.
129
+
130
+
```go
131
+
func TestYamlSuite_Message(t *testing.T) {
132
+
yaml := []byte(`
133
+
tests:
134
+
echo hello:
135
+
exit-code: 0
136
+
config:
137
+
message: "This is a very special test"
138
+
`)
139
+
140
+
got := ParseYAML(yaml)
141
+
142
+
assert.Equal(t, "This is a very special test", got.TestCases[0].Command.Message)
143
+
}
144
+
```
145
+
146
+
Run the unit tests with `make test`. It should print a result like this:
147
+
148
+
```
149
+
--- FAIL: TestYamlSuite_Message (0.00s)
150
+
yaml_suite_test.go:192:
151
+
Error Trace: yaml_suite_test.go:192
152
+
Error: Not equal:
153
+
expected: "This is a very special test"
154
+
actual : ""
155
+
156
+
Diff:
157
+
--- Expected
158
+
+++ Actual
159
+
@@ -1 +1 @@
160
+
-This is a very special test
161
+
+
162
+
Test: TestYamlSuite_Message
163
+
FAIL
164
+
```
165
+
166
+
**4. Parse YAML and convert it to `suite.Suite`**
167
+
168
+
This is a little bit complicated and error prone because it is splitted into three steps:
169
+
170
+
- Parse YAML file
171
+
- Convert YAML config structs to runtime test structs
172
+
- Assign global configuration
173
+
174
+
For this take a look at the `pkg/suite/yaml_suite.go:ParseYAML` function which is responsible for parsing the suite.
0 commit comments