def test_get_config(load_path_mock): with env(), util.tempdir() as tempdir: os.environ.pop('DCOS_CONFIG', None) os.environ[constants.DCOS_DIR_ENV] = tempdir # no config file of any type # this should create the global config config.get_config() global_toml = os.path.join(tempdir, "dcos.toml") load_path_mock.assert_called_once_with(global_toml, False) load_path_mock.reset_mock() # create old global config toml global_toml = create_global_config(tempdir) config.get_config() load_path_mock.assert_called_once_with(global_toml, False) load_path_mock.reset_mock() # clusters dir, no clusters _create_clusters_dir(tempdir) config.get_config() load_path_mock.assert_called_once_with(global_toml, False) load_path_mock.reset_mock() cluster_id = "fake-cluster" cluster_path = add_cluster_dir(cluster_id, tempdir) cluster_toml = os.path.join(cluster_path, "dcos.toml") config.get_config(True) load_path_mock.assert_called_with(cluster_toml, True)
def test_get_config(load_path_mock): with env(), util.tempdir() as tempdir: os.environ.pop('DCOS_CONFIG', None) os.environ[constants.DCOS_DIR_ENV] = tempdir # no config file of any type with pytest.raises(DCOSException) as e: config.get_config() msg = ("No cluster is attached. " "Please run `dcos cluster attach <cluster-name>`") assert str(e.value) == msg load_path_mock.assert_not_called() # create old global config toml global_toml = create_global_config(tempdir) config.get_config() load_path_mock.assert_called_once_with(global_toml, False) # clusters dir, no clusters _create_clusters_dir(tempdir) with pytest.raises(DCOSException) as e: config.get_config() assert str(e.value) == msg cluster_id = "fake-cluster" cluster_path = add_cluster_dir(cluster_id, tempdir) cluster_toml = os.path.join(cluster_path, "dcos.toml") config.get_config(True) load_path_mock.assert_any_call(cluster_toml, True)
def test_get_clusters_with_configured_link(get_linked_clusters): with env(), util.tempdir() as tempdir: os.environ[constants.DCOS_DIR_ENV] = tempdir cluster_id = "a8b53513-63d4-4059-8b08-fde4fe1f1a83" add_cluster_dir(cluster_id, tempdir) get_linked_clusters.return_value = [_linked_cluster(cluster_id)] clusters = cluster.get_clusters(True) assert len(clusters) == 1 assert type(clusters[0]) == cluster.Cluster
def test_get_cluster_name_ignore_env(): with env(): os.environ['DCOS_CLUSTER_NAME'] = 'fake-name' cluster_conf = config.Toml({ 'cluster': {'name': 'real-name'}, }) cluster_name = config.get_config_val('cluster.name', cluster_conf) assert cluster_name == 'real-name'
def test_uses_deprecated_config(): with env(), util.tempdir() as tempdir: os.environ.pop('DCOS_CONFIG', None) os.environ[constants.DCOS_DIR_ENV] = tempdir assert config.get_config_dir_path() == tempdir # create old global config toml global_toml = create_global_config(tempdir) assert config.get_global_config_path() == global_toml assert config.uses_deprecated_config() is True # create clusters subdir _create_clusters_dir(tempdir) assert config.uses_deprecated_config() is False
def test_get_clusters(): with env(), util.tempdir() as tempdir: os.environ[constants.DCOS_DIR_ENV] = tempdir # no config file of any type assert cluster.get_clusters() == [] # cluster dir exists, no cluster clusters_dir = os.path.join(tempdir, constants.DCOS_CLUSTERS_SUBDIR) util.ensure_dir_exists(clusters_dir) assert cluster.get_clusters() == [] # one cluster cluster_id = "fake_cluster" add_cluster_dir(cluster_id, tempdir) assert cluster.get_clusters() == [_cluster(cluster_id)]
def test_get_attached_cluster_path(): with env(), util.tempdir() as tempdir: os.environ[constants.DCOS_DIR_ENV] = tempdir # no clusters dir assert config.get_attached_cluster_path() is None # clusters dir, no clusters _create_clusters_dir(tempdir) assert config.get_attached_cluster_path() is None # 1 cluster, not attached cluster_id = "fake-cluster" cluster_path = add_cluster_dir(cluster_id, tempdir) assert config.get_attached_cluster_path() == cluster_path attached_path = os.path.join(cluster_path, constants.DCOS_CLUSTER_ATTACHED_FILE) assert os.path.exists(attached_path) # attached cluster assert config.get_attached_cluster_path() == cluster_path
def test_move_to_cluster_config(mock_get, mock_config): with env(), util.tempdir() as tempdir: os.environ[constants.DCOS_DIR_ENV] = tempdir create_global_config(tempdir) mock_config.return_value = "fake-url" cluster_id = "fake" mock_resp = mock.Mock() mock_resp.json.return_value = {"CLUSTER_ID": cluster_id} mock_get.return_value = mock_resp assert config.get_config_dir_path() == tempdir cluster.move_to_cluster_config() clusters_path = os.path.join(tempdir, constants.DCOS_CLUSTERS_SUBDIR) assert os.path.exists(clusters_path) cluster_path = os.path.join(clusters_path, cluster_id) assert os.path.exists(os.path.join(cluster_path, "dcos.toml")) assert os.path.exists( os.path.join(cluster_path, constants.DCOS_CLUSTER_ATTACHED_FILE))
def test_set_attached(): with env(), util.tempdir() as tempdir: os.environ[constants.DCOS_DIR_ENV] = tempdir cluster_path = add_cluster_dir("a", tempdir) # no attached_cluster assert cluster.set_attached(cluster_path) is None assert config.get_attached_cluster_path() == cluster_path cluster_path2 = add_cluster_dir("b", tempdir) # attach cluster already attached assert cluster.set_attached(cluster_path2) is None assert config.get_attached_cluster_path() == cluster_path2 # attach cluster through environment os.environ[constants.DCOS_CLUSTER] = "a" assert config.get_attached_cluster_path() == cluster_path # attach back to old cluster through environment os.environ[constants.DCOS_CLUSTER] = "b" assert config.get_attached_cluster_path() == cluster_path2
def test_setup_cluster_config(mock_get): with env(), util.tempdir() as tempdir: os.environ[constants.DCOS_DIR_ENV] = tempdir with cluster.setup_directory() as setup_temp: cluster.set_attached(setup_temp) cluster_id = "fake" mock_resp = mock.Mock() mock_resp.json.return_value = { "CLUSTER_ID": cluster_id, "cluster": cluster_id } mock_get.return_value = mock_resp path = cluster.setup_cluster_config("fake_url", setup_temp, False) expected_path = os.path.join( tempdir, constants.DCOS_CLUSTERS_SUBDIR + "/" + cluster_id) assert path == expected_path assert os.path.exists(path) assert os.path.exists(os.path.join(path, "dcos.toml")) assert not os.path.exists(setup_temp)
def test_get_clusters(): with env(), util.tempdir() as tempdir: os.environ[constants.DCOS_DIR_ENV] = tempdir # no config file of any type assert cluster.get_clusters() == [] # cluster dir exists, no cluster clusters_dir = os.path.join(tempdir, constants.DCOS_CLUSTERS_SUBDIR) util.ensure_dir_exists(clusters_dir) assert cluster.get_clusters() == [] # a valid cluster cluster_id = "a8b53513-63d4-4059-8b08-fde4fe1f1a83" add_cluster_dir(cluster_id, tempdir) # Make sure clusters dir can contain random files / folders # cf. https://jira.mesosphere.com/browse/DCOS_OSS-1782 util.ensure_file_exists(os.path.join(clusters_dir, '.DS_Store')) util.ensure_dir_exists(os.path.join(clusters_dir, 'not_a_cluster')) assert cluster.get_clusters() == [_cluster(cluster_id)]
def test_admin_ids_are_strings(): with env("ADMIN_USER_IDS", "141649488069656576"): admins = run_lemon_bot.get_admins() assert isinstance(admins[0], int) assert admins[0] == 141649488069656576