-
Notifications
You must be signed in to change notification settings - Fork 504
test(creds,dentry_cache): fix portability of integration tests on non-allowlisted GCP projects #4745
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
base: master
Are you sure you want to change the base?
test(creds,dentry_cache): fix portability of integration tests on non-allowlisted GCP projects #4745
Changes from all commits
9fb730d
d08314a
e966f89
bccd042
e76684f
bef265f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ import ( | |
| "os" | ||
| "slices" | ||
| "strings" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
|
|
||
|
|
@@ -39,22 +40,47 @@ import ( | |
| const NameOfServiceAccount = "creds-integration-tests" | ||
| const CredentialsSecretName = "gcsfuse-integration-tests" | ||
|
|
||
| var WhitelistedGcpProjects = []string{"gcs-fuse-test", "gcs-fuse-test-ml"} | ||
| var AllowlistedGcpProjects = []string{"gcs-fuse-test", "gcs-fuse-test-ml"} | ||
|
|
||
| var ( | ||
| cachedProjectID string | ||
| projectIDOnce sync.Once | ||
| ) | ||
|
|
||
| func getCachedProjectID(ctx context.Context) (string, error) { | ||
| var err error | ||
| projectIDOnce.Do(func() { | ||
| cachedProjectID, err = metadata.ProjectIDWithContext(ctx) | ||
| }) | ||
| return cachedProjectID, err | ||
| } | ||
|
|
||
| func IsAllowlistedProject(ctx context.Context) bool { | ||
| id, err := getCachedProjectID(ctx) | ||
| if err != nil { | ||
| log.Printf("Error in fetching project id: %v", err) | ||
| return false | ||
| } | ||
| if strings.Contains(id, "cloudtop") { | ||
| return true | ||
| } | ||
| return slices.Contains(AllowlistedGcpProjects, id) | ||
| } | ||
|
Comment on lines
+58
to
+68
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Every call to We can cache the fetched project ID in a package-level variable to avoid these duplicate requests. Additionally, please update the var (
projectIDCache string
projectIDCacheErr error
projectIDFetched bool
)
func fetchProjectID(ctx context.Context) (string, error) {
if !projectIDFetched {
projectIDCache, projectIDCacheErr = metadata.ProjectIDWithContext(ctx)
projectIDFetched = true
}
return projectIDCache, projectIDCacheErr
}
func IsAllowlistedProject(ctx context.Context) bool {
id, err := fetchProjectID(ctx)
if err != nil {
log.Printf("Error in fetching project id: %v", err)
return false
}
if strings.Contains(id, "cloudtop") {
return true
}
return slices.Contains(AllowlistedGcpProjects, id)
} |
||
|
|
||
| func projectID(ctx context.Context) string { | ||
| // Fetching project-id to get service account id. | ||
| id, err := metadata.ProjectIDWithContext(ctx) | ||
| id, err := getCachedProjectID(ctx) | ||
| if err != nil { | ||
| setup.LogAndExit(fmt.Sprintf("Error in fetching project id: %v", err)) | ||
| } | ||
| if strings.Contains(id, "cloudtop") { | ||
| // In cloudtop environments, well known path is used for auth. So explicitly set the project as whitelisted. | ||
| id = WhitelistedGcpProjects[0] | ||
| // In cloudtop environments, well known path is used for auth. So explicitly set the project as allowlisted. | ||
| id = AllowlistedGcpProjects[0] | ||
| } | ||
|
|
||
| // return if active GCP project is not in whitelisted gcp projects | ||
| if !slices.Contains(WhitelistedGcpProjects, id) { | ||
| log.Printf("The active GCP project is not one of: %s. So the credentials test will not run.", strings.Join(WhitelistedGcpProjects, ", ")) | ||
| // return if active GCP project is not in allowlisted gcp projects | ||
| if !slices.Contains(AllowlistedGcpProjects, id) { | ||
| log.Printf("The active GCP project is not one of: %s. So the credentials test will not run.", strings.Join(AllowlistedGcpProjects, ", ")) | ||
| } | ||
| return id | ||
| } | ||
|
|
@@ -161,6 +187,10 @@ func RevokeCustomRoleFromServiceAccountOnBucket(ctx context.Context, storageClie | |
| } | ||
|
|
||
| func RunTestsForDifferentAuthMethods(ctx context.Context, cfg *test_suite.TestConfig, storageClient *storage.Client, testFlagSet [][]string, permission string, m *testing.M) (successCode int) { | ||
| if !IsAllowlistedProject(ctx) { | ||
| log.Printf("The active GCP project is not one of: %s. So the credentials test will not run.", strings.Join(AllowlistedGcpProjects, ", ")) | ||
| return 0 | ||
| } | ||
| serviceAccount, localKeyFilePath := CreateCredentials(ctx) | ||
| defer func() { | ||
| if err := os.Remove(localKeyFilePath); err != nil { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,14 +52,29 @@ func ValidateESTALEError(t *testing.T, err error) { | |
| t.Helper() | ||
|
|
||
| require.Error(t, err) | ||
| assert.Regexp(t, syscall.ESTALE.Error(), err.Error()) | ||
| if !errors.Is(err, syscall.ESTALE) { | ||
| t.Fatalf("Expected error to be %q (ESTALE). Got: %v", syscall.ESTALE, err) | ||
| } | ||
| } | ||
|
|
||
| func ValidateESTALEOrEIOError(t *testing.T, err error) { | ||
| t.Helper() | ||
|
|
||
| require.Error(t, err) | ||
| // FUSE kernel driver can translate ESTALE to EIO (input/output error) on some operations/kernels. | ||
| // So we accept both "stale file handle" (ESTALE) and "input/output error" (EIO). | ||
| if !errors.Is(err, syscall.ESTALE) && !errors.Is(err, syscall.EIO) { | ||
| t.Fatalf("Expected error to be %q (ESTALE) or %q (EIO). Got: %v", syscall.ESTALE, syscall.EIO, err) | ||
| } | ||
| } | ||
|
Comment on lines
+60
to
+69
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using func ValidateESTALEOrEIOError(t *testing.T, err error) {
t.Helper()
require.Error(t, err)
// FUSE kernel driver can translate ESTALE to EIO (input/output error) on some operations/kernels.
// So we accept both "stale file handle" (ESTALE) and "input/output error" (EIO).
if !errors.Is(err, syscall.ESTALE) && !errors.Is(err, syscall.EIO) {
t.Fatalf("Expected error to be %v or %v. Got: %v", syscall.ESTALE, syscall.EIO, err)
}
} |
||
|
|
||
| func ValidateEIOError(t *testing.T, err error) { | ||
| t.Helper() | ||
|
|
||
| require.Error(t, err) | ||
| assert.Regexp(t, syscall.EIO.Error(), err.Error()) | ||
| if !errors.Is(err, syscall.EIO) { | ||
| t.Fatalf("Expected error to be %q (EIO). Got: %v", syscall.EIO, err) | ||
| } | ||
| } | ||
|
|
||
| func CheckErrorForReadOnlyFileSystem(t *testing.T, err error) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this package not run on GKE?