diff --git a/Makefile b/Makefile index ac9bcfd..5bdecd9 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/cmd/dp-deployer/main.go b/cmd/dp-deployer/main.go index 381365d..a918c3a 100644 --- a/cmd/dp-deployer/main.go +++ b/cmd/dp-deployer/main.go @@ -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() { @@ -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 diff --git a/engine/engine.go b/engine/engine.go index c6b2a02..1af8523 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -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) diff --git a/engine/engine_test.go b/engine/engine_test.go index ca3a31e..4a4ff5d 100644 --- a/engine/engine_test.go +++ b/engine/engine_test.go @@ -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 { @@ -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") @@ -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) diff --git a/go.mod b/go.mod index 75983ba..e76f303 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 28a7b21..c2b34a5 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/queue/queue.go b/queue/queue.go index a7964b6..db5f342 100644 --- a/queue/queue.go +++ b/queue/queue.go @@ -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) diff --git a/queue/queue_test.go b/queue/queue_test.go index 8c49452..39e1101 100644 --- a/queue/queue_test.go +++ b/queue/queue_test.go @@ -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 { @@ -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") @@ -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)