|
| 1 | +// SPDX-FileCopyrightText: 2026 SUSE LLC |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +package get |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "os" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + apitypes "github.com/uyuni-project/uyuni-tools/shared/api/types" |
| 14 | +) |
| 15 | + |
| 16 | +func TestMapJsonPathToField(t *testing.T) { |
| 17 | + tests := []struct { |
| 18 | + path string |
| 19 | + expected string |
| 20 | + }{ |
| 21 | + {".id", "ID"}, |
| 22 | + {".name", "Name"}, |
| 23 | + {".last_checkin", "LastCheckin"}, |
| 24 | + {".created", "Created"}, |
| 25 | + {".last_boot", "LastBoot"}, |
| 26 | + {"id", "ID"}, // without leading dot |
| 27 | + {"name", "Name"}, // without leading dot |
| 28 | + {".bogus", "bogus"}, // unknown field falls back (dot stripped) |
| 29 | + } |
| 30 | + |
| 31 | + for _, tt := range tests { |
| 32 | + got := mapJsonPathToField[apitypes.System](tt.path) |
| 33 | + if got != tt.expected { |
| 34 | + t.Errorf("mapJsonPathToField(%q) = %q, want %q", tt.path, got, tt.expected) |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func TestParseCustomColumns(t *testing.T) { |
| 40 | + spec := "ID:.id,NAME:.name,LAST_SEEN:.last_checkin" |
| 41 | + cols := parseCustomColumns[apitypes.System](spec) |
| 42 | + |
| 43 | + if len(cols) != 3 { |
| 44 | + t.Fatalf("expected 3 columns, got %d", len(cols)) |
| 45 | + } |
| 46 | + |
| 47 | + expected := []ColumnDef{ |
| 48 | + {Header: "ID", Field: "ID"}, |
| 49 | + {Header: "NAME", Field: "Name"}, |
| 50 | + {Header: "LAST_SEEN", Field: "LastCheckin"}, |
| 51 | + } |
| 52 | + |
| 53 | + for i, col := range cols { |
| 54 | + if col.Header != expected[i].Header { |
| 55 | + t.Errorf("col[%d].Header = %q, want %q", i, col.Header, expected[i].Header) |
| 56 | + } |
| 57 | + if col.Field != expected[i].Field { |
| 58 | + t.Errorf("col[%d].Field = %q, want %q", i, col.Field, expected[i].Field) |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestPrintOutputCustomColumns(t *testing.T) { |
| 64 | + items := []apitypes.System{ |
| 65 | + {ID: 1001, Name: "web-server-01", LastCheckin: "2026-06-20 14:00:00"}, |
| 66 | + {ID: 1002, Name: "db-server-02", LastCheckin: "2026-06-21 09:30:00"}, |
| 67 | + } |
| 68 | + |
| 69 | + var buf bytes.Buffer |
| 70 | + format := "custom-columns=ID:.id,NAME:.name,LAST_SEEN:.last_checkin" |
| 71 | + err := printOutput(format, items, nil, &buf) |
| 72 | + if err != nil { |
| 73 | + t.Fatalf("printOutput returned error: %v", err) |
| 74 | + } |
| 75 | + |
| 76 | + output := buf.String() |
| 77 | + |
| 78 | + // Check headers are present |
| 79 | + if !strings.Contains(output, "ID") { |
| 80 | + t.Error("output missing ID header") |
| 81 | + } |
| 82 | + if !strings.Contains(output, "NAME") { |
| 83 | + t.Error("output missing NAME header") |
| 84 | + } |
| 85 | + if !strings.Contains(output, "LAST_SEEN") { |
| 86 | + t.Error("output missing LAST_SEEN header") |
| 87 | + } |
| 88 | + |
| 89 | + // Check data rows are present |
| 90 | + if !strings.Contains(output, "1001") { |
| 91 | + t.Error("output missing system ID 1001") |
| 92 | + } |
| 93 | + if !strings.Contains(output, "web-server-01") { |
| 94 | + t.Error("output missing system name web-server-01") |
| 95 | + } |
| 96 | + if !strings.Contains(output, "db-server-02") { |
| 97 | + t.Error("output missing system name db-server-02") |
| 98 | + } |
| 99 | + if !strings.Contains(output, "2026-06-21 09:30:00") { |
| 100 | + t.Error("output missing last_checkin value") |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func TestPrintOutputCustomColumnsFile(t *testing.T) { |
| 105 | + // Create a temp template file |
| 106 | + content := "ID NAME LAST_SEEN\n.id .name .last_checkin\n" |
| 107 | + tmpFile, err := os.CreateTemp("", "custom-cols-*.txt") |
| 108 | + if err != nil { |
| 109 | + t.Fatalf("failed to create temp file: %v", err) |
| 110 | + } |
| 111 | + defer os.Remove(tmpFile.Name()) |
| 112 | + |
| 113 | + if _, err := tmpFile.WriteString(content); err != nil { |
| 114 | + t.Fatalf("failed to write temp file: %v", err) |
| 115 | + } |
| 116 | + tmpFile.Close() |
| 117 | + |
| 118 | + items := []apitypes.System{ |
| 119 | + {ID: 2001, Name: "test-minion", LastCheckin: "2026-06-22 10:00:00"}, |
| 120 | + } |
| 121 | + |
| 122 | + var buf bytes.Buffer |
| 123 | + format := "custom-columns-file=" + tmpFile.Name() |
| 124 | + err = printOutput(format, items, nil, &buf) |
| 125 | + if err != nil { |
| 126 | + t.Fatalf("printOutput returned error: %v", err) |
| 127 | + } |
| 128 | + |
| 129 | + output := buf.String() |
| 130 | + |
| 131 | + if !strings.Contains(output, "2001") { |
| 132 | + t.Error("output missing system ID 2001") |
| 133 | + } |
| 134 | + if !strings.Contains(output, "test-minion") { |
| 135 | + t.Error("output missing system name test-minion") |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func TestPrintOutputCustomColumnsEmpty(t *testing.T) { |
| 140 | + items := []apitypes.System{} |
| 141 | + var buf bytes.Buffer |
| 142 | + format := "custom-columns=ID:.id" |
| 143 | + err := printOutput(format, items, nil, &buf) |
| 144 | + if err != nil { |
| 145 | + t.Fatalf("printOutput returned error: %v", err) |
| 146 | + } |
| 147 | + |
| 148 | + output := buf.String() |
| 149 | + // Should still print the header even with no data |
| 150 | + if !strings.Contains(output, "ID") { |
| 151 | + t.Error("output missing ID header even with empty data") |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +func TestPrintOutputCustomColumnsNoSpec(t *testing.T) { |
| 156 | + items := []apitypes.System{} |
| 157 | + var buf bytes.Buffer |
| 158 | + format := "custom-columns=" |
| 159 | + err := printOutput(format, items, nil, &buf) |
| 160 | + if err == nil { |
| 161 | + t.Error("expected error for empty custom-columns spec, got nil") |
| 162 | + } |
| 163 | +} |
0 commit comments