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/.gitignore b/.gitignore
index 9b20783..e22ca50 100644
--- a/.gitignore
+++ b/.gitignore
@@ -27,6 +27,3 @@
 /update_engine_unittests
 /update_metadata.pb.cc
 /update_metadata.pb.h
-/UpdateAttempterTest/
-/omaha_request_action-test/
-/omaha_response_handler_action-test/
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);
 
diff --git a/omaha_request_action_unittest.cc b/omaha_request_action_unittest.cc
index 6051f01..ffcb91a 100644
--- a/omaha_request_action_unittest.cc
+++ b/omaha_request_action_unittest.cc
@@ -1490,23 +1490,27 @@
 }
 
 TEST(OmahaRequestActionTest, TestChangingToMoreStableChannel) {
-  const string kTestDir = "omaha_request_action-test";
-  ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc"));
-  ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir +
+  // Create a uniquely named test directory.
+  string test_dir;
+  ASSERT_TRUE(utils::MakeTempDirectory(
+          "omaha_request_action-test-XXXXXX", &test_dir));
+
+  ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + "/etc"));
+  ASSERT_EQ(0, System(string("mkdir -p ") + test_dir +
                       kStatefulPartition + "/etc"));
   vector<char> post_data;
   NiceMock<PrefsMock> prefs;
   ASSERT_TRUE(WriteFileString(
-      kTestDir + "/etc/lsb-release",
+      test_dir + "/etc/lsb-release",
       "CHROMEOS_RELEASE_APPID={11111111-1111-1111-1111-111111111111}\n"
       "CHROMEOS_BOARD_APPID={22222222-2222-2222-2222-222222222222}\n"
       "CHROMEOS_RELEASE_TRACK=canary-channel\n"));
   ASSERT_TRUE(WriteFileString(
-      kTestDir + kStatefulPartition + "/etc/lsb-release",
+      test_dir + kStatefulPartition + "/etc/lsb-release",
       "CHROMEOS_IS_POWERWASH_ALLOWED=true\n"
       "CHROMEOS_RELEASE_TRACK=stable-channel\n"));
   OmahaRequestParams params = kDefaultTestParams;
-  params.set_root(string("./") + kTestDir);
+  params.set_root(string("./") + test_dir);
   params.SetLockDown(false);
   params.Init("1.2.3.4", "", 0);
   EXPECT_EQ("canary-channel", params.current_channel());
@@ -1527,25 +1531,31 @@
       "appid=\"{22222222-2222-2222-2222-222222222222}\" "
       "version=\"0.0.0.0\" from_version=\"1.2.3.4\" "
       "track=\"stable-channel\" from_track=\"canary-channel\" "));
+
+  ASSERT_TRUE(utils::RecursiveUnlinkDir(test_dir));
 }
 
 TEST(OmahaRequestActionTest, TestChangingToLessStableChannel) {
-  const string kTestDir = "omaha_request_action-test";
-  ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc"));
-  ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir +
+  // Create a uniquely named test directory.
+  string test_dir;
+  ASSERT_TRUE(utils::MakeTempDirectory(
+          "omaha_request_action-test-XXXXXX", &test_dir));
+
+  ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + "/etc"));
+  ASSERT_EQ(0, System(string("mkdir -p ") + test_dir +
                       kStatefulPartition + "/etc"));
   vector<char> post_data;
   NiceMock<PrefsMock> prefs;
   ASSERT_TRUE(WriteFileString(
-      kTestDir + "/etc/lsb-release",
+      test_dir + "/etc/lsb-release",
       "CHROMEOS_RELEASE_APPID={11111111-1111-1111-1111-111111111111}\n"
       "CHROMEOS_BOARD_APPID={22222222-2222-2222-2222-222222222222}\n"
       "CHROMEOS_RELEASE_TRACK=stable-channel\n"));
   ASSERT_TRUE(WriteFileString(
-      kTestDir + kStatefulPartition + "/etc/lsb-release",
+      test_dir + kStatefulPartition + "/etc/lsb-release",
       "CHROMEOS_RELEASE_TRACK=canary-channel\n"));
   OmahaRequestParams params = kDefaultTestParams;
-  params.set_root(string("./") + kTestDir);
+  params.set_root(string("./") + test_dir);
   params.SetLockDown(false);
   params.Init("5.6.7.8", "", 0);
   EXPECT_EQ("stable-channel", params.current_channel());
@@ -1567,6 +1577,8 @@
       "version=\"5.6.7.8\" "
       "track=\"canary-channel\" from_track=\"stable-channel\""));
   EXPECT_EQ(string::npos, post_str.find( "from_version"));
+
+  ASSERT_TRUE(utils::RecursiveUnlinkDir(test_dir));
 }
 
 }  // namespace chromeos_update_engine
diff --git a/omaha_response_handler_action_unittest.cc b/omaha_response_handler_action_unittest.cc
index 06d1570..d5831d4 100644
--- a/omaha_response_handler_action_unittest.cc
+++ b/omaha_response_handler_action_unittest.cc
@@ -274,21 +274,25 @@
   in.hash = "HASHjk";
   in.size = 15;
 
