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 unit tests for pkg/antctl/raw/set package. #4307

Merged
merged 1 commit into from
Nov 15, 2022

Conversation

jainpulkit22
Copy link
Contributor

Signed-off-by: Pulkit Jain jainpu@vmware.com

@codecov
Copy link

codecov bot commented Oct 18, 2022

Codecov Report

Merging #4307 (e422c57) into main (986754c) will decrease coverage by 3.52%.
The diff coverage is n/a.

❗ Current head e422c57 differs from pull request most recent head 8c27bc3. Consider uploading reports for the commit 8c27bc3 to get more accurate results

Impacted file tree graph

@@            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     
Flag Coverage Δ
integration-tests 34.52% <0.00%> (-0.01%) ⬇️
kind-e2e-tests 48.65% <0.00%> (-0.25%) ⬇️
unit-tests 45.99% <0.00%> (-2.14%) ⬇️
Impacted Files Coverage Δ
...trollers/multicluster/label_identity_controller.go 0.00% <0.00%> (-80.73%) ⬇️
pkg/cni/client.go 0.00% <0.00%> (-77.78%) ⬇️
...s/multicluster/label_identity_export_controller.go 0.00% <0.00%> (-72.93%) ⬇️
...kg/agent/flowexporter/connections/conntrack_ovs.go 0.00% <0.00%> (-70.91%) ⬇️
...uster/commonarea/acnp_resourceimport_controller.go 0.00% <0.00%> (-69.75%) ⬇️
...rs/multicluster/commonarea/clusterinfo_importer.go 0.00% <0.00%> (-69.65%) ⬇️
...luster/controllers/multicluster/node_controller.go 0.00% <0.00%> (-68.79%) ⬇️
...s/multicluster/memberclusterannounce_controller.go 0.00% <0.00%> (-67.75%) ⬇️
...trollers/multicluster/clusterinfoexport_handler.go 0.00% <0.00%> (-67.40%) ⬇️
...ter/controllers/multicluster/gateway_controller.go 1.47% <0.00%> (-61.77%) ⬇️
... and 78 more

@jainpulkit22 jainpulkit22 mentioned this pull request Oct 18, 2022
37 tasks
@jainpulkit22 jainpulkit22 marked this pull request as ready for review October 18, 2022 12:40
@jainpulkit22 jainpulkit22 changed the title Improving the ut coverage for antctl set package. Improving the UT coverage for antctl set package. Oct 18, 2022
@jainpulkit22 jainpulkit22 changed the title Improving the UT coverage for antctl set package. Add unit tests for pkg/antctl/raw/set package. Oct 18, 2022
pkg/antctl/raw/set/flowaggregator/command.go Outdated Show resolved Hide resolved
pkg/antctl/raw/set/flowaggregator/command_test.go Outdated Show resolved Hide resolved
pkg/antctl/raw/set/flowaggregator/command_test.go Outdated Show resolved Hide resolved
Comment on lines 81 to 85
if cmd.Flag("kubeconfig") != nil {
cmd.Flag("kubeconfig").Value.Set(fakeKubeconfig.Name())
} else {
cmd.Flags().String("kubeconfig", fakeKubeconfig.Name(), "path of kubeconfig")
}
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Member

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",
Copy link
Contributor

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?

Copy link
Contributor Author

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
Copy link
Contributor

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)
Copy link
Contributor

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)
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto.

pkg/antctl/raw/set/flowaggregator/command_test.go Outdated Show resolved Hide resolved
@jainpulkit22 jainpulkit22 force-pushed the antctl-set-unit-test branch 2 times, most recently from 9abb930 to a60d0d5 Compare November 8, 2022 12:11
Copy link
Contributor

@luolanzone luolanzone left a 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.

@@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
flow_aggregator "antrea.io/antrea/pkg/antctl/raw/set/flowaggregator"
flowaggregator "antrea.io/antrea/pkg/antctl/raw/set/flowaggregator"

Comment on lines 162 to 165
buf := new(bytes.Buffer)
cmd.SetOutput(buf)
cmd.SetOut(buf)
cmd.SetErr(buf)
Copy link
Contributor

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",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

luolanzone
luolanzone previously approved these changes Nov 9, 2022
Copy link
Contributor

@luolanzone luolanzone left a comment

Choose a reason for hiding this comment

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

LGTM

@luolanzone
Copy link
Contributor

@tnqn @jianjuns @antoninbas Could you take another look? thanks.

pkg/antctl/raw/set/flowaggregator/command_test.go Outdated Show resolved Hide resolved
Comment on lines 81 to 85
if cmd.Flag("kubeconfig") != nil {
cmd.Flag("kubeconfig").Value.Set(fakeKubeconfig.Name())
} else {
cmd.Flags().String("kubeconfig", fakeKubeconfig.Name(), "path of kubeconfig")
}
Copy link
Member

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.

pkg/antctl/raw/set/flowaggregator/command_test.go Outdated Show resolved Hide resolved
pkg/antctl/raw/set/flowaggregator/command_test.go Outdated Show resolved Hide resolved
t.Run(tc.name, func(t *testing.T) {
var actualValue string
err := setCommitIntervalOrFail(&actualValue, tc.value)
if err != nil {
Copy link
Member

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 {
Copy link
Member

Choose a reason for hiding this comment

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

ditto

pkg/antctl/raw/set/flowaggregator/command_test.go Outdated Show resolved Hide resolved
if tc.expectedErr != "" {
assert.Contains(t, err.Error(), tc.expectedErr)
} else {
assert.Equal(t, tc.value, actualValue)
Copy link
Member

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)
Copy link
Member

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)
Copy link
Member

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)
Copy link
Member

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>
Copy link
Member

@tnqn tnqn left a comment

Choose a reason for hiding this comment

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

LGTM

@tnqn
Copy link
Member

tnqn commented Nov 15, 2022

/skip-all

1 similar comment
@tnqn
Copy link
Member

tnqn commented Nov 15, 2022

/skip-all

@tnqn tnqn merged commit 0582f64 into antrea-io:main Nov 15, 2022
GraysonWu pushed a commit to GraysonWu/antrea that referenced this pull request Jan 27, 2023
Added unit tests to improve the UT coverage for pkg/antctl/raw/set
package.

Signed-off-by: Pulkit Jain <jainpu@vmware.com>
heanlan pushed a commit to heanlan/antrea that referenced this pull request Mar 29, 2023
Added unit tests to improve the UT coverage for pkg/antctl/raw/set
package.

Signed-off-by: Pulkit Jain <jainpu@vmware.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants