package main import "testing" // The wiring resolver, whose refusals are otherwise a fatal path nobody can // exercise. func TestScannerWiring(t *testing.T) { env := func(m map[string]string) func(string) string { return func(k string) string { return m[k] } } t.Run("nothing set", func(t *testing.T) { addr, seed, err := scannerWiring(env(nil)) if addr != "" || seed || err != nil { t.Fatalf("= %q, %v, %v", addr, seed, err) } }) t.Run("explicit addr", func(t *testing.T) { addr, seed, err := scannerWiring(env(map[string]string{"CARLOS_CLAMD_ADDR": "scanner:3310"})) if addr != "scanner:3310" || seed || err != nil { t.Fatalf("= %q, %v, %v", addr, seed, err) } }) // The deprecated variable is still a WIRING source, retaining the socket // default. Without this a deployment that set only CARLOS_STORE_SCANNER // would get no address, no Scanner and no pass — scanning stopping // silently on upgrade, which is the failure the migration exists to close. t.Run("deprecated var wires the default socket and asks for a seed", func(t *testing.T) { addr, seed, err := scannerWiring(env(map[string]string{"CARLOS_STORE_SCANNER": "clamav"})) if addr != "/run/clamav/clamd.ctl" || !seed || err != nil { t.Fatalf("= %q, %v, %v", addr, seed, err) } }) t.Run("deprecated var does not override an explicit addr", func(t *testing.T) { addr, seed, err := scannerWiring(env(map[string]string{ "CARLOS_STORE_SCANNER": "clamav", "CARLOS_CLAMD_ADDR": "scanner:3310", })) if addr != "scanner:3310" || !seed || err != nil { t.Fatalf("= %q, %v, %v", addr, seed, err) } }) t.Run("socket alias", func(t *testing.T) { addr, _, err := scannerWiring(env(map[string]string{"CARLOS_CLAMD_SOCKET": "/tmp/c.sock"})) if addr != "/tmp/c.sock" || err != nil { t.Fatalf("= %q, %v", addr, err) } }) t.Run("conflicting addr and socket", func(t *testing.T) { if _, _, err := scannerWiring(env(map[string]string{ "CARLOS_CLAMD_ADDR": "a:1", "CARLOS_CLAMD_SOCKET": "/b", })); err == nil { t.Fatal("two disagreeing addresses were accepted") } }) t.Run("unknown engine", func(t *testing.T) { if _, _, err := scannerWiring(env(map[string]string{"CARLOS_STORE_SCANNER": "guardduty"})); err == nil { t.Fatal("an unknown engine was accepted") } }) } // The db door reads the agent's own CARLOS_HIBERNATE_* triple, parsed the // agent's way (trimmed, prefix slashes stripped), so the console reads // exactly the prefixes the activator beside it writes. func TestDBDoorWiringReadsTheAgentsTriple(t *testing.T) { t.Setenv("CARLOS_HIBERNATE_BUCKET", " data-ie ") t.Setenv("CARLOS_HIBERNATE_REGION", "eu-west-1") t.Setenv("CARLOS_HIBERNATE_PREFIX", "/instances/") t.Setenv("CARLOS_HIBERNATE_IDLE", "") t.Setenv("CARLOS_REGION", " uk ") bucket, region, prefix, placement := dbDoorWiring() if bucket != "data-ie" || region != "eu-west-1" || prefix != "instances" || placement != "uk" { t.Fatalf("= %q %q %q %q", bucket, region, prefix, placement) } } // A bad idle duration is the agent's problem, not a reason to fail console // boot; it turns the door off instead, loudly. func TestDBDoorWiringIsOffOnABadAgentConfig(t *testing.T) { t.Setenv("CARLOS_HIBERNATE_BUCKET", "data-ie") t.Setenv("CARLOS_HIBERNATE_IDLE", "soon") if bucket, _, _, _ := dbDoorWiring(); bucket != "" { t.Fatalf("bucket = %q, want the door off", bucket) } } func TestDBDoorWiringIsOffWithoutABucket(t *testing.T) { t.Setenv("CARLOS_HIBERNATE_BUCKET", "") if bucket, _, _, _ := dbDoorWiring(); bucket != "" { t.Fatalf("bucket = %q, want the door off", bucket) } }