betterNAS/apps/node-agent/internal/nodeagent/app_test.go
Harivansh Rathi 48a05fda1c Validate registration retry intervals.
Reject register-only configs that leave the shared retry interval at the
zero value so failed registration attempts cannot spin in a tight loop.
Cover the regression in tests and make the register-only integration test
explicit about its retry interval.

Generated with [Devin](https://cli.devin.ai/docs)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-04-01 14:52:37 +00:00

114 lines
2.8 KiB
Go

package nodeagent
import (
"io"
"log"
"os"
"path/filepath"
"strings"
"testing"
)
func TestNewRejectsMissingExportDirectory(t *testing.T) {
t.Parallel()
exportPath := filepath.Join(t.TempDir(), "missing-export")
_, err := New(Config{
ExportPath: exportPath,
ListenAddress: defaultListenAddress(defaultPort),
}, log.New(io.Discard, "", 0))
if err == nil {
t.Fatal("expected missing export directory to fail")
}
if !strings.Contains(err.Error(), "does not exist") {
t.Fatalf("error = %q, want missing-directory message", err.Error())
}
}
func TestNewRejectsFileExportPath(t *testing.T) {
t.Parallel()
exportPath := filepath.Join(t.TempDir(), "export.txt")
if err := os.WriteFile(exportPath, []byte("not a directory"), 0o644); err != nil {
t.Fatalf("write export file: %v", err)
}
_, err := New(Config{
ExportPath: exportPath,
ListenAddress: defaultListenAddress(defaultPort),
}, log.New(io.Discard, "", 0))
if err == nil {
t.Fatal("expected file export path to fail")
}
if !strings.Contains(err.Error(), "is not a directory") {
t.Fatalf("error = %q, want not-a-directory message", err.Error())
}
}
func TestNewRejectsInvalidListenAddress(t *testing.T) {
t.Parallel()
_, err := New(Config{
ExportPath: t.TempDir(),
ListenAddress: "localhost",
}, log.New(io.Discard, "", 0))
if err == nil {
t.Fatal("expected invalid listen address to fail")
}
if !strings.Contains(err.Error(), listenAddressEnvKey) {
t.Fatalf("error = %q, want %q", err.Error(), listenAddressEnvKey)
}
}
func TestNewAcceptsLoopbackListenAddressByDefault(t *testing.T) {
t.Parallel()
_, err := New(Config{
ExportPath: t.TempDir(),
ListenAddress: defaultListenAddress(defaultPort),
}, log.New(io.Discard, "", 0))
if err != nil {
t.Fatalf("new app: %v", err)
}
}
func TestNewRejectsRegistrationWithoutMachineID(t *testing.T) {
t.Parallel()
_, err := New(Config{
ExportPath: t.TempDir(),
ListenAddress: defaultListenAddress(defaultPort),
RegisterEnabled: true,
ControlPlaneURL: "http://127.0.0.1:8081",
}, log.New(io.Discard, "", 0))
if err == nil {
t.Fatal("expected missing machine id to fail")
}
if !strings.Contains(err.Error(), "BETTERNAS_NODE_MACHINE_ID") {
t.Fatalf("error = %q, want missing-machine-id message", err.Error())
}
}
func TestNewRejectsRegistrationWithoutHeartbeatInterval(t *testing.T) {
t.Parallel()
_, err := New(Config{
ExportPath: t.TempDir(),
ListenAddress: defaultListenAddress(defaultPort),
MachineID: "nas-1",
ControlPlaneURL: "http://127.0.0.1:8081",
RegisterEnabled: true,
}, log.New(io.Discard, "", 0))
if err == nil {
t.Fatal("expected missing registration retry interval to fail")
}
if !strings.Contains(err.Error(), "BETTERNAS_NODE_HEARTBEAT_INTERVAL") {
t.Fatalf("error = %q, want missing-heartbeat-interval message", err.Error())
}
}