Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/acceptance test coverage #15

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: golangci-lint
on:
push:
tags:
- v*
branches:
- master
- main
pull_request:
permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.17
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.29
4 changes: 2 additions & 2 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ jobs:
run: make deps
- name: Unit test
run: make ci-test
- name: Linting
run: make lint
- name: Acceptance test
run: make build acceptance-test-docker
# - name: Collect coverage data
# run: make coverage
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
env:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dist/
# Test binary, built with `go test -c`
*.test
/coverage.txt
/wait-for_coverage.txt

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
Expand Down
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
issues:
exclude-rules:
- path: _test\.go
linters:
- unused
14 changes: 12 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ clean-deps:
mkdir -p ./tmp

.PHONY: deps
deps: ./bin/tparse ./bin/golangci-lint
deps:
go install github.com/wadey/gocovmerge@b5bfa59
go get -v ./...
go mod tidy

Expand All @@ -65,10 +66,19 @@ test: ## run unit tests
ci-test: ## ci target - run tests to generate coverage data
go test -race -coverprofile=coverage.txt -covermode=atomic ./...

.PHONY: coverage
coverage: ## combine coverage reports
gocovmerge coverage.txt wait-for_coverage.txt > coverage.txt

.PHONY: acceptance-test
acceptance-test: build ## run acceptance tests
acceptance-test: ## run acceptance tests
go test ./cmd/wait-for -coverpkg=./... -c -o wait-for.test
cd test && godog

.PHONY: acceptance-test-docker
acceptance-test-docker: ## run acceptance tests in Docker (if you can't open local ports reliably)
docker-compose -f test/docker-compose.yml up --build --abort-on-container-exit godog

.PHONY: acceptance-test-docker-shell
acceptance-test-docker-shell: ## run a shell in the acceptance test docker container
docker-compose -f test/docker-compose.yml run godog bash
34 changes: 29 additions & 5 deletions cmd/wait-for/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"fmt"
"log"
"os"
"strings"

"github.com/confluentinc/bincover"
waitfor "github.com/dnnrly/wait-for"
"github.com/spf13/afero"
)
Expand All @@ -18,11 +20,20 @@ func main() {
configFile := ""
var quiet bool

flag.CommandLine.Init(os.Args[0], flag.ContinueOnError)
flag.StringVar(&timeoutParam, "timeout", timeoutParam, "time to wait for services to become available")
flag.StringVar(&httpTimeoutParam, "http_timeout", httpTimeoutParam, "timeout for requests made by a http client")
flag.StringVar(&configFile, "config", "", "configuration file to use")
flag.BoolVar(&quiet, "quiet", false, "reduce output to the minimum")
flag.Parse()
err := flag.CommandLine.Parse(os.Args[1:])
if err != nil {
if err == flag.ErrHelp {
exit(0)
} else {
exit(1)
}
return
}

fs := afero.NewOsFs()

Expand All @@ -36,13 +47,26 @@ func main() {

config, err := waitfor.OpenConfig(configFile, timeoutParam, httpTimeoutParam, fs)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(1)
_, _ = fmt.Printf("%v", err)
exit(1)
return
}

err = waitfor.WaitOn(config, logger, flag.Args(), waitfor.SupportedWaiters)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(1)
_, _ = fmt.Printf("%v", err)
exit(1)
return
}
}

func exit(code int) {
if val, found := os.LookupEnv("BINCOVER_EXIT"); found {
if strings.ToLower(val) == "true" {
bincover.ExitCode = code
return
}
}

os.Exit(code)
}
13 changes: 13 additions & 0 deletions cmd/wait-for/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"os"
"testing"

"github.com/confluentinc/bincover"
)

