|
| 1 | +package audit |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/csv" |
| 6 | + "io" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/asomensari-es/nr-tf-util/internal/resources" |
| 10 | +) |
| 11 | + |
| 12 | +func readCSV(t *testing.T, r io.Reader) [][]string { |
| 13 | + t.Helper() |
| 14 | + rows, err := csv.NewReader(r).ReadAll() |
| 15 | + if err != nil { |
| 16 | + t.Fatalf("csv parse error: %v", err) |
| 17 | + } |
| 18 | + return rows |
| 19 | +} |
| 20 | + |
| 21 | +func TestWriteCSV_Headers(t *testing.T) { |
| 22 | + var buf bytes.Buffer |
| 23 | + if err := WriteCSV(&buf, nil); err != nil { |
| 24 | + t.Fatalf("WriteCSV error: %v", err) |
| 25 | + } |
| 26 | + rows := readCSV(t, &buf) |
| 27 | + if len(rows) != 1 { |
| 28 | + t.Fatalf("expected header row only, got %d rows", len(rows)) |
| 29 | + } |
| 30 | + want := []string{ |
| 31 | + "account_id", "account_name", |
| 32 | + "asset_type", "asset_id", "asset_name", |
| 33 | + "location", "id_type", "id_value", |
| 34 | + "resolved", "entity_name", "entity_type", |
| 35 | + "suggestion", |
| 36 | + } |
| 37 | + if len(rows[0]) != len(want) { |
| 38 | + t.Fatalf("header column count = %d, want %d", len(rows[0]), len(want)) |
| 39 | + } |
| 40 | + for i, col := range want { |
| 41 | + if rows[0][i] != col { |
| 42 | + t.Errorf("header[%d] = %q, want %q", i, rows[0][i], col) |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestWriteCSV_IDFinding(t *testing.T) { |
| 48 | + results := []*AuditResult{ |
| 49 | + { |
| 50 | + Account: resources.Account{ID: 1234, Name: "Prod"}, |
| 51 | + Findings: []Finding{ |
| 52 | + { |
| 53 | + AssetType: "nrql_condition", |
| 54 | + AssetID: "1234:99", |
| 55 | + AssetName: "High Error Rate", |
| 56 | + Location: "condition query", |
| 57 | + IDType: "appId", |
| 58 | + IDValue: "555", |
| 59 | + Resolved: true, |
| 60 | + EntityName: "Checkout Service", |
| 61 | + EntityType: "APPLICATION", |
| 62 | + Suggestion: "Replace with: appName = 'Checkout Service'", |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + var buf bytes.Buffer |
| 69 | + if err := WriteCSV(&buf, results); err != nil { |
| 70 | + t.Fatalf("WriteCSV error: %v", err) |
| 71 | + } |
| 72 | + rows := readCSV(t, &buf) |
| 73 | + if len(rows) != 2 { // header + 1 finding |
| 74 | + t.Fatalf("expected 2 rows, got %d", len(rows)) |
| 75 | + } |
| 76 | + row := rows[1] |
| 77 | + checks := []struct{ col, want int }{ |
| 78 | + {0, 0}, // account_id checked separately |
| 79 | + } |
| 80 | + _ = checks |
| 81 | + |
| 82 | + assertEqual(t, row, 0, "1234") |
| 83 | + assertEqual(t, row, 1, "Prod") |
| 84 | + assertEqual(t, row, 2, "nrql_condition") |
| 85 | + assertEqual(t, row, 3, "1234:99") |
| 86 | + assertEqual(t, row, 4, "High Error Rate") |
| 87 | + assertEqual(t, row, 5, "condition query") |
| 88 | + assertEqual(t, row, 6, "appId") |
| 89 | + assertEqual(t, row, 7, "555") |
| 90 | + assertEqual(t, row, 8, "true") |
| 91 | + assertEqual(t, row, 9, "Checkout Service") |
| 92 | + assertEqual(t, row, 10, "APPLICATION") |
| 93 | + assertEqual(t, row, 11, "Replace with: appName = 'Checkout Service'") |
| 94 | +} |
| 95 | + |
| 96 | +func TestWriteCSV_ConfigFinding(t *testing.T) { |
| 97 | + results := []*AuditResult{ |
| 98 | + { |
| 99 | + Account: resources.Account{ID: 9, Name: "Staging"}, |
| 100 | + Findings: []Finding{ |
| 101 | + { |
| 102 | + AssetType: "nrql_condition", |
| 103 | + AssetID: "9:7", |
| 104 | + AssetName: "CPU Alert", |
| 105 | + Location: "condition configuration", |
| 106 | + Suggestion: "Condition is disabled — enable it or remove it to reduce policy noise", |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + } |
| 111 | + |
| 112 | + var buf bytes.Buffer |
| 113 | + if err := WriteCSV(&buf, results); err != nil { |
| 114 | + t.Fatalf("WriteCSV error: %v", err) |
| 115 | + } |
| 116 | + rows := readCSV(t, &buf) |
| 117 | + if len(rows) != 2 { |
| 118 | + t.Fatalf("expected 2 rows, got %d", len(rows)) |
| 119 | + } |
| 120 | + row := rows[1] |
| 121 | + assertEqual(t, row, 6, "") // id_type empty |
| 122 | + assertEqual(t, row, 7, "") // id_value empty |
| 123 | + assertEqual(t, row, 8, "false") // resolved false |
| 124 | + assertEqual(t, row, 9, "") // entity_name empty |
| 125 | + assertEqual(t, row, 10, "") // entity_type empty |
| 126 | + assertEqual(t, row, 11, "Condition is disabled — enable it or remove it to reduce policy noise") |
| 127 | +} |
| 128 | + |
| 129 | +func TestWriteCSV_MultipleAccounts(t *testing.T) { |
| 130 | + results := []*AuditResult{ |
| 131 | + { |
| 132 | + Account: resources.Account{ID: 1, Name: "A"}, |
| 133 | + Findings: []Finding{{AssetType: "dashboard", AssetID: "g1", AssetName: "D1", Location: "l1", IDType: "appId", IDValue: "1"}}, |
| 134 | + }, |
| 135 | + { |
| 136 | + Account: resources.Account{ID: 2, Name: "B"}, |
| 137 | + Findings: []Finding{}, |
| 138 | + }, |
| 139 | + { |
| 140 | + Account: resources.Account{ID: 3, Name: "C"}, |
| 141 | + Findings: []Finding{ |
| 142 | + {AssetType: "workflow", AssetID: "w1", AssetName: "W1", Location: "l1", IDType: "appId", IDValue: "2"}, |
| 143 | + {AssetType: "workflow", AssetID: "w1", AssetName: "W1", Location: "l2", IDType: "hostId", IDValue: "3"}, |
| 144 | + }, |
| 145 | + }, |
| 146 | + } |
| 147 | + |
| 148 | + var buf bytes.Buffer |
| 149 | + if err := WriteCSV(&buf, results); err != nil { |
| 150 | + t.Fatalf("WriteCSV error: %v", err) |
| 151 | + } |
| 152 | + rows := readCSV(t, &buf) |
| 153 | + // header + 1 (account 1) + 0 (account 2) + 2 (account 3) |
| 154 | + if len(rows) != 4 { |
| 155 | + t.Fatalf("expected 4 rows, got %d", len(rows)) |
| 156 | + } |
| 157 | + assertEqual(t, rows[1], 0, "1") |
| 158 | + assertEqual(t, rows[2], 0, "3") |
| 159 | + assertEqual(t, rows[3], 0, "3") |
| 160 | +} |
| 161 | + |
| 162 | +func assertEqual(t *testing.T, row []string, col int, want string) { |
| 163 | + t.Helper() |
| 164 | + if col >= len(row) { |
| 165 | + t.Errorf("column %d out of range (row has %d columns)", col, len(row)) |
| 166 | + return |
| 167 | + } |
| 168 | + if row[col] != want { |
| 169 | + t.Errorf("col[%d] = %q, want %q", col, row[col], want) |
| 170 | + } |
| 171 | +} |
0 commit comments