-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dgoss wrapper for goss. Add getEnv and readFile functions to templates Add --color flag to force color mode
- Loading branch information
1 parent
2cfa77b
commit 954e8ed
Showing
9 changed files
with
211 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# dgoss | ||
|
||
dgoss is a convenience wrapper around goss that aims to bring the simplicity of goss to docker containers. | ||
|
||
|
||
## Usage | ||
|
||
`dgoss [run|edit] <docker_run_params>` | ||
|
||
### Run | ||
|
||
Run is used to validate a docker container. It expects a `./goss.yaml` file to exist in the directory it was invoked from. In most cases one can just substitute the docker command for the dgoss command, for example: | ||
|
||
**run:** | ||
|
||
`docker run -e JENKINS_OPTS="--httpPort=8080 --httpsPort=-1" -e JAVA_OPTS="-Xmx1048m" jenkins:alpine` | ||
|
||
**test:** | ||
|
||
`dgoss run -e JENKINS_OPTS="--httpPort=8080 --httpsPort=-1" -e JAVA_OPTS="-Xmx1048m" jenkins:alpine` | ||
|
||
|
||
`dgoss run` will do the following: | ||
* Run the container with the flags you specified. | ||
* Stream the containers log output into the container as `/goss/docker_output.log` | ||
* This allows writing tests or waits against the docker output | ||
* (optional) Run `goss` with `$GOSS_WAIT_OPTS` if `./goss_wait.yaml` file exists in the current dir | ||
* Run `goss` with `$GOSS_OPTS` using `./goss.yaml` | ||
|
||
|
||
### Edit | ||
|
||
Edit will launch a docker container, install goss, and drop the user into an interactive shell. Once the user quits the interactive shell, any `goss.yaml` or `goss_wait.yaml` are copied out into the current directory. This allows the user to leverage the `goss add|autoadd` commands to write tests as they would on a regular machine. | ||
|
||
**Example:** | ||
|
||
`dgoss edit -e JENKINS_OPTS="--httpPort=8080 --httpsPort=-1" -e JAVA_OPTS="-Xmx1048m" jenkins:alpine` | ||
|
||
### Environment vars and defaults | ||
The following environment variables can be set to change the behavior of dgoss. | ||
|
||
##### GOSS_PATH | ||
Location of the goss binary to use. (Default: `$(which goss)`) | ||
|
||
##### GOSS_OPTS | ||
Options to use for the goss test run. (Default: `--color --format documentation`) | ||
|
||
##### GOSS_WAIT_OPTS | ||
Options to use for the goss wait run, when `./goss_wait.yaml` exists. (Default: `-r 30s -s 1s > /dev/null`) | ||
|
||
##### GOSS_SLEEP | ||
Time to sleep after running container (and optionally `goss_wait.yaml`) and before running tests. (Default: `0.2`) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
USAGE="$(basename "$0") [run|edit] <docker_run_params>" | ||
|
||
info() { echo -e "INFO: $*"; } | ||
error() { echo -e "ERROR: $*";exit 1; } | ||
|
||
cleanup() { | ||
set +e | ||
{ kill "$log_pid" && wait "$log_pid"; } 2> /dev/null | ||
rm -rf "$tmp_dir" | ||
if [[ $id ]];then | ||
info "Deleting container" | ||
docker rm -vf "$id" > /dev/null | ||
fi | ||
} | ||
|
||
run(){ | ||
# Copy in goss | ||
cp "${GOSS_PATH}" "$tmp_dir/goss" | ||
chmod 755 "$tmp_dir/goss" | ||
[[ -e goss.yaml ]] && cp goss.yaml "$tmp_dir" | ||
[[ -e goss_wait.yaml ]] && cp goss_wait.yaml "$tmp_dir" | ||
info "Starting docker container" | ||
id=$(docker run -d -v "$tmp_dir:/goss" "${@:2}") | ||
docker logs -f "$id" > "$tmp_dir/docker_output.log" 2>&1 & | ||
log_pid=$! | ||
info "Container ID: ${id:0:8}" | ||
} | ||
|
||
get_docker_file() { | ||
if docker exec "$id" sh -c "test -e $1" > /dev/null;then | ||
docker cp "$id:$1" . | ||
fi | ||
} | ||
|
||
# Main | ||
tmp_dir=$(mktemp -d) | ||
chmod 777 "$tmp_dir" | ||
trap 'ret=$?;cleanup;exit $ret' EXIT | ||
|
||
GOSS_PATH="${GOSS_PATH:-$(which goss)}" | ||
[[ $GOSS_PATH ]] || { error "Couldn't find goss installation, please set GOSS_PATH to it"; } | ||
[[ ${GOSS_OPTS+x} ]] || GOSS_OPTS="--color --format documentation" | ||
[[ ${GOSS_WAIT_OPTS+x} ]] || GOSS_WAIT_OPTS="-r 30s -s 1s > /dev/null" | ||
GOSS_SLEEP=${GOSS_SLEEP:-0.2} | ||
|
||
case "$1" in | ||
run) | ||
run "$@" | ||
if [[ -e goss_wait.yaml ]]; then | ||
info "Found goss_wait.yaml, waiting for it to pass before running tests" | ||
docker exec "$id" sh -c "/goss/goss -g /goss/goss_wait.yaml validate $GOSS_WAIT_OPTS" | ||
fi | ||
[[ $GOSS_SLEEP ]] && { info "Sleeping for $GOSS_SLEEP"; sleep "$GOSS_SLEEP"; } | ||
info "Running Tests" | ||
docker exec "$id" sh -c "/goss/goss -g /goss/goss.yaml validate $GOSS_OPTS" | ||
;; | ||
edit) | ||
run "$@" | ||
info "Run goss add/autoadd to add resources" | ||
docker exec -it "$id" sh -c 'cd /goss; PATH="/goss:$PATH" exec sh' | ||
get_docker_file "/goss/goss.yaml" | ||
get_docker_file "/goss/goss_wait.yaml" | ||
;; | ||
*) | ||
error "$USAGE" | ||
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package goss | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"strings" | ||
"text/template" | ||
) | ||
|
||
func mkSlice(args ...interface{}) []interface{} { | ||
return args | ||
} | ||
|
||
func readFile(f string) (string, error) { | ||
b, err := ioutil.ReadFile(f) | ||
if err != nil { | ||
return "", err | ||
|
||
} | ||
return strings.TrimSpace(string(b)), nil | ||
} | ||
|
||
func getEnv(key string, def ...string) string { | ||
val := os.Getenv(key) | ||
if val == "" && len(def) > 0 { | ||
return def[0] | ||
} | ||
|
||
return os.Getenv(key) | ||
} | ||
|
||
var funcMap = map[string]interface{}{ | ||
"mkSlice": mkSlice, | ||
"readFile": readFile, | ||
"getEnv": getEnv, | ||
} | ||
|
||
func NewTemplateFilter(varsFile string) func([]byte) []byte { | ||
vars, err := varsFromFile(varsFile) | ||
if err != nil { | ||
fmt.Printf("Error: loading vars file '%s'\n%v\n", varsFile, err) | ||
os.Exit(1) | ||
} | ||
tVars := &TmplVars{Vars: vars} | ||
|
||
f := func(data []byte) []byte { | ||
funcMap := funcMap | ||
t := template.New("test").Funcs(template.FuncMap(funcMap)) | ||
tmpl, err := t.Parse(string(data)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
tmpl.Option("missingkey=error") | ||
var doc bytes.Buffer | ||
err = tmpl.Execute(&doc, tVars) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
return doc.Bytes() | ||
} | ||
return f | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters