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 "testing"
4
5// The wiring resolver, whose refusals are otherwise a fatal path nobody can
6// exercise.
7func TestScannerWiring(t *testing.T) {
8 env := func(m map[string]string) func(string) string {
9 return func(k string) string { return m[k] }
10 }
11
12 t.Run("nothing set", func(t *testing.T) {
13 addr, seed, err := scannerWiring(env(nil))
14 if addr != "" || seed || err != nil {
15 t.Fatalf("= %q, %v, %v", addr, seed, err)
16 }
17 })
18
19 t.Run("explicit addr", func(t *testing.T) {
20 addr, seed, err := scannerWiring(env(map[string]string{"CARLOS_CLAMD_ADDR": "scanner:3310"}))
21 if addr != "scanner:3310" || seed || err != nil {
22 t.Fatalf("= %q, %v, %v", addr, seed, err)
23 }
24 })
25
26 // The deprecated variable is still a WIRING source, retaining the socket
27 // default. Without this a deployment that set only CARLOS_STORE_SCANNER
28 // would get no address, no Scanner and no pass — scanning stopping
29 // silently on upgrade, which is the failure the migration exists to close.
30 t.Run("deprecated var wires the default socket and asks for a seed", func(t *testing.T) {
31 addr, seed, err := scannerWiring(env(map[string]string{"CARLOS_STORE_SCANNER": "clamav"}))
32 if addr != "/run/clamav/clamd.ctl" || !seed || err != nil {
33 t.Fatalf("= %q, %v, %v", addr, seed, err)
34 }
35 })
36
37 t.Run("deprecated var does not override an explicit addr", func(t *testing.T) {
38 addr, seed, err := scannerWiring(env(map[string]string{
39 "CARLOS_STORE_SCANNER": "clamav", "CARLOS_CLAMD_ADDR": "scanner:3310",
40 }))
41 if addr != "scanner:3310" || !seed || err != nil {
42 t.Fatalf("= %q, %v, %v", addr, seed, err)
43 }
44 })
45
46 t.Run("socket alias", func(t *testing.T) {
47 addr, _, err := scannerWiring(env(map[string]string{"CARLOS_CLAMD_SOCKET": "/tmp/c.sock"}))
48 if addr != "/tmp/c.sock" || err != nil {
49 t.Fatalf("= %q, %v", addr, err)
50 }
51 })
52
53 t.Run("conflicting addr and socket", func(t *testing.T) {
54 if _, _, err := scannerWiring(env(map[string]string{
55 "CARLOS_CLAMD_ADDR": "a:1", "CARLOS_CLAMD_SOCKET": "/b",
56 })); err == nil {
57 t.Fatal("two disagreeing addresses were accepted")
58 }
59 })
60
61 t.Run("unknown engine", func(t *testing.T) {
62 if _, _, err := scannerWiring(env(map[string]string{"CARLOS_STORE_SCANNER": "guardduty"})); err == nil {
63 t.Fatal("an unknown engine was accepted")
64 }
65 })
66}
67
68// The db door reads the agent's own CARLOS_HIBERNATE_* triple, parsed the
69// agent's way (trimmed, prefix slashes stripped), so the console reads
70// exactly the prefixes the activator beside it writes.
71func TestDBDoorWiringReadsTheAgentsTriple(t *testing.T) {
72 t.Setenv("CARLOS_HIBERNATE_BUCKET", " data-ie ")
73 t.Setenv("CARLOS_HIBERNATE_REGION", "eu-west-1")
74 t.Setenv("CARLOS_HIBERNATE_PREFIX", "/instances/")
75 t.Setenv("CARLOS_HIBERNATE_IDLE", "")
76 t.Setenv("CARLOS_REGION", " uk ")
77 bucket, region, prefix, placement := dbDoorWiring()
78 if bucket != "data-ie" || region != "eu-west-1" || prefix != "instances" || placement != "uk" {
79 t.Fatalf("= %q %q %q %q", bucket, region, prefix, placement)
80 }
81}
82
83// A bad idle duration is the agent's problem, not a reason to fail console
84// boot; it turns the door off instead, loudly.
85func TestDBDoorWiringIsOffOnABadAgentConfig(t *testing.T) {
86 t.Setenv("CARLOS_HIBERNATE_BUCKET", "data-ie")
87 t.Setenv("CARLOS_HIBERNATE_IDLE", "soon")
88 if bucket, _, _, _ := dbDoorWiring(); bucket != "" {
89 t.Fatalf("bucket = %q, want the door off", bucket)
90 }
91}
92
93func TestDBDoorWiringIsOffWithoutABucket(t *testing.T) {
94 t.Setenv("CARLOS_HIBERNATE_BUCKET", "")
95 if bucket, _, _, _ := dbDoorWiring(); bucket != "" {
96 t.Fatalf("bucket = %q, want the door off", bucket)
97 }
98}
99