Unit tests for cli/command/image package#32248
Conversation
cli/command/image/tag_test.go
Outdated
There was a problem hiding this comment.
Should contain NewTag instead of Leave (same for below 😝)
There was a problem hiding this comment.
Thanks! You can figure out from where I'm copying the test structure now 😄
74489ee to
d4e6676
Compare
525cfc7 to
dd921f6
Compare
vdemeester
left a comment
There was a problem hiding this comment.
Big PR ! Nice, thanks for putting this work @icapurro. Few comments from me though 😉,
The less production code is changed when writing tests (afterwards), the better.
We shouldn't modify OutStream or InStream for writing tests (and this cli/command/stream.go, etc.. shouldn't be changed — FakeClican). Why not do a refactoring after if we feel like it, but it shouldn't change a think for the tests — if it does, we coupled our test heavily on the implementation.
On the same note, cli/compose/schema/bindata.go has no reason to change in this PR, don't know why it did, but this change shouldn't be there.
cli/command/cli.go
Outdated
There was a problem hiding this comment.
I'm pretty sure this is not required and complexify the code 😓
cli/command/cli.go
Outdated
There was a problem hiding this comment.
Same here, the only change I would see here is the addition of CredentialsStore on the Cli interface.
There was a problem hiding this comment.
I did this because I need to stub the method IsTerminal(), and I don't see any other way to do it if the Cli interface only accepts the specific OutStream struct. Do you see any other alternative?
There was a problem hiding this comment.
I think you can avoid this change by adding this to cli/command/in_test.go
func NewStubInStream(isTerminal bool) *InStream {
return &InStream{
isTerminal: isTerminal,
in: ioutil.NopCloser(strings.NewReader("")),
}
}and using that stream in the FakeCli.
You can probably leave the existing NewFakeCli() as is, and add a new NewFakeCliWithStreams() that takes all the streams, not just stdout.
There was a problem hiding this comment.
@dnephin I did what you propose, but I can't find a way to import a function from a *_test file in package cli/command into another *_test file in package cli/command/image. From what I've been reading it seems not possible, but maybe I'm missing something.
A solution could be to define that function in cli/command/in.go but we will end up with a function intended 100% for testing included in the production binary. 😿
There was a problem hiding this comment.
Ah, you're right. I guess I was mistaken.
A function like this (with a better name) might still be ok in in.go, another option would be to create a setter for isTerminal.
There was a problem hiding this comment.
Could go in the same package tree as the builders, i.e. cli/internal/test (to make sure it can't be imported outside of cli package 👼 )
cli/command/image/client_test.go
Outdated
There was a problem hiding this comment.
Hum I used the struct here client.Client, not sure if it does a different though 😛
There was a problem hiding this comment.
It shouldn't, I'll change it.
cli/command/image/history_test.go
Outdated
There was a problem hiding this comment.
I don't think we need golden usage here, a assert.Error(t, cmd.Execute(), tc.expectedError) (with the corresponding expectedError field in the anonymous struct) feels simpler.
It would be more consistent with other cli tests too.
There was a problem hiding this comment.
assert.Error does a "contains", so we only need to verify that it contains "requires exactly 1 argument(s)" or …
There was a problem hiding this comment.
Ok, I just feel that I need to generalise this as much as possible. But I didn't know assert.Error does a contains , I'll change it.
cli/command/image/history_test.go
Outdated
There was a problem hiding this comment.
I tend to prefer if tc.outputRegex == "", because it's more readable for a human (and tests are meant to be read by humans). It's a nit though 👼
There was a problem hiding this comment.
Ok, let me follow the same style then.
cli/command/image/trust.go
Outdated
cli/command/image/trust.go
Outdated
cli/internal/test/cli.go
Outdated
There was a problem hiding this comment.
If we don't introduce OutputStreamer (and I really think we shouldn't), this is not required.
cli/internal/test/in.go
Outdated
There was a problem hiding this comment.
Same as for OutStreamer, I think we shouldn't have to do that.
cli/internal/test/out.go
Outdated
|
Ah, and there is a build error on |
|
@vdemeester Thanks for the thorough review! I applied most of your comments but I still have question on the following:
|
1fdcdf7 to
d85daad
Compare
|
@dnephin I went in the direction of creating a setter FYI: |
6f83d07 to
a9d4422
Compare
a9d4422 to
1805315
Compare
|
@dnephin @vdemeester love the moby project initiative, but I think the repo rename broke the PR update... I made the changes in my branch but they are not appearing here. Should I create a new PR? |
|
I think it's broken for all PRs. We're looking into it. Don't open a new PR just yet. |
9e49b04 to
3aafdd6
Compare
3aafdd6 to
8e354a6
Compare
461c5ea to
c3ae796
Compare
|
@dnephin can we re run experimental and windowsRS1 please? thanks! |
Signed-off-by: Ignacio Capurro <[email protected]>
c3ae796 to
b2551c6
Compare
|
Should be done |
|
😢 This has broken |
|
@jhowardmsft arg, really ? I'll have to try it out and see how to fix it then 😓 |
|
Sorry I didn't have a chance to review the final version. With testify, the expected value comes before the actual value. So: should be: ... and so on. There are also some opportunities to use I can make these changes if that's helpful. |
|
Opened #32829 |
| imageSaveFunc: func(images []string) (io.ReadCloser, error) { | ||
| return ioutil.NopCloser(strings.NewReader("")), nil | ||
| }, |
There was a problem hiding this comment.
Heh; 7 Years later, and I stumble on this line to discover that tc.imageSaveFunc was not wired up here, so this test didn't actually run the tests 😂
This PR adds unit tests for the cli/command/image package. Part of the work on #31217
Signed-off-by: Ignacio Capurro [email protected]
- What I did
Added unit tests for the following commands in package cli/command/image
To accomplish the 100% package coverage the following two commands need to be tested:
I will the issues to work on them since this PR was getting really big and those two commands in particular will need some code refactor to accomplish a good coverage.
- How I did it
I followed @vdemeester previous test structure in other cli commands. Used gold files, builders created under
cli/internal/test/buildersand added new interfaces for some structs to be stubbed.- How to verify it
Run the unit tests or check the Jenkins build.
Before this PR
After this PR
- Description for the changelog
Added unit test coverage to some cli image commands.