| 1 | package activator |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os/exec" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/carlosframework/platform/internal/cgroup" |
| 10 | "github.com/carlosframework/platform/internal/hostbox" |
| 11 | "github.com/carlosframework/platform/internal/hostwrite" |
| 12 | ) |
| 13 | |
| 14 | type claimSpawnWork struct { |
| 15 | deny bool |
| 16 | calls *int |
| 17 | } |
| 18 | |
| 19 | func (w claimSpawnWork) With(_ context.Context, _ hostwrite.Identity, fn func() error) error { |
| 20 | *w.calls++ |
| 21 | if w.deny { |
| 22 | return hostwrite.ErrUnavailable |
| 23 | } |
| 24 | return fn() |
| 25 | } |
| 26 | func TestClaimSpawnNeverFallsBack(t *testing.T) { |
| 27 | calls, builds := 0, 0 |
| 28 | build := func() *exec.Cmd { builds++; return exec.Command("must-not-execute") } |
| 29 | for _, deny := range []bool{true, false} { |
| 30 | cmd, prepared, err := spawnClaimed(context.Background(), true, nil, "shop.example", cgroup.TierDefault, claimSpawnWork{deny, &calls}, hostwrite.Identity{}, build) |
| 31 | if err == nil || cmd != nil || prepared != nil { |
| 32 | t.Fatal(cmd, prepared, err) |
| 33 | } |
| 34 | if !errors.Is(err, hostbox.ErrUnsupported) { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | } |
| 38 | if builds != 0 || calls != 0 { |
| 39 | t.Fatal(builds, calls) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // spawnClaimedGen is closed: an invalid argument answers |
| 44 | // hostwrite.ErrUnavailable, and every other call answers |
| 45 | // ErrClaimIsolationUnavailable, whatever the work would decide. It never |
| 46 | // enters the host write, never calls before and never builds the command. |
| 47 | func TestClaimSpawnGenNeverFallsBack(t *testing.T) { |
| 48 | calls, builds := 0, 0 |
| 49 | beforeCalled := false |
| 50 | build := func() *exec.Cmd { builds++; return exec.Command("must-not-execute") } |
| 51 | before := func() error { beforeCalled = true; return nil } |
| 52 | |
| 53 | if _, _, err := spawnClaimedGen(context.Background(), nil, 1, cgroup.TierDefault, claimSpawnWork{false, &calls}, hostwrite.Identity{}, build, before); !errors.Is(err, hostwrite.ErrUnavailable) { |
| 54 | t.Fatalf("nil parent: %v", err) |
| 55 | } |
| 56 | if _, _, err := spawnClaimedGen(context.Background(), &cgroup.Prepared{}, 0, cgroup.TierDefault, claimSpawnWork{false, &calls}, hostwrite.Identity{}, build, before); !errors.Is(err, hostwrite.ErrUnavailable) { |
| 57 | t.Fatalf("generation 0: %v", err) |
| 58 | } |
| 59 | if calls != 0 { |
| 60 | t.Fatalf("host write entered %d times for invalid arguments", calls) |
| 61 | } |
| 62 | for _, deny := range []bool{true, false} { |
| 63 | beforeCalled = false |
| 64 | cmd, child, err := spawnClaimedGen(context.Background(), &cgroup.Prepared{}, 7, cgroup.TierDefault, claimSpawnWork{deny, &calls}, hostwrite.Identity{}, build, before) |
| 65 | if cmd != nil || child != nil { |
| 66 | t.Fatalf("deny=%v: cmd=%v child=%v", deny, cmd, child) |
| 67 | } |
| 68 | if !errors.Is(err, ErrClaimIsolationUnavailable) { |
| 69 | t.Fatalf("deny=%v: err=%v, want ErrClaimIsolationUnavailable", deny, err) |
| 70 | } |
| 71 | if beforeCalled { |
| 72 | t.Fatalf("deny=%v: before was called", deny) |
| 73 | } |
| 74 | } |
| 75 | if builds != 0 || calls != 0 { |
| 76 | t.Fatalf("builds=%d calls=%d, want 0 and 0", builds, calls) |
| 77 | } |
| 78 | } |
| 79 | |