-
Notifications
You must be signed in to change notification settings - Fork 376
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
Conversation
Codecov Report
@@ 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
|
4a244cc
to
ac007a1
Compare
ac007a1
to
b155ab8
Compare
b155ab8
to
8b3a157
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.
Try to make the test table driven if a test has two or more cases.
pkg/support/dump_test.go
Outdated
@@ -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) |
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.
you should expect the exact error statement.
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 log string is empty, it returns back with time and message "log line is empty"
pkg/support/dump_test.go
Outdated
|
||
_, err = parseTimeFromLogLine("", "2021", "ovs") | ||
assert.Error(t, err) | ||
assert.Equal(t, "", "") |
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.
if you are expecting error then maybe this is not required, and try to use table driven test here.
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.
assert.Equal usage when both the values to check for equality remain empty , what is the need for this line here
pkg/support/dump_test.go
Outdated
since := "7s" | ||
ts := timestampFilter(since) | ||
currentTime := time.Now() | ||
assert.Contains(t, ts.String(), currentTime.Format("2006-01-02")) |
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.
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.
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" 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
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.
how the time since = 7s is calculated for this test !
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.
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...
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 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)
pkg/support/dump_test.go
Outdated
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") |
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.
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.
pkg/support/dump_test.go
Outdated
basedir := "dir1/dir2" | ||
data := []byte("antct -oyaml get agentinfo") | ||
err := writeYAMLFile(fs, filepath.Join(basedir, resource), resource, data) | ||
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.
8b3a157
to
6be01c5
Compare
pkg/support/dump_test.go
Outdated
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) |
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.
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")
050e7a7
to
a6416f8
Compare
a6416f8
to
3e444ab
Compare
7218add
to
ef4895b
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.
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
Outdated
} | ||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
gotTS, err := parseTimeFromLogLine(tc.data, tc.year, tc.prefix) |
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.
use a significant name instead of gotTS
pkg/support/dump_test.go
Outdated
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
gotTS, err := parseTimeFromLogLine(tc.data, tc.year, tc.prefix) | ||
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.
instead of checking with err check if expectedErr is not nil then match the error statements else assert for result and assert NoError.
pkg/support/dump_test.go
Outdated
@@ -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" |
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 need of creating a variable for this you can directly pass the value to the function.
pkg/support/dump_test.go
Outdated
defer func() { | ||
exe = exec.New() | ||
}() | ||
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
err := varr.DumpLog(basedir) | ||
assert.NoError(t, err) | ||
|
||
} |
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.
leave a line after this
assert.NoError(t, err) | ||
|
||
} | ||
func TestDumpHostNetworkInfo(t *testing.T) { |
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.
in none of the test cases you are expecting error.
pkg/support/dump_windows_test.go
Outdated
basedir: "dir1/dir2", | ||
since: "3s", | ||
}, | ||
} |
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
pkg/support/dump_windows_test.go
Outdated
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 { |
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.
add cases with expected error and then check with expected error only instead of err.
ef4895b
to
e37e346
Compare
1776982
to
6039202
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.
make sure you address the golangci-lint errors from CI
you can reproduce locally with make golangci
0fe1a2d
to
aed06d9
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.
one more nit, otherwise LGTM
pkg/support/dump_others_test.go
Outdated
err := dumper.DumpLog(baseDir) | ||
require.NoError(t, err) | ||
|
||
x, err := afero.Exists(fs, filepath.Join(baseDir, "logs", "agent", "antrea-agent.log")) |
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.
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>
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
/test-all |
/test-all |
@antoninbas Can this PR be merged now? |
I'm waiting for tests to pass ( |
/test-e2e |
TestAntreaPolicy/TestGroupNoK8sNP/Case=ACNPFQDNPolicy failed, but I think it is not related to this PR? @antoninbas |
@XinShuYang This is a UT pr so I don't think the failed test is related |
Adds the unit test for support pkg
for #4142
Signed-off-by: Harshitha U R harshithaur1611@gmail.com