update_engine: clean up sleep process in P2PManagerTest.LookupURL

P2PManagerTest.LookupURL terminates a 'sh' process, which is put into
sleep by executing `sleep 2`. Upon receiving SIGTERM, the 'sleep'
process isn't reaped and thus becomes orphaned. As a remedy, this CL
modifies the shell command to trap SIGTERM and kill the 'sleep' process
upon receiving SIGTERM.

BUG=chromium:678643
TEST=Verified that no orphaned 'sleep' process is left after running
P2PManagerTest.LookupURL.

Reviewed-on: https://chromium-review.googlesource.com/427060
Commit-Ready: Ben Chan <benchan@chromium.org>
Tested-by: Ben Chan <benchan@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>

(cherry picked from commit 28f9a886f0699986f7c378a9898bea9c6f7515ee)

Change-Id: I9f7d53d3cfb515363f1b77d03816867fb85d9993
diff --git a/p2p_manager_unittest.cc b/p2p_manager_unittest.cc
index 463c0e2..5ffb358 100644
--- a/p2p_manager_unittest.cc
+++ b/p2p_manager_unittest.cc
@@ -506,7 +506,18 @@
 
   // Emulate p2p-client exceeding its timeout.
   test_conf_->SetP2PClientCommand({
-      "sh", "-c", "echo http://1.2.3.4/; sleep 2"});
+      "sh", "-c",
+      // The 'sleep' launched below could be left behind as an orphaned
+      // process when the 'sh' process is terminated by SIGTERM. As a
+      // remedy, trap SIGTERM and kill the 'sleep' process, which requires
+      // launching 'sleep' in background and then waiting for it.
+      "cleanup() { kill \"${sleep_pid}\"; exit 0; }; "
+      "trap cleanup TERM; "
+      "sleep 5 & "
+      "sleep_pid=$!; "
+      "echo http://1.2.3.4/; "
+      "wait"
+  });
   manager_->LookupUrlForFile("foobar", 42, TimeDelta::FromMilliseconds(500),
                              base::Bind(ExpectUrl, ""));
   loop_.Run();