| 1 | package instancectl |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/carlosframework/platform/internal/cgroup" |
| 12 | "github.com/carlosframework/platform/internal/release" |
| 13 | ) |
| 14 | |
| 15 | // The executable mount names the staged build when one is given, and the |
| 16 | // live link (plan 2's layout) when not. |
| 17 | func TestEnsureGenExeAtTargetsTheStagedPath(t *testing.T) { |
| 18 | s := &Server{ExeRoot: t.TempDir()} |
| 19 | staged := "/opt/carlos/versions/v2/blog" |
| 20 | if err := s.ensureGenExeAt(genHost, 7, StagedExe{Path: staged}); err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | if got, err := os.Readlink(filepath.Join(s.ExeRoot, GenKey(genHost, 7), "app")); err != nil || got != staged { |
| 24 | t.Fatalf("generation 7's exe = %q (%v), want %q", got, err, staged) |
| 25 | } |
| 26 | if err := s.ensureGenExeAt(genHost, 8, StagedExe{}); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | if got, _ := os.Readlink(filepath.Join(s.ExeRoot, GenKey(genHost, 8), "app")); got != "/opt/carlos/live/"+genHost { |
| 30 | t.Fatalf("generation 8's exe = %q, want the live link", got) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // A refused path answers 403 before anything is laid out, and the client |
| 35 | // reports ErrStagedExe, so the swap is abandoned rather than failed. |
| 36 | func TestGenStartRefusesAStagedExeRootCannotVouchFor(t *testing.T) { |
| 37 | exeRoot := t.TempDir() |
| 38 | _, sock := serveOnSocket(t, &fakeCtl{}, func(s *Server) { |
| 39 | s.DropInDir, s.ExeRoot, s.OptDir = t.TempDir(), exeRoot, t.TempDir() |
| 40 | s.HostApp = func(string) (string, string, error) { return release.OpsAccount, "blog", nil } |
| 41 | }) |
| 42 | _, err := NewClient(sock).StartGenExe(context.Background(), genHost, 7, cgroup.TierDefault.MemoryMax, |
| 43 | StagedExe{Path: "/etc/passwd", Version: "v2", Digest: strings.Repeat("0", 64)}) |
| 44 | if !errors.Is(err, ErrStagedExe) { |
| 45 | t.Fatalf("StartGenExe = %v, want ErrStagedExe", err) |
| 46 | } |
| 47 | if _, err := os.Stat(filepath.Join(exeRoot, GenKey(genHost, 7))); !os.IsNotExist(err) { |
| 48 | t.Fatal("an executable mount was laid out for a refused path") |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestStagedExeWithoutRegistryViewFailsClosed(t *testing.T) { |
| 53 | s := &Server{OptDir: t.TempDir()} |
| 54 | err := s.validateStagedExe(genHost, StagedExe{Version: "v2", Digest: strings.Repeat("0", 64)}) |
| 55 | if !errors.Is(err, ErrStagedExe) { |
| 56 | t.Fatalf("validateStagedExe = %v, want ErrStagedExe", err) |
| 57 | } |
| 58 | } |
| 59 | |