github.com/release-engineering/exodus-rsync@v1.11.2/internal/gw/client_upload_test.go (about) 1 package gw 2 3 import ( 4 "context" 5 "fmt" 6 "reflect" 7 "testing" 8 9 "github.com/release-engineering/exodus-rsync/internal/args" 10 "github.com/release-engineering/exodus-rsync/internal/log" 11 "github.com/release-engineering/exodus-rsync/internal/walk" 12 ) 13 14 func TestClientTypicalUpload(t *testing.T) { 15 client, _ := newClientWithFakeS3(t) 16 17 chdirInTest(t, "../../test/data/srctrees/just-files") 18 19 ctx := context.Background() 20 ctx = log.NewContext(ctx, log.Package.NewLogger(args.Config{})) 21 22 // Note: these files have to actually exist because they will be 23 // opened by the client for sending. However this test does not require 24 // the key to match the actual checksums. 25 items := []walk.SyncItem{ 26 {SrcPath: "hello-copy-one", Key: "abc123"}, 27 {SrcPath: "hello-copy-two", Key: "abc123"}, 28 {SrcPath: "subdir/some-binary", Key: "aabbcc"}, 29 } 30 31 uploaded := make([]walk.SyncItem, 0) 32 present := make([]walk.SyncItem, 0) 33 duplicate := make([]walk.SyncItem, 0) 34 35 err := client.EnsureUploaded(ctx, items, func(item walk.SyncItem) error { 36 uploaded = append(uploaded, item) 37 return nil 38 }, func(item walk.SyncItem) error { 39 present = append(present, item) 40 return nil 41 }, func(item walk.SyncItem) error { 42 duplicate = append(duplicate, item) 43 return nil 44 }) 45 46 if err != nil { 47 t.Errorf("got unexpected error %v", err) 48 } 49 50 // It should have uploaded these 51 if !reflect.DeepEqual(uploaded, []walk.SyncItem{items[0], items[2]}) && !reflect.DeepEqual(uploaded, []walk.SyncItem{items[2], items[0]}) { 52 t.Errorf("unexpected set of uploaded items: %v", uploaded) 53 } 54 55 // While this one counts as already present, since there was a duplicate 56 // object in the publish and only the first is uploaded. 57 if !reflect.DeepEqual(duplicate, []walk.SyncItem{items[1]}) { 58 t.Errorf("unexpected set of present items: %v", present) 59 } 60 } 61 62 func TestClientUploadWithLinks(t *testing.T) { 63 client, _ := newClientWithFakeS3(t) 64 65 chdirInTest(t, "../../test/data/srctrees/links") 66 67 ctx := context.Background() 68 ctx = log.NewContext(ctx, log.Package.NewLogger(args.Config{})) 69 70 // Note: these files have to actually exist because they will be 71 // opened by the client for sending. However this test does not require 72 // the key to match the actual checksums. 73 items := []walk.SyncItem{ 74 {SrcPath: "link-to-regular-file", LinkTo: "subdir/regular-file"}, 75 {SrcPath: "subdir/rand2", LinkTo: "../../../rand2"}, 76 {SrcPath: "subdir/regular-file", Key: "5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03"}, 77 {SrcPath: "subdir2/dir-link", LinkTo: "../subdir"}, 78 {SrcPath: "subdir/rand1", LinkTo: "../../../rand1:"}, 79 } 80 81 uploaded := make([]walk.SyncItem, 0) 82 present := make([]walk.SyncItem, 0) 83 duplicate := make([]walk.SyncItem, 0) 84 85 err := client.EnsureUploaded(ctx, items, func(item walk.SyncItem) error { 86 uploaded = append(uploaded, item) 87 return nil 88 }, func(item walk.SyncItem) error { 89 present = append(present, item) 90 return nil 91 }, func(item walk.SyncItem) error { 92 duplicate = append(duplicate, item) 93 return nil 94 }) 95 96 if err != nil { 97 t.Errorf("got unexpected error %v", err) 98 } 99 100 // It should have uploaded just the one 101 if !reflect.DeepEqual(uploaded, []walk.SyncItem{items[2]}) { 102 t.Errorf("unexpected set of uploaded items: %v", uploaded) 103 } 104 } 105 106 func TestClientPresentItem(t *testing.T) { 107 client, s3 := newClientWithFakeS3(t) 108 109 chdirInTest(t, "../../test/data/srctrees/just-files") 110 111 ctx := context.Background() 112 ctx = log.NewContext(ctx, log.Package.NewLogger(args.Config{})) 113 114 items := []walk.SyncItem{ 115 {SrcPath: "existing-blob", Key: "a1b2c3"}, 116 {SrcPath: "hello-copy-one", Key: "abc123"}, 117 {SrcPath: "hello-copy-two", Key: "abc123"}, 118 {SrcPath: "subdir/some-binary", Key: "aabbcc"}, 119 } 120 121 uploaded := make([]walk.SyncItem, 0) 122 present := make([]walk.SyncItem, 0) 123 duplicate := make([]walk.SyncItem, 0) 124 125 s3.blobs = blobMap{"a1b2c3": []error{nil}} 126 127 err := client.EnsureUploaded(ctx, items, func(item walk.SyncItem) error { 128 uploaded = append(uploaded, item) 129 return nil 130 }, func(item walk.SyncItem) error { 131 present = append(present, item) 132 return nil 133 }, func(item walk.SyncItem) error { 134 duplicate = append(duplicate, item) 135 return nil 136 }) 137 138 if err != nil { 139 t.Errorf("got expected error %v", err) 140 } 141 // It should determine that we should upload only two items 142 if !reflect.DeepEqual(uploaded, []walk.SyncItem{items[1], items[3]}) && !reflect.DeepEqual(uploaded, []walk.SyncItem{items[3], items[1]}) { 143 t.Errorf("unexpected set of uploaded items: %v", uploaded) 144 } 145 146 // Only one item already existed in the s3 bucket 147 if !reflect.DeepEqual(present, []walk.SyncItem{items[0]}) { 148 t.Errorf("unexpected set of uploaded items: %v", present) 149 } 150 151 // Only one item is already slated for upload 152 if !reflect.DeepEqual(duplicate, []walk.SyncItem{items[2]}) { 153 t.Errorf("unexpected set of uploaded items: %v", duplicate) 154 } 155 } 156 157 func TestClientUploadCallbackError(t *testing.T) { 158 client, _ := newClientWithFakeS3(t) 159 160 chdirInTest(t, "../../test/data/srctrees/just-files") 161 162 ctx := context.Background() 163 ctx = log.NewContext(ctx, log.Package.NewLogger(args.Config{})) 164 165 items := []walk.SyncItem{ 166 {SrcPath: "hello-copy-one", Key: "abc123"}, 167 {SrcPath: "hello-copy-two", Key: "abc123"}, 168 {SrcPath: "subdir/some-binary", Key: "aabbcc"}, 169 } 170 171 err := client.EnsureUploaded(ctx, items, func(item walk.SyncItem) error { 172 return fmt.Errorf("error from callback") 173 }, func(item walk.SyncItem) error { 174 return nil 175 }, func(item walk.SyncItem) error { 176 return nil 177 }) 178 179 if err.Error() != "error from callback" { 180 t.Errorf("did not get expected error, got: %v", err) 181 } 182 } 183 184 func TestClientUploadPresentCallbackError(t *testing.T) { 185 client, s3 := newClientWithFakeS3(t) 186 187 chdirInTest(t, "../../test/data/srctrees/just-files") 188 189 ctx := context.Background() 190 ctx = log.NewContext(ctx, log.Package.NewLogger(args.Config{})) 191 192 items := []walk.SyncItem{ 193 {SrcPath: "hello-copy-one", Key: "abc123"}, 194 {SrcPath: "hello-copy-two", Key: "abc123"}, 195 {SrcPath: "subdir/some-binary", Key: "aabbcc"}, 196 } 197 198 s3.blobs = blobMap{"aabbcc": []error{nil}} 199 200 err := client.EnsureUploaded(ctx, items, func(item walk.SyncItem) error { 201 return nil 202 }, func(item walk.SyncItem) error { 203 return fmt.Errorf("error from callback") 204 }, func(item walk.SyncItem) error { 205 return nil 206 }) 207 208 if err.Error() != "error from callback" { 209 t.Errorf("did not get expected error, got: %v", err) 210 } 211 } 212 213 func TestClientUploadDuplicateCallbackError(t *testing.T) { 214 client, _ := newClientWithFakeS3(t) 215 216 chdirInTest(t, "../../test/data/srctrees/just-files") 217 218 ctx := context.Background() 219 ctx = log.NewContext(ctx, log.Package.NewLogger(args.Config{})) 220 221 items := []walk.SyncItem{ 222 {SrcPath: "hello-copy-one", Key: "abc123"}, 223 {SrcPath: "hello-copy-two", Key: "abc123"}, 224 {SrcPath: "subdir/some-binary", Key: "aabbcc"}, 225 } 226 227 err := client.EnsureUploaded(ctx, items, func(item walk.SyncItem) error { 228 return nil 229 }, func(item walk.SyncItem) error { 230 return nil 231 }, func(item walk.SyncItem) error { 232 return fmt.Errorf("error from callback") 233 234 }) 235 236 if err.Error() != "error from callback" { 237 t.Errorf("did not get expected error, got: %v", err) 238 } 239 }