func TestBincoverRunMain(t *testing.T) {
os.Setenv("BINCOVER_EXIT", "true")
bincover.RunTest(main)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ module github.com/dnnrly/wait-for
go 1.14

require (
github.com/confluentinc/bincover v0.2.0
github.com/cucumber/godog v0.10.0
github.com/gofrs/uuid v3.3.0+incompatible // indirect
github.com/google/uuid v1.3.0
github.com/hashicorp/go-memdb v1.3.0 // indirect
github.com/spf13/afero v1.4.1
github.com/stretchr/testify v1.6.1
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
github.com/aslakhellesoy/gox v1.0.100/go.mod h1:AJl542QsKKG96COVsv0N74HHzVQgDIQPceVUh1aeU2M=
github.com/confluentinc/bincover v0.2.0 h1:WSS3MqzwJbosCLMOuF3tJ0pMpALzBfrm80Tb+/3gbQs=
github.com/confluentinc/bincover v0.2.0/go.mod h1:qeI1wx0RxdGTZtrJY0HVlgJ4NqC/X2Z+fHbvy87tgHE=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/cucumber/gherkin-go/v11 v11.0.0 h1:cwVwN1Qn2VRSfHZNLEh5x00tPBmZcjATBWDpxsR5Xug=
github.com/cucumber/gherkin-go/v11 v11.0.0/go.mod h1:CX33k2XU2qog4e+TFjOValoq6mIUq0DmVccZs238R9w=
Expand All @@ -15,6 +17,8 @@ github.com/gofrs/uuid v3.3.0+incompatible h1:8K4tyRfvU1CYPgJsveYFQMhpFd/wXNM7iK6
github.com/gofrs/uuid v3.3.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls=
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/go-immutable-radix v1.2.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-immutable-radix v1.3.0 h1:8exGP7ego3OmkfksihtSouGMZ+hQrhxx+FVELeXpVPE=
github.com/hashicorp/go-immutable-radix v1.3.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
Expand All @@ -30,6 +34,7 @@ github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uG
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
Expand All @@ -38,6 +43,7 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down Expand Up @@ -66,6 +72,7 @@ gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=
Expand Down
4 changes: 2 additions & 2 deletions test/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ RUN go mod download
RUN go install github.com/cucumber/godog/cmd/[email protected]

ADD . .
RUN go build -o wait-for ./cmd/wait-for
RUN go test ./cmd/wait-for -covermode=atomic -coverpkg=./... -c -o wait-for.test

WORKDIR /app/test/
ENTRYPOINT . files/godog.sh
CMD . files/godog.sh
35 changes: 26 additions & 9 deletions test/cli_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package test

import (
"bytes"
"context"
"fmt"
"log"
Expand All @@ -12,10 +11,13 @@ import (
"sync"
"time"

"github.com/confluentinc/bincover"
"github.com/cucumber/godog"
"github.com/stretchr/testify/assert"
)

var binPath string = "../wait-for.test"

type stepsData struct {
assertError error

Expand Down Expand Up @@ -120,17 +122,30 @@ func (s *stepsData) Errorf(format string, args ...interface{}) {
}

func (s *stepsData) iRunWaitforWithParameters(params string) error {
cmd := exec.Command("../wait-for", strings.Split(params, " ")...)

var b bytes.Buffer
cmd.Stderr = &b
cmd.Stdout = &b
collector := bincover.NewCoverageCollector("wait-for_coverage.txt", true)
err := collector.Setup()
if err != nil {
return err
}
defer func() {
err := collector.TearDown()
if err != nil {
panic(err)
}
}()

start := time.Now()

_ = cmd.Run()
s.output = b.String()
s.statusCode = cmd.ProcessState.ExitCode()
_, s.statusCode, _ = collector.RunBinary(
binPath,
"TestBincoverRunMain",
[]string{},
strings.Split(params, " "),
bincover.PostExec(func(cmd *exec.Cmd, output string, err error) error {
s.output = output
return nil
}),
)

s.duration = time.Since(start)

Expand Down Expand Up @@ -238,6 +253,8 @@ func (s *stepsData) listeningServerWaitsThenResponds(port int, duration string,
func InitializeTestSuite(ctx *godog.TestSuiteContext) {
ctx.BeforeSuite(func() {
})
ctx.AfterSuite(func() {
})
}

func InitializeScenario(ctx *godog.ScenarioContext) {
Expand Down
2 changes: 2 additions & 0 deletions test/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ services:
build:
context: ..
dockerfile: test/Dockerfile
volumes:
- ../:/working
4 changes: 4 additions & 0 deletions test/files/godog.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#!/usr/bin/env sh

set -o xtrace

godog $@
chmod a+rw wait-for_coverage.txt
cp wait-for_coverage.txt /working