The pgrep / pkill tests predate ATF and are not particularly robust.
Convert two of them to ATF as a proof of concept. Hopefully someone
will be inspired to continue the conversion; the converted tests are
parallelizable, much faster, and more robust than the originals.
Details
- Reviewers
ngie siva - Commits
- rG93f8c45c0314: pkill: Convert pgrep / pkill -j tests to ATF
Diff Detail
- Repository
- rG FreeBSD src repository
- Lint
Lint Skipped - Unit
Tests Skipped - Build Status
Buildable 80145 Build 77028: arc lint + arc unit
Event Timeline
I really prefer this approach over https://reviews.freebsd.org/D62287.
I want to respect your time and focus on this over the other review since this is a legacy issue with the tests.
Thank you again for looking into this!!
| bin/pkill/tests/pgrep-j_test.sh | ||
|---|---|---|
| 36–37 | Could this test for a pidfile to make sure the sleep process has started, or is a simple "test daemon" that uses pidfile_open needed here? | |
| 52–54 | (This is not required - just something I'm putting up on the review to consider..) Having setup fixtures is one of the benefits of using pytest over atf-sh (another reminder for me to work on https://github.com/freebsd/kyua/issues/320). _TEST_PIDFILES_TRACKER="test_pidfiles.txt"
append_test_pidfile()
{
[ $# -eq 1 ] || return 1
echo "${1}" >> "${_TEST_PIDFILES_TRACKER}"
}
clean_up_test_pidfiles()
{
if [ ! -f "${TEST_PIDFILES_TRACKER}" ]; then
return 0
fi
while read -r pidfile; do
pkill -F "${pidfile}" || :
done << "${_TEST_PIDFILES_TRACKER}"
} | |
| bin/pkill/tests/pgrep-j_test.sh | ||
|---|---|---|
| 36–37 | It would not help, because daemon creates the PID file before it forks the child, and the child writes its PID to it before executing sleep, so even checking that the PID file is non-empty is racy. | |
| 52–54 | If you mean “can we avoid trying to remove a jail that no longer exists” then no, because the jail is removed automatically when there are no more processes running inside it, and we don't control that directly. In some of the pkill tests we kill the sleep process inside the jail but we don't kill the daemon process. Besides, I also want to guard against the possibility that the process might not terminate when we want it to; removing the jail is important, because we can't run the same test again until it is gone. If you just want to reduce repetition or avoid trying to remove a jail we hadn't created yet when the test failed, we could create a jail.conf describing our jails and use jail -f jail.conf -r '*', but creating and appending to the file adds more code than using it later saves. | |
| bin/pkill/tests/pgrep-j_test.sh | ||
|---|---|---|
| 52–54 | Reordering the reply a bit since you answered my main concern in the second paragraph.
☝️ I was mostly concerned about repetition and likelihood of human error for future editors.
Interesting to know (I didn't see the atf_get ident in my first skim through the review) and I agree with your concerns: we definitely want to remove the jail for the reasons you mentioned. Looking at the size of the scripts, it doesn't make practical sense to implement what I requested. I'll do a full review now. PS This is just one of those things where if I see a pattern like this, I think back to when I got stung by typos in code/tests: that's why I started the thread. | |
Tangentially, writing these tests gave me ideas for a couple of improvements to ATF and / or Kyua:
- ATF should make items like ident, srcdir etc available to the test case through shell variables so we don't have to call atf_get_ident or atf_get_srcdir to get them.
- We might want to also make the temporary directory available as an ATF variable (maybe it is already? the documentation doesn't say) and similarly as a shell variable ($PWD only works until you chdir)
- ATF should provide each test case with a random alphanumeric token which Kyua can override to guarantee its uniqueness at least within the current session; I could then use that instead of $(atf_get_ident) to construct truly unique jail names. I realize I can use $(basename $PWD) for this, but that won't work if I'm running the test manually.
I would make long_sleep unique to each testcase - otherwise you risk having the state from one test leak into another in the pkill_j_any testcase, or worse, the other tests' daemon processes could be killed from pkill_j_any.
| bin/pkill/tests/pgrep-j_test.sh | ||
|---|---|---|
| 40 | (reminder to self: sort takes more than a single file as input and sorts the combined output) | |
| bin/pkill/tests/pkill-j_test.sh | ||
| 13 | ✅ - conversion looks good! | |
| 59–61 | (non-shipblocking - this pattern occurs elsewhere in the conversion, so I'm just commenting on the first occurrence) | |
| 64 | ✅ - conversion looks good! | |
| 84–85 | This tests more than the original - nice addition! | |
| 113 | ✅ - conversion looks good! | |
| 127–131 | (Note to self: slight difference: the original test started "pid2" before "pid1". I'm not sure if it mattered, but I don't think it matters in this case.) | |
| 157 | ✅ - conversion looks good! | |
| 177–178 | This tests more than the original - nice add! | |
@des:
I'm providing a provincial ship-it before I go offline for a while.
My concern with collisions with the -j any testing is the only thing that really concerns me with this change - otherwise it's a huge step in the right direction.
I trust you'll do the right thing resolving that.
| bin/pkill/tests/pgrep-j_test.sh | ||
|---|---|---|
| 43 | (picking a single instance; this pattern is present in other areas in the proposed code) | |
That's precisely why we make a copy of /bin/sleep with a unique name.
Fair. I missed that extremely nuanced point. Please feel free to commit the change (I checked the pgrep changes in the interim last night).