From 8c98574edd21759f1882d0ec4240f562039913fd Mon Sep 17 00:00:00 2001 From: Nick White Date: Tue, 13 Jul 2021 12:08:20 +0100 Subject: Fix up tests a bit --- aws.go | 26 +++++++++++++++----------- internal/pipeline/pipeline_test.go | 11 ++++++----- local.go | 5 +++++ 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/aws.go b/aws.go index b1681f1..21a215c 100644 --- a/aws.go +++ b/aws.go @@ -51,6 +51,7 @@ type AwsConn struct { downloader *s3manager.Downloader uploader *s3manager.Uploader wipequrl, prequrl, ocrpgqurl, analysequrl string + testqurl string wipstorageid string } @@ -128,6 +129,19 @@ func (a *AwsConn) Init() error { return nil } +// TestInit initialises extra aws services needed for running tests. +func (a *AwsConn) TestInit() error { + a.Logger.Println("Getting test queue URL") + result, err := a.sqssvc.GetQueueUrl(&sqs.GetQueueUrlInput{ + QueueName: aws.String(queueTest), + }) + if err != nil { + return errors.New(fmt.Sprintf("Error getting test queue URL: %s\n", err)) + } + a.testqurl = *result.QueueUrl + return nil +} + func (a *AwsConn) CheckQueue(url string, timeout int64) (Qmsg, error) { msgResult, err := a.sqssvc.ReceiveMessage(&sqs.ReceiveMessageInput{ MaxNumberOfMessages: aws.Int64(1), @@ -337,17 +351,7 @@ func (a *AwsConn) WIPStorageId() string { } func (a *AwsConn) TestQueueId() string { - // We don't bother saving the queue id in Init(), as it's only - // used for testing - a.Logger.Println("Getting test queue URL") - result, err := a.sqssvc.GetQueueUrl(&sqs.GetQueueUrlInput{ - QueueName: aws.String(queueTest), - }) - if err != nil { - a.Logger.Printf("Error getting test queue URL: %s\n", err) - return "" - } - return *result.QueueUrl + return a.testqurl } func (a *AwsConn) ListObjects(bucket string, prefix string) ([]string, error) { diff --git a/internal/pipeline/pipeline_test.go b/internal/pipeline/pipeline_test.go index 4a08d3b..73e8223 100644 --- a/internal/pipeline/pipeline_test.go +++ b/internal/pipeline/pipeline_test.go @@ -30,6 +30,7 @@ func (t *StrLog) Write(p []byte) (n int, err error) { type PipelineTester interface { Pipeliner + TestInit() error DeleteObjects(bucket string, keys []string) error TestQueueId() string } @@ -309,6 +310,10 @@ func Test_upAndQueue(t *testing.T) { if err != nil { t.Fatalf("Could not initialise %s connection: %v\nLog: %s", conn.name, err, slog.log) } + err = conn.c.TestInit() + if err != nil { + t.Fatalf("Failed in test initialisation for %s: %v\nLog: %s", conn.name, err, slog.log) + } slog.log = "" tempDir := filepath.Join(os.TempDir(), "pipelinetest") err = os.MkdirAll(tempDir, 0700) @@ -390,11 +395,7 @@ func Test_upAndQueue(t *testing.T) { t.Fatalf("Uploaded file differs from expected, expected: '%s', got '%s'\nLog: %s", c.contents, dled, slog.log) } - target, err := filepath.Rel(os.TempDir(), filepath.Join(tempDir, c.ul)) - if err != nil { - t.Fatalf("Error removing TempDir prefix: %v", err) - } - queueExpected := target + " test" + queueExpected := "pipelinetest/" + c.ul + " test" if msg.Body != queueExpected { _ = conn.c.DelFromQueue(queueurl, msg.Handle) t.Fatalf("Queue contents not as expected, expected: '%s', got '%s'\nLog: %s", queueExpected, msg.Body, slog.log) diff --git a/local.go b/local.go index 463fb61..615f9a6 100644 --- a/local.go +++ b/local.go @@ -64,6 +64,11 @@ func (a *LocalConn) Init() error { return nil } +// TestInit does nothing for local connections +func (a *LocalConn) TestInit() error { + return nil +} + // CheckQueue checks for any messages in a queue func (a *LocalConn) CheckQueue(url string, timeout int64) (Qmsg, error) { f, err := os.Open(filepath.Join(a.TempDir, url)) -- cgit v1.2.1-24-ge1ad