metrics: Fix metrics_uploader on VMs

metrics_uploader must be in testing mode so that it does not try to grab the
real hardware id (not available on VMs).

BUG=chromium:413256
TEST=FEATURES=test emerge-amd64-generic metrics.
TEST=platform_MetricsUploader succeeds on a gizmo VM.

Change-Id: I9e508c8661dfdb7933161b0d41ef4cf9bd7db2c6
Reviewed-on: https://chromium-review.googlesource.com/217760
Reviewed-by: Bertrand Simonnet <bsimonnet@chromium.org>
Commit-Queue: Bertrand Simonnet <bsimonnet@chromium.org>
Tested-by: Bertrand Simonnet <bsimonnet@chromium.org>
diff --git a/metrics/metrics_daemon.cc b/metrics/metrics_daemon.cc
index 07c3220..c0c77fa 100644
--- a/metrics/metrics_daemon.cc
+++ b/metrics/metrics_daemon.cc
@@ -187,6 +187,8 @@
 }
 
 void MetricsDaemon::RunUploaderTest() {
+  upload_service_.reset(new UploadService(testing_));
+  upload_service_->Init();
   upload_service_->UploadEvent();
 }
 
@@ -311,7 +313,7 @@
       g_timeout_add(kUpdateStatsIntervalMs, &HandleUpdateStatsTimeout, this);
 
   if (uploader_active) {
-    upload_service_.reset(new UploadService());
+    upload_service_.reset(new UploadService(testing_));
     upload_service_->Init();
   }
 }
diff --git a/metrics/metrics_daemon_main.cc b/metrics/metrics_daemon_main.cc
index 93e2414..e529afd 100644
--- a/metrics/metrics_daemon_main.cc
+++ b/metrics/metrics_daemon_main.cc
@@ -65,7 +65,7 @@
   MetricsLibrary metrics_lib;
   metrics_lib.Init();
   MetricsDaemon daemon;
-  daemon.Init(false,
+  daemon.Init(FLAGS_uploader_test,
               FLAGS_uploader | FLAGS_uploader_test,
               &metrics_lib,
               MetricsMainDiskStatsPath(),
diff --git a/metrics/uploader/system_profile_cache.cc b/metrics/uploader/system_profile_cache.cc
index 12e8bec..604c060 100644
--- a/metrics/uploader/system_profile_cache.cc
+++ b/metrics/uploader/system_profile_cache.cc
@@ -22,9 +22,9 @@
 const char* SystemProfileCache::kPersistentSessionIdFilename =
     "Sysinfo.SessionId";
 
-SystemProfileCache::SystemProfileCache()
+SystemProfileCache::SystemProfileCache(bool testing)
     : initialized_(false),
-      is_testing_(false),
+      testing_(testing),
       session_id_(new chromeos_metrics::PersistentInteger(
           kPersistentSessionIdFilename)) {
 }
@@ -49,7 +49,7 @@
   profile_.channel = ProtoChannelFromString(channel_string);
 
   profile_.client_id =
-      is_testing_ ? "client_id_test" : GetPersistentGUID(kPersistentGUIDFile);
+      testing_ ? "client_id_test" : GetPersistentGUID(kPersistentGUIDFile);
 
   // Increment the session_id everytime we initialize this. If metrics_daemon
   // does not crash, this should correspond to the number of reboots of the
@@ -105,7 +105,7 @@
 bool SystemProfileCache::GetHardwareId(std::string* hwid) {
   CHECK(hwid);
 
-  if (is_testing_) {
+  if (testing_) {
     // if we are in test mode, we do not call crossystem directly.
     DLOG(INFO) << "skipping hardware id";
     *hwid = "";
diff --git a/metrics/uploader/system_profile_cache.h b/metrics/uploader/system_profile_cache.h
index 03e92fc..f85c1b1 100644
--- a/metrics/uploader/system_profile_cache.h
+++ b/metrics/uploader/system_profile_cache.h
@@ -36,7 +36,7 @@
 // The cache is populated lazily. The only method needed is Populate.
 class SystemProfileCache : public SystemProfileSetter {
  public:
-  SystemProfileCache();
+  explicit SystemProfileCache(bool testing);
 
   // Populates the ProfileSystem protobuf with system information.
   void Populate(metrics::ChromeUserMetricsExtension* profile_proto) override;
@@ -69,7 +69,7 @@
   bool GetHardwareId(std::string* hwid);
 
   bool initialized_;
-  bool is_testing_;
+  bool testing_;
   scoped_ptr<chromeos_metrics::PersistentInteger> session_id_;
   SystemProfile profile_;
 };
diff --git a/metrics/uploader/upload_service.cc b/metrics/uploader/upload_service.cc
index 8094fec..8e08f28 100644
--- a/metrics/uploader/upload_service.cc
+++ b/metrics/uploader/upload_service.cc
@@ -38,8 +38,8 @@
 
 const int UploadService::kMaxFailedUpload = 10;
 
-UploadService::UploadService()
-    : system_profile_setter_(new SystemProfileCache()),
+UploadService::UploadService(bool testing)
+    : system_profile_setter_(new SystemProfileCache(testing)),
       histogram_snapshot_manager_(this),
       sender_(new HttpSender(FLAGS_server)) {
 }
diff --git a/metrics/uploader/upload_service.h b/metrics/uploader/upload_service.h
index 0d17e0e..114c5e4 100644
--- a/metrics/uploader/upload_service.h
+++ b/metrics/uploader/upload_service.h
@@ -54,7 +54,7 @@
 //
 class UploadService : public base::HistogramFlattener {
  public:
-  UploadService();
+  explicit UploadService(bool testing);
 
   void Init();
 
diff --git a/metrics/uploader/upload_service_test.cc b/metrics/uploader/upload_service_test.cc
index d60413d..94453b2 100644
--- a/metrics/uploader/upload_service_test.cc
+++ b/metrics/uploader/upload_service_test.cc
@@ -22,7 +22,9 @@
 class UploadServiceTest : public testing::Test {
  protected:
   UploadServiceTest()
-      : upload_service_(), exit_manager_(new base::AtExitManager()) {
+      : cache_(true),
+        upload_service_(true),
+        exit_manager_(new base::AtExitManager()) {
     sender_ = new SenderMock;
     upload_service_.sender_.reset(sender_);
     upload_service_.system_profile_setter_.reset(new MockSystemProfileSetter());
@@ -34,7 +36,6 @@
     upload_service_.GatherHistograms();
     upload_service_.Reset();
     sender_->Reset();
-    cache_.is_testing_ = true;
 
     chromeos_metrics::PersistentInteger::SetTestingMode(true);
     cache_.session_id_.reset(new chromeos_metrics::PersistentInteger(
@@ -123,7 +124,7 @@
 }
 
 TEST_F(UploadServiceTest, LogEmptyByDefault) {
-  UploadService upload_service;
+  UploadService upload_service(true);
 
   // current_log_ should be initialized later as it needs AtExitManager to exit
   // in order to gather system information from SysInfo.
@@ -191,8 +192,7 @@
   base::SysInfo::SetChromeOSVersionInfoForTest(content, base::Time());
   scoped_ptr<metrics::MetricSample> histogram =
       metrics::MetricSample::SparseHistogramSample("myhistogram", 1);
-  SystemProfileCache* local_cache_ = new SystemProfileCache;
-  local_cache_->is_testing_ = true;
+  SystemProfileCache* local_cache_ = new SystemProfileCache(true);
   local_cache_->session_id_.reset(new chromeos_metrics::PersistentInteger(
         dir_.path().Append("session_id").value()));