53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
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: "Maintenance\nReady 🔧"}
|
|
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")
|
|
}
|
|
}
|