package config import ( "os" "path/filepath" "testing" ) func TestOperationsDefaultsEnvironmentAndValidation(t *testing.T) { t.Setenv("XDG_CONFIG_HOME", t.TempDir()) if err := os.WriteFile(filepath.Join(os.Getenv("XDG_CONFIG_HOME"), "netssh.yaml"), []byte("hosts:\n - name: router\n address: 192.0.2.1\n"), 0600); err != nil { t.Fatal(err) } cfg, err := Load() if err != nil { t.Fatal(err) } if cfg.TUI.HistoryLimit != 10 || cfg.Activity.Enabled || cfg.Activity.RetentionDays != 30 || cfg.Activity.MaxEntries != 1000 { t.Fatalf("%+v %+v", cfg.TUI, cfg.Activity) } t.Setenv("NETSSH_TUI_HISTORY_LIMIT", "25") t.Setenv("NETSSH_SSH_POST_DISCONNECT", "echo done") cfg, err = Load() if err != nil || cfg.TUI.HistoryLimit != 25 || cfg.SSH.PostDisconnect != "echo done" { t.Fatalf("%+v %v", cfg, err) } cfg.TUI.HistoryLimit = -1 if cfg.ValidateOperations() == nil { t.Fatal("negative history accepted") } for _, timeout := range []string{"-1s", "0s", "invalid"} { cfg.Commands = map[string]HostCommand{"check": {Args: []string{"echo", "{host}"}, Timeout: timeout}} if cfg.ValidateCommands() == nil { t.Fatalf("accepted %q", timeout) } } } func TestPostDisconnectLayering(t *testing.T) { cfg := Config{ SSH: SSHConfig{SSHOptions: SSHOptions{PostDisconnect: "global"}, Rules: []SSHRule{{Match: SSHMatch{Tag: "lab"}, SSHOptions: SSHOptions{PostDisconnect: "none"}}}}, NetBoxes: []NetBoxConfig{{Name: "prod", SSH: SSHOptions{PostDisconnect: "instance"}}}, } for _, tc := range []struct { host SSHHost want string }{ {SSHHost{Name: "web"}, "global"}, {SSHHost{Name: "web", Instance: "prod"}, "instance"}, {SSHHost{Name: "web", Instance: "prod", Tags: []string{"lab"}}, ""}, } { if got := HookCommand(cfg.SSHOptionsFor(tc.host).PostDisconnect); got != tc.want { t.Fatalf("%q want %q", got, tc.want) } } }