Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# RoutheOS API

## 🐛 Testing

The project can be testing with the command:
> go test -v ./...
50 changes: 50 additions & 0 deletions model/message_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package model

import "testing"

func TestMessage_GetHostname(t *testing.T) {
m := Message{Hostname: "vm-ABC-123", IpAddress: "10.0.0.1"}

if m.Hostname != "vm-ABC-123" {
t.Fatalf("expected hostname to be %q, got %q", "vm-ABC-123", m.Hostname)
}

// Ensure calling GetSerial does not mutate Hostname
_ = m.GetSerial()
if m.Hostname != "vm-ABC-123" {
t.Fatalf("hostname was mutated by GetSerial: got %q", m.Hostname)
}
}

func TestMessage_GetSerial(t *testing.T) {
tests := []struct {
name string
hostname string
want string
}{
{"no_dash", "vm", ""},
{"single_dash", "vm-12345", "12345"},
{"multi_dash", "vm-123-456", "123-456"},
{"leading_dash", "-SERIAL", "SERIAL"},
{"empty_hostname", "", ""},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := Message{Hostname: tt.hostname}
got := m.GetSerial()
if got != tt.want {
t.Fatalf("GetSerial() for hostname %q = %q, want %q", tt.hostname, got, tt.want)
}

// Ensure result is cached in m.serial after first call, but only when non-empty
original := got
m.Hostname = "vm-CHANGED-999"
if original != "" {
if cached := m.GetSerial(); cached != original {
t.Fatalf("expected cached serial %q after hostname change, got %q", original, cached)
}
}
})
}
}
32 changes: 32 additions & 0 deletions model/virtualmachine_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package model

import "testing"

func TestVirtualMachine_NewVM(t *testing.T) {
netbox := &Netbox{}
msg := Message{
Hostname: "vm-12345",
}

vm := NewVM(netbox, msg)
if vm.Serial != "12345" {
t.Fatalf("expected serial to be %q, got %q", "12345", vm.Serial)
}

}

func TestVirtualMachine_Exists(t *testing.T) {
// When VM has no Netbox client and no NetboxId, Exists should early-return (false, 0, nil)
vm := &VirtualMachine{}

exists, id, err := vm.Exists("some-host", "some-serial")
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if exists {
t.Fatalf("expected exists to be false, got true")
}
if id != 0 {
t.Fatalf("expected id to be 0, got %d", id)
}
}