Add local and NetBox server notes and improve reliability
Release / release (push) Successful in 6m52s

This commit is contained in:
Sebastian Unterschütz committed 2026-10-27 17:51:59 +01:00
1 parent 2852dff2c4
commit 12cc64fdd9
17 files changed
+848 -83

No files matched your search

+52
View File
@@ -0,0 +1,52 @@
package notes
import (
"context"
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox"
"os"
"testing"
)
func TestLocalNotesPersistAndStayIsolated(t *testing.T) {
ctx := context.Background()
s := &Service{Dir: t.TempDir(), Namespace: "https://netbox.example"}
host := netbox.HostEntry{Name: "web01", Kind: "device"}
n := Note{Host: host, Text: "Wartung\nGrüße 🔧"}
if err := s.Write(ctx, n, false); err != nil {
t.Fatal(err)
}
reloaded := &Service{Dir: s.Dir, Namespace: s.Namespace}
got, err := reloaded.Read(ctx, host, false)
if err != nil || got.Text != n.Text {
t.Fatalf("round trip: %+v, %v", got, err)
}
info, err := os.Stat(s.path(host))
if err != nil {
t.Fatal(err)
}
if info.Mode().Perm() != 0o600 {
t.Fatalf("permissions: %v", info.Mode())
}
host.Kind = "vm"
got, err = s.Read(ctx, host, false)
if err != nil || got.Text != "" {
t.Fatalf("kind isolation: %+v, %v", got, err)
}
host.Kind = "device"
reloaded.Namespace = "https://other.example"
got, err = reloaded.Read(ctx, host, false)
if err != nil || got.Text != "" {
t.Fatalf("instance isolation: %+v, %v", got, err)
}
}
func TestUnreadableNoteIsNotTreatedAsEmpty(t *testing.T) {
s := &Service{Dir: t.TempDir()}
host := netbox.HostEntry{Name: "web01", Kind: "vm"}
if err := os.Mkdir(s.path(host), 0o700); err != nil {
t.Fatal(err)
}
if _, err := s.Read(context.Background(), host, false); err == nil {
t.Fatal("expected read error")
}
}