Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(securitycenter): issues related to flaky tests in SHA & ETD #5090

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion securitycenter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/google/uuid v1.6.0
google.golang.org/api v0.196.0
google.golang.org/genproto v0.0.0-20240903143218-8af14fe29dc1
google.golang.org/grpc v1.66.0
google.golang.org/protobuf v1.34.2
)

Expand Down Expand Up @@ -44,5 +45,4 @@ require (
golang.org/x/time v0.6.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/grpc v1.66.0 // indirect
)
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import (
"context"
"fmt"
"io"
"math/rand"
"time"
"regexp"

securitycentermanagement "cloud.google.com/go/securitycentermanagement/apiv1"
securitycentermanagementpb "cloud.google.com/go/securitycentermanagement/apiv1/securitycentermanagementpb"
"github.com/google/uuid"
expr "google.golang.org/genproto/googleapis/type/expr"
)

Expand All @@ -42,10 +42,12 @@ func createSecurityHealthAnalyticsCustomModule(w io.Writer, parent string) error
}
defer client.Close()

// Seed the random number generator
rand.Seed(time.Now().UnixNano())
// Generate a unique suffix
uniqueSuffix := fmt.Sprintf("%d_%d", time.Now().Unix(), rand.Intn(1000))
uniqueSuffix := uuid.New().String()

// Remove invalid characters (anything that isn't alphanumeric or an underscore)
re := regexp.MustCompile(`[^a-zA-Z0-9_]`)
uniqueSuffix = re.ReplaceAllString(uniqueSuffix, "_")
Comment on lines +47 to +49

Choose a reason for hiding this comment

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

low

It's a good practice to precompile the regex for better performance.

47:	// Remove invalid characters (anything that isn't alphanumeric or an underscore)
48:	var re = regexp.MustCompile(`[^a-zA-Z0-9_]`)
49:	uniqueSuffix = re.ReplaceAllString(uniqueSuffix, "_")


// Create unique display name
displayName := fmt.Sprintf("go_sample_sha_custom_module_%s", uniqueSuffix)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ import (
"context"
"fmt"
"io"
"math/rand"
"time"

securitycentermanagement "cloud.google.com/go/securitycentermanagement/apiv1"
securitycentermanagementpb "cloud.google.com/go/securitycentermanagement/apiv1/securitycentermanagementpb"
"github.com/google/uuid"
"google.golang.org/protobuf/types/known/structpb"
)

Expand All @@ -42,10 +41,7 @@ func createEventThreatDetectionCustomModule(w io.Writer, parent string) error {
}
defer client.Close()

// Seed the random number generator
rand.Seed(time.Now().UnixNano())
// Generate a unique suffix
uniqueSuffix := fmt.Sprintf("%d-%d", time.Now().Unix(), rand.Intn(1000))
uniqueSuffix := uuid.New().String()

Choose a reason for hiding this comment

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

low

Consider adding logic to handle potential UUID collisions, similar to what's done in create_security_health_analytics_custom_module.go.

44:	uniqueSuffix := uuid.New().String()
45:
46:	// Remove invalid characters (anything that isn't alphanumeric or an underscore)
47:	re := regexp.MustCompile(`[^a-zA-Z0-9_]`)
48:	uniqueSuffix = re.ReplaceAllString(uniqueSuffix, "_")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed.

// Create unique display name
displayName := fmt.Sprintf("go_sample_etd_custom_module_%s", uniqueSuffix)

Expand Down
Loading
Loading