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 && rootrunner
2
3package host
4
5import (
6 "context"
7 "encoding/json"
8 "errors"
9 "os"
10 "os/exec"
11 "path/filepath"
12 "testing"
13
14 "github.com/carlosframework/platform/internal/release"
15 "github.com/carlosframework/platform/internal/rootparticipation"
16 "github.com/carlosframework/platform/internal/rootstate"
17)
18
19func TestPlan14LaunchVerifierRootRunner(t *testing.T) {
20 dir := os.Getenv("PLAN14_E43_VERIFIER_CHILD")
21 if dir == "" {
22 dir = generationRootRunnerDir(t)
23 binary, err := filepath.Abs(os.Args[0])
24 if err != nil {
25 t.Fatal(err)
26 }
27 cmd := exec.CommandContext(t.Context(), "unshare", "--mount", "--pid", "--fork", "--mount-proc", binary, "-test.run=^TestPlan14LaunchVerifierRootRunner$", "-test.v", "-test.timeout=2m")
28 cmd.Dir = dir
29 cmd.Env = append(os.Environ(), "PLAN14_E43_VERIFIER_CHILD="+dir)
30 if out, err := cmd.CombinedOutput(); err != nil {
31 t.Fatalf("root verifier: %v\n%s", err, out)
32 }
33 return
34 }
35 t.Setenv("CARLOS_OPT_DIR", dir)
36 if err := os.MkdirAll(filepath.Join(dir, "versions"), 0755); err != nil {
37 t.Fatal(err)
38 }
39 state, err := rootstate.Open(filepath.Join(dir, "participation"), true, false)
40 if err != nil {
41 t.Fatal(err)
42 }
43 defer state.Close()
44 generations, err := rootstate.Open(filepath.Join(dir, "generations"), true, false)
45 if err != nil {
46 t.Fatal(err)
47 }
48 generations.Close()
49 x := &release.ReleaseExclusion{Open: func() (rootstate.Store, error) { return rootstate.Open(filepath.Join(dir, "release"), true, false) }}
50 l := rootparticipation.Ledger{State: state, ExcludeLaunches: x.With}
51 c := rootparticipation.Census{Schema: 1, Box: "box", Enrollment: "en", Migration: 1, Revision: 1, LaunchRevision: 1, Complete: true}
52 write := func() {
53 raw, _ := json.Marshal(c)
54 if err := state.Write("census.json", raw); err != nil {
55 t.Fatal(err)
56 }
57 }
58 write()
59 v := rootLaunchVerifier{exclude: x, open: func() (rootstate.Store, error) {
60 return rootstate.Open(filepath.Join(dir, "generations"), false, false)
61 }, reconcile: generationReconciler{
62 read: func(context.Context) (generationInventory, error) {
63 c, err := l.RecoveryCensus()
64 return generationInventory{Census: c}, err
65 }, kernel: productionGenerationKernelCensus,
66 }}
67 l.VerifyLaunches = v.verify
68 if err := rootparticipation.RegisterLaunchVerifier(v.verify, x.With); err != nil {
69 t.Fatal(err)
70 }
71 if err := rootparticipation.RegisterLaunchVerifier(v.verify, x.With); !errors.Is(err, rootparticipation.ErrUnavailable) {
72 t.Fatal("registered twice", err)
73 }
74 if err := l.Idle(t.Context()); !errors.Is(err, rootparticipation.ErrUnavailable) {
75 t.Fatal("missing token", err)
76 }
77 if err := l.ActivationReady(t.Context()); !errors.Is(err, rootparticipation.ErrUnavailable) {
78 t.Fatal("activation accepted missing token", err)
79 }
80 for _, name := range []string{"idle", "begin", "end", "recover", "activation"} {
81 t.Run(name, func(t *testing.T) {
82 if name == "recover" {
83 c.Pending = "acquisition"
84 write()
85 }
86 if err := x.With(t.Context(), func(ctx context.Context) error {
87 switch name {
88 case "idle":
89 return l.Idle(ctx)
90 case "begin":
91 return l.BeginUpdate(ctx, "update")
92 case "end":
93 return l.EndUpdateWith(ctx, "update", nil)
94 case "recover":
95 return l.RecoverAcquisition(ctx)
96 default:
97 return l.ActivationReady(ctx)
98 }
99 }); err != nil {
100 t.Fatal(err)
101 }
102 })
103 }
104 path := filepath.Join(dir, "versions", "unreserved")
105 if err := os.WriteFile(path, []byte("unreserved staged object"), 0644); err != nil {
106 t.Fatal(err)
107 }
108 file, err := os.Open(path)
109 if err != nil {
110 t.Fatal(err)
111 }
112 if err := x.With(t.Context(), l.Idle); err == nil {
113 file.Close()
114 t.Fatal("kernel reference escaped verifier")
115 }
116 file.Close()
117 if err := x.With(t.Context(), l.Idle); err != nil {
118 t.Fatal("released reference refused", err)
119 }
120}
121