-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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 unit tests for cmd/bundle #3247
add unit tests for cmd/bundle #3247
Conversation
@estroz started work on this. I've had to refactor a couple things to make them accessible from the tests. I'm leaving the Run() tests as pending for the moment as I'm not sure how to test that stuff without doing the counterfeiter refactor, which we should probably wait for the KB integration before we do that. Please tag anyone else you think might be interested in this. |
44fbc81
to
156ab49
Compare
this should be good to go |
PDescribe("Run", func() { | ||
}) |
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.
why it is required?
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 put in pending tests just as a reminder that they need to be implemented, but was going to wait until after kubebuilder is merged in because it'll require some additional refactoring. I can remove them if you think that's not needed.
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.
could you please add // todo(jberkhahn): {desc of what should be done} or remove them ?
PDescribe("Run", func() { | ||
}) |
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.
Why it is required?
cmd/operator-sdk/bundle/cmd.go
Outdated
channels string | ||
generateOnly bool | ||
GenerateOnly bool |
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.
why we need these changes?
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 had to make a bunch of fields public to be able to set them in the tests. It's because cobra normally populated the fields set via flag when you initialize the command struct, but I can't do it that way because I'm not cobra. So I have to make them public so I can manually set them.
cmd/operator-sdk/bundle/cmd.go
Outdated
newCreateCmd(), | ||
newValidateCmd(), | ||
NewCreateCmd(), | ||
NewValidateCmd(), |
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.
Why we need to export the NewCreateCmd and NewValidateCmd?
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.
So i can call them from the tests.
}) | ||
}) | ||
|
||
Describe("Validate", func() { |
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.
Why the validate is in the create_test.go and not in the validate_test.go?
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 is testing the argument validation method of the create command, not the bundle validate command
) | ||
|
||
var _ = Describe("Running a bundle validate command", func() { | ||
Describe("NewCreateCmd", func() { |
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.
Describe("NewCreateCmd", func() { | |
Describe("NewCreateCmd should", func() { |
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 prefer to leave those describes as just the name of the method they're testing
cmd/operator-sdk/bundle/cmd_test.go
Outdated
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package bundle_test |
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.
Are you unable to write these tests in the same package? I've done this before without issues.
package bundle_test | |
package bundle |
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 can, but I didn't on purpose. I'm trying to test the functionality of the package as a unit, so I don't want to be able to reach inside and use private stuff, as other packages won't be able to do that.
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 would agree if these packages were intended to be libraries. For now can you keep tests in the same package so we don't have to export a bunch of stuff? Before exporting private stuff we should have a conversation about general CLI refactoring.
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.
ok, I'll move everything into a single package
updated. hopefully I didn't screw up squashing the commits |
cmd/operator-sdk/bundle/create.go
Outdated
@@ -32,7 +32,7 @@ import ( | |||
"github.com/spf13/pflag" | |||
) | |||
|
|||
type bundleCreateCmd struct { | |||
type createCmd struct { |
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 agree that the name is redundant and we could/should change but it is not part of the scope of this PR.
Could we do a PR just to do the renames?
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.
woops, forgot to revert this when I moved everything back to private. fixed.
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.
IMO: we are not testing here what we should test. See my comments.
Updated. Cut out most of the simple reflective tests, had to do some actual refactoring because something touched validate and added a new image builder, so I incorporated it into the validate func there and added a test for it. |
308a77e
to
5c73f15
Compare
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
cmd/operator-sdk/bundle/validate.go
Outdated
if c.imageBuilder != containertools.DockerTool.String() && | ||
c.imageBuilder != containertools.PodmanTool.String() && | ||
c.imageBuilder != containertools.NoneTool.String() { | ||
return fmt.Errorf("unrecognized image-builder option: %s", c.imageBuilder) | ||
} | ||
|
||
return nil |
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 should be another PR it is not part of the ad unit test scope. Could we please split it?
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.
Just 2 nits. otherwise, it shows great.
Really tks for your terrific contribution 🥇
Co-authored-by: Camila Macedo <[email protected]>
@camilamacedo86 updated |
Is there anything else that needs to be changed here or is it good to go? @camilamacedo86 @estroz |
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
/approve
👍
Description of the change:
Add unit tests for the
bundle validate
andbundle create
commands.Motivation for the change:
Golang unit tests are another layer of robust CI when paired with the e2e and script testing we currently have.