| 1 | package objstore |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestMemProvisionerIdempotence(t *testing.T) { |
| 10 | ctx := context.Background() |
| 11 | p := NewMemProvisioner("eu-west-1") |
| 12 | for i := 0; i < 2; i++ { // every verb is safe to repeat |
| 13 | if region, err := p.EnsureBucket(ctx, "b1"); err != nil || region != "eu-west-1" { |
| 14 | t.Fatalf("EnsureBucket #%d: %q %v", i, region, err) |
| 15 | } |
| 16 | if arn, err := p.EnsureUser(ctx, "u1", "/carlos-store/", "arn:aws:s3:::b1", nil); err != nil || arn == "" { |
| 17 | t.Fatalf("EnsureUser #%d: %v", i, err) |
| 18 | } |
| 19 | } |
| 20 | id1, sec1, err := p.MintKey(ctx, "u1") |
| 21 | if err != nil || id1 == "" || sec1 == "" { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | id2, _, err := p.MintKey(ctx, "u1") |
| 25 | if err != nil || id2 == id1 { |
| 26 | t.Fatalf("second mint: %q vs %q, %v", id2, id1, err) |
| 27 | } |
| 28 | if err := p.DeleteKey(ctx, "u1", id1); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | if err := p.DeleteKey(ctx, "u1", id1); err != nil { // already gone == success |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | id3, _, err := p.MintKey(ctx, "u1") |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | if err := p.ReapKeys(ctx, "u1", []string{id3}); err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | if got := len(p.Users["u1"].Keys); got != 1 { // id2 reaped, id3 kept |
| 42 | t.Fatalf("after reap: %d keys", got) |
| 43 | } |
| 44 | if err := p.EmptyBucket(ctx, "b1"); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | if err := p.DeleteBucket(ctx, "b1"); err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | if err := p.DeleteBucket(ctx, "b1"); err != nil { // already gone == success |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | if err := p.DeleteUser(ctx, "u1"); err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | if err := p.DeleteUser(ctx, "u1"); err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func TestMemProvisionerFailNext(t *testing.T) { |
| 62 | ctx := context.Background() |
| 63 | p := NewMemProvisioner("eu-west-1") |
| 64 | boom := errors.New("boom") |
| 65 | p.FailNext["EnsureBucket"] = boom |
| 66 | if _, err := p.EnsureBucket(ctx, "b1"); !errors.Is(err, boom) { |
| 67 | t.Fatalf("want injected failure, got %v", err) |
| 68 | } |
| 69 | if _, err := p.EnsureBucket(ctx, "b1"); err != nil { // one-shot |
| 70 | t.Fatalf("retry should succeed: %v", err) |
| 71 | } |
| 72 | } |
| 73 | |