|
3 | 3 | package tfjson |
4 | 4 |
|
5 | 5 | import ( |
| 6 | + "encoding/json" |
6 | 7 | "testing" |
7 | 8 | "time" |
8 | 9 |
|
@@ -97,3 +98,70 @@ func TestLogging_generic(t *testing.T) { |
97 | 98 | } |
98 | 99 | } |
99 | 100 | } |
| 101 | + |
| 102 | +func TestLogging_query(t *testing.T) { |
| 103 | + testCases := []struct { |
| 104 | + rawMessage string |
| 105 | + expectedMessage LogMsg |
| 106 | + }{ |
| 107 | + { |
| 108 | + `{"@level":"info","@message":"list.concept_pet.pets: Starting query...","@module":"terraform.ui","@timestamp":"2025-08-28T18:07:11.534006+00:00","list_start":{"address":"list.concept_pet.pets","resource_type":"concept_pet"},"type":"list_start"}`, |
| 109 | + ListStartMessage{ |
| 110 | + baseLogMessage: baseLogMessage{ |
| 111 | + Lvl: Info, |
| 112 | + Msg: "list.concept_pet.pets: Starting query...", |
| 113 | + Time: time.Date(2025, 8, 28, 18, 7, 11, 534006000, time.UTC), |
| 114 | + }, |
| 115 | + ListStart: ListStartData{ |
| 116 | + Address: "list.concept_pet.pets", |
| 117 | + ResourceType: "concept_pet", |
| 118 | + InputConfig: nil, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + { |
| 123 | + `{"@level":"info","@message":"list.concept_pet.pets: Result found","@module":"terraform.ui","@timestamp":"2025-08-28T18:07:11.534589+00:00","list_resource_found":{"address":"list.concept_pet.pets","display_name":"This is a easy-antelope","identity":{"id":"easy-antelope","legs":6},"resource_type":"concept_pet"},"type":"list_resource_found"}`, |
| 124 | + ListResourceFoundMessage{ |
| 125 | + baseLogMessage: baseLogMessage{ |
| 126 | + Lvl: Info, |
| 127 | + Msg: "list.concept_pet.pets: Result found", |
| 128 | + Time: time.Date(2025, 8, 28, 18, 7, 11, 534589000, time.UTC), |
| 129 | + }, |
| 130 | + ListResourceFound: ListResourceFoundData{ |
| 131 | + Address: "list.concept_pet.pets", |
| 132 | + ResourceType: "concept_pet", |
| 133 | + DisplayName: "This is a easy-antelope", |
| 134 | + Identity: map[string]json.RawMessage{ |
| 135 | + "id": json.RawMessage(`"easy-antelope"`), |
| 136 | + "legs": json.RawMessage("6"), |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + { |
| 142 | + `{"@level":"info","@message":"list.concept_pet.pets: List complete","@module":"terraform.ui","@timestamp":"2025-08-28T18:07:11.534661+00:00","list_complete":{"address":"list.concept_pet.pets","resource_type":"concept_pet","total":5},"type":"list_complete"}`, |
| 143 | + ListCompleteMessage{ |
| 144 | + baseLogMessage: baseLogMessage{ |
| 145 | + Lvl: Info, |
| 146 | + Msg: "list.concept_pet.pets: List complete", |
| 147 | + Time: time.Date(2025, 8, 28, 18, 7, 11, 534661000, time.UTC), |
| 148 | + }, |
| 149 | + ListComplete: ListCompleteData{ |
| 150 | + Address: "list.concept_pet.pets", |
| 151 | + ResourceType: "concept_pet", |
| 152 | + Total: 5, |
| 153 | + }, |
| 154 | + }, |
| 155 | + }, |
| 156 | + } |
| 157 | + |
| 158 | + for _, tc := range testCases { |
| 159 | + msg, err := UnmarshalLogMessage([]byte(tc.rawMessage)) |
| 160 | + if err != nil { |
| 161 | + t.Fatal(err) |
| 162 | + } |
| 163 | + if diff := cmp.Diff(tc.expectedMessage, msg, cmpOpts); diff != "" { |
| 164 | + t.Fatalf("unexpected message: %s", diff) |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments