Skip to content

Commit

Permalink
Add stronger projects CLI test verifying that conda environment is us…
Browse files Browse the repository at this point in the history
…ed to run projects (mlflow#300)
  • Loading branch information
smurching authored and aarondav committed Aug 15, 2018
1 parent 55828ab commit b215b41
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
19 changes: 18 additions & 1 deletion tests/projects/test_projects_cli.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
import hashlib
import os

import pytest

from mlflow import cli
from mlflow.utils import process, logging_utils
from tests.integration.utils import invoke_cli_runner
from tests.projects.utils import TEST_PROJECT_DIR, GIT_PROJECT_URI, SSH_PROJECT_URI,\
TEST_NO_SPEC_PROJECT_DIR
from tests.projects.utils import tracking_uri_mock # pylint: disable=unused-import


@pytest.mark.large
def test_run_local(tracking_uri_mock): # pylint: disable=unused-argument
def test_run_local_params(tracking_uri_mock): # pylint: disable=unused-argument
excitement_arg = 2
name = "friend"
invoke_cli_runner(cli.run, [TEST_PROJECT_DIR, "-e", "greeter", "-P",
"greeting=hi", "-P", "name=%s" % name,
"-P", "excitement=%s" % excitement_arg])


@pytest.mark.large
def test_run_local_conda_env(tracking_uri_mock): # pylint: disable=unused-argument
with open(os.path.join(TEST_PROJECT_DIR, "conda.yaml"), "r") as handle:
conda_env_contents = handle.read()
expected_env_name = "mlflow-%s" % hashlib.sha1(conda_env_contents.encode("utf-8")).hexdigest()
try:
process.exec_cmd(cmd=["conda", "env", "remove", "--name", expected_env_name])
except process.ShellCommandException:
logging_utils.eprint(
"Unable to remove conda environment %s. The environment may not have been present, "
"continuing with running the test." % expected_env_name)
invoke_cli_runner(cli.run, [TEST_PROJECT_DIR, "-e", "check_conda_env", "-P",
"conda_env_name=%s" % expected_env_name])


@pytest.mark.large
def test_run_local_no_spec(tracking_uri_mock): # pylint: disable=unused-argument
# Run an example project that doesn't contain an MLproject file
Expand Down
4 changes: 4 additions & 0 deletions tests/resources/example_project/MLproject
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ entry_points:
parameters:
use_start_run: bool
command: "python tracking_test.py {use_start_run}"
check_conda_env:
parameters:
conda_env_name: string
command: "python check_conda_env.py {conda_env_name}"
21 changes: 21 additions & 0 deletions tests/resources/example_project/check_conda_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Import a dependency in MLflow's setup.py that's in our conda.yaml but not included with MLflow
# by default, verify that we can use it.

import os
import sys

import psutil

import mlflow


def main(expected_env_name):
actual_conda_env = os.environ.get("CONDA_DEFAULT_ENV", None)
assert actual_conda_env == expected_env_name,\
"Script expected to be run from conda env %s but was actually run from env" \
" %s" % (expected_env_name, actual_conda_env)
mlflow.log_metric("CPU usage", psutil.cpu_percent())


if __name__ == "__main__":
main(sys.argv[1])
3 changes: 3 additions & 0 deletions tests/resources/example_project/conda.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Adding a comment here to distinguish the hash of this conda.yaml from the hashes of existing
# conda.yaml files in the test environment.
name: tutorial
channels:
- anaconda
Expand All @@ -6,3 +8,4 @@ dependencies:
- python=3.6
- pip:
- mlflow
- psutil

0 comments on commit b215b41

Please sign in to comment.