carlos / platform Public

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

Plain git — no account needed to clone.

Download

Download this file

1package host
2
3import (
4 "context"
5 "errors"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10
11 "github.com/carlosframework/platform/internal/activator"
12 "github.com/carlosframework/platform/internal/provision"
13 "github.com/carlosframework/platform/internal/registry"
14)
15
16func TestClaimFirstWakeCreatesAuthorizedInstanceDirectory(t *testing.T) {
17 b := &activator.ExecBackend{DataDir: t.TempDir()}
18 host := "first-claim.example"
19 rt := registry.Route{Host: host, DBPath: provision.NestedDBPath(b.DataDir, host), Socket: provision.NestedSocket(host)}
20 parent := filepath.Join(b.DataDir, "instances")
21 if err := os.Mkdir(parent, 0700); err != nil {
22 t.Fatal(err)
23 }
24 denied := errors.New("not authorized")
25 if err := ensureClaimLaunchToken(t.Context(), b, rt, func(context.Context, func() error) error { return denied }); !errors.Is(err, denied) {
26 t.Fatal("denial", err)
27 }
28 dir := provision.InstanceDir(b.DataDir, host)
29 if _, err := os.Lstat(dir); !errors.Is(err, os.ErrNotExist) {
30 t.Fatal("denial created directory", err)
31 }
32 authorize := func(_ context.Context, write func() error) error { return write() }
33 if err := ensureClaimLaunchToken(t.Context(), b, rt, authorize); err != nil {
34 t.Fatal("first claimed wake", err)
35 }
36 raw, err := os.ReadFile(filepath.Join(dir, host+".env"))
37 if err != nil {
38 t.Fatal(err)
39 }
40 if !strings.Contains(string(raw), "CARLOS_ADMIN_TOKEN=") || !strings.Contains(string(raw), "CARLOS_INSTANCE_KEY=") {
41 t.Fatal("missing initialized launch credentials")
42 }
43 if err := os.RemoveAll(dir); err != nil {
44 t.Fatal(err)
45 }
46 victim := t.TempDir()
47 if err := os.Symlink(victim, dir); err != nil {
48 t.Fatal(err)
49 }
50 if err := ensureClaimLaunchToken(t.Context(), b, rt, authorize); err == nil {
51 t.Fatal("symlinked instance accepted")
52 }
53 entries, err := os.ReadDir(victim)
54 if err != nil || len(entries) != 0 {
55 t.Fatal("symlink target changed", entries, err)
56 }
57}
58