Ejemplo n.º 1
0
def BuildLabConfigPool(lab_config_path, key_path=None):
  """Build lab config pool based on configured config path."""
  if not lab_config_path:
    logger.debug('No lab config path set.')
    lab_config_pool = lab_config.LabConfigPool()
  elif lab_config_path.startswith('gs:'):
    logger.debug('Use lab config path %s.', lab_config_path)
    if key_path:
      cred = google_auth_util.CreateCredentialFromServiceAccount(
          key_path,
          scopes=[google_auth_util.GCS_READ_SCOPE])
    else:
      cred = google_auth_util.GetGCloudCredential(
          command_util.CommandContext())
    gcs_client = gcs_file_util.CreateGCSClient(_MTT_PROJECT, cred)
    lab_config_pool = lab_config.LabConfigPool(
        gcs_file_util.GCSFileEnumerator(
            gcs_client, lab_config_path, lab_config.IsYaml))
  else:
    logger.debug('Use lab config path %s.', lab_config_path)
    lab_config_pool = lab_config.LabConfigPool(
        lab_config.LocalFileEnumerator(
            lab_config_path, lab_config.IsYaml))
  lab_config_pool.LoadConfigs()
  return lab_config_pool
Ejemplo n.º 2
0
 def testGetLabConfig(self):
     """Test ConfigPool LoadConfigs can load one config file."""
     config_path = GetTestFilePath('valid/config.yaml')
     pool = lab_config.LabConfigPool(
         lab_config.LocalFileEnumerator(config_path, lab_config.IsYaml))
     pool.LoadConfigs()
     self.assertIsNotNone(pool._lab_to_lab_config_pb.get('lab1'))
     config = pool.GetLabConfig()
     self.assertEqual('lab1', config.lab_name)
     self.assertEqual('lab_user1', config.host_login_name)
     self.assertEqual(['lab_user1', 'user1'], config.owners)
     self.assertEqual('tfc_url', config.control_server_url)
     self.assertEqual('lab_docker_image', config.docker_image)
     self.assertEqual('docker_server_1', config.docker_server)
     self.assertTrue(config.enable_stackdriver)
     self.assertTrue(config.enable_autoupdate)
     self.assertTrue(config.enable_ui_update)
     self.assertEqual('path/to/key.json',
                      config.service_account_json_key_path)
     self.assertEqual('secret_project_id', config.secret_project_id)
     self.assertEqual('lab_sv_key', config.service_account_key_secret_id)
     self.assertEqual('*****@*****.**', config.service_account)
     self.assertEqual('AStringToRepresentApiKey', config.engprod_api_key)
     self.assertEqual(
         '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null '
         '-F /path/to/ssh/config -C', config.ssh_arg)
Ejemplo n.º 3
0
 def testGetHostConfig_notExist(self):
     """Test get host config for not exist host from LabConfigPool works."""
     config_path = GetTestFilePath('valid/config.yaml')
     pool = lab_config.LabConfigPool(
         lab_config.LocalFileEnumerator(config_path, lab_config.IsYaml))
     pool.LoadConfigs()
     host = pool.GetHostConfig('not_exist')
     self.assertIsNone(host)
Ejemplo n.º 4
0
 def testGetHostConfigs_all(self):
     """Test get hosts from LabConfigPool works."""
     config_path = GetTestFilePath('valid/config.yaml')
     pool = lab_config.LabConfigPool(
         lab_config.LocalFileEnumerator(config_path, lab_config.IsYaml))
     pool.LoadConfigs()
     hosts = pool.GetHostConfigs()
     self.assertEqual(5, len(hosts))
Ejemplo n.º 5
0
 def testLoadConfigs_noConfigs(self):
     """Test LabConfigPool LoadConfigs fail when there is config under path."""
     config_path = GetTestFilePath('no_config')
     with six.assertRaisesRegex(self, lab_config.ConfigError,
                                r'.* no lab config files under the path.'):
         pool = lab_config.LabConfigPool(
             lab_config.LocalFileEnumerator(config_path, lab_config.IsYaml))
         pool.LoadConfigs()
