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

Add additional test coverage for helper functions in Util #4651

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

gajananan
Copy link
Contributor

@gajananan gajananan commented Oct 5, 2024

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:

  • Bug fix (resolves an issue without affecting existing features)
  • Feature (adds new functionality without breaking changes)
  • Breaking change (may impact existing functionalities or require documentation updates)
  • Documentation (updates or additions to documentation)
  • Refactoring or test improvements (no bug fixes or new functionality)

Testing

Outline how the changes were tested, including steps to reproduce and any relevant configurations.
Attach screenshots if helpful.

Review Checklist:

  • Reviewed my own code for quality and clarity.
  • Added comments to complex or tricky code sections.
  • Updated any affected documentation.
  • Included tests that validate the fix or feature.
  • Checked that related changes are merged.

Currently helper functions in Util lacks test coverage

This commit adds test coverage helper functions in Util

Signed-off-by: Kugamoorthy Gajananan <[email protected]>
@coveralls
Copy link

coveralls commented Oct 5, 2024

Coverage Status

coverage: 53.769% (+0.5%) from 53.264%
when pulling 00dd4ba on gajananan:improve-test-coverage-util-helpers
into 49f192a on stacklok:main.

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]>
@gajananan gajananan marked this pull request as ready for review October 8, 2024 12:15
t.Parallel()

// Create a unique directory for each test
testDir := fmt.Sprintf("testdir_%s", sanitizeTestName(tt.name))
Copy link
Contributor

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?

Copy link
Contributor Author

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)
Copy link
Member

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.

Copy link
Member

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.

Comment on lines +232 to +241
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")
Copy link
Member

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:

  1. Use t.TempDir() to create a temp directory.
  2. Set XDG_CONFIG_HOME to point at the directory from (1).

Copy link
Contributor Author

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)
Copy link
Member

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)
Copy link
Member

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.

Comment on lines +398 to +399
originalEnv := os.Getenv("XDG_CONFIG_HOME")
err = os.Setenv("XDG_CONFIG_HOME", tempDir)
Copy link
Member

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)
Copy link
Member

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()

Copy link
Contributor Author

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()

Copy link
Member

Choose a reason for hiding this comment

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

Yes, thanks!

@evankanderson
Copy link
Member

@gajananan

It looks like this is close to the finish line -- any chance you can incorporate the feedback (and fix conflicts) in the next week?

@evankanderson
Copy link
Member

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.

@evankanderson
Copy link
Member

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!

Copy link
Contributor

This PR needs additional information before we can continue. It is now marked as stale because it has been open for 30 days with no activity. Please provide the necessary details to continue or it will be closed in 30 days.

@github-actions github-actions bot added the Stale label Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants