Files
nmon/internal/app/app_test.go
Vibe Coder f31fb3712a
Some checks failed
ci / lint (push) Successful in 4m19s
ci / openapi (push) Successful in 29s
ci / postgres (push) Successful in 4m55s
ci / test (push) Failing after 19m48s
Add settings forms, first-install wizard, webhook presets, help and poll tools
Make nmon easier to install and run without editing YAML by hand:

- Settings: point-and-click forms over config.yaml (GET/PUT
  /api/v1/settings/form); the YAML editor moves to /settings/yaml.
- First install: with no sign-in configured nmon starts in install
  mode, serves only the setup wizard (sign-in, basics, devices,
  alerting) and closes it for good once sign-in is saved. A
  self-signed certificate is generated when no TLS source is given.
  poller.interval becomes a config setting.
- Reverse proxies: opt-in --http-listen plain-HTTP listener beside
  HTTPS; sign-in stays required.
- Webhooks: Generic, Teams Adaptive Card, Slack and Discord presets,
  template preview and test send.
- Help: the operations, coverage and profile docs served at /help.
- Polling: per-device "Poll now", a dry-run test poll returning an
  SNMP trace, and time-limited in-memory SNMP debug recording, in the
  UI and as `nmon device poll`.
- Ansible: deploys into install mode when no sign-in is set, and no
  longer overwrites the config.yaml the wizard wrote.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017TuKgyKve2XLFtrrXWMi2w
2026-10-25 06:31:12 +00:00

94 lines
2.7 KiB
Go

package app
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"github.com/v10networks/nmon/internal/config"
"github.com/v10networks/nmon/internal/secrets"
)
func TestStartRefusals(t *testing.T) {
ctx := context.Background()
data := filepath.Join(t.TempDir(), "data")
outside := filepath.Join(t.TempDir(), "secret.key")
for name, tc := range map[string]struct {
f config.Flags
want string
}{
"no data dir": {config.Flags{InsecureHTTP: true, SecretKeyFile: outside}, "--data-dir"},
"key inside data": {config.Flags{DataDir: data, SecretKeyFile: filepath.Join(data, "keys", "secret.key"), InsecureHTTP: true}, "inside --data-dir"},
"key is data dir": {config.Flags{DataDir: data, SecretKeyFile: data, InsecureHTTP: true}, "inside --data-dir"},
} {
t.Run(name, func(t *testing.T) {
a, err := Start(ctx, tc.f)
if err == nil {
_ = a.Stop(ctx) // already failing the test
t.Fatal("started")
}
if !strings.Contains(err.Error(), tc.want) {
t.Fatalf("error %q, want it to mention %q", err, tc.want)
}
})
}
if _, err := os.Stat(outside); err == nil {
t.Error("a refused start created the key file")
}
}
func TestStartRefusesMissingKeyWithExistingInventory(t *testing.T) {
data := t.TempDir()
if err := os.WriteFile(config.InventoryDB(data), nil, 0o600); err != nil {
t.Fatal(err)
}
key := filepath.Join(t.TempDir(), "secret.key")
_, err := Start(context.Background(), config.Flags{DataDir: data, SecretKeyFile: key, InsecureHTTP: true})
if err == nil || !strings.Contains(err.Error(), "inventory exists") {
t.Fatalf("error %v", err)
}
if _, err := os.Stat(key); err == nil {
t.Error("a new key was generated next to an existing inventory")
}
}
func TestEnsureKeyGeneratesOnce(t *testing.T) {
key := filepath.Join(t.TempDir(), "sub", "secret.key")
if err := ensureKey(testLogger(), key, false); err != nil {
t.Fatal(err)
}
st, err := os.Stat(key)
if err != nil || st.Mode().Perm() != 0o600 {
t.Fatalf("key file: %v %v", st, err)
}
if _, err := secrets.LoadKeyring(key); err != nil {
t.Fatal(err)
}
before, _ := os.ReadFile(key)
if err := ensureKey(testLogger(), key, true); err != nil {
t.Fatal(err)
}
if after, _ := os.ReadFile(key); string(after) != string(before) {
t.Error("existing key was replaced")
}
}
func TestInside(t *testing.T) {
for _, tc := range []struct {
dir, path string
want bool
}{
{"/var/lib/nmon", "/var/lib/nmon/secret.key", true},
{"/var/lib/nmon", "/var/lib/nmon", true},
{"/var/lib/nmon", "/var/lib/nmon2/secret.key", false},
{"/var/lib/nmon", "/etc/nmon/secret.key", false},
{"/var/lib/nmon", "/var/lib/secret.key", false},
} {
if got := inside(tc.dir, tc.path); got != tc.want {
t.Errorf("inside(%s, %s) = %v", tc.dir, tc.path, got)
}
}
}