Ejemplo n.º 6
0
 def testLoadConfigs_notExist(self):
     """Test LabConfigPool LoadConfigs fail when config doesn't exist."""
     config_path = GetTestFilePath('valid/non_exist_config.yaml')
     with six.assertRaisesRegex(self, lab_config.ConfigError,
                                r'.* doesn\'t exist.'):
         pool = lab_config.LabConfigPool(
             lab_config.LocalFileEnumerator(config_path, lab_config.IsYaml))
         pool.LoadConfigs()
Ejemplo n.º 7
0
 def testLoadConfigs_loadMultipleLab(self):
     """Test LabConfigPool LoadConfigs can load configs in a folder."""
     config_path = GetTestFilePath('valid/config.yaml')
     pool = lab_config.LabConfigPool(
         lab_config.LocalFileEnumerator(os.path.dirname(config_path),
                                        lab_config.IsYaml))
     with self.assertRaisesRegex(lab_config.ConfigError,
                                 r'There are multiple labs configured.*'):
         pool.LoadConfigs()
Ejemplo n.º 8
0
 def testBuildHostConfig_emptyConfigPool(self):
     """Test build new host config from LabConfigPool works."""
     pool = lab_config.LabConfigPool()
     pool.LoadConfigs()
     host = pool.BuildHostConfig('new_host',
                                 cluster_name='new_cluster',
                                 host_login_name='new_user')
     self.assertEqual('new_user', host.host_login_name)
     self.assertEqual('new_host', host.hostname)
     self.assertEqual('new_cluster', host.cluster_name)
     self.assertEqual('', host.tf_global_config_path)
Ejemplo n.º 9
0
 def testLoadConfigs(self):
     """Test ConfigPool LoadConfigs can load one config file."""
     config_path = GetTestFilePath('valid/config.yaml')
     pool = lab_config.LabConfigPool(
         lab_config.LocalFileEnumerator(config_path, lab_config.IsYaml))
     pool.LoadConfigs()
     self.assertIsNotNone(pool._lab_to_lab_config_pb.get('lab1'))
     self.assertIsNotNone(
         pool._cluster_to_cluster_config_pb.get('cluster1'))
     self.assertIsNotNone(
         pool._cluster_to_cluster_config_pb.get('cluster2'))
Ejemplo n.º 10
0
 def testBuildHostConfig_useCluster(self):
     """Test build host config with cluster from LabConfigPool works."""
     config_path = GetTestFilePath('valid/config.yaml')
     pool = lab_config.LabConfigPool(
         lab_config.LocalFileEnumerator(config_path, lab_config.IsYaml))
     pool.LoadConfigs()
     host = pool.BuildHostConfig('new_host', cluster_name='cluster1')
     self.assertEqual('new_host', host.hostname)
     self.assertEqual('user1', host.host_login_name)
     self.assertEqual('cluster1', host.cluster_name)
     self.assertEqual('lab1', host.lab_name)
     self.assertEqual('path/to/config.xml', host.tf_global_config_path)
