refactor: move V1 code into v1/ subdirectory
- git mv cmd/ internal/ pkg/ test/ go.mod go.sum Makefile build.sh docs/ v1/ - Create v1/README.md with V1 documentation - Update root README for V1 + V2 structure - V1 still builds (cd v1 && go build ./cmd/hostkeeper) and 105 tests pass - Root is now clean for V2 development
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.tukangketik.id/swanadiva/hostkeeper/internal/models"
|
||||
)
|
||||
|
||||
func TestListCommandExists(t *testing.T) {
|
||||
if listCmd == nil {
|
||||
t.Fatal("listCmd should not be nil")
|
||||
}
|
||||
|
||||
if listCmd.Use != "list" {
|
||||
t.Errorf("expected Use 'list', got '%s'", listCmd.Use)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListCommandFlags(t *testing.T) {
|
||||
expectedFlags := []string{"group", "tag", "format", "sort"}
|
||||
for _, flagName := range expectedFlags {
|
||||
if listCmd.Flags().Lookup(flagName) == nil {
|
||||
t.Errorf("flag '%s' should be defined", flagName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterByGroup(t *testing.T) {
|
||||
hosts := []*models.Host{
|
||||
{Name: "web1", Group: "production"},
|
||||
{Name: "web2", Group: "staging"},
|
||||
{Name: "db1", Group: "production"},
|
||||
{Name: "cache1", Group: "staging"},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
group string
|
||||
wantLen int
|
||||
}{
|
||||
{"production", 2},
|
||||
{"staging", 2},
|
||||
{"nonexistent", 0},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.group, func(t *testing.T) {
|
||||
result := filterByGroup(hosts, tt.group)
|
||||
if len(result) != tt.wantLen {
|
||||
t.Errorf("expected %d hosts for group '%s', got %d", tt.wantLen, tt.group, len(result))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterByTag(t *testing.T) {
|
||||
hosts := []*models.Host{
|
||||
{Name: "web1", Tags: []string{"web", "frontend"}},
|
||||
{Name: "db1", Tags: []string{"database", "backend"}},
|
||||
{Name: "web2", Tags: []string{"web", "frontend"}},
|
||||
{Name: "cache1", Tags: []string{"cache", "backend"}},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
tag string
|
||||
wantLen int
|
||||
}{
|
||||
{"web", 2},
|
||||
{"database", 1},
|
||||
{"backend", 2},
|
||||
{"nonexistent", 0},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.tag, func(t *testing.T) {
|
||||
result := filterByTag(hosts, tt.tag)
|
||||
if len(result) != tt.wantLen {
|
||||
t.Errorf("expected %d hosts for tag '%s', got %d", tt.wantLen, tt.tag, len(result))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSortHosts(t *testing.T) {
|
||||
hosts := []*models.Host{
|
||||
{Name: "zebra", Hostname: "10.0.0.3", Group: "c"},
|
||||
{Name: "alpha", Hostname: "10.0.0.1", Group: "a"},
|
||||
{Name: "mike", Hostname: "10.0.0.2", Group: "b"},
|
||||
}
|
||||
|
||||
// Test sort by name
|
||||
sortHosts(hosts, "name")
|
||||
if hosts[0].Name != "alpha" {
|
||||
t.Errorf("expected first host to be 'alpha' when sorted by name, got '%s'", hosts[0].Name)
|
||||
}
|
||||
|
||||
// Test sort by hostname
|
||||
sortHosts(hosts, "hostname")
|
||||
if hosts[0].Hostname != "10.0.0.1" {
|
||||
t.Errorf("expected first host to have hostname '10.0.0.1' when sorted by hostname, got '%s'", hosts[0].Hostname)
|
||||
}
|
||||
|
||||
// Test sort by group
|
||||
sortHosts(hosts, "group")
|
||||
if hosts[0].Group != "a" {
|
||||
t.Errorf("expected first host to have group 'a' when sorted by group, got '%s'", hosts[0].Group)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user