def testMergeBotoConfig(self):
    boto_config = os.path.join(self.tempdir, 'boto.cfg')
    osutils.WriteFile(boto_config, '[S]\nk = v')
    os.environ['BOTO_CONFIG'] = boto_config

    with boto_compat.FixBotoCerts(strict=True):
      config = configparser.SafeConfigParser()
      config.read(os.environ['BOTO_CONFIG'])
      self.assertEqual(config.get('S', 'k'), 'v')
      self.assertTrue(config.has_option('Boto', 'ca_certificates_file'))

    self.assertEqual(os.environ['BOTO_CONFIG'], boto_config)
  def testCaFix(self):
    os.environ['BOTO_CONFIG'] = os.path.join(self.tempdir, 'fake')
    with boto_compat.FixBotoCerts(strict=True):
      boto_config = os.environ['BOTO_CONFIG']
      self.assertExists(boto_config)

      config = configparser.SafeConfigParser()
      config.read(boto_config)

      cafile = config.get('Boto', 'ca_certificates_file')
      self.assertExists(cafile)

    self.assertNotExists(boto_config)
  def testMergeBotoPath(self):
    cfgfile1 = os.path.join(self.tempdir, 'boto1.cfg')
    osutils.WriteFile(cfgfile1, '[S]\nk = v\nk2 = v1')
    cfgfile2 = os.path.join(self.tempdir, 'boto2.cfg')
    osutils.WriteFile(cfgfile2, '[S]\nk2 = v2')
    os.environ['BOTO_PATH'] = boto_path = '%s:%s' % (cfgfile1, cfgfile2)

    with boto_compat.FixBotoCerts(strict=True):
      config = configparser.SafeConfigParser()
      config.read(os.environ['BOTO_CONFIG'])
      self.assertEqual(config.get('S', 'k'), 'v')
      self.assertEqual(config.get('S', 'k2'), 'v2')
      self.assertTrue(config.has_option('Boto', 'ca_certificates_file'))

    self.assertEqual(os.environ['BOTO_PATH'], boto_path)
def Cbuildbot(buildroot, depot_tools_path, argv):
    """Start cbuildbot in specified directory with all arguments.

  Args:
    buildroot: Directory to be passed to cbuildbot with --buildroot.
    depot_tools_path: Directory for depot_tools to be used by cbuildbot.
    argv: Command line options passed to cbuildbot_launch.

  Returns:
    Return code of cbuildbot as an integer.
  """
    logging.info('Bootstrap cbuildbot in: %s', buildroot)

    # Fixup buildroot parameter.
    argv = argv[:]
    for i, arg in enumerate(argv):
        if arg in ('-r', '--buildroot'):
            argv[i + 1] = buildroot

    # This filters out command line arguments not supported by older versions
    # of cbuildbot.
    parser = cbuildbot.CreateParser()
    options = cbuildbot.ParseCommandLine(parser, argv)
    cbuildbot_path = os.path.join(buildroot, 'chromite', 'bin', 'cbuildbot')
    cmd = sync_stages.BootstrapStage.FilterArgsForTargetCbuildbot(
        buildroot, cbuildbot_path, options)

    # We want cbuildbot to use branched depot_tools scripts from our manifest,
    # so that depot_tools is branched to match cbuildbot.
    logging.info('Adding depot_tools into PATH: %s', depot_tools_path)
    extra_env = {'PATH': PrependPath(depot_tools_path)}

    # TODO(crbug.com/845304): Remove once underlying boto issues are resolved.
    fix_boto = ShouldFixBotoCerts(options)

    with boto_compat.FixBotoCerts(activate=fix_boto):
        result = cros_build_lib.run(cmd,
                                    extra_env=extra_env,
                                    error_code_ok=True,
                                    cwd=buildroot)

    return result.returncode
 def testActivateFalse(self):
   os.environ.pop('BOTO_CONFIG', None)
   with boto_compat.FixBotoCerts(strict=True, activate=False):
     self.assertNotIn('BOTO_CONFIG', os.environ)