Пример #1
0
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
Пример #2
0
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)
Пример #3
0
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)]
Пример #4
0
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)
Пример #5
0
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
Пример #6
0
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)]
Пример #7
0
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