test(securitycenter): fix tests#5522
test(securitycenter): fix tests#5522bhshkh wants to merge 3 commits intoGoogleCloudPlatform:mainfrom
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces retry logic for custom module tests and setup in both Event Threat Detection and Security Health Analytics to improve test stability. Feedback recommends using the more idiomatic structpb.NewStruct for configuration mapping instead of manual construction and ensuring that temporary test modules are registered for cleanup immediately after creation to avoid resource leaks during test failures.
| customModule := &securitycentermanagementpb.EventThreatDetectionCustomModule{ | ||
| Config: configStruct, | ||
| //Replace with desired Display Name. | ||
| Config: &structpb.Struct{ | ||
| Fields: map[string]*structpb.Value{ | ||
| "metadata": structpb.NewStructValue(&structpb.Struct{ | ||
| Fields: map[string]*structpb.Value{ | ||
| "description": structpb.NewStringValue("Sample custom module description"), | ||
| "severity": structpb.NewStringValue("HIGH"), | ||
| "recommendation": structpb.NewStringValue("Sample recommendation"), | ||
| }, | ||
| }), | ||
| "complianceStatus": structpb.NewStringValue("COMPLIANT"), | ||
| }, | ||
| }, | ||
| DisplayName: displayName, | ||
| EnablementState: securitycentermanagementpb.EventThreatDetectionCustomModule_ENABLED, | ||
| Type: "CONFIGURABLE_BAD_IP", | ||
| } |
There was a problem hiding this comment.
The manual construction of structpb.Struct is quite verbose and harder to maintain than using structpb.NewStruct. Using structpb.NewStruct with a map is the idiomatic way to create these structures in Go and significantly improves readability. Additionally, the ips field was removed in this version; if it is required for the CONFIGURABLE_BAD_IP type, it should be included. When wrapping errors with fmt.Errorf, use the %w verb instead of %v to preserve the original error for inspection by callers.
| customModule := &securitycentermanagementpb.EventThreatDetectionCustomModule{ | |
| Config: configStruct, | |
| //Replace with desired Display Name. | |
| Config: &structpb.Struct{ | |
| Fields: map[string]*structpb.Value{ | |
| "metadata": structpb.NewStructValue(&structpb.Struct{ | |
| Fields: map[string]*structpb.Value{ | |
| "description": structpb.NewStringValue("Sample custom module description"), | |
| "severity": structpb.NewStringValue("HIGH"), | |
| "recommendation": structpb.NewStringValue("Sample recommendation"), | |
| }, | |
| }), | |
| "complianceStatus": structpb.NewStringValue("COMPLIANT"), | |
| }, | |
| }, | |
| DisplayName: displayName, | |
| EnablementState: securitycentermanagementpb.EventThreatDetectionCustomModule_ENABLED, | |
| Type: "CONFIGURABLE_BAD_IP", | |
| } | |
| config, err := structpb.NewStruct(map[string]interface{}{ | |
| "metadata": map[string]interface{}{ | |
| "description": "Sample custom module description", | |
| "severity": "HIGH", | |
| "recommendation": "Sample recommendation", | |
| }, | |
| "complianceStatus": "COMPLIANT", | |
| }) | |
| if err != nil { | |
| return "", fmt.Errorf("structpb.NewStruct: %w", err) | |
| } | |
| customModule := &securitycentermanagementpb.EventThreatDetectionCustomModule{ | |
| Config: config, | |
| DisplayName: displayName, | |
| EnablementState: securitycentermanagementpb.EventThreatDetectionCustomModule_ENABLED, | |
| Type: "CONFIGURABLE_BAD_IP", | |
| } |
References
- In Go, when wrapping errors with
fmt.Errorf, use the%wverb instead of%vto preserve the original error for inspection by callers.
…_module_test.go Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
…eat_detection_custom_module_test.go Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Description
Fixes #
Note: Before submitting a pull request, please open an issue for discussion if you are not associated with Google.
Checklist
go test -v ./..(see Testing)gofmt(see Formatting)go vet(see Formatting)