Beispiel #1
0
def test_init_sync_not_supported(open_mock, process_helpers, copytree_mock,
                                 check_output_mock, config_helpers_mock,
                                 copyfile_mock, listdir_mock, binary_path_mock,
                                 update_yaml_for_sync_mock,
                                 init_git_repo_mock):
    check_output_mock.return_value.decode.return_value = 'bar'
    new_dir = str(uuid.uuid4())

    init_dict = {
        'init': True,
        '--template': 'hello-world',
        '--template-repo': project.basedir(),
        '--registry': None,
        '--namespace': None,
        '--skip-crd-check': True,
        '--enable-sync': True,
        '<name>': new_dir
    }
    update_yaml_for_sync_mock.rerun_value = False
    init_git_repo_mock.return_value = False
    config_helpers_mock.get_template_parameters_from_file.return_value = \
        [{"name": "greeting", "value": "hello"}]
    init = InitCommand(init_dict)
    init.action()
    assert init.app_name == new_dir
Beispiel #2
0
def test_init(open_mock, proc_helpers, shutil_mock):
    new_dir = str(uuid.uuid4())
    init_dict = {
        'init': True,
        '--template': 'hello-world',
        '--template-repo': project.basedir(),
        '--registry': True,
        '--namespace': None,
        '<name>': new_dir
    }
    init = InitCommand(init_dict)
    init.action()
    assert init.app_name == new_dir
Beispiel #3
0
def test_no_template_params():
    new_dir = str(uuid.uuid4())
    init_dict = {
        'init': True,
        '--template': 'tf-dist-mnist',
        '--template-repo': project.basedir(),
        '--registry': True,
        '--namespace': None,
        '<name>': new_dir
    }
    init = InitCommand(init_dict)
    template_params = None
    result = init._build_mlt_json(template_params, None)
    assert constants.TEMPLATE_PARAMETERS not in result
Beispiel #4
0
def test_init_ksync_missing(open_mock, process_helpers, copytree_mock,
                            check_output_mock, config_helpers_mock,
                            copyfile_mock, listdir_mock, binary_path_mock):
    check_output_mock.return_value.decode.return_value = 'bar'
    new_dir = str(uuid.uuid4())

    init_dict = {
        'init': True,
        '--template': 'hello-world',
        '--template-repo': project.basedir(),
        '--registry': None,
        '--namespace': None,
        '--skip-crd-check': True,
        '--enable-sync': True,
        '<name>': new_dir
    }
    binary_path_mock.return_value = False
    config_helpers_mock.get_template_parameters_from_file.return_value = \
        [{"name": "greeting", "value": "hello"}]
    with catch_stdout() as caught_output:
        with pytest.raises(SystemExit) as bad_init:
            InitCommand(init_dict).action()
            assert \
                caught_output.getvalue() == "ksync is not installed on " \
                                            "localhost"
            assert bad_init.value.code == 1
Beispiel #5
0
def test_init_dir_exists(open_mock, process_helpers, copytree_mock,
                         check_output_mock, config_helpers_mock, colored_mock,
                         traceback_mock, errno_op):
    new_dir = str(uuid.uuid4())
    init_dict = {
        'init': True,
        '--template': 'hello-world',
        '<name>': new_dir,
        '--skip-crd-check': True,
        '--template-repo': project.basedir()
    }
    copytree_mock.side_effect = OSError(errno_op, 'error')
    with catch_stdout() as caught_output:
        with pytest.raises(SystemExit) as bad_init:
            InitCommand(init_dict).action()

        assert bad_init.value.code == 1
        if errno_op == errno.EEXIST:
            assert \
                caught_output.getvalue().strip() == \
                "Directory '{}' already exists: delete ".format(
                    new_dir) + "before trying to initialize new " \
                               "application"
        else:
            traceback_mock.print_exc.assert_called_once()
Beispiel #6
0
def test_debug_on_fail_init(get_template_params_mock, copy_mock, copytree_mock,
                            open_mock, checking_crds_mock, check_output_mock,
                            process_helpers, os_path_isdir_mock, listdir_mock,
                            yaml_load_mock):
    """ Tests that when debug_on_fail is a template parameter, 'copy' is
    called, since we copy the kubernetes debug wrapper to the app. """
    get_template_params_mock.return_value = [{
        'name': 'debug_on_fail',
        'value': 'false'
    }]
    os_path_isdir_mock.return_value = True
    listdir_mock.return_value = ["job.yaml"]
    yaml_load_mock.return_value = {
        "containers": [{
            "name": "container",
            "env": [{
                "name": "DEBUG_ON_FAIL",
                "value": True
            }]
        }, {
            "name": "container2",
            "env": [{
                "name": "random",
                "value": False
            }]
        }]
    }

    new_dir = str(uuid.uuid4())
    init_dict = {
        'init': True,
        '--template': 'tf-dist-mnist',
        '--template-repo': project.basedir(),
        '--registry': True,
        '--namespace': None,
        '<name>': new_dir,
        '--skip-crd-check': False,
        '--enable-sync': False
    }
    init = InitCommand(init_dict)
    init.action()
    assert copy_mock.called
Beispiel #7
0
def test_no_gcloud_or_registry(open_mock, process_helpers, copytree_mock,
                               check_output_mock, config_helpers_mock, error):
    """No such file or directory" OSError to simulate gcloud not found
       With and without the error `No such file or directory`
       If there was a file or dir, then we `raise` the error triggered
    """
    check_output_mock.side_effect = OSError(error)
    new_dir = str(uuid.uuid4())
    init_dict = {
        'init': True,
        '--template': 'tf-dist-mnist',
        '--template-repo': project.basedir(),
        '--namespace': 'test-namespace',
        '<name>': new_dir,
        '--registry': False,
        '--skip-crd-check': False,
        '--enable-sync': False
    }
    init = InitCommand(init_dict)

    with catch_stdout() as output:
        if "No such file or directory" in error:
            init.action()
            output = output.getvalue()
            assert "No registry name was provided and gcloud was not "\
                   "found.  Please set your container registry name" in output
        else:
            # raising OSError triggers a sys.exit(1) call in action()
            with pytest.raises(SystemExit):
                init.action()

    assert init.app_name == new_dir
Beispiel #8
0
def test_init(open_mock, process_helpers, copytree_mock, check_output_mock,
              config_helpers_mock):
    check_output_mock.return_value.decode.return_value = 'bar'
    new_dir = str(uuid.uuid4())

    init_dict = {
        'init': True,
        '--template': 'hello-world',
        '--template-repo': project.basedir(),
        '--registry': None,
        '--namespace': None,
        '--skip-crd-check': True,
        '<name>': new_dir
    }
    config_helpers_mock.get_template_parameters_from_file.return_value = [{
        "name":
        "greeting",
        "value":
        "hello"
    }]
    init = InitCommand(init_dict)
    init.action()
    assert init.app_name == new_dir
Beispiel #9
0
def test_init_crd_check(checking_crds_mock, process_helpers,
                        check_output_mock):
    new_dir = str(uuid.uuid4())
    init_dict = {
        'init': True,
        '--template': 'tf-distributed',
        '--template-repo': project.basedir(),
        '--registry': True,
        '--namespace': None,
        '--skip-crd-check': False,
        '<name>': new_dir
    }
    checking_crds_mock.return_value = {'tfjobs.kubeflow.org'}
    init = InitCommand(init_dict)
    try:
        with catch_stdout() as caught_output:
            init.action()
            output = caught_output.getvalue()

        message_code = output.find("tfjobs.kubeflow.org")
        assert message_code >= 0
    finally:
        shutil.rmtree(new_dir)
Beispiel #10
0
def test_template_params():
    new_dir = str(uuid.uuid4())
    init_dict = {
        'init': True,
        '--template': 'tf-dist-mnist',
        '--template-repo': project.basedir(),
        '--registry': True,
        '--namespace': None,
        '<name>': new_dir
    }
    init = InitCommand(init_dict)
    template_params = [{
        'name': 'num_ps',
        'value': 1
    }, {
        'name': 'num_workers',
        'value': 2
    }]
    result = init._build_mlt_json(template_params, None)
    assert constants.TEMPLATE_PARAMETERS in result
    result_params = result[constants.TEMPLATE_PARAMETERS]
    for param in template_params:
        assert param["name"] in result_params
        assert param["value"] == result_params[param["name"]]
Beispiel #11
0
def test_init_crd_check(open_mock, checking_crds_mock, process_helpers,
                        check_output_mock, copytree_mock, os_path_exists_mock):
    new_dir = str(uuid.uuid4())
    init_dict = {
        'init': True,
        '--template': 'tf-distributed',
        '--template-repo': project.basedir(),
        '--registry': True,
        '--namespace': None,
        '--skip-crd-check': False,
        '--enable-sync': False,
        '<name>': new_dir
    }
    checking_crds_mock.return_value = {'tfjobs.kubeflow.org'}
    # set crd_file as true since we don't actually create a dir
    os_path_exists_mock.return_value = True

    init = InitCommand(init_dict)
    with catch_stdout() as caught_output:
        init.action()
        output = caught_output.getvalue()

    message_code = output.find("tfjobs.kubeflow.org")
    assert message_code >= 0
Beispiel #12
0
def test_init_dir_exists():
    new_dir = str(uuid.uuid4())
    os.mkdir(new_dir)
    init_dict = {
        'init': True,
        '--template': 'hello-world',
        '<name>': new_dir,
        '--template-repo': project.basedir()
    }
    try:
        with catch_stdout() as caught_output:
            with pytest.raises(SystemExit) as bad_init:
                InitCommand(init_dict).action()
                assert caught_output.getvalue() == \
                    "Directory '{}' already exists: delete ".format(
                        new_dir) + "before trying to initialize new " + \
                    "application"
                assert bad_init.value.code == 1
    finally:
        os.rmdir(new_dir)