diff --git a/cmd/goss/goss.go b/cmd/goss/goss.go index b89ce5bf6..de75b4437 100644 --- a/cmd/goss/goss.go +++ b/cmd/goss/goss.go @@ -8,9 +8,10 @@ import ( "github.com/aelsabbahy/goss" "github.com/aelsabbahy/goss/outputs" "github.com/urfave/cli" - //"time" ) +const gossName = "goss" + var version string func main() { @@ -18,7 +19,7 @@ func main() { app := cli.NewApp() app.EnableBashCompletion = true app.Version = version - app.Name = "goss" + app.Name = gossName app.Usage = "Quick and Easy server validation" app.Flags = []cli.Flag{ cli.StringFlag{ @@ -286,11 +287,11 @@ func main() { Value: 5 * time.Second, }, cli.StringFlag{ - Name: "username, u", + Name: "username, u", Usage: "Username for basic auth", }, cli.StringFlag{ - Name: "password, p", + Name: "password, p", Usage: "Password for basic auth", }, }, diff --git a/cmd/goss/goss_integration_test.go b/cmd/goss/goss_integration_test.go new file mode 100644 index 000000000..4546909ff --- /dev/null +++ b/cmd/goss/goss_integration_test.go @@ -0,0 +1,86 @@ +// +build integration + +package main + +import ( + "fmt" + "io/ioutil" + "os" + "os/exec" + "path/filepath" + "testing" + + "github.com/rendon/testcli" + "github.com/stretchr/testify/assert" +) + +func TestMain(m *testing.M) { + err := os.Chdir("../..") + if err != nil { + fmt.Printf("could not change dir: %v", err) + os.Exit(1) + } + build := exec.Command("go", "build", "./cmd/goss") + err = build.Run() + if err != nil { + fmt.Printf("could not make binary for %s: %v", gossName, err) + os.Exit(1) + } + err = os.Chdir(filepath.Join("cmd", "goss")) + if err != nil { + fmt.Printf("could not cd back to cmd/goss to run test suite: %v", err) + } + os.Exit(m.Run()) +} + +func TestGossSuccess(t *testing.T) { + t.Parallel() + testCases := map[string]struct { + args []string + }{ + "no_args": { + args: []string{}, + }, + "help": { + args: []string{"--help"}, + }, + } + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { + c := command(tc.args) + c.Run() + assert.True(t, c.Success()) + stdoutGolden := golden("goss", name, "stdout", true) + stderrGolden := golden("goss", name, "stderr", true) + if os.Getenv("UPDATE_GOLDEN") != "" { + ioutil.WriteFile(stdoutGolden, []byte(c.Stdout()), 0644) + ioutil.WriteFile(stderrGolden, []byte(c.Stderr()), 0644) + } + assert.Equal(t, read(t, stdoutGolden), c.Stdout()) + assert.Equal(t, read(t, stderrGolden), c.Stderr()) + }) + } +} + +func command(args []string) *testcli.Cmd { + return testcli.Command(filepath.Join("..", "../", "goss"), args...) +} + +func golden(command string, caseName string, streamName string, isPass bool) string { + outcome := "fail" + if isPass { + outcome = "pass" + } + return filepath.Join("testdata", command, fmt.Sprintf("%v.%v.%v.golden", caseName, streamName, outcome)) +} + +func read(t *testing.T, golden string) string { + content, err := ioutil.ReadFile(golden) + if err != nil { + if os.IsNotExist(err) { + return "" // this is fine; failure will happen on assert. + } + t.Fatalf("Could not read golden from %v: %v", golden, err) + } + return string(content) +} diff --git a/cmd/goss/testdata/goss/help.stderr.pass.golden b/cmd/goss/testdata/goss/help.stderr.pass.golden new file mode 100644 index 000000000..e69de29bb diff --git a/cmd/goss/testdata/goss/help.stdout.pass.golden b/cmd/goss/testdata/goss/help.stdout.pass.golden new file mode 100644 index 000000000..77b145e8c --- /dev/null +++ b/cmd/goss/testdata/goss/help.stdout.pass.golden @@ -0,0 +1,20 @@ +NAME: + goss - Quick and Easy server validation + +USAGE: + goss [global options] command [command options] [arguments...] + +COMMANDS: + validate, v Validate system + serve, s Serve a health endpoint + render, r render gossfile after imports + autoadd, aa automatically add all matching resource to the test suite + add, a add a resource to the test suite + help, h Shows a list of commands or help for one command + +GLOBAL OPTIONS: + --gossfile value, -g value Goss file to read from / write to (default: "./goss.yaml") [$GOSS_FILE] + --vars value json/yaml file containing variables for template [$GOSS_VARS] + --package value Package type to use [rpm, deb, apk, pacman] + --help, -h show help + --version, -v print the version diff --git a/cmd/goss/testdata/goss/no_args.stderr.pass.golden b/cmd/goss/testdata/goss/no_args.stderr.pass.golden new file mode 100644 index 000000000..e69de29bb diff --git a/cmd/goss/testdata/goss/no_args.stdout.pass.golden b/cmd/goss/testdata/goss/no_args.stdout.pass.golden new file mode 100644 index 000000000..77b145e8c --- /dev/null +++ b/cmd/goss/testdata/goss/no_args.stdout.pass.golden @@ -0,0 +1,20 @@ +NAME: + goss - Quick and Easy server validation + +USAGE: + goss [global options] command [command options] [arguments...] + +COMMANDS: + validate, v Validate system + serve, s Serve a health endpoint + render, r render gossfile after imports + autoadd, aa automatically add all matching resource to the test suite + add, a add a resource to the test suite + help, h Shows a list of commands or help for one command + +GLOBAL OPTIONS: + --gossfile value, -g value Goss file to read from / write to (default: "./goss.yaml") [$GOSS_FILE] + --vars value json/yaml file containing variables for template [$GOSS_VARS] + --package value Package type to use [rpm, deb, apk, pacman] + --help, -h show help + --version, -v print the version diff --git a/docs/manual.md b/docs/manual.md index 057362a07..728f71905 100644 --- a/docs/manual.md +++ b/docs/manual.md @@ -408,6 +408,7 @@ If you want to keep your tests in separate files, the best way to obtain a singl * [http](#http) * [interface](#interface) * [kernel-param](#kernel-param) +* [matching](#matching) * [mount](#mount) * [package](#package) * [port](#port) diff --git a/glide.lock b/glide.lock index 568d4c1e2..5e9a9bc08 100644 --- a/glide.lock +++ b/glide.lock @@ -1,8 +1,10 @@ -hash: ee9c9147007d86588eb760fe7985f4017b3798255d99b23d3240c6a0d8b33291 -updated: 2016-11-09T02:23:29.857676716Z +hash: 3bcece2cef7703bc9f2d4c47dcae8842b242425a716028d25e0437b9493ee36f +updated: 2018-11-10T14:10:15.73118Z imports: - name: github.com/achanda/go-sysctl version: 6be7678c45d2052640e72060e4f5db6165b1ecab +- name: github.com/aelsabbahy/go-ps + version: 443386855ca1f4d28d990ae9e46e9bc1533de994 - name: github.com/aelsabbahy/GOnetstat version: edf89f784e0876818dc19f7744a16742a0a66f16 - name: github.com/cheekybits/genny @@ -21,30 +23,35 @@ imports: version: 66b8e73f3f5cda9f96b69efd03dd3d7fc4a5cdb8 - name: github.com/miekg/dns version: 58f52c57ce9df13460ac68200cef30a008b9c468 -- name: github.com/aelsabbahy/go-ps - version: 443386855ca1f4d28d990ae9e46e9bc1533de994 - name: github.com/oleiade/reflections version: 0e86b3c98b2ff33e30c85cfe97d9a63d439fe7eb - name: github.com/onsi/gomega version: ff4bc6b6f9f5affa66635cd04d31d2a7ee21ffd6 subpackages: - - types + - format - internal/assertion - internal/asyncassertion + - internal/oraclematcher - internal/testingtsupport - matchers - - internal/oraclematcher - - format - matchers/support/goraph/bipartitegraph - matchers/support/goraph/edge - matchers/support/goraph/node - matchers/support/goraph/util + - types - name: github.com/opencontainers/runc version: 8779fa57eb4a810a7360187dfa5e168a76cf5d21 subpackages: - libcontainer/user - name: github.com/patrickmn/go-cache version: 1881a9bccb818787f68c52bfba648c6cf34c34fa +- name: github.com/rendon/testcli + version: 6283090d169f51a2410b4e260341a01c9a4c0ca7 +- name: github.com/stretchr/testify + version: f35b8ab0b5a2cef36673838d662e249dd9c94686 + subpackages: + - assert + - require - name: github.com/urfave/cli version: d86a009f5e13f83df65d0d6cee9a2e3f1445f0da - name: golang.org/x/sys diff --git a/glide.yaml b/glide.yaml index 017d81c3a..3d5e8d391 100644 --- a/glide.yaml +++ b/glide.yaml @@ -22,3 +22,9 @@ import: - pkg/mount - package: github.com/patrickmn/go-cache - package: github.com/miekg/dns +- package: github.com/rendon/testcli +- package: github.com/stretchr/testify + version: ~1.2.2 + subpackages: + - assert + - require diff --git a/integration-tests/Dockerfile_wheezy b/integration-tests/Dockerfile_wheezy index 75cf85838..986752351 100644 --- a/integration-tests/Dockerfile_wheezy +++ b/integration-tests/Dockerfile_wheezy @@ -1,3 +1,39 @@ +# tester build stage runs the golang integration tests within the container +FROM debian:wheezy as tester + +ARG arch=amd64 +ARG golang_version=1.9.3 + +RUN apt-get update && \ + apt-get install -y \ + ca-certificates \ + curl \ + git \ + unzip && \ + apt-get remove -y vim-tiny && \ + apt-get clean +RUN curl -L "https://dl.google.com/go/go${golang_version}.linux-${arch}.tar.gz" --output golang.tgz && \ + tar zxvf golang.tgz --directory /usr/local >/dev/null && \ + ln -s /usr/local/go/bin/go /usr/local/bin/go && \ + rm -rf golang.tgz +COPY ./ /code/src/github.com/aelsabbahy/goss +RUN cd /code/src/github.com/aelsabbahy/goss && \ + export GOPATH=/code && \ + curl -L https://github.com/Masterminds/glide/releases/download/0.10.2/glide-0.10.2-linux-${arch}.zip --output glide.zip && \ + rm -rf linux-${arch} && \ + unzip glide.zip && \ + linux-${arch}/glide install && \ + go test -v -tags integration ./cmd/goss && \ + rm -rf /code && \ + rm /usr/local/bin/go && \ + rm -rf /usr/local/bin/go && \ + unset GOPATH && \ + apt-get remove -y \ + curl \ + git \ + unzip && \ + apt-get clean + FROM debian:wheezy MAINTAINER Ahmed diff --git a/integration-tests/test.sh b/integration-tests/test.sh index 3385cfe86..1a209e60a 100755 --- a/integration-tests/test.sh +++ b/integration-tests/test.sh @@ -4,6 +4,7 @@ set -xeu os=$1 arch=$2 +golang_version="${TRAVIS_GO_VERSION-1.9.3}" seccomp_opts() { local docker_ver minor_ver @@ -17,7 +18,12 @@ seccomp_opts() { cp "../release/goss-linux-$arch" "goss/$os/" # Run build if Dockerfile has changed but hasn't been pushed to dockerhub if ! md5sum -c "Dockerfile_${os}.md5"; then - docker build -t "aelsabbahy/goss_${os}:latest" - < "Dockerfile_$os" + docker build \ + -t "aelsabbahy/goss_${os}:latest" \ + --file "Dockerfile_${os}" \ + --build-arg "golang_version=${golang_version}" \ + --build-arg "arch=${arch}" \ + ".." # Pull if image doesn't exist locally elif ! docker images | grep "aelsabbahy/goss_$os";then docker pull "aelsabbahy/goss_$os"