| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/http/httptest" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/carlosframework/platform/internal/activator" |
| 11 | "github.com/carlosframework/platform/internal/hostbox" |
| 12 | "github.com/carlosframework/platform/internal/hostclaim" |
| 13 | "github.com/carlosframework/platform/internal/lease" |
| 14 | "github.com/carlosframework/platform/internal/lease/leasetest" |
| 15 | ) |
| 16 | |
| 17 | func TestClaimEmptyBootstrapWithRealLease(t *testing.T) { |
| 18 | f := plan9D5Fixture(t) |
| 19 | handler, _ := leasetest.NewS3Server(f.bucket) |
| 20 | server := httptest.NewServer(handler) |
| 21 | defer server.Close() |
| 22 | for k, v := range map[string]string{"AWS_ACCESS_KEY_ID": "test-key", "AWS_SECRET_ACCESS_KEY": "test-secret", "AWS_SESSION_TOKEN": "", "AWS_EC2_METADATA_DISABLED": "true", "AWS_CONFIG_FILE": "/nonexistent", "AWS_SHARED_CREDENTIALS_FILE": "/nonexistent", "CARLOS_S3_ENDPOINT": server.URL} { |
| 23 | t.Setenv(k, v) |
| 24 | } |
| 25 | created := plan9D5Observed(hostclaim.Holder{Stamp: 3}) |
| 26 | in, err := f.owners.Select(t.Context(), plan9D5Route, created, activator.StartPolicy{MaxStarts: 5, Window: 30 * time.Second}) |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | held, err := lease.Acquire(t.Context(), "data", created.Claim.Holder.Location, "eu-west-1") |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | defer func() { |
| 35 | if err := held.Release(context.Background()); err != nil { |
| 36 | t.Error(err) |
| 37 | } |
| 38 | }() |
| 39 | admitted := plan9D5Observed(hostclaim.Holder{Stamp: 5, Session: 5, Generation: in.Admission.NewGeneration, WriterBox: "box-1", Admissions: []hostclaim.AdmissionRecord{{Session: 5, Box: "box-1", Generation: in.Admission.NewGeneration, Region: "ie", Mode: "cold"}}}) |
| 40 | if err := f.owners.EmptyAuthority(t.Context(), admitted, in); err != nil { |
| 41 | t.Fatal("first writer holding its lease", err) |
| 42 | } |
| 43 | for _, suffix := range []string{"generation.json", "g-foreign/lock.json", "lock.json/child", "lock.json.old"} { |
| 44 | key := created.Claim.Holder.Location + "/" + suffix |
| 45 | id := f.bucket.Put("data", key, []byte("foreign")) |
| 46 | if err := f.owners.EmptyAuthority(t.Context(), admitted, in); !errors.Is(err, hostbox.ErrChanged) { |
| 47 | t.Fatalf("accepted %s: %v", suffix, err) |
| 48 | } |
| 49 | if err := f.bucket.DeleteVersion(t.Context(), "data", key, id); err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |