Skip to content

test(securitycenter): fix tests#5522

Draft
bhshkh wants to merge 3 commits intoGoogleCloudPlatform:mainfrom
bhshkh:tests/fix-securitycenter
Draft

test(securitycenter): fix tests#5522
bhshkh wants to merge 3 commits intoGoogleCloudPlatform:mainfrom
bhshkh:tests/fix-securitycenter

Conversation

@bhshkh
Copy link
Copy Markdown
Contributor

@bhshkh bhshkh commented Apr 16, 2026

Description

Fixes #

Note: Before submitting a pull request, please open an issue for discussion if you are not associated with Google.

Checklist

  • I have followed Contributing Guidelines from CONTRIBUTING.MD
  • Tests pass: go test -v ./.. (see Testing)
  • Code formatted: gofmt (see Formatting)
  • Vetting pass: go vet (see Formatting)
  • These samples need a new API enabled in testing projects to pass (let us know which ones)
  • These samples need a new/updated env vars in testing projects set to pass (let us know which ones)
  • This sample adds a new sample directory, and I updated the CODEOWNERS file with the codeowners for this sample
  • This sample adds a new Product API, and I updated the Blunderbuss issue/PR auto-assigner with the codeowners for this sample
  • Please merge this PR for me once it is approved

@product-auto-label product-auto-label Bot added api: securitycenter Issues related to the Security Command Center API. samples Issues that are directly related to samples. labels Apr 16, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines 190 to 206
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",
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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
  1. In Go, when wrapping errors with fmt.Errorf, use the %w verb instead of %v to preserve the original error for inspection by callers.

bhshkh and others added 2 commits April 15, 2026 18:04
…_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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: securitycenter Issues related to the Security Command Center API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant