def testNoBuildTargetFails(self):
        """No build target argument should fail."""
        request = self._GetRequest()

        with self.assertRaises(cros_build_lib.DieSystemExit):
            packages_controller.GetTargetVersions(request, self.response,
                                                  self.api_config)
    def testValidateOnly(self):
        """Sanity check that a validate only call does not execute any logic."""
        builds_chrome = self.PatchObject(packages_service,
                                         'builds',
                                         return_value=True)
        patch_version = self.PatchObject(packages_service,
                                         'determine_android_version')
        patch_branch_version = self.PatchObject(packages_service,
                                                'determine_android_branch')
        patch_target_version = self.PatchObject(packages_service,
                                                'determine_android_target')
        chrome_version = self.PatchObject(packages_service,
                                          'determine_chrome_version')
        platform_version = self.PatchObject(packages_service,
                                            'determine_platform_version')
        milestone_version = self.PatchObject(packages_service,
                                             'determine_milestone_version')
        full_version = self.PatchObject(packages_service,
                                        'determine_full_version')

        request = self._GetRequest(board='betty')
        packages_controller.GetTargetVersions(request, self.response,
                                              self.validate_only_config)
        patch_version.assert_not_called()
        patch_branch_version.assert_not_called()
        patch_target_version.assert_not_called()
        builds_chrome.assert_not_called()
        chrome_version.assert_not_called()
        platform_version.assert_not_called()
        milestone_version.assert_not_called()
        full_version.assert_not_called()
  def testGetTargetVersionsWithPackagesSet(self):
    """Verify packages pass through and basic return values."""
    # TODO(crbug.com/1124393): Migrate this test to use portage_testables
    # rather than mocking the boundary to portage calls such as
    # packages_service.builds).
    builds_mock = self.PatchObject(packages_service, 'builds',
                                   return_value=True)
    # Mock that chrome is built and set the chrome_version.
    chrome_version = '76.0.1.2'
    self.PatchObject(packages_service, 'determine_chrome_version',
                     return_value=chrome_version)
    android_version = 'android_test_version'
    self.PatchObject(packages_service, 'determine_android_version',
                     return_value=android_version)
    android_branch = 'android_test_branch'
    self.PatchObject(packages_service, 'determine_android_branch',
                     return_value=android_branch)
    android_target = 'android_test_target'
    self.PatchObject(packages_service, 'determine_android_target',
                     return_value=android_target)
    platform_version = '12345.1.2'
    self.PatchObject(packages_service, 'determine_platform_version',
                     return_value=platform_version)
    milestone_version = '79'
    self.PatchObject(packages_service, 'determine_milestone_version',
                     return_value=milestone_version)
    full_version = 'R79-12345.1.2'
    self.PatchObject(packages_service, 'determine_full_version',
                     return_value=full_version)
    request = self._GetRequest(board='betty')
    # Add optional packages to the request.
    cpv_package_list = []
    package = request.packages.add()
    package.package_name = 'test'
    package.category = 'chromeos-base'
    package.version = '0.0.1-r1'
    cpv_package_list.append(controller_util.PackageInfoToCPV(package))
    package = request.packages.add()
    package.package_name = 'target-fuzzers'
    package.category = 'virtual'
    cpv_package_list.append(controller_util.PackageInfoToCPV(package))

    packages_controller.GetTargetVersions(request, self.response,
                                          self.api_config)
    self.assertEqual(self.response.android_version, android_version)
    self.assertEqual(self.response.android_branch_version, android_branch)
    self.assertEqual(self.response.android_target_version, android_target)
    self.assertEqual(self.response.chrome_version, chrome_version)
    self.assertEqual(self.response.platform_version, platform_version)
    self.assertEqual(self.response.milestone_version, milestone_version)
    # Verify call to packages.builds passes the package list.
    builds_mock.assert_called_with(constants.CHROME_CP,
                                   mock.ANY,  # Match the build target object
                                   packages=cpv_package_list)
 def testGetTargetVersions(self):
   """Verify basic return values."""
   # Mock that chrome is built and set the chrome_version.
   self.PatchObject(packages_service, 'builds', return_value=True)
   chrome_version = '76.0.1.2'
   chrome_version_mock = self.PatchObject(packages_service,
                                          'determine_chrome_version',
                                          return_value=chrome_version)
   android_version = 'android_test_version'
   android_version_mock = self.PatchObject(packages_service,
                                           'determine_android_version',
                                           return_value=android_version)
   android_branch = 'android_test_branch'
   android_branch_mock = self.PatchObject(packages_service,
                                          'determine_android_branch',
                                          return_value=android_branch)
   android_target = 'android_test_target'
   android_target_mock = self.PatchObject(packages_service,
                                          'determine_android_target',
                                          return_value=android_target)
   platform_version = '12345.1.2'
   self.PatchObject(packages_service, 'determine_platform_version',
                    return_value=platform_version)
   milestone_version = '79'
   self.PatchObject(packages_service, 'determine_milestone_version',
                    return_value=milestone_version)
   full_version = 'R79-12345.1.2'
   self.PatchObject(packages_service, 'determine_full_version',
                    return_value=full_version)
   request = self._GetRequest(board='betty')
   packages_controller.GetTargetVersions(request, self.response,
                                         self.api_config)
   self.assertEqual(self.response.android_version, android_version)
   self.assertEqual(self.response.android_branch_version, android_branch)
   self.assertEqual(self.response.android_target_version, android_target)
   self.assertEqual(self.response.chrome_version, chrome_version)
   self.assertEqual(self.response.platform_version, platform_version)
   self.assertEqual(self.response.milestone_version, milestone_version)
   self.assertEqual(self.response.full_version, full_version)
   # Verify call to determine_chrome_version passes a build_target object.
   build_target = build_target_lib.BuildTarget('betty')
   chrome_version_mock.assert_called_with(build_target)
   # Verify call to determine_android_version passes a list of the board name.
   android_version_mock.assert_called_with(['betty'])
   # Verify call to determine_android_branch passes a board name.
   android_branch_mock.assert_called_with('betty')
   # Verify call to determine_android_target passes a board name.
   android_target_mock.assert_called_with('betty')
 def testGetTargetVersions(self):
     """Verify basic return values."""
     # Mock that chrome is built and set the chrome_version.
     self.PatchObject(packages_service, 'builds', return_value=True)
     chrome_version = '76.0.1.2'
     self.PatchObject(packages_service,
                      'determine_chrome_version',
                      return_value=chrome_version)
     android_version = 'android_test_version'
     self.PatchObject(packages_service,
                      'determine_android_version',
                      return_value=android_version)
     android_branch = 'android_test_branch'
     self.PatchObject(packages_service,
                      'determine_android_branch',
                      return_value=android_branch)
     android_target = 'android_test_target'
     self.PatchObject(packages_service,
                      'determine_android_target',
                      return_value=android_target)
     platform_version = '12345.1.2'
     self.PatchObject(packages_service,
                      'determine_platform_version',
                      return_value=platform_version)
     milestone_version = '79'
     self.PatchObject(packages_service,
                      'determine_milestone_version',
                      return_value=milestone_version)
     full_version = 'R79-12345.1.2'
     self.PatchObject(packages_service,
                      'determine_full_version',
                      return_value=full_version)
     request = self._GetRequest(board='betty')
     packages_controller.GetTargetVersions(request, self.response,
                                           self.api_config)
     self.assertEqual(self.response.android_version, android_version)
     self.assertEqual(self.response.android_branch_version, android_branch)
     self.assertEqual(self.response.android_target_version, android_target)
     self.assertEqual(self.response.chrome_version, chrome_version)
     self.assertEqual(self.response.platform_version, platform_version)
     self.assertEqual(self.response.milestone_version, milestone_version)
     self.assertEqual(self.response.full_version, full_version)
 def testGetTargetVersionNoAndroidNoChrome(self):
   """Verify return values on a board that does not have android."""
   platform_version = '12345.1.2'
   self.PatchObject(packages_service, 'determine_platform_version',
                    return_value=platform_version)
   self.PatchObject(packages_service, 'builds', return_value=False)
   self.PatchObject(packages_service, 'determine_android_version',
                    return_value=None)
   self.PatchObject(packages_service, 'determine_android_branch',
                    return_value=None)
   self.PatchObject(packages_service, 'determine_android_target',
                    return_value=None)
   request = self._GetRequest(board='betty')
   packages_controller.GetTargetVersions(request, self.response,
                                         self.api_config)
   self.assertFalse(self.response.chrome_version)
   self.assertFalse(self.response.android_version)
   self.assertFalse(self.response.android_branch_version)
   self.assertFalse(self.response.android_target_version)
   self.assertEqual(self.response.platform_version, platform_version)
    def testMockCall(self):
        """Test that a mock call does not execute logic, returns mocked value."""
        builds_chrome = self.PatchObject(packages_service,
                                         'builds',
                                         return_value=True)
        patch_version = self.PatchObject(packages_service,
                                         'determine_android_version')
        patch_branch_version = self.PatchObject(packages_service,
                                                'determine_android_branch')
        patch_target_version = self.PatchObject(packages_service,
                                                'determine_android_target')
        chrome_version = self.PatchObject(packages_service,
                                          'determine_chrome_version')
        platform_version = self.PatchObject(packages_service,
                                            'determine_platform_version')
        milestone_version = self.PatchObject(packages_service,
                                             'determine_milestone_version')
        full_version = self.PatchObject(packages_service,
                                        'determine_full_version')

        request = self._GetRequest(board='betty')
        packages_controller.GetTargetVersions(request, self.response,
                                              self.mock_call_config)

        patch_version.assert_not_called()
        patch_branch_version.assert_not_called()
        patch_target_version.assert_not_called()
        builds_chrome.assert_not_called()
        chrome_version.assert_not_called()
        platform_version.assert_not_called()
        milestone_version.assert_not_called()
        full_version.assert_not_called()

        self.assertTrue(self.response.android_version)
        self.assertTrue(self.response.android_branch_version)
        self.assertTrue(self.response.android_target_version)
        self.assertTrue(self.response.chrome_version)
        self.assertTrue(self.response.platform_version)
        self.assertTrue(self.response.milestone_version)
        self.assertTrue(self.response.full_version)