Ejemplo n.º 11
0
 def testGetHostConfigs(self):
     """Test get hosts from LabConfigPool works."""
     config_path = GetTestFilePath('valid/config.yaml')
     pool = lab_config.LabConfigPool(
         lab_config.LocalFileEnumerator(config_path, lab_config.IsYaml))
     pool.LoadConfigs()
     hosts = pool.GetHostConfigs('cluster1')
     self.assertEqual(3, len(hosts))
     self.assertEqual('lab1', hosts[0].lab_name)
     self.assertEqual('host1', hosts[0].hostname)
     self.assertEqual('user1', hosts[0].host_login_name)
     self.assertEqual('cluster1', hosts[0].cluster_name)
     self.assertEqual('path/to/config.xml', hosts[0].tf_global_config_path)
     self.assertEqual('tfc_url', hosts[0].control_server_url)
     self.assertCountEqual(['lab_user1', 'user1', 'user2'], hosts[0].owners)
     self.assertTrue(hosts[0].graceful_shutdown)
     self.assertTrue(hosts[0].enable_stackdriver)
     self.assertTrue(hosts[0].enable_autoupdate)
     self.assertEqual('gcr.io/dockerized-tradefed/tradefed:golden',
                      hosts[0].docker_image)
     self.assertEqual('docker_server_2', hosts[0].docker_server)
     self.assertEqual(['--arg1', 'value1', '--arg2', 'value2'],
                      hosts[0].extra_docker_args)
     self.assertEqual('host2', hosts[1].hostname)
     self.assertEqual('user1', hosts[1].host_login_name)
     self.assertEqual('cluster1', hosts[1].cluster_name)
     self.assertEqual('path/to/config.xml', hosts[1].tf_global_config_path)
     self.assertEqual('tfc_url', hosts[1].control_server_url)
     self.assertCountEqual(['lab_user1', 'user1', 'user2'], hosts[1].owners)
     self.assertEqual('gcr.io/dockerized-tradefed/tradefed:golden',
                      hosts[1].docker_image)
     self.assertEqual('docker_server_2', hosts[1].docker_server)
     self.assertEqual(['--arg1', 'value1'], hosts[1].extra_docker_args)
     self.assertEqual('host3', hosts[2].hostname)
     self.assertEqual('user1', hosts[2].host_login_name)
     self.assertEqual('cluster1', hosts[2].cluster_name)
     self.assertEqual('path/to/new/config.xml',
                      hosts[2].tf_global_config_path)
     self.assertCountEqual(['lab_user1', 'user1', 'user2'], hosts[2].owners)
     self.assertEqual('tfc_url', hosts[2].control_server_url)
     self.assertEqual('gcr.io/dockerized-tradefed/tradefed:canary',
                      hosts[2].docker_image)
     self.assertEqual('docker_server_3', hosts[2].docker_server)
     hosts = pool.GetHostConfigs('cluster2')
     self.assertEqual(2, len(hosts))
     self.assertEqual('lab1', hosts[0].lab_name)
     self.assertEqual('lab_user1', hosts[0].host_login_name)
     self.assertEqual('tfc_control_server_url', hosts[0].control_server_url)
     self.assertEqual('lab_docker_image', hosts[0].docker_image)
     self.assertEqual('docker_server_1', hosts[0].docker_server)
     self.assertTrue(hosts[0].enable_stackdriver)
     self.assertTrue(hosts[0].enable_autoupdate)
Ejemplo n.º 12
0
 def testBuildHostConfig_newHostNewCluster(self):
     """Test build new host config from LabConfigPool works."""
     config_path = GetTestFilePath('valid/config.yaml')
     pool = lab_config.LabConfigPool(
         lab_config.LocalFileEnumerator(config_path, lab_config.IsYaml))
     pool.LoadConfigs()
     host = pool.BuildHostConfig('new_host',
                                 cluster_name='new_cluster',
                                 host_login_name='new_user')
     self.assertEqual('new_user', host.host_login_name)
     self.assertEqual('new_host', host.hostname)
     self.assertEqual('new_cluster', host.cluster_name)
     self.assertEqual('', host.tf_global_config_path)
     self.assertEqual('', host.lab_name)
Ejemplo n.º 13
0
 def testGetHostConfig(self):
     """Test get host config from LabConfigPool works."""
     config_path = GetTestFilePath('valid/config.yaml')
     pool = lab_config.LabConfigPool(
         lab_config.LocalFileEnumerator(config_path, lab_config.IsYaml))
     pool.LoadConfigs()
     host = pool.GetHostConfig('host1')
     self.assertEqual('host1', host.hostname)
     self.assertEqual('user1', host.host_login_name)
     self.assertEqual('cluster1', host.cluster_name)
     self.assertEqual('path/to/config.xml', host.tf_global_config_path)
     self.assertEqual(2, len(host.tmpfs_configs))
     self.assertEqual('/atmpfs', host.tmpfs_configs[0].path)
     self.assertEqual(2000, host.tmpfs_configs[0].size)
     self.assertEqual('750', host.tmpfs_configs[0].mode)
     self.assertEqual('secret_project_id', host.secret_project_id)
     self.assertEqual('lab_sv_key', host.service_account_key_secret_id)
     self.assertEqual('*****@*****.**', host.service_account)
     self.assertEqual(
         '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null '
         '-F /path/to/ssh/config -C', host.ssh_arg)
Ejemplo n.º 14
0
 def _GetHostConfig(self, config_path, hostname):
     pool = lab_config.LabConfigPool(
         lab_config.LocalFileEnumerator(config_path))
     pool.LoadConfigs()
     return pool.GetHostConfig(hostname)