From 4b7a4bd1b87d1dbba283e577aa03ce7e390d85d8 Mon Sep 17 00:00:00 2001 From: Nick White Date: Mon, 2 Aug 2021 10:45:53 +0100 Subject: internal/pipeline: Add test (incomplete but working) for UploadImages --- internal/pipeline/pipeline_test.go | 1 + internal/pipeline/put.go | 7 +++++-- internal/pipeline/put_test.go | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) (limited to 'internal') diff --git a/internal/pipeline/pipeline_test.go b/internal/pipeline/pipeline_test.go index 73e8223..fc223b3 100644 --- a/internal/pipeline/pipeline_test.go +++ b/internal/pipeline/pipeline_test.go @@ -33,6 +33,7 @@ type PipelineTester interface { TestInit() error DeleteObjects(bucket string, keys []string) error TestQueueId() string + TestStorageId() string } type connection struct { diff --git a/internal/pipeline/put.go b/internal/pipeline/put.go index c08df53..26d581c 100644 --- a/internal/pipeline/put.go +++ b/internal/pipeline/put.go @@ -77,7 +77,10 @@ func DetectQueueType(dir string, conn Queuer) string { } } -func UploadImages(dir string, bookname string, conn Uploader) error { +// UploadImages uploads all files (except those which start with a ".") +// from a directory (recursively) into a given storage id, prefixed with +// the given bookname and a slash +func UploadImages(dir string, bookname string, conn Uploader, storageId string) error { walker := make(fileWalk) go func() { _ = filepath.Walk(dir, walker.Walk) @@ -86,7 +89,7 @@ func UploadImages(dir string, bookname string, conn Uploader) error { for path := range walker { name := filepath.Base(path) - err := conn.Upload(conn.WIPStorageId(), filepath.Join(bookname, name), path) + err := conn.Upload(storageId, bookname + "/" + name, path) if err != nil { return fmt.Errorf("Failed to upload %s: %v", path, err) } diff --git a/internal/pipeline/put_test.go b/internal/pipeline/put_test.go index af18ab4..912a4ef 100644 --- a/internal/pipeline/put_test.go +++ b/internal/pipeline/put_test.go @@ -6,6 +6,7 @@ package pipeline import ( "errors" + "log" "os" "rescribe.xyz/bookpipeline" "testing" @@ -73,3 +74,38 @@ func Test_DetectQueueType(t *testing.T) { }) } } + +func Test_UploadImages(t *testing.T) { + var slog StrLog + vlog := log.New(&slog, "", 0) + var conns []connection + + conns = append(conns, connection{name: "local", c: &bookpipeline.LocalConn{Logger: vlog}}) + + if !testing.Short() { + conns = append(conns, connection{name: "aws", c: &bookpipeline.AwsConn{Logger: vlog}}) + } + + for _, conn := range conns { + t.Run(conn.name, func(t *testing.T) { + err := conn.c.Init() + 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 initialisalisation for %s: %v\nLog: %s", conn.name, err, slog.log) + } + slog.log = "" + + err = UploadImages("testdata/good", "good", conn.c, conn.c.TestStorageId()) + if err != nil { + t.Fatalf("Error in UploadImages for %s: %v\nLog: %s", conn.name, err, slog.log) + } + + // TODO: decide on whether to use TestStorageId or just the WIPStorageId as with other tests, and align other tests to this if needed + // TODO: download all files and test that they match + // TODO: remove test files from conn storage + }) + } +} -- cgit v1.2.1-24-ge1ad