Skip to content

Commit

Permalink
Merge branch 'feature/fix-test-shutdown-lib'
Browse files Browse the repository at this point in the history
  • Loading branch information
gedge committed Apr 1, 2022
2 parents 21ac6e4 + 1aa467f commit 0628ec9
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 33 deletions.
9 changes: 2 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,22 @@ GIT_COMMIT=$(shell git rev-parse HEAD)
VERSION ?= $(shell git tag --points-at HEAD | grep ^v | head -n 1)
LDFLAGS=-ldflags "-w -s -X 'main.Version=${VERSION}' -X 'main.BuildTime=$(BUILD_TIME)' -X 'main.GitCommit=$(GIT_COMMIT)'"

.PHONY: all
all: audit test build

.PHONY: audit
audit:
go list -m all | nancy sleuth --exclude-vulnerability-file ./.nancy-ignore

.PHONY: build
build:
@mkdir -p $(BUILD_ARCH)/$(BIN_DIR)
go build $(LDFLAGS) -o $(BUILD_ARCH)/$(BIN_DIR)/dp-deployer cmd/dp-deployer/main.go

.PHONY: generate
generate:
go generate ./...

.PHONY: debug
debug: build
HUMAN_LOG=1 go run $(LDFLAGS) -race cmd/dp-deployer/main.go
HUMAN_LOG=1 go run $(LDFLAGS) -race cmd/dp-deployer/main.go

.PHONY: test
test:
go test -cover -race ./...

.PHONY: all audit build debug test generate
22 changes: 12 additions & 10 deletions cmd/dp-deployer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func main() {
}

log.Info(ctx, "shutdown with timeout:", log.Data{"Timeout": cfg.GracefulShutdownTimeout})
shutdownContext, cancel := context.WithTimeout(context.Background(), cfg.GracefulShutdownTimeout)
shutdownContext, shutdownCtxCancel := context.WithTimeout(context.Background(), cfg.GracefulShutdownTimeout)

go func() {

Expand All @@ -157,16 +157,18 @@ func main() {

e.Close()

<-shutdownContext.Done()
if shutdownContext.Err() == context.DeadlineExceeded {
log.Error(shutdownContext, "shutdown timeout", shutdownContext.Err())
os.Exit(1)
} else {
log.Error(shutdownContext, "done shutdown gracefully", errors.New("done shutdown gracefully"), log.Data{"context": shutdownContext.Err()})
os.Exit(0)
}

shutdownCtxCancel()
}()

<-shutdownContext.Done()
if shutdownContext.Err() == context.DeadlineExceeded {
log.Error(shutdownContext, "shutdown timeout", shutdownContext.Err())
os.Exit(1)
} else {
log.Error(shutdownContext, "done shutdown gracefully", errors.New("done shutdown gracefully"), log.Data{"context": shutdownContext.Err()})
os.Exit(0)
}

}

// TODO: remove once new queue implemented fully
Expand Down
7 changes: 4 additions & 3 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import (
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/clearsign"

"github.com/cenkalti/backoff"

"github.com/ONSdigital/dp-deployer/config"
ssqs "github.com/ONSdigital/dp-ssqs"
"github.com/ONSdigital/go-ns/common"
"github.com/ONSdigital/goamz/aws"
"github.com/ONSdigital/goamz/sqs"
"github.com/ONSdigital/log.go/v2/log"
"github.com/cenkalti/backoff"
"github.com/goamz/goamz/aws"
"github.com/goamz/goamz/sqs"
)

// maxConcurrentHandlers limit on goroutines (each handling a message)
Expand Down
26 changes: 21 additions & 5 deletions engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ package engine
import (
"context"
"errors"
"net/http"
"os"
"sync"
"testing"
"time"

"github.com/ONSdigital/dp-deployer/config"
"github.com/ONSdigital/go-ns/common"
. "github.com/smartystreets/goconvey/convey"

ssqs "github.com/ONSdigital/dp-ssqs"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/aws/aws-sdk-go/service/sqs/sqsiface"

"github.com/ONSdigital/dp-deployer/config"
ssqs "github.com/ONSdigital/dp-ssqs"
"github.com/ONSdigital/go-ns/common"
goamz "github.com/ONSdigital/goamz/aws"

. "github.com/smartystreets/goconvey/convey"
)

