Skip to content

Commit 7eb671f

Browse files
committed
test: Add tests for parsing logs from init commands
1 parent 41f8aaf commit 7eb671f

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

logging_test.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,124 @@ func TestLogging_query(t *testing.T) {
165165
}
166166
}
167167
}
168+
169+
// Includes a typical sequence of logs that happen when initializing a working directory
170+
//
171+
// Currently `init` creates some logs with "type":"log" and others with "type":"init_output"
172+
// Type "init_output" logs include a specific field called "message_code" that takes a string value.
173+
func TestLogging_init(t *testing.T) {
174+
testCases := []struct {
175+
rawMessage string
176+
expectedMessage LogMsg
177+
}{
178+
{
179+
`{"@level":"info","@message":"Terraform 1.15.0-dev","@module":"terraform.ui","@timestamp":"2025-11-17T17:17:58.540604Z","terraform":"1.15.0-dev","type":"version","ui":"1.2"}`,
180+
VersionLogMessage{
181+
baseLogMessage: baseLogMessage{
182+
Lvl: Info,
183+
Msg: "Terraform 1.15.0-dev",
184+
Time: time.Date(2025, 11, 17, 17, 17, 58, 540604000, time.UTC),
185+
},
186+
Terraform: version.Must(version.NewSemver("1.15.0-dev")),
187+
UI: version.Must(version.NewSemver("1.2.0")),
188+
},
189+
},
190+
{
191+
`{"@level":"info","@message":"Initializing provider plugins found in the configuration...","@module":"terraform.ui","@timestamp":"2025-11-17T17:18:04.314Z","message_code":"initializing_provider_plugin_from_config_message","type":"init_output"}`,
192+
UnknownLogMessage{
193+
baseLogMessage: baseLogMessage{
194+
Lvl: Info,
195+
Msg: "Initializing provider plugins found in the configuration...",
196+
Time: time.Date(2025, 11, 17, 17, 18, 04, 314000000, time.UTC),
197+
},
198+
},
199+
},
200+
{
201+
`{"@level":"info","@message":"hashicorp/aws: Finding latest version...","@module":"terraform.ui","@timestamp":"2025-11-17T17:18:04.314594Z","type":"log"}`,
202+
LogMessage{
203+
baseLogMessage: baseLogMessage{
204+
Lvl: Info,
205+
Msg: "hashicorp/aws: Finding latest version...",
206+
Time: time.Date(2025, 11, 17, 17, 18, 04, 314594000, time.UTC),
207+
},
208+
},
209+
},
210+
{
211+
`{"@level":"info","@message":"Installing provider version: hashicorp/aws v6.21.0...","@module":"terraform.ui","@timestamp":"2025-11-17T17:18:04.784659Z","type":"log"}`,
212+
LogMessage{
213+
baseLogMessage: baseLogMessage{
214+
Lvl: Info,
215+
Msg: "Installing provider version: hashicorp/aws v6.21.0...",
216+
Time: time.Date(2025, 11, 17, 17, 18, 04, 784659000, time.UTC),
217+
},
218+
},
219+
},
220+
{
221+
`{"@level":"info","@message":"Installed provider version: hashicorp/aws v6.21.0 (signed by HashiCorp)","@module":"terraform.ui","@timestamp":"2025-11-17T17:18:26.345919Z","type":"log"}`,
222+
LogMessage{
223+
baseLogMessage: baseLogMessage{
224+
Lvl: Info,
225+
Msg: "Installed provider version: hashicorp/aws v6.21.0 (signed by HashiCorp)",
226+
Time: time.Date(2025, 11, 17, 17, 18, 26, 345919000, time.UTC),
227+
},
228+
},
229+
},
230+
{
231+
`{"@level":"info","@message":"Initializing the backend...","@module":"terraform.ui","@timestamp":"2025-11-17T17:18:52.256Z","message_code":"initializing_backend_message","type":"init_output"}`,
232+
UnknownLogMessage{
233+
baseLogMessage: baseLogMessage{
234+
Lvl: Info,
235+
Msg: "Initializing the backend...",
236+
Time: time.Date(2025, 11, 17, 17, 18, 52, 256000000, time.UTC),
237+
},
238+
},
239+
},
240+
// At this point in an init command's output there is a log message that isn't presented in JSON format:
241+
// /*
242+
// Successfully configured the backend "local"! Terraform will automatically
243+
// use this backend unless the backend configuration changes.
244+
// */
245+
//
246+
// See this GitHub issue: https://github.com/hashicorp/terraform/issues/37911
247+
{
248+
`{"@level":"info","@message":"Terraform has created a lock file .terraform.lock.hcl to record the provider\nselections it made above. Include this file in your version control repository\nso that Terraform can guarantee to make the same selections by default when\nyou run \"terraform init\" in the future.","@module":"terraform.ui","@timestamp":"2025-11-17T17:19:06.698Z","message_code":"lock_info","type":"init_output"}`,
249+
UnknownLogMessage{
250+
baseLogMessage: baseLogMessage{
251+
Lvl: Info,
252+
Msg: "Terraform has created a lock file .terraform.lock.hcl to record the provider\nselections it made above. Include this file in your version control repository\nso that Terraform can guarantee to make the same selections by default when\nyou run \"terraform init\" in the future.",
253+
Time: time.Date(2025, 11, 17, 17, 19, 06, 698000000, time.UTC),
254+
},
255+
},
256+
},
257+
{
258+
`{"@level":"info","@message":"Terraform has been successfully initialized!","@module":"terraform.ui","@timestamp":"2025-11-17T17:19:09.915Z","message_code":"output_init_success_message","type":"init_output"}`,
259+
UnknownLogMessage{
260+
baseLogMessage: baseLogMessage{
261+
Lvl: Info,
262+
Msg: "Terraform has been successfully initialized!",
263+
Time: time.Date(2025, 11, 17, 17, 19, 9, 915000000, time.UTC),
264+
},
265+
},
266+
},
267+
{
268+
`{"@level":"info","@message":"You may now begin working with Terraform. Try running \"terraform plan\" to see\nany changes that are required for your infrastructure. All Terraform commands\nshould now work.\n\nIf you ever set or change modules or backend configuration for Terraform,\nrerun this command to reinitialize your working directory. If you forget, other\ncommands will detect it and remind you to do so if necessary.","@module":"terraform.ui","@timestamp":"2025-11-17T17:19:10.553Z","message_code":"output_init_success_cli_message","type":"init_output"}`,
269+
UnknownLogMessage{
270+
baseLogMessage: baseLogMessage{
271+
Lvl: Info,
272+
Msg: "You may now begin working with Terraform. Try running \"terraform plan\" to see\nany changes that are required for your infrastructure. All Terraform commands\nshould now work.\n\nIf you ever set or change modules or backend configuration for Terraform,\nrerun this command to reinitialize your working directory. If you forget, other\ncommands will detect it and remind you to do so if necessary.",
273+
Time: time.Date(2025, 11, 17, 17, 19, 10, 553000000, time.UTC),
274+
},
275+
},
276+
},
277+
}
278+
279+
for _, tc := range testCases {
280+
msg, err := UnmarshalLogMessage([]byte(tc.rawMessage))
281+
if err != nil {
282+
t.Fatal(err)
283+
}
284+
if diff := cmp.Diff(tc.expectedMessage, msg, cmpOpts); diff != "" {
285+
t.Fatalf("unexpected message: %s", diff)
286+
}
287+
}
288+
}

0 commit comments

Comments
 (0)