-
Notifications
You must be signed in to change notification settings - Fork 42
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
Add additional test coverage for helper functions in Util #4651
base: main
Are you sure you want to change the base?
Add additional test coverage for helper functions in Util #4651
Conversation
Currently helper functions in Util lacks test coverage This commit adds test coverage helper functions in Util Signed-off-by: Kugamoorthy Gajananan <[email protected]>
Currently helper functions in Util lacks test coverage This commit fixes the lint errors and handles errors. Signed-off-by: Kugamoorthy Gajananan <[email protected]>
Currently helper functions in Util lacks test coverage This commit - fixes the lint errors and handles errors. - adds test coverage for OpenFileArg, ExpandFileArgs, functions Signed-off-by: Kugamoorthy Gajananan <[email protected]>
Currently helper functions in Util lacks test coverage. This commit - fixes the lint errors and handles errors. - Adds test coverage for LoadCredentials function Signed-off-by: Kugamoorthy Gajananan <[email protected]>
Currently helper functions in Util lacks test coverage This commit fixes the lint errors after fixing lint version. Signed-off-by: Kugamoorthy Gajananan <[email protected]>
Currently helper functions in Util lacks test coverage This commit adds test coverage for RevokeToken function. Signed-off-by: Kugamoorthy Gajananan <[email protected]>
t.Parallel() | ||
|
||
// Create a unique directory for each test | ||
testDir := fmt.Sprintf("testdir_%s", sanitizeTestName(tt.name)) |
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.
You could probably leverage golang's utilities instead: https://pkg.go.dev/testing#B.TempDir
This will create a temporary directory for tests and even clean up afterwards.
wdyt?
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.
thank you. I agree. that is a clean approach.
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
err := os.Setenv("XDG_CONFIG_HOME", tt.envVar) |
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.
Doing this in t.Parallel
may cause interference with other tests.
What I've done in the past is to put some explicit locks on these tests; without that, these tests will tend to be flaky when running on multiple cores at the same time.
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.
Additionally, you'll want to reset the value of XDG_CONFIG_HOME
after this test. You can use t.Setenv()
to do this.
xdgConfigHome := os.Getenv("XDG_CONFIG_HOME") | ||
if xdgConfigHome == "" { | ||
homeDir, err := os.UserHomeDir() | ||
if err != nil { | ||
t.Fatalf("error getting home directory: %v", err) | ||
} | ||
xdgConfigHome = filepath.Join(homeDir, ".config") | ||
} | ||
|
||
filePath := filepath.Join(xdgConfigHome, "minder", "credentials.json") |
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.
This environment-related stuff is going to conflict with TestGetGrpcConnection; it will also end up touching the user's actual home directory.
What I would do:
- Use
t.TempDir()
to create a temp directory. - Set
XDG_CONFIG_HOME
to point at the directory from (1).
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.
Thank you. it is good to learn about the impact to other tests.
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
err := os.Setenv(MinderAuthTokenEnvVar, tt.envToken) |
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.
Again, this use of env vars is likely to cause trouble. At a minimum, you should restore the previous value of the environment variable at the end of the t.Run()
function (e.g. with defer
)
parsedURL, _ := url.Parse(server.URL) | ||
tt.issuerUrl = parsedURL.String() | ||
|
||
result, err := RefreshCredentials(tt.refreshToken, tt.issuerUrl, tt.clientId) |
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.
Since this function is not side-effect-free, you may need to again set XDG_CONFIG_HOME
to point to a different location.
originalEnv := os.Getenv("XDG_CONFIG_HOME") | ||
err = os.Setenv("XDG_CONFIG_HOME", tempDir) |
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.
t.Setenv()
will do this for you.
|
||
// Create a temporary file for testing | ||
if tc.filePath == "testfile.txt" { | ||
err := os.WriteFile(tc.filePath, []byte(tc.expectedDesc), 0600) |
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.
We should do this in t.TestDir()
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.
Thank you. I assume, you were referring to t.TempDir()
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.
Yes, thanks!
It looks like this is close to the finish line -- any chance you can incorporate the feedback (and fix conflicts) in the next week? |
Hi @gajananan, We'd love to get this merged! If you don't have time to work on this, we'll probably have someone over here at Stacklok apply the requested changes and merge it. |
Hey @gajananan -- we'd love to get this PR in. I ran into one of your co-workers at KubeCon, and they said that your work schedule might be preventing you from updating. A quick note of "I can get to this within a week" or "please carry this forward, I'm busy now" would be handy. Thanks again for your work so far! |
Summary
Currently helper functions in Util lacks test coverage
This commit adds test coverage helper functions in Util
Ref #4380
Change Type
Mark the type of change your PR introduces:
Testing
Outline how the changes were tested, including steps to reproduce and any relevant configurations.
Attach screenshots if helpful.
Review Checklist: