-
Notifications
You must be signed in to change notification settings - Fork 6.3k
feat: Add tests for - argocd admin import
command
#22780
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?
Conversation
Signed-off-by: yaten2302 <[email protected]>
❌ Preview Environment undeployed from BunnyshellAvailable commands (reply to this comment):
|
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #22780 +/- ##
==========================================
+ Coverage 60.27% 60.33% +0.06%
==========================================
Files 345 345
Lines 59097 59111 +14
==========================================
+ Hits 35618 35665 +47
+ Misses 20612 20566 -46
- Partials 2867 2880 +13 ☔ View full report in Codecov by Sentry. |
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 PR ready for review? I only see one test case.
@nitishfy , added 5 test cases 👍 |
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're required to add all the test cases that checks these scenarios:
- Resource should be pruned when --prune and missing from backup
- Resource should be created when missing from live
- Resource tracking annotation should not be updated when --ignore-tracking
- Application operation should removed when --stop-operation
- Update operation should retry on conflict when --override-on-conflict
- Resource should not be pruned if present in backup and backup resource matches --skip-resources-with-label
- Resource should not be pruned if missing from backup, but live matches --skip-resources-with-label
- Test application-namespace feature
- Test applicationset-namespace feature
- Dry run should not modify any resources
I'd suggest learning about these things one at a time. For eg. What is the use of import command, why do we use the prune flag, and what is the application namespace feature? Pick one test case at a time. Try to understand what this feature means, look at the code to see how it is written and then write a test case to validate it. You may be required to even put that logic in a new function and call that function in the test case to validate the scenario. |
Yes, sure, will add with the flags as well 👍 |
Yes, I've already read the entire input command code, and now I've a good understanding of the same. |
Signed-off-by: yaten2302 <[email protected]>
…flags Signed-off-by: yaten2302 <[email protected]>
Signed-off-by: yaten2302 <[email protected]>
Signed-off-by: yaten2302 <[email protected]>
Signed-off-by: yaten2302 <[email protected]>
Hey @nitishfy , could you kindly have a look at the tests for the |
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.
I like the structure of the tests, but reading a few different scenario names, I am not sure what it is actually testing. For instance, "It should update the live object according to the backup object if the backup object doesn't have the skip label". This test the happy path. where is the test that validates that it should not update the resource when it matches the label?
Similar comment for the following tests, they seem to all be testing the happy path, and not the conditional scenarios
- "Spec should be updated correctly in live object according to the backup object"
- "It should update the data of the live object according to the backup object"
- "It should update live object's spec according to the backup object"
Yes @agaudreault , actually I forgot to update the names of the test cases. Pushing the changes for the same 👍 |
Signed-off-by: yaten2302 <[email protected]>
…backup Signed-off-by: yaten2302 <[email protected]>
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.
Looks good! I'd wait for @agaudreault reviews before approving.
@agaudreault , a friendly follow up on this PR :) |
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
bakObj := decodeYAMLToUnstructured(t, tt.args.bak) |
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.
extract all the test setup code to a function, the method should be generic enough so that it accepts a liveObj and returns only the client. You can then provide the faeClient to the "import" method (needs refactoring) and validate the result by doing a Get on the liveObj after the import.
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.
@agaudreault , I didn't get this completely, could you kindly elaborate a bit more on this please?
Like, currently, I've created the decodeYAMLToUnstructured()
func, which accepts the YAML of backup and the live object and decodes them to Unstructured format.
Now, as you said that, it should accept the liveObj
and return only the client. I wasn't able to get this exactly?
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.
Hi @agaudreault , wanted to confirm that if I've understood this correctly, then I should extract this import test setup code to a new func let's say - setupImportTest()
. And then I should call this func to the Test_importResources()
?
Have I understood this correctly?
dynClient = dynamicClient.Resource(secretResource).Namespace(bakObj.GetNamespace()) | ||
} | ||
|
||
if slices.Contains(tt.applicationNamespaces, bakObj.GetNamespace()) || slices.Contains(tt.applicationsetNamespaces, bakObj.GetNamespace()) { |
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 test seems to re-implement the logic of the run function.
The unit test should be able to test a unit of code for which it can mock its dependencies. In this case, we want to test the NewImportCommand Run
function. However, it is quite complex to provide a "mock" of the Kubernetes dependency this way. Instead, extract a function that you can test, that will receive a fake Kubernetes client and the command arguments. This way, you can unit test that code.
Example: func (opts *importOpts) executeImport(client *dynamic.DynamicClient)
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.
+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.
Hi @nitishfy @agaudreault , if I've understood this correctly, then currently the behaviour is - "I'm rewriting the Run() func in the backup_test.go and then testing it."
Expected behaviour: "The test should be such that, instead of rewriting the Run() func, I should pass the fake kubernetes client and the command args, like - argocd admin import --dry-run import_file.txt
. Like, this command should be passed to the test and the fake kubernetes and then according to the command it'll generate the result"?
Have I understood this correctly?
Hi @nitishfy @agaudreault , wanted to update that I didn't get time to work on this PR. I'll push the changes in some time (most probably by this Saturday or Sunday). |
Signed-off-by: Yaten <[email protected]>
Hi @nitishfy @agaudreault , I've pushed a commit according to the suggestions. Since, I didn't have any reviews how to proceed, I tried to refactor the Run func and I broke it down into Kindly have a look at it, if I'm going in the correct direction? Thanks |
Fixes #21895
This PR adds unit test for

argocd admin import
command.Checklist: