예제 #1
0
def test_environment_create_modify_save_load():
    user = USER_19
    test_environment_file = get_test_environment_file(user=user)
    test_environment_file2 = get_test_environment_file(user=user) + '_2'
    with ExitStack() as stack:
        stack.enter_context(clear_environment(user))

        assert show_clusters() == {}

        add_cluster(name=TEST_CLUSTER, user=user, host='localhost')

        config = show_cluster(TEST_CLUSTER).config
        check_config_is_default(config=config, user=user)
        try:
            save_environment(path=test_environment_file)
            with open(test_environment_file, 'r') as test_file:
                contents = test_file.read().splitlines()

            pprint(contents)
            assert contents == get_default_config_contents(user=user)

            config = show_cluster(TEST_CLUSTER).config
            config.host = 'localhost2'
            config.port = 2222
            config.user = '******'
            config.auth = AuthMethod.PUBLIC_KEY
            config.key = './fake-key'
            config.install_key = False
            config.disable_sshd = True
            config.setup_actions.jupyter = ['abc']
            config.setup_actions.dask = ['abc', 'def']
            config.scratch = '$HOME2'
            config.use_jupyter_lab = False
            set_log_level(logging.INFO)

            check_config_is_modified(config=config)

            save_environment(path=test_environment_file2)
            with open(test_environment_file2, 'r') as test_file:
                contents = test_file.read().splitlines()

            pprint(contents)
            assert contents == get_modified_config_contents()

            load_environment(test_environment_file)

            config = show_cluster(TEST_CLUSTER).config
            check_config_is_default(config=config, user=user)

            load_environment(test_environment_file2)

            config = show_cluster(TEST_CLUSTER).config
            check_config_is_modified(config=config)

        finally:
            try:
                os.remove(test_environment_file)
            finally:
                os.remove(test_environment_file2)
예제 #2
0
def test_environment():
    user = USER_2
    test_environment_file = get_test_environment_file(user=user)
    with ExitStack() as stack:
        stack.enter_context(clear_environment(user))
        stack.enter_context(set_password(get_test_user_password(user)))

        clusters = show_clusters()
        assert clusters == {}

        cluster = add_cluster(name=TEST_CLUSTER,
                              user=user,
                              host='localhost',
                              port=2222)

        clusters = show_clusters()
        assert show_cluster(name=TEST_CLUSTER) is cluster
        assert len(show_clusters()) == 1
        assert clusters[TEST_CLUSTER] == cluster
        assert cluster.name == TEST_CLUSTER

        try:
            save_environment(path=test_environment_file)
            with clear_environment(user):
                assert show_clusters() == {}
                load_environment(path=test_environment_file)
                cluster2 = show_cluster(name=TEST_CLUSTER)
                assert cluster2 is not cluster
                assert cluster2 == cluster
        finally:
            os.remove(test_environment_file)
예제 #3
0
def test_notebook_app_run():
    user = USER_49
    environment_file = get_test_environment_file(user=user)
    args = [
        TEST_CLUSTER, '--environment', environment_file, '--walltime',
        '0:10:00'
    ]
    with run_notebook_app(user=user,
                          environment_file=environment_file,
                          args=args) as result:
        output = result.output
        assert "Loading environment." in output
        assert "Resetting allocation parameters to defaults." not in output
        assert "Saving defaults." not in output
        assert "Nodes: 1\n" in output
        assert "Cores: 1\n" in output
        assert "Memory per node: 1GiB\n" in output
        assert "Walltime: 0:10:00\n" in output
        assert "No native arguments." in output
        assert "Allocating nodes." in output
        assert "Pushing the allocation deployment." in output
        assert "Pushing the notebook deployment." in output
        assert ("To access the allocation and notebook"
                " deployments from cluster, you can use"
                " the following snippet.") in output
        assert "Notebook address: http://localhost:" in output
        assert "Nodes are still running." in output
        assert "This is a test run, cancelling the allocation." in output
        assert "Nodes are no longer running." in output
        assert result.exit_code == 0
예제 #4
0
def reset_environment(user: str, auth: AuthMethod = AuthMethod.ASK):
    """Clears the environment and adds the testing cluster.

        :param user: User to connect to the cluster as,
                     and whose home dir should be cleaned.

        :param auth: Authentication method to use.

    """
    # pylint: disable=protected-access
    saved_state = EnvironmentProvider._state
    EnvironmentProvider._state = None

    os.environ['IDACT_KEY_LOCATION'] = get_test_key_location(user=user)
    os.environ['IDACT_CONFIG_PATH'] = get_test_environment_file(user=user)

    cluster = ClusterConfigImpl(host=get_testing_host(),
                                port=get_testing_port(),
                                user=user,
                                auth=auth,
                                retries=get_default_retries_heavy_load())
    cluster.retries[Retry.PORT_INFO] = set_retry(count=0)

    EnvironmentProvider(initial_environment=EnvironmentImpl(
        config=ClientConfig(clusters={TEST_CLUSTER: cluster})))
    set_log_level(DEBUG)
    try:
        yield
    finally:
        EnvironmentProvider._state = saved_state
        clear_home(user=user)
예제 #5
0
def clear_environment(user: str):
    """Clears the environment, but does not add any clusters.

        :param user: User, whose home dir should be cleaned.

    """
    # pylint: disable=protected-access
    saved_state = EnvironmentProvider._state
    EnvironmentProvider._state = None
    EnvironmentProvider(initial_environment=EnvironmentImpl())
    set_log_level(logging.DEBUG)
    os.environ['IDACT_CONFIG_PATH'] = get_test_environment_file(user=user)
    try:
        yield
    finally:
        EnvironmentProvider._state = saved_state
        clear_home(user=user)