Ejemplo n.º 1
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()
Ejemplo n.º 2
0
def test_template_list():
    args = {
        'template': 'test',
        'list': True,
        '--template-repo': project.basedir()
    }
    templates = TemplatesCommand(args)
    with catch_stdout() as caught_output:
        templates.action()
        assert caught_output.getvalue() is not None
Ejemplo n.º 3
0
def test_templates():
    output = call_template_list(project.basedir())
    desired_template_output = """Template             Description
-------------------  --------------------------------------------------------------------------------------------------
hello-world          A TensorFlow python HelloWorld example run through Kubernetes Jobs.
pytorch              Sample distributed application taken from http://pytorch.org/tutorials/intermediate/dist_tuto.html
pytorch-distributed  A distributed PyTorch MNIST example run using the pytorch-operator.
tf-dist-mnist        A distributed TensorFlow MNIST model which designates worker 0 as the chief.
tf-distributed       A distributed TensorFlow matrix multiplication run through the TensorFlow Kubernetes Operator.
"""
    assert output == desired_template_output
Ejemplo n.º 4
0
def test_templates():
    output = check_output(['mlt', 'templates', 'list',
                           '--template-repo={}'.format(project.basedir())]
                          ).decode("utf-8")
    desired_template_output = """Template        Description
--------------  --------------------------------------------------------------------------------------------------
hello-world     A TensorFlow python HelloWorld example run through Kubernetes Jobs.
pytorch         Sample distributed application taken from http://pytorch.org/tutorials/intermediate/dist_tuto.html
tf-dist-mnist   A distributed TensorFlow MNIST model which designates worker 0 as the chief.
tf-distributed  A distributed TensorFlow matrix multiplication run through the TensorFlow Kubernetes Operator.
"""
    assert output == desired_template_output
Ejemplo n.º 5
0
    def test_local_templates(self):
        """
        Tests creating a new template in a clone of mlt.  Verifies that we can
        specify the template-repo for mlt template list and see new template in
        the list.  Then uses mlt init to create an app with new template and
        verifies that the app directory exists.
        """
        # Create a git clone of mlt to use as a local template diretory
        with git_helpers.clone_repo(project.basedir()) as temp_clone:
            # Add a new test template to the local mlt template directory
            templates_directory = os.path.join(
                temp_clone, constants.TEMPLATES_DIR)
            new_template_name = "test-local"
            new_template_directory = os.path.join(templates_directory,
                                                  new_template_name)
            os.mkdir(new_template_directory)
            new_template_file = os.path.join(
                new_template_directory, "README.md")
            with open(new_template_file, "w") as f:
                f.write("New local template for testing")

            # call mlt template list and then check the output
            output = self._call_template_list(temp_clone)

            # template list should include our new test-local template
            desired_template_output = """Template             Description
-------------------  ---------------------------------------------------------\
-----------------------------------------
experiments          Runs hyperparameter experiments for a demo job.
hello-world          A TensorFlow python HelloWorld example run through \
Kubernetes Jobs.
horovod              A distributed model training using horovod and openmpi \
with added support for S3 storage.
pytorch              Sample distributed application taken from \
http://pytorch.org/tutorials/intermediate/dist_tuto.html
pytorch-distributed  A distributed PyTorch MNIST example run using the \
pytorch-operator.
tensorboard-bm       A TensorBoard service in Kubernetes Bare Metal cluster.
tensorboard-gke      A TensorBoard service in Google Kubernetes cluster.
test-local           New local template for testing
tf-dist-mnist        A distributed TensorFlow MNIST model which designates \
worker 0 as the chief.
tf-distributed       A distributed TensorFlow matrix multiplication run \
through the TensorFlow Kubernetes Operator.
"""
            assert output == desired_template_output

            # init an app with this new template and verify that the app exists
            self.init(template=new_template_name, template_repo=temp_clone)
            assert os.path.isdir(self.project_dir)
            assert os.path.isfile(os.path.join(
                self.project_dir, "README.md"))
Ejemplo n.º 6
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
Ejemplo n.º 7
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
Ejemplo n.º 8
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
Ejemplo n.º 9
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,
        '--enable-sync': False,
        '<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
Ejemplo n.º 10
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)
Ejemplo n.º 11
0
def test_local_templates():
    """ Tests creating a new template in a clone of mlt.  Verifies that we can
    specify the template-repo for mlt template list and see the new template in
    the list.  Then uses mlt init to create an app with our new template and
    verifies that the app directory exists. """
    # Create a git clone of mlt to use as a local template diretory
    with git_helpers.clone_repo(project.basedir()) as temp_clone:
        # Add a new test template to the local mlt template directory
        templates_directory = os.path.join(temp_clone, constants.TEMPLATES_DIR)
        new_template_name = "test-local"
        new_template_directory = os.path.join(templates_directory,
                                              new_template_name)
        os.mkdir(new_template_directory)
        new_template_file = os.path.join(new_template_directory, "README.md")
        with open(new_template_file, "w") as f:
            f.write("New local template for testing")

        # call mlt template list and then check the output
        output = call_template_list(temp_clone)

        # template list should include our new test-local template
        desired_template_output = """Template             Description
-------------------  --------------------------------------------------------------------------------------------------
hello-world          A TensorFlow python HelloWorld example run through Kubernetes Jobs.
pytorch              Sample distributed application taken from http://pytorch.org/tutorials/intermediate/dist_tuto.html
pytorch-distributed  A distributed PyTorch MNIST example run using the pytorch-operator.
test-local           New local template for testing
tf-dist-mnist        A distributed TensorFlow MNIST model which designates worker 0 as the chief.
tf-distributed       A distributed TensorFlow matrix multiplication run through the TensorFlow Kubernetes Operator.
"""
        assert output == desired_template_output

        # init an app with this new template and verify that the app exists
        with create_work_dir() as workdir:
            commands = CommandTester(workdir)
            commands.init(template=new_template_name, template_repo=temp_clone)
            app_directory = os.path.join(workdir, commands.app_name)
            assert os.path.isdir(app_directory)
            assert os.path.isfile(os.path.join(app_directory, "README.md"))
Ejemplo n.º 12
0
    def test_templates(self):
        output = self._call_template_list(project.basedir())
        desired_template_output = """Template             Description
-------------------  ---------------------------------------------------------\
-----------------------------------------
experiments          Runs hyperparameter experiments for a demo job.
hello-world          A TensorFlow python HelloWorld example run through \
Kubernetes Jobs.
horovod              A distributed model training using horovod and openmpi \
with added support for S3 storage.
pytorch              Sample distributed application taken from \
http://pytorch.org/tutorials/intermediate/dist_tuto.html
pytorch-distributed  A distributed PyTorch MNIST example run using the \
pytorch-operator.
tensorboard-bm       A TensorBoard service in Kubernetes Bare Metal cluster.
tensorboard-gke      A TensorBoard service in Google Kubernetes cluster.
tf-dist-mnist        A distributed TensorFlow MNIST model which designates \
worker 0 as the chief.
tf-distributed       A distributed TensorFlow matrix multiplication run \
through the TensorFlow Kubernetes Operator.
"""

        assert output == desired_template_output
Ejemplo n.º 13
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)
Ejemplo n.º 14
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"]]
Ejemplo n.º 15
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
Ejemplo n.º 16
0
def os_path_exists_mock(patch):
    return patch('os.path.exists')


@pytest.fixture
def os_path_isfile_mock(patch):
    return patch('os.path.isfile')


@pytest.fixture
def sorted_mock(patch):
    return patch('sorted')


@pytest.mark.parametrize("valid_template_dir", [
    project.basedir(),
    "[email protected]:IntelAI/mlt.git",
    "https://github.com/IntelAI/mlt",
])
def test_template_list(open_mock, valid_template_dir, copy_tree_mock,
                       listdir_mock, os_path_exists_mock, os_path_isfile_mock,
                       sorted_mock):
    args = {
        'template': 'test',
        'list': True,
        '--template-repo': valid_template_dir
    }
    os_path_exists_mock.return_value = True
    os_path_isfile_mock.return_value = True
    sorted_mock.return_value = ['hello-world', 'tf-dist-mnist']