|
14 | 14 |
|
15 | 15 | package puppetreport |
16 | 16 |
|
17 | | -import "testing" |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "testing" |
| 20 | +) |
18 | 21 |
|
19 | 22 | func TestLoadReport(t *testing.T) { |
20 | | - report, err := load("last_run_report.yaml") |
21 | | - if err != nil { |
22 | | - t.Fatal(err) |
| 23 | + testCases := map[string]interpretedReport{ |
| 24 | + "last_run_report": interpretedReport{ |
| 25 | + RunAt: 1618957125.5901103, |
| 26 | + RunDuration: 17.199882286, |
| 27 | + CatalogVersion: "1618957129", |
| 28 | + RunSuccess: 1, |
| 29 | + }, |
| 30 | + "last_run_report-5.4.0": interpretedReport{ |
| 31 | + RunAt: 1725776230.602652, |
| 32 | + RunDuration: 0.03196727, |
| 33 | + CatalogVersion: "1725776230", |
| 34 | + RunSuccess: 1, |
| 35 | + ResourceCount: 8, |
| 36 | + ResourceStates: map[string]float64{ |
| 37 | + "skipped": 0, |
| 38 | + "failed": 0, |
| 39 | + "failed_to_restart": 0, |
| 40 | + "restarted": 0, |
| 41 | + "changed": 0, |
| 42 | + "out_of_sync": 0, |
| 43 | + "scheduled": 0, |
| 44 | + "corrective_change": 0, |
| 45 | + }, |
| 46 | + EventStates: map[string]float64{ |
| 47 | + "failure": 0, |
| 48 | + "success": 0, |
| 49 | + }, |
| 50 | + }, |
| 51 | + "last_run_report-6.28.0": interpretedReport{ |
| 52 | + RunAt: 1725776354.854867, |
| 53 | + RunDuration: 0.004820335, |
| 54 | + CatalogVersion: "1725776354", |
| 55 | + RunSuccess: 1, |
| 56 | + ResourceCount: 8, |
| 57 | + ResourceStates: map[string]float64{ |
| 58 | + "skipped": 0, |
| 59 | + "failed": 0, |
| 60 | + "failed_to_restart": 0, |
| 61 | + "restarted": 0, |
| 62 | + "changed": 0, |
| 63 | + "out_of_sync": 0, |
| 64 | + "scheduled": 0, |
| 65 | + "corrective_change": 0, |
| 66 | + }, |
| 67 | + EventStates: map[string]float64{ |
| 68 | + "failure": 0, |
| 69 | + "success": 0, |
| 70 | + }, |
| 71 | + }, |
| 72 | + "last_run_report-7.32.1": interpretedReport{ |
| 73 | + RunAt: 1725776438.356112, |
| 74 | + RunDuration: 0.006013873, |
| 75 | + CatalogVersion: "1725776438", |
| 76 | + RunSuccess: 1, |
| 77 | + ResourceCount: 8, |
| 78 | + ResourceStates: map[string]float64{ |
| 79 | + "skipped": 0, |
| 80 | + "failed": 0, |
| 81 | + "failed_to_restart": 0, |
| 82 | + "restarted": 0, |
| 83 | + "changed": 0, |
| 84 | + "out_of_sync": 0, |
| 85 | + "scheduled": 0, |
| 86 | + "corrective_change": 0, |
| 87 | + }, |
| 88 | + EventStates: map[string]float64{ |
| 89 | + "failure": 0, |
| 90 | + "success": 0, |
| 91 | + }, |
| 92 | + }, |
| 93 | + "last_run_report-8.8.1": interpretedReport{ |
| 94 | + RunAt: 1725776515.039312, |
| 95 | + RunDuration: 0.005837204, |
| 96 | + CatalogVersion: "1725776515", |
| 97 | + RunSuccess: 1, |
| 98 | + ResourceCount: 8, |
| 99 | + ResourceStates: map[string]float64{ |
| 100 | + "skipped": 0, |
| 101 | + "failed": 0, |
| 102 | + "failed_to_restart": 0, |
| 103 | + "restarted": 0, |
| 104 | + "changed": 0, |
| 105 | + "out_of_sync": 0, |
| 106 | + "scheduled": 0, |
| 107 | + "corrective_change": 0, |
| 108 | + }, |
| 109 | + EventStates: map[string]float64{ |
| 110 | + "failure": 0, |
| 111 | + "success": 0, |
| 112 | + }, |
| 113 | + }, |
23 | 114 | } |
24 | 115 |
|
25 | | - ir := report.interpret() |
26 | | - expected := interpretedReport{ |
27 | | - RunAt: 1618957125.5901103, |
28 | | - RunDuration: 17.199882286, |
29 | | - CatalogVersion: "1618957129", |
30 | | - RunSuccess: 1, |
| 116 | + for name, tc := range testCases { |
| 117 | + want := tc |
| 118 | + t.Run(name, func(t *testing.T) { |
| 119 | + report, err := load("testdata/" + name + ".yaml") |
| 120 | + if err != nil { |
| 121 | + t.Fatal(err) |
| 122 | + } |
| 123 | + |
| 124 | + got := report.interpret() |
| 125 | + |
| 126 | + if want.RunAt != got.RunAt { |
| 127 | + t.Fatalf("RunAt: want %f; got %f", want.RunAt, got.RunAt) |
| 128 | + } |
| 129 | + |
| 130 | + if want.RunDuration != got.RunDuration { |
| 131 | + t.Fatalf("RunDuration: want %f; got %f", want.RunDuration, got.RunDuration) |
| 132 | + } |
| 133 | + |
| 134 | + if want.CatalogVersion != got.CatalogVersion { |
| 135 | + t.Fatalf("CatalogVersion: want %q; got %q", want.CatalogVersion, got.CatalogVersion) |
| 136 | + } |
| 137 | + |
| 138 | + if want.RunSuccess != got.RunSuccess { |
| 139 | + t.Fatalf("RunSuccess: want %f; got %f", want.RunSuccess, got.RunSuccess) |
| 140 | + } |
| 141 | + |
| 142 | + if want.ResourceCount != got.ResourceCount { |
| 143 | + t.Fatalf("ResourceCount: want %f; got %f", want.ResourceCount, got.ResourceCount) |
| 144 | + } |
| 145 | + |
| 146 | + if want.ChangeCount != got.ChangeCount { |
| 147 | + t.Fatalf("ChangeCount: want %f; got %f", want.ChangeCount, got.ChangeCount) |
| 148 | + } |
| 149 | + |
| 150 | + if want.EventCount != got.EventCount { |
| 151 | + t.Fatalf("EventCount: want %f; got %f", want.EventCount, got.EventCount) |
| 152 | + } |
| 153 | + |
| 154 | + if err := mapCompare(want.ResourceStates, got.ResourceStates); err != "" { |
| 155 | + t.Fatalf("ResourceStates: %s", err) |
| 156 | + } |
| 157 | + |
| 158 | + if err := mapCompare(want.EventStates, got.EventStates); err != "" { |
| 159 | + t.Fatalf("EventStates: %s", err) |
| 160 | + } |
| 161 | + }) |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +func mapCompare(l, r map[string]float64) string { |
| 166 | + if len(l) != len(r) { |
| 167 | + return fmt.Sprintf("length: want %d, got %d", len(l), len(r)) |
31 | 168 | } |
32 | | - if ir != expected { |
33 | | - t.Fatalf("%+v != %+v", ir, expected) |
| 169 | + |
| 170 | + for k, want := range l { |
| 171 | + got, ok := r[k] |
| 172 | + if !ok { |
| 173 | + return fmt.Sprintf("key %q is missing", k) |
| 174 | + } |
| 175 | + |
| 176 | + if want != got { |
| 177 | + return fmt.Sprintf("key %q: want %f, got %f", k, want, got) |
| 178 | + } |
34 | 179 | } |
| 180 | + |
| 181 | + return "" |
35 | 182 | } |
0 commit comments