-
Notifications
You must be signed in to change notification settings - Fork 363
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 pkg/antctl/raw/set package. #4307
Conversation
Codecov Report
@@ Coverage Diff @@
## main #4307 +/- ##
==========================================
- Coverage 64.41% 60.88% -3.53%
==========================================
Files 393 378 -15
Lines 55542 54721 -821
==========================================
- Hits 35775 33318 -2457
- Misses 17147 18868 +1721
+ Partials 2620 2535 -85
|
e7ea898
to
9855dad
Compare
9855dad
to
8ca8de4
Compare
acbc9bf
to
487fd38
Compare
487fd38
to
8c27bc3
Compare
8c27bc3
to
da16c60
Compare
if cmd.Flag("kubeconfig") != nil { | ||
cmd.Flag("kubeconfig").Value.Set(fakeKubeconfig.Name()) | ||
} else { | ||
cmd.Flags().String("kubeconfig", fakeKubeconfig.Name(), "path of kubeconfig") | ||
} |
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 is the check needed? I feel you just need to assign the kubeconfig
instead of checking cmd.Flag("kubeconfig") != nil
. Please check if this is redundant or not.
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.
Since it iterates two times, it is necessary to do a nil check because in first case if it is nil it means that the flag is not present and we need to add that flag, and in the second case when it is nil it means that the flag is already there we just need to update or set the value. If we try to set the value when the flag is not present it will generate an error in that case.
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 test should initialize the cmd variable inside the loop to avoid side effect caused by previous subtests and such unnecessary check.
expectedErr string | ||
}{ | ||
{ | ||
name: "valid configMap and nil args", |
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 didn't see you check output of the command here when it's successful, I suppose it will print some flow aggregator information?
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.
no it does not print any flow aggregator information, that's why i just checked for the error
name string | ||
b bool | ||
value string | ||
parsedValue 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.
better to name parsedValue
to expectedValue
|
||
for _, tc := range tcs { | ||
t.Run(tc.name, func(t *testing.T) { | ||
err := setBoolOrFail(&tc.b, tc.value) |
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 think you can remove field b
on the tcs
, and just create a new variable before this.
var actualValue bool
}, | ||
} | ||
|
||
_ = setStringOrFail(&tc[0].b, tc[0].value) |
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.
b
is too simple for a field name, if there is only one case, you may just remove table defined tests.
func TestSetCommitIntervalOrFail(t *testing.T) { | ||
tcs := []struct { | ||
name string | ||
b string |
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.
Ditto.
9abb930
to
a60d0d5
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 overall, a few nits.
pkg/antctl/raw/set/command.go
Outdated
@@ -17,7 +17,7 @@ package set | |||
import ( | |||
"github.com/spf13/cobra" | |||
|
|||
flow_aggregator "antrea.io/antrea/pkg/antctl/raw/set/flow-aggregator" | |||
flow_aggregator "antrea.io/antrea/pkg/antctl/raw/set/flowaggregator" |
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.
flow_aggregator "antrea.io/antrea/pkg/antctl/raw/set/flowaggregator" | |
flowaggregator "antrea.io/antrea/pkg/antctl/raw/set/flowaggregator" |
buf := new(bytes.Buffer) | ||
cmd.SetOutput(buf) | ||
cmd.SetOut(buf) | ||
cmd.SetErr(buf) |
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 don't think you need this if you won't use buf
to compare the command output with expected result.
expectedOutput map[string]string | ||
}{ | ||
{ | ||
name: "valid configMap and nil args", |
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.
name: "valid configMap and nil args", | |
name: "valid ConfigMap and nil args", |
Can you check all K8s resources and update them with upper case? For example pod
to Pod
, namespace
to Namespace
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
a60d0d5
to
2d8df12
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
@tnqn @jianjuns @antoninbas Could you take another look? thanks. |
if cmd.Flag("kubeconfig") != nil { | ||
cmd.Flag("kubeconfig").Value.Set(fakeKubeconfig.Name()) | ||
} else { | ||
cmd.Flags().String("kubeconfig", fakeKubeconfig.Name(), "path of kubeconfig") | ||
} |
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 test should initialize the cmd variable inside the loop to avoid side effect caused by previous subtests and such unnecessary check.
t.Run(tc.name, func(t *testing.T) { | ||
var actualValue string | ||
err := setCommitIntervalOrFail(&actualValue, tc.value) | ||
if err != 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.
ditto
for _, tc := range tcs { | ||
t.Run(tc.name, func(t *testing.T) { | ||
cm, err := GetFAConfigMap(k8sClient, tc.configMapName) | ||
if err != 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.
ditto
7ccad52
to
e97a32e
Compare
if tc.expectedErr != "" { | ||
assert.Contains(t, err.Error(), tc.expectedErr) | ||
} else { | ||
assert.Equal(t, tc.value, actualValue) |
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.
ditto
if tc.expectedErr != "" { | ||
assert.Contains(t, err.Error(), tc.expectedErr) | ||
} else { | ||
assert.Equal(t, fakeConfigMap, cm) |
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.
ditto
if tc.expectedErr == "" { | ||
assert.NoError(t, err) | ||
} else { | ||
assert.Contains(t, err.Error(), tc.expectedErr) |
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 code would panic when err is nil.
assert.ErrorContains
takes care of nil check and is more suitable here.
defer os.Unsetenv("FA_CONFIG_MAP_NAME") | ||
err := updateRunE(cmd, tc.args) | ||
if tc.expectedErr != "" { | ||
assert.Contains(t, err.Error(), tc.expectedErr) |
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.
ditto
Added unit tests to imporove the UT coverage for pkg/antctl/raw/set package. Signed-off-by: Pulkit Jain <jainpu@vmware.com>
e97a32e
to
12aca79
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
/skip-all |
1 similar comment
/skip-all |
Added unit tests to improve the UT coverage for pkg/antctl/raw/set package. Signed-off-by: Pulkit Jain <jainpu@vmware.com>
Added unit tests to improve the UT coverage for pkg/antctl/raw/set package. Signed-off-by: Pulkit Jain <jainpu@vmware.com>
Signed-off-by: Pulkit Jain jainpu@vmware.com