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

Adds the unit test for pkg/support #4452

Merged
merged 1 commit into from
Jun 7, 2023

Conversation

urharshitha
Copy link
Contributor

Adds the unit test for support pkg
for #4142
Signed-off-by: Harshitha U R harshithaur1611@gmail.com

@codecov
Copy link

codecov bot commented Dec 7, 2022

Codecov Report

Merging #4452 (e7dc787) into main (7e055c6) will increase coverage by 1.02%.
The diff coverage is n/a.

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

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4452      +/-   ##
==========================================
+ Coverage   67.15%   68.18%   +1.02%     
==========================================
  Files         400      404       +4     
  Lines       58151    58202      +51     
==========================================
+ Hits        39052    39684     +632     
+ Misses      16371    15767     -604     
- Partials     2728     2751      +23     
Flag Coverage Δ
integration-tests 34.80% <0.00%> (+0.17%) ⬆️
kind-e2e-tests 48.06% <0.00%> (+1.93%) ⬆️
unit-tests 56.78% <0.00%> (-0.18%) ⬇️
Impacted Files Coverage Δ
cmd/antrea-agent/options_linux.go 0.00% <0.00%> (-100.00%) ⬇️
cmd/antrea-agent/options.go 23.20% <0.00%> (-13.27%) ⬇️
pkg/controller/externalippool/controller.go 86.16% <0.00%> (-6.25%) ⬇️
...trollers/multicluster/resourceexport_controller.go 76.20% <0.00%> (-4.28%) ⬇️
pkg/agent/controller/networkpolicy/packetin.go 72.97% <0.00%> (-4.06%) ⬇️
pkg/util/ip/ip.go 86.99% <0.00%> (-3.26%) ⬇️
...gent/controller/networkpolicy/status_controller.go 79.16% <0.00%> (-2.50%) ⬇️
pkg/controller/grouping/controller.go 65.13% <0.00%> (-1.98%) ⬇️
...kg/apiserver/registry/system/supportbundle/rest.go 83.24% <0.00%> (-1.53%) ⬇️
...pportbundlecollection/support_bundle_controller.go 64.10% <0.00%> (-1.47%) ⬇️
... and 52 more

@urharshitha urharshitha force-pushed the supportTesting branch 3 times, most recently from 4a244cc to ac007a1 Compare December 8, 2022 07:47
@urharshitha urharshitha changed the title Adds the unit test for pkg/support/dump_others.go Adds the unit test for pkg/support/dump.go Dec 19, 2022
@urharshitha urharshitha marked this pull request as ready for review December 19, 2022 09:53
Copy link
Contributor

@jainpulkit22 jainpulkit22 left a comment

Choose a reason for hiding this comment

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

Try to make the test table driven if a test has two or more cases.

