Skip to content

Commit af59da8

Browse files
integration_test: Create RunForEachLoggingSubagent to improve otel logging integration tests. (GoogleCloudPlatform#1943)
1 parent 25c150d commit af59da8

1 file changed

Lines changed: 87 additions & 89 deletions

File tree

integration_test/ops_agent_test/main_test.go

Lines changed: 87 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,26 @@ func retrieveOtelConfig(ctx context.Context, logger *log.Logger, vm *gce.VM) (co
206206
return gce.RetrieveContent(ctx, logger, vm, agents.GetOtelConfigPath(vm.ImageSpec))
207207
}
208208

209+
// RunForEachLoggingSubagent runs a subtest for the logging subagent fluent-bit and otel.
210+
func RunForEachLoggingSubagent(t *testing.T, testBody func(t *testing.T, otel bool)) {
211+
t.Helper()
212+
t.Run("fluent-bit", func(t *testing.T) {
213+
testBody(t, false)
214+
})
215+
216+
t.Run("otel", func(t *testing.T) {
217+
if gce.IsOpsAgentUAPPlugin() {
218+
t.SkipNow()
219+
}
220+
testBody(t, true)
221+
})
222+
}
223+
224+
// setExperimentalFeatures sets the EXPERIMENTAL_FEATURES environment variable.
225+
func setExperimentalFeatures(ctx context.Context, logger *log.Logger, vm *gce.VM, feature string) error {
226+
return gce.SetEnvironmentVariables(ctx, logger, vm, map[string]string{"EXPERIMENTAL_FEATURES": feature})
227+
}
228+
209229
func TestParseMultilineFileJava(t *testing.T) {
210230
t.Parallel()
211231
gce.RunForEachImage(t, func(t *testing.T, imageSpec string) {
@@ -1844,25 +1864,14 @@ func TestResourceNameLabel(t *testing.T) {
18441864

18451865
func TestLogFilePathLabel(t *testing.T) {
18461866
t.Parallel()
1847-
if gce.IsOpsAgentUAPPlugin() {
1848-
t.SkipNow()
1849-
}
1850-
t.Run("fluent-bit", func(t *testing.T) {
1851-
testLogFilePathLabel(t, false)
1852-
})
1853-
t.Run("otel", func(t *testing.T) {
1854-
testLogFilePathLabel(t, true)
1855-
})
1856-
}
1857-
1858-
func testLogFilePathLabel(t *testing.T, otel bool) {
1859-
t.Parallel()
1860-
gce.RunForEachImage(t, func(t *testing.T, imageSpec string) {
1867+
RunForEachLoggingSubagent(t, func(t *testing.T, otel bool) {
18611868
t.Parallel()
1862-
ctx, logger, vm := setupMainLogAndVM(t, imageSpec)
1863-
file1 := fmt.Sprintf("%s_1", logPathForImage(vm.ImageSpec))
1869+
gce.RunForEachImage(t, func(t *testing.T, imageSpec string) {
1870+
t.Parallel()
1871+
ctx, logger, vm := setupMainLogAndVM(t, imageSpec)
1872+
file1 := fmt.Sprintf("%s_1", logPathForImage(vm.ImageSpec))
18641873

1865-
config := fmt.Sprintf(`logging:
1874+
config := fmt.Sprintf(`logging:
18661875
receivers:
18671876
f1:
18681877
type: files
@@ -1880,34 +1889,34 @@ func testLogFilePathLabel(t *testing.T, otel bool) {
18801889
processors: [json]
18811890
`, file1, otel)
18821891

1883-
if otel {
1884-
// Turn on the otel feature gate.
1885-
if err := gce.SetEnvironmentVariables(ctx, logger, vm, map[string]string{"EXPERIMENTAL_FEATURES": "otel_logging"}); err != nil {
1886-
t.Fatal(err)
1892+
if otel {
1893+
if err := setExperimentalFeatures(ctx, logger, vm, "otel_logging"); err != nil {
1894+
t.Fatal(err)
1895+
}
18871896
}
1888-
}
18891897

1890-
if err := agents.SetupOpsAgent(ctx, logger, vm, config); err != nil {
1891-
t.Fatal(err)
1892-
}
1898+
if err := agents.SetupOpsAgent(ctx, logger, vm, config); err != nil {
1899+
t.Fatal(err)
1900+
}
18931901

1894-
line := `{"default_present":"original"}` + "\n"
1895-
if err := gce.UploadContent(ctx, logger, vm, strings.NewReader(line), file1); err != nil {
1896-
t.Fatalf("error uploading log: %v", err)
1897-
}
1902+
line := `{"default_present":"original"}` + "\n"
1903+
if err := gce.UploadContent(ctx, logger, vm, strings.NewReader(line), file1); err != nil {
1904+
t.Fatalf("error uploading log: %v", err)
1905+
}
18981906

1899-
// In Windows the generated log_file_path "C:\mylog_1" uses a backslash.
1900-
// When constructing the query in WaithForLog the backslashes are escaped so
1901-
// replacing with two backslahes correctly queries for "C:\mylog_1" label.
1902-
if gce.IsWindows(imageSpec) {
1903-
file1 = strings.Replace(file1, `\`, `\\`, 1)
1904-
}
1907+
// In Windows the generated log_file_path "C:\mylog_1" uses a backslash.
1908+
// When constructing the query in WaithForLog the backslashes are escaped so
1909+
// replacing with two backslahes correctly queries for "C:\mylog_1" label.
1910+
if gce.IsWindows(imageSpec) {
1911+
file1 = strings.Replace(file1, `\`, `\\`, 1)
1912+
}
19051913

1906-
// Expect to see log with label added.
1907-
check := fmt.Sprintf(`labels."agent.googleapis.com/log_file_path"="%s" AND jsonPayload.default_present="original"`, file1)
1908-
if err := gce.WaitForLog(ctx, logger, vm, "f1", time.Hour, check); err != nil {
1909-
t.Error(err)
1910-
}
1914+
// Expect to see log with label added.
1915+
check := fmt.Sprintf(`labels."agent.googleapis.com/log_file_path"="%s" AND jsonPayload.default_present="original"`, file1)
1916+
if err := gce.WaitForLog(ctx, logger, vm, "f1", time.Hour, check); err != nil {
1917+
t.Error(err)
1918+
}
1919+
})
19111920
})
19121921
}
19131922

@@ -2492,27 +2501,16 @@ func TestWindowsEventLogWithNonDefaultTimeZone(t *testing.T) {
24922501

24932502
func TestSystemdLog(t *testing.T) {
24942503
t.Parallel()
2495-
if gce.IsOpsAgentUAPPlugin() {
2496-
t.SkipNow()
2497-
}
2498-
t.Run("fluent-bit", func(t *testing.T) {
2499-
testSystemdLog(t, false)
2500-
})
2501-
t.Run("otel", func(t *testing.T) {
2502-
testSystemdLog(t, true)
2503-
})
2504-
}
2505-
2506-
func testSystemdLog(t *testing.T, otel bool) {
2507-
t.Parallel()
2508-
gce.RunForEachImage(t, func(t *testing.T, imageSpec string) {
2504+
RunForEachLoggingSubagent(t, func(t *testing.T, otel bool) {
25092505
t.Parallel()
2510-
if gce.IsWindows(imageSpec) {
2511-
t.SkipNow()
2512-
}
2513-
ctx, logger, vm := setupMainLogAndVM(t, imageSpec)
2506+
gce.RunForEachImage(t, func(t *testing.T, imageSpec string) {
2507+
t.Parallel()
2508+
if gce.IsWindows(imageSpec) {
2509+
t.SkipNow()
2510+
}
2511+
ctx, logger, vm := setupMainLogAndVM(t, imageSpec)
25142512

2515-
config := fmt.Sprintf(`logging:
2513+
config := fmt.Sprintf(`logging:
25162514
receivers:
25172515
systemd_logs:
25182516
type: systemd_journald
@@ -2523,44 +2521,44 @@ func testSystemdLog(t *testing.T, otel bool) {
25232521
receivers: [systemd_logs]
25242522
`, otel)
25252523

2526-
if otel {
2527-
// Turn on the otel feature gate.
2528-
if err := gce.SetEnvironmentVariables(ctx, logger, vm, map[string]string{"EXPERIMENTAL_FEATURES": "otel_logging"}); err != nil {
2529-
t.Fatal(err)
2524+
if otel {
2525+
if err := setExperimentalFeatures(ctx, logger, vm, "otel_logging"); err != nil {
2526+
t.Fatal(err)
2527+
}
25302528
}
2531-
}
25322529

2533-
if err := agents.SetupOpsAgent(ctx, logger, vm, config); err != nil {
2534-
t.Fatal(err)
2535-
}
2530+
if err := agents.SetupOpsAgent(ctx, logger, vm, config); err != nil {
2531+
t.Fatal(err)
2532+
}
25362533

2537-
if _, err := gce.RunRemotely(ctx, logger, vm, "echo 'my_systemd_info_log_message' | systemd-cat --priority=info"); err != nil {
2538-
t.Fatalf("Error writing dummy Systemd log line: %v", err)
2539-
}
2534+
if _, err := gce.RunRemotely(ctx, logger, vm, "echo 'my_systemd_info_log_message' | systemd-cat --priority=info"); err != nil {
2535+
t.Fatalf("Error writing dummy Systemd log line: %v", err)
2536+
}
25402537

2541-
querySystemdInfoLog := fmt.Sprintf(`severity="INFO" AND jsonPayload.MESSAGE="my_systemd_info_log_message" AND jsonPayload.PRIORITY="6"`)
2542-
if err := gce.WaitForLog(ctx, logger, vm, "systemd_logs", time.Hour, querySystemdInfoLog); err != nil {
2543-
t.Error(err)
2544-
}
2538+
querySystemdInfoLog := fmt.Sprintf(`severity="INFO" AND jsonPayload.MESSAGE="my_systemd_info_log_message" AND jsonPayload.PRIORITY="6"`)
2539+
if err := gce.WaitForLog(ctx, logger, vm, "systemd_logs", time.Hour, querySystemdInfoLog); err != nil {
2540+
t.Error(err)
2541+
}
25452542

2546-
if _, err := gce.RunRemotely(ctx, logger, vm, "echo 'my_systemd_error_log_message' | systemd-cat --priority=err"); err != nil {
2547-
t.Fatalf("Error writing dummy Systemd log line: %v", err)
2548-
}
2543+
if _, err := gce.RunRemotely(ctx, logger, vm, "echo 'my_systemd_error_log_message' | systemd-cat --priority=err"); err != nil {
2544+
t.Fatalf("Error writing dummy Systemd log line: %v", err)
2545+
}
25492546

2550-
querySystemdErrorLog := fmt.Sprintf(`severity="ERROR" AND jsonPayload.MESSAGE="my_systemd_error_log_message" AND jsonPayload.PRIORITY="3"`)
2551-
if err := gce.WaitForLog(ctx, logger, vm, "systemd_logs", time.Hour, querySystemdErrorLog); err != nil {
2552-
t.Error(err)
2553-
}
2547+
querySystemdErrorLog := fmt.Sprintf(`severity="ERROR" AND jsonPayload.MESSAGE="my_systemd_error_log_message" AND jsonPayload.PRIORITY="3"`)
2548+
if err := gce.WaitForLog(ctx, logger, vm, "systemd_logs", time.Hour, querySystemdErrorLog); err != nil {
2549+
t.Error(err)
2550+
}
25542551

2555-
// TODO: b/400435104 - Re-enable when the `googlecloudexporter` supports all LogSeverity levels.
2556-
// if _, err := gce.RunRemotely(ctx, logger, vm, "echo 'my_systemd_notice_log_message' | systemd-cat --priority=notice"); err != nil {
2557-
// t.Fatalf("Error writing dummy Systemd log line: %v", err)
2558-
// }
2552+
// TODO: b/400435104 - Re-enable when the `googlecloudexporter` supports all LogSeverity levels.
2553+
// if _, err := gce.RunRemotely(ctx, logger, vm, "echo 'my_systemd_notice_log_message' | systemd-cat --priority=notice"); err != nil {
2554+
// t.Fatalf("Error writing dummy Systemd log line: %v", err)
2555+
// }
25592556

2560-
// querySystemdNoticeLog := fmt.Sprintf(`severity="NOTICE" AND jsonPayload.MESSAGE="my_systemd_notice_log_message" AND jsonPayload.PRIORITY="5"`)
2561-
// if err := gce.WaitForLog(ctx, logger, vm, "systemd_logs", time.Hour, querySystemdNoticeLog); err != nil {
2562-
// t.Error(err)
2563-
// }
2557+
// querySystemdNoticeLog := fmt.Sprintf(`severity="NOTICE" AND jsonPayload.MESSAGE="my_systemd_notice_log_message" AND jsonPayload.PRIORITY="5"`)
2558+
// if err := gce.WaitForLog(ctx, logger, vm, "systemd_logs", time.Hour, querySystemdNoticeLog); err != nil {
2559+
// t.Error(err)
2560+
// }
2561+
})
25642562
})
25652563
}
25662564

0 commit comments

Comments
 (0)