github.com/release-engineering/exodus-rsync@v1.11.2/internal/gw/client_upload_errors_test.go (about)

     1  package gw
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/release-engineering/exodus-rsync/internal/args"
    11  	"github.com/release-engineering/exodus-rsync/internal/log"
    12  	"github.com/release-engineering/exodus-rsync/internal/walk"
    13  )
    14  
    15  type setupBlobs func(blobMap)
    16  
    17  func defaultBlobs(blobMap) {}
    18  
    19  func typicalError(blobs blobMap) {
    20  	// Interacting with this blob gives an error.
    21  	blobs["abc123"] = []error{fmt.Errorf("simulated error")}
    22  }
    23  
    24  func putError(blobs blobMap) {
    25  	// Querying this blob says it doesn't exist, but then uploading it fails.
    26  	blobs["abc123"] = []error{
    27  		awserr.New("NotFound", "not found", nil), // HEAD succeeds and says object doesn't exist
    28  		fmt.Errorf("simulated error"),            // PUT fails
    29  	}
    30  }
    31  
    32  func TestClientUploadErrors(t *testing.T) {
    33  	client, s3 := newClientWithFakeS3(t)
    34  
    35  	chdirInTest(t, "../../test/data/srctrees/just-files")
    36  
    37  	ctx := context.Background()
    38  	ctx = log.NewContext(ctx, log.Package.NewLogger(args.Config{}))
    39  
    40  	tests := []struct {
    41  		name      string
    42  		items     []walk.SyncItem
    43  		setup     setupBlobs
    44  		wantError string
    45  	}{
    46  
    47  		{"error checking blob",
    48  			[]walk.SyncItem{{SrcPath: "some-file", Key: "abc123"}},
    49  			typicalError,
    50  			"checking for presence of abc123: simulated error"},
    51  		{"nonexistent file",
    52  			[]walk.SyncItem{{SrcPath: "nonexistent-file", Key: "abc123"}},
    53  			defaultBlobs,
    54  			"open nonexistent-file: no such file or directory"},
    55  		{"PUT fails",
    56  			[]walk.SyncItem{{SrcPath: "hello-copy-one", Key: "abc123"}},
    57  			putError,
    58  			"upload hello-copy-one: simulated error"},
    59  	}
    60  
    61  	for _, tt := range tests {
    62  		t.Run(tt.name, func(t *testing.T) {
    63  			s3.reset()
    64  			tt.setup(s3.blobs)
    65  
    66  			err := client.EnsureUploaded(ctx, tt.items, func(item walk.SyncItem) error {
    67  				t.Fatal("unexpectedly uploaded something", item)
    68  				return nil
    69  			}, func(item walk.SyncItem) error {
    70  				t.Fatal("unexpectedly found blob", item)
    71  				return nil
    72  			}, func(item walk.SyncItem) error {
    73  				t.Fatal("unexpectedly created duplicate blob", item)
    74  				return nil
    75  			})
    76  
    77  			// It should tell us why it failed
    78  			if !strings.Contains(fmt.Sprint(err), tt.wantError) {
    79  				t.Errorf("did not get expected error, got err = %v", err)
    80  			}
    81  		})
    82  	}
    83  
    84  }