@@ -30,6 +34,10 @@ func TestParseLogDate(t *testing.T) {
ts, err = parseTimeFromLogLine(data, "2021", "ovs")
assert.Nil(t, err)
assert.Equal(t, ts.String(), "2021-06-01 09:30:43 +0000 UTC")

_, err = parseTimeFromLogLine("", "2021", "ovs")
assert.Error(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

you should expect the exact error statement.

Copy link
Contributor

Choose a reason for hiding this comment

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

since log string is empty, it returns back with time and message "log line is empty"


_, err = parseTimeFromLogLine("", "2021", "ovs")
assert.Error(t, err)
assert.Equal(t, "", "")
Copy link
Contributor

Choose a reason for hiding this comment

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

if you are expecting error then maybe this is not required, and try to use table driven test here.

Copy link
Contributor

Choose a reason for hiding this comment

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

assert.Equal usage when both the values to check for equality remain empty , what is the need for this line here

since := "7s"
ts := timestampFilter(since)
currentTime := time.Now()
assert.Contains(t, ts.String(), currentTime.Format("2006-01-02"))
Copy link
Contributor

Choose a reason for hiding this comment

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

in expected output you are not using the since option that you passed as the parameter and also try using assert.Equal and match the exact output.

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" is used only to call the function , and for matching exact output it includes time also so every time it changes due to seconds, minutes ,so I tried matching with the date format

Copy link
Contributor

Choose a reason for hiding this comment

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

how the time since = 7s is calculated for this test !

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is not calculated ,timestampFilter gives the time when dump have started ,to get that exact time ,the time specified as since is subtracted from current time( time.Now() ) so for testing purpose it is given as "7s"(7 seconds) we can also give "1h", "3m" etc...

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that the test is not great as it is. Something like this would make more sense IMO:

ts := timestampFilter("2m")
currentTime := time.Now()
require.NotNil(t, ts)
expectedTime := currentTime.Add(-2*time.Minute)
assert.WithinDuration(t, expectedTime, *ts, 10*time.Second)

data := []byte("antct -oyaml get networkpolicies")
err := writeFile(fs, filepath.Join(basedir, resource), resource, data)
if err != nil {
require.EqualError(t, err, "error when writing networkpolicies to file: open dir1/dir2/networkpolicies: no such file or directory")
Copy link
Contributor

Choose a reason for hiding this comment

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

here you have to be sure with the test case that whether the error will be generated or not, and you can cover these in two separate test cases. Also when you are checking for no error you should also check the contents of the file written.

basedir := "dir1/dir2"
data := []byte("antct -oyaml get agentinfo")
err := writeYAMLFile(fs, filepath.Join(basedir, resource), resource, data)
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto.

if err != nil {
require.EqualError(t, err, "error when writing networkpolicies to file: open dir1\\dir2\\networkpolicies: The system cannot find the path specified.")
} else {
require.NoError(t, err)
Copy link
Contributor

@rajnkamr rajnkamr Dec 20, 2022

Choose a reason for hiding this comment

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

when err is nil it comes to line 66, so try to print additional informative lines for example require.NoError(t, err, "error when opening a stub database connection")

@urharshitha urharshitha force-pushed the supportTesting branch 2 times, most recently from 050e7a7 to a6416f8 Compare December 22, 2022 21:09
@urharshitha urharshitha changed the title Adds the unit test for pkg/support/dump.go Adds the unit test for pkg/support Dec 22, 2022
@urharshitha urharshitha force-pushed the supportTesting branch 2 times, most recently from 7218add to ef4895b Compare December 23, 2022 05:25
@urharshitha urharshitha added this to the Antrea v1.11 release milestone Jan 3, 2023
Copy link
Contributor

@jainpulkit22 jainpulkit22 left a comment

Choose a reason for hiding this comment

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

when checking for err, we should always check with expected err, instead of err, because there may be cases where your expected error is not nil but error is nil and there may be other such panic situations as well.

pkg/support/dump_test.go Show resolved Hide resolved
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
gotTS, err := parseTimeFromLogLine(tc.data, tc.year, tc.prefix)
Copy link
Contributor

Choose a reason for hiding this comment

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

use a significant name instead of gotTS

pkg/support/dump_test.go Show resolved Hide resolved
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
gotTS, err := parseTimeFromLogLine(tc.data, tc.year, tc.prefix)
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of checking with err check if expectedErr is not nil then match the error statements else assert for result and assert NoError.

@@ -38,3 +100,309 @@ func TestParseFileName(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, ts.String(), "2021-08-17 09:47:58 +0000 UTC")
}

func TestTimestampFilter(t *testing.T) {
since := "7s"
Copy link
Contributor

Choose a reason for hiding this comment

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

no need of creating a variable for this you can directly pass the value to the function.

defer func() {
exe = exec.New()
}()
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

err := varr.DumpLog(basedir)
assert.NoError(t, err)

}
Copy link
Contributor

Choose a reason for hiding this comment

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

leave a line after this

assert.NoError(t, err)

}
func TestDumpHostNetworkInfo(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

in none of the test cases you are expecting error.

basedir: "dir1/dir2",
since: "3s",
},
}
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

t.Run(tc.name, func(t *testing.T) {
varr := NewAgentDumper(fs, exe, nil, nil, nil, tc.since, tc.v4Enabled, tc.v6Enabled)
err := varr.DumpHostNetworkInfo(tc.basedir)
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

add cases with expected error and then check with expected error only instead of err.

@urharshitha urharshitha requested review from antoninbas and tnqn January 5, 2023 06:51
@urharshitha urharshitha force-pushed the supportTesting branch 2 times, most recently from 1776982 to 6039202 Compare January 5, 2023 08:21
@NamanAg30 NamanAg30 requested a review from antoninbas June 2, 2023 16:52
@NamanAg30 NamanAg30 requested a review from antoninbas June 2, 2023 20:38
Copy link
Contributor

@antoninbas antoninbas left a comment

Choose a reason for hiding this comment

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

make sure you address the golangci-lint errors from CI
you can reproduce locally with make golangci

pkg/support/dump_test.go Show resolved Hide resolved
pkg/support/dump_others_test.go Outdated Show resolved Hide resolved
pkg/support/dump_test.go Outdated Show resolved Hide resolved
pkg/support/dump_windows_test.go Outdated Show resolved Hide resolved
@NamanAg30 NamanAg30 force-pushed the supportTesting branch 6 times, most recently from 0fe1a2d to aed06d9 Compare June 5, 2023 17:09
@NamanAg30 NamanAg30 requested a review from antoninbas June 5, 2023 19:26
Copy link
Contributor

@antoninbas antoninbas left a comment

Choose a reason for hiding this comment

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

one more nit, otherwise LGTM

err := dumper.DumpLog(baseDir)
require.NoError(t, err)

x, err := afero.Exists(fs, filepath.Join(baseDir, "logs", "agent", "antrea-agent.log"))
Copy link
Contributor

Choose a reason for hiding this comment

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

rename the x variables to ok (there is another test like this as well), which is more in line with Go conventions

…ws.go

Signed-off-by: Harshitha U R <harshithaur1611@gmail.com>
Copy link
Contributor

@antoninbas antoninbas left a comment

Choose a reason for hiding this comment

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

LGTM

@antoninbas
Copy link
Contributor

/test-all

@NamanAg30 NamanAg30 requested a review from antoninbas June 6, 2023 16:24
@antoninbas
Copy link
Contributor

/test-all

@NamanAg30
Copy link
Contributor

@antoninbas Can this PR be merged now?

@antoninbas
Copy link
Contributor

@antoninbas Can this PR be merged now?

I'm waiting for tests to pass (test-e2e) and the testbeds seem to be broken. cc @XinShuYang

@XinShuYang
Copy link
Contributor

/test-e2e

@XinShuYang
Copy link
Contributor

TestAntreaPolicy/TestGroupNoK8sNP/Case=ACNPFQDNPolicy failed, but I think it is not related to this PR? @antoninbas

@NamanAg30
Copy link
Contributor

@XinShuYang This is a UT pr so I don't think the failed test is related

@antoninbas antoninbas merged commit 85917f5 into antrea-io:main Jun 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/test Issues or PRs related to unit and integration tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants