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

Refactor Encryption logic for improved testing #645

Merged
merged 8 commits into from
Jan 14, 2025

Conversation

mallardduck
Copy link
Member

@mallardduck mallardduck commented Jan 14, 2025

Before the change there are 2 files in util; 0% and 22% respectively.
Before this change, the util package is currently at 50% coverage.

After the change there are 3 files (counting new sub package); 0%, 13% and 70.4% respectively.
After this change, the util package will be at 66.7% coverage.

@mallardduck mallardduck requested a review from a team as a code owner January 14, 2025 21:28
Comment on lines +29 to +30
encryptionConfigFilepath := filepath.Join("testdata", "encryption-provider-config-wildcard.yaml")
configBytes, _ := os.ReadFile(encryptionConfigFilepath)
Copy link
Contributor

@alexandreLamarre alexandreLamarre Jan 14, 2025

Choose a reason for hiding this comment

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

Instead of the filepath + os.ReadFile can use:

func Data(filename string) []byte {

this way the tests aren't dependent on specific paths

can also probably move that into a pkg/test instead of e2e/test if you want

Copy link
Member Author

@mallardduck mallardduck Jan 14, 2025

Choose a reason for hiding this comment

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

Maybe as a follow up PR we move that to test, and consider making it more generic? To use:

func Data(testFs embed.FS, filename string) []byte {

So then we can still keep organizing each pkgs test stubs in place?

NVM, I forgot we need to pass the k8s apis we're re-using a file path. So I'm not sure the Data() trick works here, since the apiserver code reads the bytes out the config file itself.

Making a mental note that this area is also something we could potentially workaround in the future with expansions to BROs logic around encryption.

@mallardduck mallardduck merged commit 5647b15 into rancher:main Jan 14, 2025
7 checks passed
@rancher rancher deleted a comment from github-actions bot Jan 14, 2025
@mallardduck
Copy link
Member Author

/backport release/v6.x

Copy link

Not creating port PR, there was an error running git am -3:

Applying: move encryption config specific functions to new package
Applying: refactor to use new package for encryption config
Using index info to reconstruct a base tree...
A	e2e/backup/backup_test.go
A	e2e/backup/setup_test.go
M	pkg/controllers/backup/controller.go
M	pkg/controllers/restore/controller.go
M	pkg/resourcesets/collector.go
M	pkg/util/util.go
Falling back to patching base and 3-way merge...
Auto-merging pkg/util/util.go
CONFLICT (content): Merge conflict in pkg/util/util.go
Auto-merging pkg/resourcesets/collector.go
Auto-merging pkg/controllers/restore/controller.go
Auto-merging pkg/controllers/backup/controller.go
CONFLICT (modify/delete): e2e/backup/setup_test.go deleted in HEAD and modified in refactor to use new package for encryption config. Version refactor to use new package for encryption config of e2e/backup/setup_test.go left in tree.
CONFLICT (modify/delete): e2e/backup/backup_test.go deleted in HEAD and modified in refactor to use new package for encryption config. Version refactor to use new package for encryption config of e2e/backup/backup_test.go left in tree.
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config advice.mergeConflict false"
Patch failed at 0002 refactor to use new package for encryption config

@mallardduck
Copy link
Member Author

Manual backporting it is...

Comment on lines +58 to +63
// Open the file for reading (or other operations) and return the handle
file, err := os.Open(EncryptionProviderConfigKey)
if err != nil {
return nil, err
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't notice this before, but you we should be careful about opening files inside a function and expecting others to close said file.

Can lead to concurrent closes or file descriptor leaks.
We should look into alternative return types instead of os.File. I also don't see this being used by anything other than the tests at the moment.

Will we need this inside the controller logic?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants