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 "errors"
5 "testing"
6)
7
8func envFunc(m map[string]string) func(string) string {
9 return func(k string) string { return m[k] }
10}
11
12func TestResolveFleetIdentityNotAFleetBox(t *testing.T) {
13 _, err := resolveFleetIdentity(envFunc(map[string]string{}))
14 if !errors.Is(err, errNotFleetBox) {
15 t.Fatalf("err = %v, want errNotFleetBox", err)
16 }
17 // Explicitly empty is the same as unset.
18 _, err = resolveFleetIdentity(envFunc(map[string]string{"CARLOS_FLEET": ""}))
19 if !errors.Is(err, errNotFleetBox) {
20 t.Fatalf("err = %v, want errNotFleetBox for empty CARLOS_FLEET", err)
21 }
22}
23
24func TestResolveFleetIdentityExplicitBoxLabel(t *testing.T) {
25 id, err := resolveFleetIdentity(envFunc(map[string]string{
26 "CARLOS_FLEET": "pi",
27 "CARLOS_FLEET_BOX": "amber-gliding",
28 }))
29 if err != nil {
30 t.Fatalf("resolveFleetIdentity: %v", err)
31 }
32 if id.Fleet != "pi" || id.Label != "amber-gliding" {
33 t.Fatalf("id = %+v, want {pi amber-gliding}", id)
34 }
35}
36
37func TestResolveFleetIdentityDefaultsLabelFromTunnelName(t *testing.T) {
38 id, err := resolveFleetIdentity(envFunc(map[string]string{
39 "CARLOS_FLEET": "pi",
40 "CARLOS_TUNNEL_NAME": "pi/amber-gliding",
41 }))
42 if err != nil {
43 t.Fatalf("resolveFleetIdentity: %v", err)
44 }
45 if id.Fleet != "pi" || id.Label != "amber-gliding" {
46 t.Fatalf("id = %+v, want {pi amber-gliding}", id)
47 }
48}
49
50// An explicit CARLOS_FLEET_BOX must win over CARLOS_TUNNEL_NAME's derived
51// value — the explicit override is the reliable path the doc comment names
52// for anything the standard "<fleet>/<label>" tunnel-name shape can't cover.
53func TestResolveFleetIdentityExplicitBoxLabelWinsOverTunnelName(t *testing.T) {
54 id, err := resolveFleetIdentity(envFunc(map[string]string{
55 "CARLOS_FLEET": "pi",
56 "CARLOS_FLEET_BOX": "explicit-label",
57 "CARLOS_TUNNEL_NAME": "pi/tunnel-derived-label",
58 }))
59 if err != nil {
60 t.Fatalf("resolveFleetIdentity: %v", err)
61 }
62 if id.Label != "explicit-label" {
63 t.Fatalf("Label = %q, want explicit-label (CARLOS_FLEET_BOX must win)", id.Label)
64 }
65}
66
67// Fix round 2 (Opus re-review): a bare legacy tunnel name — the LIVE shape
68// in today's runbook, e.g. CARLOS_TUNNEL_NAME="pi-box" with no "<fleet>/"
69// prefix at all — must be refused LOUDLY when no CARLOS_FLEET_BOX is set,
70// not silently accepted as the label. Before this fix, TrimPrefix would
71// no-op (the string doesn't start with "pi/") and leave "pi-box" itself as
72// the "label" — which still passes ValidTunnelName, so it slipped through
73// silently. That would have published a Box the edge could never dial:
74// fleetChannelFor's rule 1 checks hub.State("pi" + "/" + "pi-box"), but the
75// tunnel hub only ever knows this box's channel by its raw
76// CARLOS_TUNNEL_NAME, "pi-box" — never "pi/pi-box".
77func TestResolveFleetIdentityRefusesBareLegacyTunnelNameWithoutFleetPrefix(t *testing.T) {
78 _, err := resolveFleetIdentity(envFunc(map[string]string{
79 "CARLOS_FLEET": "pi",
80 "CARLOS_TUNNEL_NAME": "pi-box", // no "pi/" prefix — the legacy shape
81 }))
82 if err == nil || errors.Is(err, errNotFleetBox) {
83 t.Fatalf("err = %v, want a loud, non-errNotFleetBox refusal", err)
84 }
85}
86
87// An explicit CARLOS_FLEET_BOX must still work regardless of
88// CARLOS_TUNNEL_NAME's shape — including the bare legacy form the previous
89// test refuses to derive a label from. The explicit override never goes
90// anywhere near the prefix-stripping path.
91func TestResolveFleetIdentityExplicitBoxLabelWorksWithLegacyTunnelName(t *testing.T) {
92 id, err := resolveFleetIdentity(envFunc(map[string]string{
93 "CARLOS_FLEET": "pi",
94 "CARLOS_FLEET_BOX": "amber-gliding",
95 "CARLOS_TUNNEL_NAME": "pi-box", // legacy shape; must be ignored entirely
96 }))
97 if err != nil {
98 t.Fatalf("resolveFleetIdentity: %v", err)
99 }
100 if id.Label != "amber-gliding" {
101 t.Fatalf("Label = %q, want amber-gliding (explicit CARLOS_FLEET_BOX must work regardless of tunnel-name form)", id.Label)
102 }
103}
104
105func TestResolveFleetIdentityInvalidFleetNameRefuses(t *testing.T) {
106 _, err := resolveFleetIdentity(envFunc(map[string]string{
107 "CARLOS_FLEET": "Not Valid!",
108 "CARLOS_FLEET_BOX": "amber-gliding",
109 }))
110 if err == nil || errors.Is(err, errNotFleetBox) {
111 t.Fatalf("err = %v, want a non-nil, non-errNotFleetBox validation error", err)
112 }
113}
114
115func TestResolveFleetIdentityMissingLabelRefuses(t *testing.T) {
116 // CARLOS_FLEET set, neither CARLOS_FLEET_BOX nor CARLOS_TUNNEL_NAME set
117 // at all — nothing to derive a label from.
118 _, err := resolveFleetIdentity(envFunc(map[string]string{
119 "CARLOS_FLEET": "pi",
120 }))
121 if err == nil || errors.Is(err, errNotFleetBox) {
122 t.Fatalf("err = %v, want a non-nil, non-errNotFleetBox validation error", err)
123 }
124}
125
126func TestResolveFleetIdentityInvalidLabelRefuses(t *testing.T) {
127 _, err := resolveFleetIdentity(envFunc(map[string]string{
128 "CARLOS_FLEET": "pi",
129 "CARLOS_FLEET_BOX": "not a valid label!",
130 }))
131 if err == nil || errors.Is(err, errNotFleetBox) {
132 t.Fatalf("err = %v, want a non-nil, non-errNotFleetBox validation error", err)
133 }
134}
135