Skip to content

Commit

Permalink
Merge pull request #114 from SimonBaeumer/add-docker-executor
Browse files Browse the repository at this point in the history
Add docker executor
  • Loading branch information
SimonBaeumer committed Feb 21, 2020
2 parents bd82909 + 7dca238 commit bc35fc5
Show file tree
Hide file tree
Showing 642 changed files with 277,263 additions and 10 deletions.
8 changes: 7 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ jobs:
- make integration-windows

- name: Unit tests
script:
- make test

- name: Unit test all
before_script:
- curl https://s3.amazonaws.com/codeclimate/test-reporter/test-reporter-0.6.3-linux-amd64 --output test-reporter
- chmod +x test-reporter
Expand All @@ -68,7 +72,9 @@ jobs:
- ./test-reporter after-build -t gocov --exit-code $TRAVIS_TEST_RESULT

- name: Integration test
script: make integration-linux
script:
- docker pull docker.io/library/alpine:3.11
- make integration-linux

- stage: deploy
name: "Deployment"
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Added `nodes` which allow remote execution of tests
- Added `SSHExecutor` and `LocalExecutor`
- Removed `concurrent` argument from `test` command
- Added `DockerExecutor`

# v1.3.0

Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ test-coverage:
go test -coverprofile c.out ./...


test-coverage-all: export COMMANDER_TEST_ALL = 1
test-coverage-all: export COMMANDER_SSH_TEST = 1
test-coverage-all: export COMMANDER_TEST_SSH_HOST = localhost:2222
test-coverage-all: export COMMANDER_TEST_SSH_USER = root
Expand Down
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ For more information take a look at the [quick start](#quick-start), the [exampl
+ [Nodes](#nodes)
- [local](#local)
- [ssh](#ssh)
- [docker](#docker)
+ [Development](#development)
* [Misc](#misc)

Expand Down Expand Up @@ -643,6 +644,7 @@ Available node types are currently:

- `local`, execute tests locally
- `ssh`, execute tests viá ssh
- `docker`, execute tests inside a docker container

```yaml
nodes: # define nodes in the node section
Expand Down Expand Up @@ -690,9 +692,11 @@ tests:

#### ssh

The `ssh` will execute tests against a configured node using ssh.
The `ssh` node type will execute tests against a configured node using ssh.

**Limitations:** The `inhereit-env` config is disabled for ssh hosts, nevertheless it is possible to set env variables
**Limitations:**
- The `inhereit-env` config is disabled for ssh hosts, nevertheless it is possible to set env variables
- Private registries are not supported at the moment

```yaml
nodes: # define nodes in the node section
Expand All @@ -711,6 +715,27 @@ tests:
exit-code: 0
```
#### docker
The `docker` node type executes the given command inside a docker container.

**Notes:** If the default docker registry should be used prefix the container with the registry `docker.io/library/`

```yaml:
nodes:
docker-host:
type: docker
image: docker.io/library/alpine:3.11
user: 1000 # define the owner of the executed command
config:
nodes:
- docker-host

tests:
"id -u":
stdout: "1001"
```
### Development
```
Expand Down Expand Up @@ -744,10 +769,12 @@ $ make deps

### Unit tests

`COMMANDER_TEST_ALL` will enable all tests which are depending on external systems like docker or databases.
Enables ssh tests in unit test suite and sets the credentials for the target host.
`COMMANDER_SSH_TEST` must be set to `1` to enable ssh tests.

```
export COMMANDER_TEST_ALL = 1
export COMMANDER_TEST_SSH=1
export COMMANDER_TEST_SSH_HOST=localhost:2222
export COMMANDER_TEST_SSH_PASS=pass
Expand Down
5 changes: 5 additions & 0 deletions commander_linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ tests:
- ✓ [ssh-host] it should test multiple hosts
- ✓ [ssh-host-default] it should test multiple hosts
- ✓ [local] it should test multiple hosts
exit-code: 0

test docker:
command: ./commander test integration/linux/docker.yaml
stdout: ✓ [docker-host] cat /etc/os-release
exit-code: 0
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
module github.com/SimonBaeumer/commander

require (
github.com/Microsoft/go-winio v0.4.14 // indirect
github.com/SimonBaeumer/cmd v1.1.0
github.com/antchfx/xmlquery v1.1.0
github.com/antchfx/xpath v1.1.0 // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v1.13.1
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e
github.com/magiconair/properties v1.8.1 // indirect
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0
github.com/stretchr/objx v0.2.0 // indirect
github.com/stretchr/testify v1.4.0
github.com/tidwall/gjson v1.3.2
github.com/urfave/cli v1.20.0
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
golang.org/x/net v0.0.0-20191014212845-da9a3fd4c582 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.2.4
Expand Down
26 changes: 26 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/Microsoft/go-winio v0.4.14 h1:+hMXMk01us9KgxGb7ftKQt2Xpf5hH/yky+TDA+qxleU=
github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=
github.com/SimonBaeumer/cmd v1.1.0 h1:tr5dUMlly/8bLiC5B0J1AcE4ISru8POEfzAirWnUJnY=
github.com/SimonBaeumer/cmd v1.1.0/go.mod h1:4mc/LDXDWNbkeooqHP83yx3JXtInPHjJkF8zhzqqmZE=
github.com/antchfx/jsonquery v1.0.0 h1:1Yhk496SrCoY6fJkFZqpXEqbwOw5sFtLns9la4NoK3I=
Expand All @@ -10,12 +12,31 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docker/distribution v2.7.1+incompatible h1:a5mlkVzth6W5A4fOsS3D2EO5BUmsJpcB+cRlLU7cSug=
github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
github.com/docker/docker v1.13.1 h1:IkZjBSIc8hBjLpqeAbeE5mca5mNgeatLHBy3GO78BWo=
github.com/docker/docker v1.13.1/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ=
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw=
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e h1:9MlwzLdW7QSDrhDjFlsEYmxpFyIoXmYRon3dt0io31k=
github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/opencontainers/go-digest v1.0.0-rc1 h1:WzifXhOVOEOuFYOJAW6aQqW0TooG2iki3E3Ii+WN7gQ=
github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
Expand All @@ -28,10 +49,15 @@ github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20191014212845-da9a3fd4c582 h1:p9xBe/w/OzkeYVKm234g55gMdD1nSIooTir5kV11kfA=
golang.org/x/net v0.0.0-20191014212845-da9a3fd4c582/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b h1:ag/x1USPSsqHud38I9BAC88qdNLDHHtQ4mlgQIZPPNA=
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
19 changes: 19 additions & 0 deletions hack/docker-private-registry.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
nodes:
docker-host:
type: docker
image: docker.io/library/simonbaeumer/test-private:latest

config:
nodes:
- docker-host

tests:
cat /etc/os-release:
stdout:
exactly: |-
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.11.3
PRETTY_NAME="Alpine Linux v3.11"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://bugs.alpinelinux.org/"
23 changes: 23 additions & 0 deletions integration/linux/docker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
nodes:
docker-host:
type: docker
image: docker.io/library/alpine:3.11
user: 1001

config:
nodes:
- docker-host

tests:
cat /etc/os-release:
stdout:
exactly: |-
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.11.3
PRETTY_NAME="Alpine Linux v3.11"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://bugs.alpinelinux.org/"
"id -u":
stdout: "1001"
111 changes: 111 additions & 0 deletions pkg/runtime/docker_executor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package runtime

import (
"bytes"
"context"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"log"
"strings"
"time"
)

// DockerExecutor executes the test inside a docker container
type DockerExecutor struct {
Image string // Image which is started to execute the test
Privileged bool // Enable privileged mode for the container
User string // User defines which user executes the test
}

// Execute executes the script inside a docker container
func (e DockerExecutor) Execute(test TestCase) TestResult {
ctx := context.Background()
cli, err := client.NewEnvClient()
if err != nil {
test.Result.Error = err
return TestResult{
TestCase: test,
}
}

log.Printf("Pulling image %s\n", e.Image)
reader, err := cli.ImagePull(ctx, e.Image, types.ImagePullOptions{})
if err != nil {
test.Result.Error = fmt.Errorf("could not pull image '%s' with error: '%s'", e.Image, err)
return TestResult{
TestCase: test,
}
}
buf := bytes.Buffer{}
buf.ReadFrom(reader)
log.Printf("Pull log image'%s':\n %s\n", e.Image, buf.String())

var env []string
for k, v := range test.Command.Env {
env = append(env, fmt.Sprintf("%s=%s", k, v))
}

log.Printf("Create container %s\n", e.Image)
resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: e.Image,
WorkingDir: test.Command.Dir,
Env: env,
User: e.User,
Cmd: []string{"/bin/sh", "-c", test.Command.Cmd},
Tty: false,
}, nil, nil, "")
if err != nil {
test.Result.Error = fmt.Errorf("could not pull image '%s' with error: '%s'", e.Image, err)
return TestResult{
TestCase: test,
}
}

log.Printf("Started container %s %s\n", e.Image, resp.ID)
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
test.Result.Error = fmt.Errorf("could not pull image '%s' with error: '%s'", e.Image, err)
return TestResult{
TestCase: test,
}
}
duration := time.Duration(1 * time.Second)
defer cli.ContainerStop(ctx, resp.ID, &duration)

status, err := cli.ContainerWait(ctx, resp.ID)
if err != nil {
panic(err)
}

out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true, ShowStderr: true})
if err != nil {
panic(err)
}

stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}

_, err = stdcopy.StdCopy(stdout, stderr, out)
if err != nil {
panic(err)
}

log.Println("title: '"+test.Title+"'", " Command: ", test.Command.Cmd)
log.Println("title: '"+test.Title+"'", " Directory: ", test.Command.Dir)
log.Println("title: '"+test.Title+"'", " Env: ", test.Command.Env)

// Write test result
test.Result = CommandResult{
ExitCode: int(status),
Stdout: strings.TrimSpace(strings.Replace(stdout.String(), "\r\n", "\n", -1)),
Stderr: strings.TrimSpace(strings.Replace(stderr.String(), "\r\n", "\n", -1)),
}

log.Println("title: '"+test.Title+"'", " ExitCode: ", test.Result.ExitCode)
log.Println("title: '"+test.Title+"'", " Stdout: ", test.Result.Stdout)
log.Println("title: '"+test.Title+"'", " Stderr: ", test.Result.Stderr)

return Validate(test)
}
Loading

0 comments on commit bc35fc5

Please sign in to comment.