| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // Fable L8 on 49d8df3: "storage is full" is the platform refusing the |
| 13 | // Put (403), read from the response's status, never from text that |
| 14 | // happens to hold "403". |
| 15 | func TestStorageIsFullOnlyWhenThePutIsRefused(t *testing.T) { |
| 16 | for status, full := range map[int]bool{ |
| 17 | http.StatusForbidden: true, http.StatusInternalServerError: false} { |
| 18 | bucket := httptest.NewServer(http.HandlerFunc( |
| 19 | func(writer http.ResponseWriter, request *http.Request) { |
| 20 | http.Error(writer, "request 403 of the day failed", status) |
| 21 | })) |
| 22 | objects, err := bucketFrom(func(name string) string { |
| 23 | return map[string]string{"CARLOS_STORE_BUCKET": "mapa", |
| 24 | "CARLOS_STORE_REGION": "eu-west-1", |
| 25 | "CARLOS_STORE_ENDPOINT": bucket.URL, |
| 26 | "CARLOS_STORE_ACCESS_KEY": "key", |
| 27 | "CARLOS_STORE_SECRET_KEY": "secret"}[name] |
| 28 | }) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | store, _, _ := imageRig(t) |
| 33 | store.UseImages(ImageSetup{Objects: objects}) |
| 34 | _, err = store.UploadImage(ctx(), ana, bytes.NewReader(picture(t, 1))) |
| 35 | if errors.Is(err, ErrStorageFull) != full { |
| 36 | t.Errorf("a %d: %v", status, err) |
| 37 | } |
| 38 | bucket.Close() |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // Fable N5 on 4c95863: a Put that fails on the wire names no presigned |
| 43 | // URL. *url.Error's text holds the address, signature and all, and the |
| 44 | // upload's error is logged. |
| 45 | func TestAFailedPutNamesNoSignedAddress(t *testing.T) { |
| 46 | closed := httptest.NewServer(http.NotFoundHandler()) |
| 47 | closed.Close() |
| 48 | objects, err := bucketFrom(func(name string) string { |
| 49 | return map[string]string{"CARLOS_STORE_BUCKET": "mapa", |
| 50 | "CARLOS_STORE_REGION": "eu-west-1", |
| 51 | "CARLOS_STORE_ENDPOINT": closed.URL, |
| 52 | "CARLOS_STORE_ACCESS_KEY": "key", |
| 53 | "CARLOS_STORE_SECRET_KEY": "secret"}[name] |
| 54 | }) |
| 55 | if err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | store, _, _ := imageRig(t) |
| 59 | store.UseImages(ImageSetup{Objects: objects}) |
| 60 | _, err = store.UploadImage(ctx(), ana, bytes.NewReader(picture(t, 1))) |
| 61 | if err == nil || strings.Contains(err.Error(), "X-Amz-") { |
| 62 | t.Fatalf("the error: %v", err) |
| 63 | } |
| 64 | } |
| 65 | |