Update engine should use the release channel policy if it exists.

The release channel (aka update track) can be specified by a device
policy. When this is the case, the update engine should use the
value specified by the policy instead of the value specified in
/etc/lsb-release.

BUG=chromium-os:17015
TEST=Added two new tests:
- Added test that OmahaRequestParams uses the release channel passed
  in to it when the value is valid, and otherwise uses /etc/lsb-release.
- Added test that the update engine correctly picks up the release
  channel that's specified by the policy.

Change-Id: I2fe03712220bb3286476b12cd1f1b330ad006d7c
Reviewed-on: http://gerrit.chromium.org/gerrit/5072
Tested-by: Patrick Dubroy <dubroy@chromium.org>
Reviewed-by: Andrew de los Reyes <adlr@chromium.org>
diff --git a/update_attempter_unittest.cc b/update_attempter_unittest.cc
index b61377b..673471c 100644
--- a/update_attempter_unittest.cc
+++ b/update_attempter_unittest.cc
@@ -4,6 +4,8 @@
 
 #include <base/file_util.h>
 #include <gtest/gtest.h>
+#include <policy/libpolicy.h>
+#include <policy/mock_device_policy.h>
 
 #include "update_engine/action_mock.h"
 #include "update_engine/action_processor_mock.h"
@@ -60,14 +62,19 @@
     attempter_.prefs_ = &prefs_;
   }
 
+  void QuitMainLoop();
+  static gboolean StaticQuitMainLoop(gpointer data);
+
   void UpdateTestStart();
   void UpdateTestVerify();
   static gboolean StaticUpdateTestStart(gpointer data);
   static gboolean StaticUpdateTestVerify(gpointer data);
+
   void PingOmahaTestStart();
-  void PingOmahaTestDone();
   static gboolean StaticPingOmahaTestStart(gpointer data);
-  static gboolean StaticPingOmahaTestDone(gpointer data);
+
+  void ReadTrackFromPolicyTestStart();
+  static gboolean StaticReadTrackFromPolicyTestStart(gpointer data);
 
   MockDbusGlib dbus_;
   UpdateAttempterUnderTest attempter_;
@@ -237,6 +244,15 @@
                UpdateStatusToString(static_cast<UpdateStatus>(-1)));
 }
 
+void UpdateAttempterTest::QuitMainLoop() {
+  g_main_loop_quit(loop_);
+}
+
+gboolean UpdateAttempterTest::StaticQuitMainLoop(gpointer data) {
+  reinterpret_cast<UpdateAttempterTest*>(data)->QuitMainLoop();
+  return FALSE;
+}
+
 gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) {
   reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart();
   return FALSE;
@@ -252,8 +268,9 @@
   return FALSE;
 }
 
-gboolean UpdateAttempterTest::StaticPingOmahaTestDone(gpointer data) {
-  reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestDone();
+gboolean UpdateAttempterTest::StaticReadTrackFromPolicyTestStart(
+    gpointer data) {
+  reinterpret_cast<UpdateAttempterTest*>(data)->ReadTrackFromPolicyTestStart();
   return FALSE;
 }
 
@@ -320,11 +337,7 @@
       .Times(1);
   EXPECT_CALL(*processor_, StartProcessing()).Times(1);
   attempter_.PingOmaha();
-  g_idle_add(&StaticPingOmahaTestDone, this);
-}
-
-void UpdateAttempterTest::PingOmahaTestDone() {
-  g_main_loop_quit(loop_);
+  g_idle_add(&StaticQuitMainLoop, this);
 }
 
 TEST_F(UpdateAttempterTest, PingOmahaTest) {
@@ -366,4 +379,32 @@
             attempter_.error_event_->error_code);
 }
 
+TEST_F(UpdateAttempterTest, ReadTrackFromPolicy) {
+  loop_ = g_main_loop_new(g_main_context_default(), FALSE);
+  g_idle_add(&StaticReadTrackFromPolicyTestStart, this);
+  g_main_loop_run(loop_);
+  g_main_loop_unref(loop_);
+  loop_ = NULL;
+}
+
+void UpdateAttempterTest::ReadTrackFromPolicyTestStart() {
+  // Tests that the update track (aka release channel) is properly fetched
+  // from the device policy.
+
+  policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
+  attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
+
+  EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
+
+  EXPECT_CALL(*device_policy, GetReleaseChannel(_))
+      .WillRepeatedly(DoAll(
+          SetArgumentPointee<0>(std::string("canary-channel")),
+          Return(true)));
+
+  attempter_.Update("", "", false, false);
+  EXPECT_EQ("canary-channel", attempter_.omaha_request_params_.app_track);
+
+  g_idle_add(&StaticQuitMainLoop, this);
+}
+
 }  // namespace chromeos_update_engine