Download this file
| 1 | //go:build unix |
| 2 | |
| 3 | package hostguard |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "errors" |
| 8 | "github.com/carlosframework/platform/internal/hostclaim" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "testing" |
| 12 | "time" |
| 13 | ) |
| 14 | |
| 15 | func TestWithLocalSerializesWithoutClaimAuthority(t *testing.T) { |
| 16 | dir := t.TempDir() |
| 17 | locks := t.TempDir() |
| 18 | if err := os.Mkdir(filepath.Join(dir, "hostguard"), 0770); err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | g := Guard{DataDir: dir, LockDir: locks} |
| 22 | entered := make(chan struct{}) |
| 23 | release := make(chan struct{}) |
| 24 | done := make(chan error, 1) |
| 25 | go func() { |
| 26 | done <- g.WithLocal(t.Context(), "shop.example.com", func() error { close(entered); <-release; return nil }) |
| 27 | }() |
| 28 | <-entered |
| 29 | ctx, cancel := context.WithTimeout(t.Context(), 25*time.Millisecond) |
| 30 | defer cancel() |
| 31 | called := false |
| 32 | err := g.WithLocal(ctx, "shop.example.com", func() error { called = true; return nil }) |
| 33 | close(release) |
| 34 | if first := <-done; first != nil { |
| 35 | t.Fatal(first) |
| 36 | } |
| 37 | if !errors.Is(err, context.DeadlineExceeded) || called { |
| 38 | t.Fatalf("concurrent local write: %v %v", err, called) |
| 39 | } |
| 40 | // No Claims reader is configured: WithLocal may clean up, but With cannot |
| 41 | // turn the same lock into installation authority. |
| 42 | if err := g.With(t.Context(), "shop.example.com", Captured{}, func(_ *hostclaim.Claim) error { return nil }, func() error { return nil }); !errors.Is(err, ErrNoClaims) { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | } |
| 46 | |