package host import ( "context" "errors" "net/http/httptest" "testing" "time" "github.com/carlosframework/platform/internal/activator" "github.com/carlosframework/platform/internal/hostbox" "github.com/carlosframework/platform/internal/hostclaim" "github.com/carlosframework/platform/internal/lease" "github.com/carlosframework/platform/internal/lease/leasetest" ) func TestClaimEmptyBootstrapWithRealLease(t *testing.T) { f := plan9D5Fixture(t) handler, _ := leasetest.NewS3Server(f.bucket) server := httptest.NewServer(handler) defer server.Close() 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} { t.Setenv(k, v) } created := plan9D5Observed(hostclaim.Holder{Stamp: 3}) in, err := f.owners.Select(t.Context(), plan9D5Route, created, activator.StartPolicy{MaxStarts: 5, Window: 30 * time.Second}) if err != nil { t.Fatal(err) } held, err := lease.Acquire(t.Context(), "data", created.Claim.Holder.Location, "eu-west-1") if err != nil { t.Fatal(err) } defer func() { if err := held.Release(context.Background()); err != nil { t.Error(err) } }() 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"}}}) if err := f.owners.EmptyAuthority(t.Context(), admitted, in); err != nil { t.Fatal("first writer holding its lease", err) } for _, suffix := range []string{"generation.json", "g-foreign/lock.json", "lock.json/child", "lock.json.old"} { key := created.Claim.Holder.Location + "/" + suffix id := f.bucket.Put("data", key, []byte("foreign")) if err := f.owners.EmptyAuthority(t.Context(), admitted, in); !errors.Is(err, hostbox.ErrChanged) { t.Fatalf("accepted %s: %v", suffix, err) } if err := f.bucket.DeleteVersion(t.Context(), "data", key, id); err != nil { t.Fatal(err) } } }