-
Notifications
You must be signed in to change notification settings - Fork 47
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
Unit tests for waitForOperationToComplete, isOperationComplete (#688) #614
Unit tests for waitForOperationToComplete, isOperationComplete (#688) #614
Conversation
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## main #614 +/- ##
==========================================
+ Coverage 60.94% 61.14% +0.19%
==========================================
Files 99 99
Lines 17865 17869 +4
==========================================
+ Hits 10888 10926 +38
+ Misses 5755 5720 -35
- Partials 1222 1223 +1
☔ View full report in Codecov by Sentry. |
@keithchong Left few comments, other than that everything LGTM , thank you |
/lgtm |
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 great, thanks @keithchong and thanks @Rizwana777 for reviewing! Some small tweaks then good to go!
Re: service_account.go, I was confused, but I understand this now: I'm fine with leaving service_account.go out of this PR/story.
- I figured out why it wasn't running in some cases: it requires a valid cluster to be defined in kubeconfig, otherwise it gets skipped (I had forgotten we had added that logic).
And one final request:
It looks like the following block in IsOperationComplete
is not covered:
// Either the operation couldn't be found (which shouldn't happen here), or some other issue, so return it
return false, err
It should be a quick fix to just call IsOperationComplete with a fake db.Operation
that doesn't actually exist in the database.
Expect(operationget.State).To(BeIdenticalTo(db.OperationState_Waiting)) | ||
|
||
// Update the operation state to Completed | ||
operationget.State = db.OperationState_Completed | ||
err = dbq.UpdateOperation(ctx, &operationget) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
// Get the operation again and check state | ||
err = dbq.GetOperationById(ctx, &operationget) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(operationget.State).To(BeIdenticalTo(db.OperationState_Completed)) | ||
|
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'm not 100% sure, but I think these Expect functions should instead be return
calls: I'm not sure if Expect
is supported inside an Eventually (I had presumed that it is not, but if you want to test it to prove me wrong, feel free!)
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.
With the existing code that runs/passes successfully, I simply changed a ToNot
to a To
, and reran it again, and a failure was reported.
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.
@keithchong Ah, I guess one more question: if one of the Expect
calls does fail, does it permanently fail the test (I would assume so), or does it keep trying via the loop?
My guess is that we don't want any of those to be Expect calls to be permanent fails (e.g. a failed Expect will immediately fail the full test), we want the loop to keep trying over and over until it succeeds (or times out and thus fails).
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.
It will fail immediately and will not retry. I think that if it cannot set the operation state to Completed, for example, then it probably will eventually fail repeatedly as well and it will 'eventually' time out. Perhaps it's similar to testing these conditions in the main thread as the primary test, and if it fails, then there is no point in continuing.
This is why the only situation where I return false is when checking if the operation exists. This is because the main thread creates the operation. So 'eventually', it will be created, so it's ok to loop until it exists. The other checks are a bit like 'primary' tests.
I have changed it all to return false. Either way, I think we will know the condition that failed, whether it is directly on the Expect or whether it is repeatedly called and the condition isn't satisfied and it will time out.
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.
Regarding the loop, I moved the location of the loop to better show why we need it. I want to defer setting the operation state. It does make a difference in the behaviour of the test. If we want to allow the for loop in waitForOperationToComplete to loop several times, and allow isOperationComplete to be called several times as well, then we need it. I suppose this test behaviour goes beyond code coverage.
// Allow waitForOperationToComplete to iterate/wait | ||
time.Sleep(2 * time.Second) |
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.
Would it be possible to remove the time.Sleep
? I ask because Sleep statements in test make me very wary (as a source for race conditions)... I checked the logic of the test and I THINK this time.Sleep
isn't actually doing anything, so it should be easy to remove.
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.
The purpose of the delay is to give time for the for loop in waitForOperationToComplete
in types.go to iterate a few times. Removing the delay does seems to work ok, and the for loop gets executed once.
Try adding a Println just inside the for loop and try adjusting the delay to 2-5 seconds. Your println statement will get executed more than once. Thus, IsOperationComplete also gets called multiple times.
Instead of sleep, I've tried the following, and it simulates the delay too.
currentTime := time.Now()
for time.Now().Before(currentTime.Add(2 * time.Second)) {
}
If you prefer, we can just remove this.
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 believe you can get rid of the delay as long as you convert the Expects to return bools, as above.
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.
See my other comment. #614 (comment)
Done. Added a new testcase. See latest commit. |
…t-appstudio#688) Signed-off-by: Keith Chong <kykchong@redhat.com>
…t-appstudio#688) Signed-off-by: Keith Chong <kykchong@redhat.com>
…t-appstudio#688) Signed-off-by: Keith Chong <kykchong@redhat.com>
…t-appstudio#688) Signed-off-by: Keith Chong <kykchong@redhat.com>
…pstudio#688) Signed-off-by: Keith Chong <kykchong@redhat.com>
907fb9c
to
9d779e9
Compare
I've rebased the PR, and added some additional code/test tweaks. The PR has been open for long enough that I would say lets just go ahead with testing within the original code coverage parameters of the story, just so we can get this one merged and off our plates 😅. I've updated the PR to that effect. |
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.
LGTM, thanks @keithchong!
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: jgwest, keithchong The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Description:
Link to JIRA Story (if applicable):
https://issues.redhat.com/browse/GITOPSRVCE-688