AU/unittest: eliminate use of fixed named test directories

This fixes race conditions when tests are run in parallel.

Note that integration_unittest.cc is currently not (never was?) being
compiled, so all changes are lexically oriented and visually verified
only.

BUG=chromium:236465
TEST=Tests successful

Change-Id: I6181a2cc0c10f6fcf8f982fc263c7a02e5082eeb
Reviewed-on: https://gerrit.chromium.org/gerrit/62954
Tested-by: Gilad Arnold <garnold@chromium.org>
Reviewed-by: Don Garrett <dgarrett@chromium.org>
Commit-Queue: Gilad Arnold <garnold@chromium.org>
diff --git a/integration_unittest.cc b/integration_unittest.cc
index af5fee4..10e6633 100644
--- a/integration_unittest.cc
+++ b/integration_unittest.cc
@@ -32,12 +32,11 @@
 class IntegrationTest : public ::testing::Test { };
 
 namespace {
-const char* kTestDir = "/tmp/update_engine-integration-test";
 
 class IntegrationTestProcessorDelegate : public ActionProcessorDelegate {
  public:
-  IntegrationTestProcessorDelegate()
-      : loop_(NULL), processing_done_called_(false) {}
+  IntegrationTestProcessorDelegate(const string& test_dir)
+      : loop_(NULL), processing_done_called_(false), test_dir_(test_dir) {}
   virtual ~IntegrationTestProcessorDelegate() {
     EXPECT_TRUE(processing_done_called_);
   }
@@ -58,7 +57,7 @@
       InstallAction* install_action = static_cast<InstallAction*>(action);
       old_dev_ = install_action->GetOutputObject();
       string dev;
-      BindToUnusedDevice(kTestDir + "/dev2", &dev);
+      BindToUnusedDevice(test_dir_ + "/dev2", &dev);
       install_action->SetOutputObject(dev);
     } else if (action->Type() == PostinstallRunnerAction::StaticType()) {
       PostinstallRunnerAction* postinstall_runner_action =
@@ -85,6 +84,10 @@
   // but the PostinstallRunnerAction requires a real device. We set up a
   // loop device pointing to the file when necessary.
   string old_dev_;
+
+  // A per-instance, customizable test directory. Used for avoiding race
+  // conditions while running multiple tests concurrently.
+  string test_dir_;
 };
 
 gboolean TestStarter(gpointer data) {
@@ -99,8 +102,14 @@
   ASSERT_EQ(0, getuid());
   GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
 
+  // Create a uniquely named test directory.
+  string test_dir;
+  ASSERT_TRUE(utils::MakeTempDirectory(
+          "/tmp/update_engine-integration-test-XXXXXX", &test_dir));
+  ScopedPathUnlinker test_dir_unlinker(test_dir);
+
   ActionProcessor processor;
-  IntegrationTestProcessorDelegate delegate;
+  IntegrationTestProcessorDelegate delegate(test_dir);
   delegate.set_loop(loop);
   processor.set_delegate(&delegate);