type handlerError struct {
Expand Down Expand Up @@ -141,6 +144,13 @@ var (

var defaultErrHandler = ErrHandler

type BadTransport struct{}

func (nowt BadTransport) RoundTrip(*http.Request) (*http.Response, error) {
resp := &http.Response{}
return resp, nil
}

func TestNew(t *testing.T) {
os.Clearenv()
os.Setenv("AWS_CREDENTIAL_FILE", "/i/hope/this/path/does/not/exist")
Expand Down Expand Up @@ -219,6 +229,12 @@ func TestNew(t *testing.T) {
},
}

goamz.RetryingClient = &http.Client{
// force the goamz http call (to AWS to get auth details for an instance role)
// to return failure and hence stops auth succeeding inside CI (when we need it to fail)
Transport: BadTransport{},
}

for _, fixture := range fixtures {
Convey("an error is returned with invalid configuration", t, func() {
e, err := New(fixture.config, nil)
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ require (
github.com/ONSdigital/dp-ssqs v0.0.0-20170720062323-643bf97d9e14
github.com/ONSdigital/dp-vault v1.1.1
github.com/ONSdigital/go-ns v0.0.0-20210831102424-ebdecc20fe9e
github.com/ONSdigital/goamz v0.0.0-20211118152127-9b03aca7c244
github.com/ONSdigital/log.go/v2 v2.0.9
github.com/aws/aws-sdk-go v1.42.6
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/goamz/goamz v0.0.0-20180131231218-8b901b531db8
github.com/goamz/goamz v0.0.0-20180131231218-8b901b531db8 // indirect
github.com/gorilla/mux v1.8.0
github.com/hashicorp/nomad v1.2.6
github.com/hashicorp/nomad/api v0.0.0-20210902134234-9ba1a2fba7d6
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ github.com/ONSdigital/dp-vault v1.1.1/go.mod h1:ZcC6ZNL4c94JCKK4a8uVUcMBTiMg5zdn
github.com/ONSdigital/go-ns v0.0.0-20191104121206-f144c4ec2e58/go.mod h1:iWos35il+NjbvDEqwtB736pyHru0MPFE/LqcwkV1wDc=
github.com/ONSdigital/go-ns v0.0.0-20210831102424-ebdecc20fe9e h1:tXdCJg2SUi2vLPA22bgVmeAY+sp3pKtUsAboBrOkO5Y=
github.com/ONSdigital/go-ns v0.0.0-20210831102424-ebdecc20fe9e/go.mod h1:BCx4ULp5nT3dT7Mft5iMrp9439JG9lqIlc0JOPmsHTg=
github.com/ONSdigital/goamz v0.0.0-20211118152127-9b03aca7c244 h1:Tx+C7UsLqBIAEPos1wDU6OYGgnVe+XfdDKBalynmvwI=
github.com/ONSdigital/goamz v0.0.0-20211118152127-9b03aca7c244/go.mod h1:ZiA9nvPeQMNTmF0MUnmtBoYSGOOi16/4gB+5YKJgtGM=
github.com/ONSdigital/log.go v0.0.0-20191127134126-2a610b254f20/go.mod h1:BD7D8FWP1fzwUWsrCopEG72jl9cchCaVNIGSz6YvL+Y=
github.com/ONSdigital/log.go v1.0.0/go.mod h1:UnGu9Q14gNC+kz0DOkdnLYGoqugCvnokHBRBxFRpVoQ=
github.com/ONSdigital/log.go v1.0.1-0.20200805084515-ee61165ea36a/go.mod h1:dDnQATFXCBOknvj6ZQuKfmDhbOWf3e8mtV+dPEfWJqs=
Expand Down
4 changes: 2 additions & 2 deletions queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (
"github.com/ONSdigital/dp-deployer/message"
ssqs "github.com/ONSdigital/dp-ssqs"
"github.com/ONSdigital/go-ns/common"
"github.com/ONSdigital/goamz/aws"
"github.com/ONSdigital/goamz/sqs"
"github.com/ONSdigital/log.go/v2/log"
"github.com/cenkalti/backoff"
"github.com/goamz/goamz/aws"
"github.com/goamz/goamz/sqs"
)

// maxConcurrentHandlers limit on goroutines (each handling a message)
Expand Down
25 changes: 20 additions & 5 deletions queue/queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ package queue
import (
"context"
"errors"
"net/http"
"os"
"sync"
"testing"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/aws/aws-sdk-go/service/sqs/sqsiface"

"github.com/ONSdigital/dp-deployer/config"
"github.com/ONSdigital/dp-deployer/message"
ssqs "github.com/ONSdigital/dp-ssqs"
"github.com/ONSdigital/go-ns/common"
. "github.com/smartystreets/goconvey/convey"
goamz "github.com/ONSdigital/goamz/aws"

ssqs "github.com/ONSdigital/dp-ssqs"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/aws/aws-sdk-go/service/sqs/sqsiface"
. "github.com/smartystreets/goconvey/convey"
)

type handlerError struct {
Expand Down Expand Up @@ -144,6 +147,12 @@ var (

var defaultErrHandler = ErrHandler

type BadTransport struct{}

func (nowt BadTransport) RoundTrip(*http.Request) (*http.Response, error) {
resp := &http.Response{}
return resp, nil
}
func TestNew(t *testing.T) {
os.Clearenv()
os.Setenv("AWS_CREDENTIAL_FILE", "/i/hope/this/path/does/not/exist")
Expand Down Expand Up @@ -222,6 +231,12 @@ func TestNew(t *testing.T) {
},
}

goamz.RetryingClient = &http.Client{
// force the goamz http call (to AWS to get auth details for an instance role)
// to return failure and hence stops auth succeeding inside CI (when we need it to fail)
Transport: BadTransport{},
}

for _, fixture := range fixtures {
Convey("an error is returned with invalid configuration", t, func() {
q, err := New(fixture.config, nil)
Expand Down

0 comments on commit 0628ec9

Please sign in to comment.