carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1//go:build linux
2
3package activator
4
5import (
6 "context"
7 "errors"
8 "os"
9 "os/exec"
10 "path/filepath"
11 "testing"
12
13 "github.com/carlosframework/platform/internal/cgroup"
14 "github.com/carlosframework/platform/internal/hostbox"
15 "github.com/carlosframework/platform/internal/hostclaim"
16 "github.com/carlosframework/platform/internal/hostguard"
17 "github.com/carlosframework/platform/internal/hostwrite"
18)
19
20func TestLaunchExecutableValidationPrecedesChild(t *testing.T) {
21 ctx := t.Context()
22 j, err := hostbox.OpenLaunchJournal(filepath.Join(t.TempDir(), "launch.db"))
23 if err != nil {
24 t.Fatal(err)
25 }
26 defer j.Close()
27 target := hostguard.FenceTarget{Host: "app.test", Incarnation: "inc", Stamp: 1, Session: 1, Box: "box", Enrollment: "enroll", BootID: "boot", ControllerID: "ctrl"}
28 if err := j.Prelaunch(ctx, hostbox.LaunchInventory{Target: target, ControllerPID: 42, ProcessStart: "start", Groups: []hostbox.LaunchGroup{{RelativePath: "parent", Device: 1, Inode: 1}}, NoState: true}); err != nil {
29 t.Fatal(err)
30 }
31 work, _ := (hostwrite.Config{}).Select(ctx, target.Host, "spawn")
32 invalid := errors.New("invalid executable")
33 checked := false
34 l := RoleLaunch{Parent: &cgroup.Prepared{}, ParentPath: "parent", Target: target, Work: work, Journal: j, RegisterProducer: func(context.Context, string) (RoleProducer, error) {
35 t.Fatal("registered invalid executable")
36 return RoleProducer{}, nil
37 }, Executable: func(context.Context, *exec.Cmd) (*os.File, error) { checked = true; return nil, invalid }}
38 attempt := hostclaim.WakeRetryAttempt{ID: "a", InvocationID: "inv", LaunchInputs: "inputs", Session: 1, Box: "box", Enrollment: "enroll", BootID: "boot", ControllerID: "ctrl"}
39 _, child, _, err := l.prepareRoleReservation(ctx, "main", attempt, func() *exec.Cmd { return exec.Command("unused") })
40 if !errors.Is(err, invalid) || !checked || child != nil {
41 t.Fatalf("checked=%v child=%v err=%v", checked, child, err)
42 }
43 in, err := j.Read(ctx, target)
44 if err != nil || len(in.Reservations) != 0 || len(in.Groups) != 1 {
45 t.Fatalf("invalid executable changed journal: %+v %v", in, err)
46 }
47}
48