-  const string kTestDir = "omaha_response_handler_action-test";
-  ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc"));
-  ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir +
+  // Create a uniquely named test directory.
+  string test_dir;
+  ASSERT_TRUE(utils::MakeTempDirectory(
+          "omaha_response_handler_action-test-XXXXXX", &test_dir));
+
+  ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + "/etc"));
+  ASSERT_EQ(0, System(string("mkdir -p ") + test_dir +
                       kStatefulPartition + "/etc"));
   ASSERT_TRUE(WriteFileString(
-      kTestDir + "/etc/lsb-release",
+      test_dir + "/etc/lsb-release",
       "CHROMEOS_RELEASE_TRACK=canary-channel\n"));
   ASSERT_TRUE(WriteFileString(
-      kTestDir + kStatefulPartition + "/etc/lsb-release",
+      test_dir + kStatefulPartition + "/etc/lsb-release",
       "CHROMEOS_IS_POWERWASH_ALLOWED=true\n"
       "CHROMEOS_RELEASE_TRACK=stable-channel\n"));
 
   MockSystemState mock_system_state;
   OmahaRequestParams params(&mock_system_state);
-  params.set_root(string("./") + kTestDir);
+  params.set_root(string("./") + test_dir);
   params.SetLockDown(false);
   params.Init("1.2.3.4", "", 0);
   EXPECT_EQ("canary-channel", params.current_channel());
@@ -301,6 +305,8 @@
   EXPECT_TRUE(DoTestCommon(&mock_system_state, in, "/dev/sda5", "",
                            &install_plan));
   EXPECT_TRUE(install_plan.powerwash_required);
+
+  ASSERT_TRUE(utils::RecursiveUnlinkDir(test_dir));
 }
 
 TEST_F(OmahaResponseHandlerActionTest, ChangeToLessStableChannelTest) {
@@ -312,20 +318,24 @@
   in.hash = "HASHjk";
   in.size = 15;
 
-  const string kTestDir = "omaha_response_handler_action-test";
-  ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc"));
-  ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir +
+  // Create a uniquely named test directory.
+  string test_dir;
+  ASSERT_TRUE(utils::MakeTempDirectory(
+          "omaha_response_handler_action-test-XXXXXX", &test_dir));
+
+  ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + "/etc"));
+  ASSERT_EQ(0, System(string("mkdir -p ") + test_dir +
                       kStatefulPartition + "/etc"));
   ASSERT_TRUE(WriteFileString(
-      kTestDir + "/etc/lsb-release",
+      test_dir + "/etc/lsb-release",
       "CHROMEOS_RELEASE_TRACK=stable-channel\n"));
   ASSERT_TRUE(WriteFileString(
-      kTestDir + kStatefulPartition + "/etc/lsb-release",
+      test_dir + kStatefulPartition + "/etc/lsb-release",
       "CHROMEOS_RELEASE_TRACK=canary-channel\n"));
 
   MockSystemState mock_system_state;
   OmahaRequestParams params(&mock_system_state);
-  params.set_root(string("./") + kTestDir);
+  params.set_root(string("./") + test_dir);
   params.SetLockDown(false);
   params.Init("5.6.7.8", "", 0);
   EXPECT_EQ("stable-channel", params.current_channel());
@@ -339,6 +349,8 @@
   EXPECT_TRUE(DoTestCommon(&mock_system_state, in, "/dev/sda5", "",
                            &install_plan));
   EXPECT_FALSE(install_plan.powerwash_required);
+
+  ASSERT_TRUE(utils::RecursiveUnlinkDir(test_dir));
 }
 
 }  // namespace chromeos_update_engine
diff --git a/update_attempter_unittest.cc b/update_attempter_unittest.cc
index b5df68c..91c6dfe 100644
--- a/update_attempter_unittest.cc
+++ b/update_attempter_unittest.cc
@@ -21,6 +21,7 @@
 #include "update_engine/test_utils.h"
 #include "update_engine/update_attempter.h"
 #include "update_engine/update_check_scheduler.h"
+#include "update_engine/utils.h"
 
 using std::string;
 using testing::_;
@@ -52,7 +53,10 @@
         loop_(NULL) {
     mock_system_state_.set_connection_manager(&mock_connection_manager);
   }
+
   virtual void SetUp() {
+    CHECK(utils::MakeTempDirectory("UpdateAttempterTest-XXXXXX", &test_dir_));
+
     EXPECT_EQ(NULL, attempter_.dbus_service_);
     EXPECT_TRUE(attempter_.system_state_ != NULL);
     EXPECT_EQ(NULL, attempter_.update_check_scheduler_);
@@ -70,6 +74,10 @@
     prefs_ = mock_system_state_.mock_prefs();
   }
 
+  virtual void TearDown() {
+    utils::RecursiveUnlinkDir(test_dir_);
+  }
+
   void QuitMainLoop();
   static gboolean StaticQuitMainLoop(gpointer data);
 
@@ -116,6 +124,8 @@
   NiceMock<PrefsMock>* prefs_; // shortcut to mock_system_state_->mock_prefs()
   NiceMock<MockConnectionManager> mock_connection_manager;
   GMainLoop* loop_;
+
+  string test_dir_;
 };
 
 TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
@@ -612,7 +622,8 @@
       DoAll(SetArgumentPointee<0>(std::string("beta-channel")),
       Return(true)));
 
-  attempter_.omaha_request_params_->set_root("./UpdateAttempterTest");
+  ASSERT_FALSE(test_dir_.empty());
+  attempter_.omaha_request_params_->set_root(test_dir_);
   attempter_.Update("", "", false, false, false);
   EXPECT_EQ("beta-channel",
             attempter_.omaha_request_params_->target_channel());