carlos / platform Public

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

Plain git — no account needed to clone.

Download

Download this file

1package main
2
3import (
4 "fmt"
5 "os"
6 "path/filepath"
7 "strings"
8 "testing"
9
10 "github.com/carlosframework/platform/internal/console"
11)
12
13func TestClaimAdministrativeDBIsGated(t *testing.T) {
14 for _, flag := range []string{"", "1"} {
15 t.Run("flag="+flag, func(t *testing.T) {
16 t.Setenv("CARLOS_HOST_CLAIM", flag)
17 path := filepath.Join(t.TempDir(), "console.db")
18 if flag != "" {
19 // The console package's test probe is not linked into this package.
20 // Exercise the production UUID ioctl on persistent storage while
21 // leaving TMPDIR short for this package's Unix socket tests.
22 path = claimAdministrativeDBPath(t)
23
24 }
25 t.Setenv("CONSOLE_DB", path)
26 st := openConsoleDB()
27 defer st.Close()
28 err := st.Grant("operator@example.com", "demo")
29 if flag == "" && err != nil {
30 t.Fatal(err)
31 }
32 if flag != "" && (err == nil || !strings.Contains(err.Error(), "claim: app write not permitted")) {
33 t.Fatalf("unguarded legacy grant: %v", err)
34 }
35 })
36 }
37}
38
39func claimAdministrativeDBPath(t *testing.T) string {
40 t.Helper()
41 t.Cleanup(console.UseSQLInstanceIdentityForTest("i-claim-admin-test"))
42 roots := []struct {
43 name, path string
44 err error
45 }{
46 {name: "CARLOS_TEST_DB_ROOT", path: os.Getenv("CARLOS_TEST_DB_ROOT")},
47 {name: "t.TempDir()", path: t.TempDir()},
48 }
49 cwd, cwdErr := os.Getwd()
50 cache, cacheErr := os.UserCacheDir()
51 roots = append(roots, struct {
52 name, path string
53 err error
54 }{"package working directory", cwd, cwdErr}, struct {
55 name, path string
56 err error
57 }{"os.UserCacheDir()", cache, cacheErr})
58 var failures []string
59 for _, root := range roots {
60 err := root.err
61 if err == nil && root.path == "" {
62 err = fmt.Errorf("not configured")
63 }
64 var dir string
65 if err == nil {
66 err = os.MkdirAll(root.path, 0700)
67 }
68 if err == nil {
69 dir, err = os.MkdirTemp(root.path, "carlos-claim-sql-")
70 }
71 path := filepath.Join(dir, "console.db")
72 if err == nil {
73 var st *console.Store
74 st, err = console.OpenGated(path)
75 if err == nil {
76 err = st.Close()
77 }
78 }
79 if err == nil {
80 t.Cleanup(func() {
81 if err := os.RemoveAll(dir); err != nil {
82 t.Errorf("remove claim SQL fixture: %v", err)
83 }
84 })
85 return path
86 }
87 if dir != "" {
88 if cleanupErr := os.RemoveAll(dir); cleanupErr != nil {
89 t.Errorf("remove failed claim SQL fixture: %v", cleanupErr)
90 }
91 }
92 failures = append(failures, fmt.Sprintf("%s (%q): %v", root.name, root.path, err))
93 }
94 t.Skipf("no supported writable claim SQL fixture: %s", strings.Join(failures, "; "))
95 return ""
96}
97
98func TestClaimAdministrativeDBFallsBackFromUnusableOverride(t *testing.T) {
99 t.Setenv("CARLOS_TEST_DB_ROOT", "/proc/carlos-test-db-root")
100 path := claimAdministrativeDBPath(t)
101 st, err := console.OpenGated(path)
102 if err != nil {
103 t.Fatal(err)
104 }
105 if err := st.Close(); err != nil {
106 t.Fatal(err)
107 }
108}
109