def testGetAvailableKernelProfiles(self):

    def MockGsList(path):
      unused = {
          'content_length': None,
          'creation_time': None,
          'generation': None,
          'metageneration': None
      }
      path = path.replace('*', '%s')
      return [
          gs.GSListResult(
              url=(path % ('4.4', 'R63-9901.21-1506581597')), **unused),
          gs.GSListResult(
              url=(path % ('4.4', 'R63-9999.99-1500000000')), **unused),
          gs.GSListResult(
              url=(path % ('3.8', 'R61-9765.70-1506575230')), **unused),
      ]

    self.PatchObject(gs.GSContext,
                     'List', lambda _, path, **kwargs: MockGsList(path))
    profiles = afdo.GetAvailableKernelProfiles()
    self.assertEqual([[61, 9765, 70, 1506575230]], profiles['3.8'])
    self.assertEqual([[63, 9999, 99, 1500000000], [63, 9901, 21, 1506581597]],
                     profiles['4.4'])
    def PerformStage(self):
        version_info = self._run.GetVersionInfo()
        build_version = [
            int(x) for x in version_info.VersionString().split('.')
        ]
        chrome_version = int(version_info.chrome_branch)
        target_version = [chrome_version] + build_version
        profile_versions = afdo.GetAvailableKernelProfiles()
        candidates = sorted(afdo.FindKernelEbuilds())
        expire_soon = set()
        not_found = set()
        expired = set()
        for candidate, kver in candidates:
            profile_version = None
            if kver in afdo.KERNEL_SKIP_AFDO_UPDATE:
                continue
            if kver in profile_versions:
                profile_version = afdo.FindLatestProfile(
                    target_version, profile_versions[kver])
            if not profile_version:
                not_found.add(kver)
                continue
            if afdo.ProfileAge(
                    profile_version) > afdo.KERNEL_ALLOWED_STALE_DAYS:
                expired.add('%s: %s' % (kver, profile_version))
                continue
            if afdo.ProfileAge(profile_version) > afdo.KERNEL_WARN_STALE_DAYS:
                expire_soon.add('%s: %s' % (kver, profile_version))

            afdo.PatchKernelEbuild(candidate, profile_version)
            # If the *-9999.ebuild is not the last entry in its directory, Manifest
            # will contain an unused line for previous profile which is still fine.
            if candidate.endswith('-9999.ebuild'):
                afdo.UpdateManifest(path_util.ToChrootPath(candidate))
        afdo.CommitIfChanged(afdo.KERNEL_EBUILD_ROOT,
                             'Update profiles and manifests for Kernel.')

        if not_found or expired:
            raise afdo.NoValidProfileFound(
                'Cannot find AutoFDO profiles: %s or expired: %s' %
                (not_found, expired))

        if expire_soon:
            self._WarnSheriff(expire_soon)