// SPDX-License-Identifier: GPL-3.0-or-later package config_test import ( "os" "path/filepath" "strings" "testing" "git.trevon.dev/trevon/hub/internal/config" ) func env(home, cwd string, vars map[string]string) config.Env { return config.Env{Getenv: func(k string) string { return vars[k] }, Home: home, Cwd: cwd} } func TestDefaults(t *testing.T) { home := t.TempDir() cfg, err := config.Load(env(home, home, nil)) if err != nil { t.Fatal(err) } if cfg.FleetURL != "" || cfg.RequireFleetURL() == nil || !strings.Contains(cfg.RequireFleetURL().Error(), "set fleet_url") || cfg.TokenFile != filepath.Join(home, ".config/security-hub/fleet-token") { t.Errorf("defaults = %+v", cfg) } if cfg.RepoPath != "" || cfg.RequireRepo() == nil { t.Errorf("no repo can be found from an empty directory: %+v", cfg) } } func TestFileThenEnvPrecedence(t *testing.T) { home := t.TempDir() file := filepath.Join(home, "hub.json") body := `{"fleet_url":"http://file:1","token_file":"~/tok","repo_path":"~/repo","inventory":"~/inv"}` if err := os.WriteFile(file, []byte(body), 0o600); err != nil { t.Fatal(err) } cfg, err := config.Load(env(home, home, map[string]string{"HUB_CONFIG": file})) if err != nil { t.Fatal(err) } if cfg.FleetURL != "http://file:1" || cfg.TokenFile != filepath.Join(home, "tok") || cfg.RepoPath != filepath.Join(home, "repo") || cfg.Inventory != filepath.Join(home, "inv") { t.Errorf("file = %+v", cfg) } cfg, _ = config.Load(env(home, home, map[string]string{ "HUB_CONFIG": file, "SECURITY_HUB_FLEET_URL": "http://env:2", "HUB_TOKEN_FILE": "/x/tok", "HUB_REPO": "/r", "HUB_INVENTORY": "/r/h", })) if cfg.FleetURL != "http://env:2" || cfg.TokenFile != "/x/tok" || cfg.RepoPath != "/r" || cfg.Inventory != "/r/h" { t.Errorf("env must win: %+v", cfg) } if cfg.HubDir() != "/r/salt-security-hub" || cfg.ProfileDir() != "/r/local/profiles" { t.Errorf("dirs: %s %s", cfg.HubDir(), cfg.ProfileDir()) } } func TestBadConfigFile(t *testing.T) { home := t.TempDir() file := filepath.Join(home, "hub.json") for _, body := range []string{`{`, `{"token":"secret-in-config"}`} { _ = os.WriteFile(file, []byte(body), 0o600) _, err := config.Load(env(home, home, map[string]string{"HUB_CONFIG": file})) if err == nil { t.Errorf("accepted %q", body) } } } func TestDetectRepoFromSubdirectory(t *testing.T) { root := t.TempDir() _ = os.MkdirAll(filepath.Join(root, "salt-security-hub", "scripts"), 0o755) _ = os.WriteFile(filepath.Join(root, "hosts"), []byte("[x]\n"), 0o644) cfg, err := config.Load(env(t.TempDir(), filepath.Join(root, "salt-security-hub", "scripts"), nil)) if err != nil { t.Fatal(err) } if cfg.RepoPath != root || cfg.Inventory != filepath.Join(root, "hosts") { t.Errorf("detected = %+v", cfg) } } func TestManagedPathsFileAndUpdateSettings(t *testing.T) { home := t.TempDir() file := filepath.Join(home, "hub.json") body := `{"managed_paths_file":"~/mp.txt","update_url":"https://forge.example.org","update_repo":"o/hub"}` if err := os.WriteFile(file, []byte(body), 0o600); err != nil { t.Fatal(err) } cfg, err := config.Load(env(home, home, map[string]string{"HUB_CONFIG": file})) if err != nil { t.Fatal(err) } if cfg.ManagedPathsFile != filepath.Join(home, "mp.txt") || cfg.UpdateURL != "https://forge.example.org" || cfg.UpdateRepo != "o/hub" { t.Errorf("file = %+v", cfg) } cfg, _ = config.Load(env(home, home, map[string]string{"HUB_CONFIG": file, "HUB_MANAGED_PATHS": "/env/paths"})) if cfg.ManagedPathsFile != "/env/paths" { t.Errorf("env must win: %+v", cfg) } }