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

Add cmd ut #1765

Merged
merged 3 commits into from
Oct 11, 2022
Merged

Add cmd ut #1765

merged 3 commits into from
Oct 11, 2022

Conversation

Stevent-fei
Copy link
Collaborator

Describe what this PR does / why we need it

Does this pull request fix one issue?

Describe how you did it

Describe how to verify it

Special notes for reviews

var i net.IP
for _, ip := range hosts {
for _, i = range ip.IPS {
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

unfinished code here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The code has been updated

"net"
"testing"

"github.com/sealerio/sealer/cmd/sealer/cmd/types"
Copy link
Collaborator

Choose a reason for hiding this comment

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

usually, there should be three groups separated by blank for go-imports

the golang official package
the package for the current project
the others

}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if res := removeIPList(tt.clusterIPList, tt.toBeDeletedIPList); (res != nil) != tt.wantErr {
Copy link
Member

Choose a reason for hiding this comment

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

I don't check the "removeIPList".
But I guess it aims to remove "toBeDeletedIPList" from "clusterIPList", and return the left "ip list"?
I think we could make the test more readable like this:
struct {
name string
IPList []net.IP
IPListToDelete []net.IP
IPListExpected []net.IP
}

Compare the "res := removeIPList" and "IPListExpected". Otherwise the test case is hard to be understood.

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if res := removeIPList(tt.clusterIPList, tt.toBeDeletedIPList); (res != nil) != tt.wantErr {
fmt.Println(res)
Copy link
Member

Choose a reason for hiding this comment

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

t.Error

if res := removeIPList(tt.clusterIPList, tt.toBeDeletedIPList); (res != nil) != tt.wantErr {
fmt.Println(res)
}
logrus.Error("is empty")
Copy link
Member

Choose a reason for hiding this comment

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

t.Error here, and make the error message more detailed.

assert.NotNil(t, res)
result := assert.Equal(t, tt.IPListExpected, res)
if !result {
t.Errorf("The filtered ip address is different from the expected value")
Copy link
Member

Choose a reason for hiding this comment

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

t.Errorf => t.Error

false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if res := removeIPList(tt.clusterIPList, tt.toBeDeletedIPList); (res != nil) != tt.wantErr {
fmt.Println(res)
assert.NotNil(t, res)
Copy link
Member

Choose a reason for hiding this comment

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

"(res != nil) != tt.wantErr"
"assert.NotNil(t, res)"
Are them duplicated?
IMO, both of them are unnecessary.
Just do "assert.Equal(t, tt.IPListExpected, res)".
In addition, I guess "if !result " will not be executed if "assert" error happened?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, the judgment does repeat itself

Make changes to the reviewed questions: modify the return error message function
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

It's not simple enough. Let's finish the last change.
IMO, "(res != nil) != tt.wantErr" means if res != nil. In this case, wantErr should be removed.
And let's do like this:

  1. remove wantErr
res := removeIPList(tt.clusterIPList, tt.toBeDeletedIPList)
if res == nil {
continue
}

assert.Equal(t, tt.IPListExpected, res)

Or make the expected IPList to be nil
Then you don't have to make sure the res != nil.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This judgment is inside a for loop, so you can't use continue?

Or make the expected IPList to be nil

Then you don't have to make sure the res ! = nil.

The meaning of this sentence is not quite understood

But my understanding is this:

If expected IPList is empty, an error will be reported when the res and expected IPList functions are detected

@codecov-commenter
Copy link

Codecov Report

Base: 19.89% // Head: 20.68% // Increases project coverage by +0.78% 🎉

Coverage data is based on head (a897148) compared to base (a20019a).
Patch has no changes to coverable lines.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1765      +/-   ##
==========================================
+ Coverage   19.89%   20.68%   +0.78%     
==========================================
  Files          69       69              
  Lines        6478     6478              
==========================================
+ Hits         1289     1340      +51     
+ Misses       5012     4957      -55     
- Partials      177      181       +4     
Impacted Files Coverage Δ
cmd/sealer/cmd/utils/cluster.go 57.30% <0.00%> (+57.30%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Member

@justadogistaken justadogistaken left a comment

Choose a reason for hiding this comment

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

LGTM

@justadogistaken justadogistaken merged commit 50b1c7a into sealerio:main Oct 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants