-
Notifications
You must be signed in to change notification settings - Fork 361
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
validation: improve error message when failing to load a TracingPolicy #2031
Conversation
func createTempFile(data string) string { | ||
file, err := ioutil.TempFile("", "*") | ||
if err != nil { | ||
logger.GetLogger().Panic("cannot create temporary file") |
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 just return an error, then in the caller do t.Fatalf() in 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.
yeah don't use the logger and panic in tests, if you have t *testing.T
available (see my other remark) just call t.Fatal
if you want to stop this test here because of the error.
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.
So lgtm, just a minor fix in the test so we fatal in the test getting the context.
Also for that .gitignore yes another PR is welcome ;-)
Thank you!
Some CI code format are failing too. Thank you |
Thanks a lot that's nice, an issue is not needed for a trivial change like that! |
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.
Welcome to contributing to Tetragon! 🤗
That's very nice of you to take the time to add tests for invalid tracing policies. I have a few minor comments, hope it's helpful with Djalal's review as well.
func deleteTempFile(path string) { | ||
err := os.Remove(path) | ||
if err != nil { | ||
logger.GetLogger().Panicf("cannot remove temporary file %q", path) | ||
} | ||
} |
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 can remove this function by passing t *testing.T
to your createTempFile
function and register your cleanup function t.Cleanup(f func())
. It's a bit like a defer statement but that executes when the test or subtest is done.
func createTempFile(data string) string { | ||
file, err := ioutil.TempFile("", "*") | ||
if err != nil { | ||
logger.GetLogger().Panic("cannot create temporary file") |
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.
yeah don't use the logger and panic in tests, if you have t *testing.T
available (see my other remark) just call t.Fatal
if you want to stop this test here because of the error.
@@ -527,3 +529,82 @@ func TestYamlNamespaced(t *testing.T) { | |||
_, ok := tp.(TracingPolicyNamespaced) | |||
require.True(t, ok) | |||
} | |||
|
|||
func createTempFile(data string) string { | |||
file, err := ioutil.TempFile("", "*") |
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.
As the linter said, you can use https://pkg.go.dev/os#CreateTemp here, you can add a prefix or a suffix with tetragon
in it btw it can be useful in case the temp file fails to cleanup, not necessary since it should be in tmp
but still it's nice. Maybe you can find other example in the codebase, not sure!
} | ||
err = file.Close() | ||
if err != nil { | ||
logger.GetLogger().Panicf("cannot close temporary file %q", path) |
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.
maybe you can just T.Fail
here or just warn since it's not a big deal if we fail to close that file, and should not happen that often.
Incorporates reviewer's comments on PR cilium#2031 Signed-off-by: Christian Hörtnagl <christian2@univie.ac.at>
As agreed in PR cilium#2031 Signed-off-by: Christian Hörtnagl <christian2@univie.ac.at>
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 are a few comments, could you also squash your commits? You can use:
git rebase -i <first commit of PR>^
# mark commit to squash as s
# if you are happy with the rebase, you can check with git log and git show
git push origin <your branch> --force-with-lease
t.Cleanup(func() { | ||
err := os.Remove(path) | ||
if err != nil { | ||
t.Error(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.
since you are using file, err := os.CreateTemp(t.TempDir(), "tetragon-")
, you can remove the cleanup part since the TempDir
folder is already automatically removed:
TempDir returns a temporary directory for the test to use. The directory is automatically removed by Cleanup when the test and all its subtests complete.
func createTempFile(t *testing.T, data string) string { | ||
file, err := os.CreateTemp(t.TempDir(), "tetragon-") | ||
if err != nil { | ||
t.Error(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.
I think you should use Fatal
here since you cannot recover from this and you should stop the test here.
|
||
_, err = file.WriteString(data) | ||
if err != nil { | ||
t.Error(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.
Same I think you should use Fatal
here.
} | ||
}) | ||
|
||
return path |
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.
return path | |
return file.Name() |
if err != nil { | ||
t.Error(err) | ||
} | ||
path := file.Name() |
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.
path := file.Name() |
aa4494e
to
cfcdf5c
Compare
As agreed in PR cilium#2031 Signed-off-by: Christian Hörtnagl <christian2@univie.ac.at>
@mtardy I've taken your suggestions from the second round of reviewing on board now. The only difference is that I opted for |
Yeah it's fine. One can argue that it should not be an |
As agreed in PR #2031 Signed-off-by: Christian Hörtnagl <christian2@univie.ac.at>
@mtardy Yep. I'd propose I'll consider this if we shall need another round of changes. All your input on this issue has been very constructive and helpful so far, so I'd have absolutely no problem with that. Thanks again. |
it's up to you, we can merge as-is if you want. |
Then I'd rather revert to |
Callers of tracingpolicy.FromFile() no longer receive highly specific errors, but a generic error, which can also be presented to end users. Callers of tracingpolicy.FromJSON() still receive the specific errors, and any generic error wraps a specific one. Signed-off-by: Christian Hörtnagl <christian2@univie.ac.at>
cfcdf5c
to
1c23baa
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.
Thanks again! 🎉
Callers of
tracingpolicy.FromFile()
no longer receive highly specific errors, but a generic error, which can also be presented to end users. Callers oftracingpolicy.FromJSON()
still receive the specific errors, and any generic error wraps a specific one.The implementation is along the lines suggested by @mtardy in #2022. (Thanks for that input.) Please indicate if my use of
Panicf()
inpkg/tracingpolicy/generictracingpolicy_test.go
is unwelcome.BTW, it seems to me that
drop-privileges
should be added tocontrib/tester-progs/.gitignore
. I could create another PR for that, if applicable (plus an issue, if necessary for the trivial change).Fixes #2022
Signed-off-by: Christian Hörtnagl christian2@univie.ac.at