Skip to content

Commit bd82909

Browse files
authored
Merge pull request #110 from SimonBaeumer/add-ssh-executor
Create exectuor interface and LocalExecutor for interchangable executors
2 parents 3fa13b0 + 7935bac commit bd82909

34 files changed

+1128
-206
lines changed

.travis.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ go:
1515
sudo: required
1616
dist: trusty
1717

18+
services:
19+
- docker
20+
1821
before_install:
1922
- go get -u golang.org/x/lint/golint
2023
- curl -L https://github.com/SimonBaeumer/commander/releases/download/v1.2.1/commander-linux-amd64 -o ~/bin/commander
@@ -36,7 +39,7 @@ jobs:
3639
script:
3740
- curl -L https://github.com/SimonBaeumer/commander/releases/download/v0.3.0/commander-darwin-amd64 -o ~/bin/commander
3841
- chmod +x ~/bin/commander
39-
- make integration
42+
- make integration-unix
4043

4144
- name: windows Unit
4245
os: windows
@@ -60,12 +63,12 @@ jobs:
6063
- chmod +x test-reporter
6164
- ./test-reporter before-build
6265
script:
63-
- make test-coverage
66+
- make test-coverage-all
6467
after_script:
6568
- ./test-reporter after-build -t gocov --exit-code $TRAVIS_TEST_RESULT
6669

6770
- name: Integration test
68-
script: make integration
71+
script: make integration-linux
6972

7073
- stage: deploy
7174
name: "Deployment"

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# v2.0.0
2+
3+
- Added `nodes` which allow remote execution of tests
4+
- Added `SSHExecutor` and `LocalExecutor`
5+
- Removed `concurrent` argument from `test` command
6+
17
# v1.3.0
28

39
- Added `xml` assertion to `stdout` and `stderr`

Makefile

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
exe = cmd/commander/*
22
cmd = commander
33
TRAVIS_TAG ?= "0.0.0"
4+
CWD = $(shell pwd)
45

56
.PHONY: deps lint test integration integration-windows git-hooks init
67

@@ -30,9 +31,27 @@ test-coverage:
3031
$(info INFO: Starting build $@)
3132
go test -coverprofile c.out ./...
3233

33-
integration: build
34+
35+
test-coverage-all: export COMMANDER_SSH_TEST = 1
36+
test-coverage-all: export COMMANDER_TEST_SSH_HOST = localhost:2222
37+
test-coverage-all: export COMMANDER_TEST_SSH_USER = root
38+
test-coverage-all: export COMMANDER_TEST_SSH_IDENTITY_FILE = $(CWD)/integration/containers/ssh/id_rsa
39+
test-coverage-all:
40+
$(info INFO: Starting build $@)
41+
./integration/setup_unix.sh
42+
go test -coverprofile c.out ./...
43+
./integration/teardown_unix.sh
44+
45+
integration-unix: build
46+
$(info INFO: Starting build $@)
47+
commander test commander_unix.yaml
48+
49+
integration-linux: build
3450
$(info INFO: Starting build $@)
51+
./integration/setup_unix.sh
3552
commander test commander_unix.yaml
53+
commander test commander_linux.yaml
54+
./integration/teardown_unix.sh
3655

3756
integration-windows: build
3857
$(info INFO: Starting build $@)

README.md

Lines changed: 129 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
Define language independent tests for your command line scripts and programs in simple `yaml` files.
1111

1212
- It runs on `windows`, `osx` and `linux`
13+
- It can validate local machines, ssh hosts and docker containers
1314
- It is a self-contained binary - no need to install a heavy lib or language
1415
- It is easy and fast to write
1516

@@ -47,6 +48,10 @@ For more information take a look at the [quick start](#quick-start), the [exampl
4748
- [interval](#interval)
4849
- [retries](#retries)
4950
- [timeout](#timeout)
51+
- [nodes](#nodes)
52+
+ [Nodes](#nodes)
53+
- [local](#local)
54+
- [ssh](#ssh)
5055
+ [Development](#development)
5156
* [Misc](#misc)
5257

@@ -108,12 +113,32 @@ Count: 1, Failed: 0
108113
Here you can see an example with all features for a quick reference
109114

110115
```yaml
116+
nodes:
117+
ssh-host1:
118+
type: ssh
119+
addr: 192.168.0.1:22
120+
user: root
121+
pass: pass
122+
ssh-host2:
123+
type: ssh
124+
addr: 192.168.0.1:22
125+
user: root
126+
identity-file: /home/user/id_rsa.pub
127+
docker-host1:
128+
type: docker
129+
image: alpine:2.4
130+
docker-host2:
131+
type: docker
132+
instance: alpine_instance_1
133+
111134
config: # Config for all executed tests
112135
dir: /tmp #Set working directory
113136
env: # Environment variables
114137
KEY: global
115138
timeout: 50s # Define a timeout for a command under test
116139
retries: 2 # Define retries for each test
140+
nodes:
141+
- ssh-host1 # define default hosts
117142

118143
tests:
119144
echo hello: # Define command as title
@@ -141,7 +166,7 @@ tests:
141166
command: echo hello
142167
stdout:
143168
contains:
144-
- hello #See test "it should fail"
169+
- hello #See test "it should fail"
145170
exactly: hello
146171
line-count: 1
147172
config:
@@ -152,6 +177,9 @@ tests:
152177
ANOTHER: yeah # Add another env variable
153178
timeout: 1s # Overwrite timeout
154179
retries: 5
180+
nodes: # overwrite default nodes
181+
- docker-host1
182+
- docker-host2
155183
exit-code: 0
156184
```
157185
@@ -607,6 +635,82 @@ If a tests exceeds the given `timeout` the test will fail.
607635
timeout: 600s
608636
```
609637

638+
### Nodes
639+
640+
`Commander` has the option to execute tests against other hosts, i.e. via ssh.
641+
642+
Available node types are currently:
643+
644+
- `local`, execute tests locally
645+
- `ssh`, execute tests viá ssh
646+
647+
```yaml
648+
nodes: # define nodes in the node section
649+
ssh-host:
650+
type: ssh # define the type of the connection
651+
user: root # set the user which is used by the connection
652+
pass: password # set password for authentication
653+
addr: 192.168.0.100:2222 # target host address
654+
identity-file: ~/.ssh/id_rsa # auth with private key
655+
tests:
656+
echo hello:
657+
config:
658+
nodes: # define on which host the test should be executed
659+
- ssh-host
660+
stdout: hello
661+
exit-code: 0
662+
```
663+
664+
You can identify on which node a test failed by inspecting the test output.
665+
The `[local]` and `[ssh-host]` represent the node name on which the test were executed.
666+
667+
```
668+
[local] it should test ssh host
669+
[ssh-host] it should fail if env could not be set
670+
```
671+
672+
#### local
673+
674+
The `local` node is the default execution and will be applied if nothing else was configured.
675+
It is always pre-configured and available, i.e. if you want to execute tests on a node and locally.
676+
677+
```yaml
678+
nodes:
679+
ssh-host:
680+
addr: 192.168.1.100
681+
user: ...
682+
tests:
683+
echo hello:
684+
config:
685+
nodes: # will be executed on local and ssh-host
686+
- ssh-host
687+
- local
688+
exit-code: 0
689+
```
690+
691+
#### ssh
692+
693+
The `ssh` will execute tests against a configured node using ssh.
694+
695+
**Limitations:** The `inhereit-env` config is disabled for ssh hosts, nevertheless it is possible to set env variables
696+
697+
```yaml
698+
nodes: # define nodes in the node section
699+
ssh-host:
700+
type: ssh # define the type of the connection
701+
user: root # set the user which is used by the connection
702+
pass: password # set password for authentication
703+
addr: 192.168.0.100:2222 # target host address
704+
identity-file: ~/.ssh/id_rsa # auth with private key
705+
tests:
706+
echo hello:
707+
config:
708+
nodes: # define on which host the test should be executed
709+
- ssh-host
710+
stdout: hello
711+
exit-code: 0
712+
```
713+
610714
### Development
611715
612716
```
@@ -622,13 +726,35 @@ $ make test
622726
# Coverage
623727
$ make test-coverage
624728

625-
# Integration tests
626-
$ make integration
729+
# Coverage with more complex tests like ssh execution
730+
$ make test-coverage-all
731+
732+
# Integration tests for linux and macos
733+
$ make integration-unix
734+
735+
# Integration on linux
736+
$ make integration-linux
737+
738+
# Integration windows
739+
$ make integration-windows
627740

628741
# Add depdencies to vendor
629742
$ make deps
630743
```
631744

745+
### Unit tests
746+
747+
Enables ssh tests in unit test suite and sets the credentials for the target host.
748+
`COMMANDER_SSH_TEST` must be set to `1` to enable ssh tests.
749+
750+
```
751+
export COMMANDER_TEST_SSH=1
752+
export COMMANDER_TEST_SSH_HOST=localhost:2222
753+
export COMMANDER_TEST_SSH_PASS=pass
754+
export COMMANDER_TEST_SSH_USER=root
755+
export COMMANDER_TEST_SSH_IDENTITY_FILE=integration/containers/ssh/.ssh/id_rsa
756+
```
757+
632758
## Misc
633759

634760
Heavily inspired by [goss](https://github.com/aelsabbahy/goss).

cmd/commander/commander.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ func createTestCommand() cli.Command {
4848
Usage: "Execute the test suite, by default it will use the commander.yaml from your current directory",
4949
ArgsUsage: "[file] [title]",
5050
Flags: []cli.Flag{
51-
cli.IntFlag{
52-
Name: "concurrent",
53-
EnvVar: "COMMANDER_CONCURRENT",
54-
Usage: "Set the max amount of tests which should run concurrently",
55-
},
5651
cli.BoolFlag{
5752
Name: "no-color",
5853
EnvVar: "COMMANDER_NO_COLOR",

commander_linux.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests:
2+
test nodes:
3+
command: ./commander test integration/linux/nodes.yaml
4+
stdout:
5+
contains:
6+
- ✓ [ssh-host] it should test ssh host
7+
- ✓ [ssh-host] it should set env variable
8+
- ✓ [ssh-host-default] it should be executed on ssh-host-default
9+
- ✓ [ssh-host] it should test multiple hosts
10+
- ✓ [ssh-host-default] it should test multiple hosts
11+
- ✓ [local] it should test multiple hosts
12+
exit-code: 0

commander_unix.yaml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ tests:
1414
command: ./commander test ./integration/unix/commander_test.yaml
1515
stdout:
1616
contains:
17-
- ✓ it should exit with error code
17+
- [local] it should exit with error code
1818
line-count: 16
1919
exit-code: 0
2020

2121
it should assert that commander will fail:
2222
command: ./commander test ./integration/unix/failing_suite.yaml
2323
stdout:
2424
contains:
25-
- ✗ 'it will fail', on property 'ExitCode'
26-
- ✗ 'test timeout' could not be executed with error message
25+
- [local] 'it will fail', on property 'ExitCode'
26+
- [local] 'test timeout' could not be executed with error message
2727
- Command timed out after 10ms
2828
- "Count: 2, Failed: 2"
2929
exit-code: 1
@@ -32,7 +32,7 @@ tests:
3232
command: ./commander test ./integration/unix/test_big_output.yaml
3333
stdout:
3434
contains:
35-
- ✓ cat ./integration/unix/_fixtures/big_out.txt
35+
- [local] cat ./integration/unix/_fixtures/big_out.txt
3636
- "Count: 1, Failed: 0"
3737
exit-code: 0
3838

@@ -43,9 +43,9 @@ tests:
4343
COMMANDER_FROM_SHELL: from_shell
4444
stdout:
4545
contains:
46-
- ✓ should print global env value
47-
- ✓ should print local env value
48-
- ✓ should print env var from shell
46+
- [local] should print global env value
47+
- [local] should print local env value
48+
- [local] should print env var from shell
4949
exit-code: 0
5050

5151
test add command:
@@ -61,7 +61,7 @@ tests:
6161
command: ./commander test integration/unix/retries.yaml
6262
stdout:
6363
contains:
64-
- ✗ echo hello, retries 3
65-
- ✓ it should retry failed commands, retries 2
66-
- ✗ it should retry failed commands with an interval, retries 2
67-
exit-code: 1
64+
- [local] echo hello, retries 3
65+
- [local] it should retry failed commands, retries 2
66+
- [local] it should retry failed commands with an interval, retries 2
67+
exit-code: 1

commander_windows.yaml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ tests:
2121
command: commander.exe test ./integration/windows/failing_suite.yaml
2222
stdout:
2323
contains:
24-
- ✗ 'it will fail', on property 'ExitCode'
25-
- ✗ 'test timeout' could not be executed with error message
24+
- [local] 'it will fail', on property 'ExitCode'
25+
- [local] 'test timeout' could not be executed with error message
2626
- "Count: 2, Failed: 2"
2727
exit-code: 1
2828

2929
it should validate a big output:
3030
command: commander.exe test ./integration/windows/test_big_output.yaml
3131
stdout:
3232
contains:
33-
- ✓ type integration\windows\_fixtures\big_out.txt
33+
- [local] type integration\windows\_fixtures\big_out.txt
3434
- "Count: 1, Failed: 0"
3535
exit-code: 0
3636

@@ -41,10 +41,10 @@ tests:
4141
COMMANDER_FROM_SHELL: from_shell
4242
stdout:
4343
contains:
44-
- ✓ should print global
45-
- ✓ should print local
46-
- ✓ should execute in given dir
47-
- ✓ should work with timeout
44+
- [local] should print global
45+
- [local] should print local
46+
- [local] should execute in given dir
47+
- [local] should work with timeout
4848
exit-code: 0
4949

5050
test add command:
@@ -60,5 +60,5 @@ tests:
6060
command: commander.exe test integration/windows/retries.yaml
6161
stdout:
6262
contains:
63-
- ✗ echo hello, retries 3
63+
- [local] echo hello, retries 3
6464
exit-code: 1

0 commit comments

Comments
 (0)