def test_get_open_ce_version(): ''' Simple test to read open-ce version from the conda environment file ''' expected_version = "open-ce-v1.0.0" test_conda_env_file = os.path.join(test_dir, "test-conda-env2.yaml") assert utils.get_open_ce_version(test_conda_env_file) == expected_version test_conda_env_file = os.path.join(test_dir, "test-conda-env.yaml") assert utils.get_open_ce_version(test_conda_env_file) == "open-ce"
def test_channel_update_in_conda_env(mocker): ''' Test to see if channel is being updated in the conda env file before passing to build_runtime_image ''' open_ce_version = utils.get_open_ce_version(TEST_CONDA_ENV_FILE) intended_image_name = build_image.REPO_NAME + ":" + open_ce_version + TEST_IMAGE_NAME_SUFFIX container_tool = utils.DEFAULT_CONTAINER_TOOL mocker.patch( 'os.system', return_value=0, side_effect=(lambda x: helpers.validate_cli( x, expect= ["{} build".format(container_tool), "-t " + intended_image_name]))) mocker.patch('os.remove', return_value=0) mocker.patch('shutil.rmtree', return_value=1) channel_index_before, _ = get_channel_being_modified(TEST_CONDA_ENV_FILE) arg_strings = [ "build", build_image.COMMAND, "--local_conda_channel", TEST_LOCAL_CONDA_CHANNEL_DIR, "--conda_env_file", TEST_CONDA_ENV_FILE ] opence._main(arg_strings) # We copy conda environment file to the passed local conda channel before updating it channel_index_after, channel_modified = get_channel_being_modified( os.path.join(TEST_LOCAL_CONDA_CHANNEL_DIR, build_image.TEMP_FILES, "test-conda-env-runtime.yaml")) assert channel_modified == "file:/{}".format(build_image.TARGET_DIR) assert channel_index_before == channel_index_after
def test_local_conda_channel_with_absolute_path(mocker): ''' Test for build_runtime_image with local conda channel with its absolute path ''' open_ce_version = utils.get_open_ce_version(TEST_CONDA_ENV_FILE) intended_image_name = build_image.REPO_NAME + ":" + open_ce_version + TEST_IMAGE_NAME_SUFFIX container_tool = utils.DEFAULT_CONTAINER_TOOL mocker.patch( 'os.system', return_value=0, side_effect=(lambda x: helpers.validate_cli( x, expect= ["{} build".format(container_tool), "-t " + intended_image_name]))) arg_strings = [ "build", build_image.COMMAND, "--local_conda_channel", TEST_LOCAL_CONDA_CHANNEL_DIR, "--conda_env_file", TEST_CONDA_ENV_FILE ] opence._main(arg_strings)
def build_runtime_container_image(args): """ Create a runtime image which will have a conda environment created using locally built conda packages and environment file. """ if not args.container_tool: raise OpenCEError(Error.NO_CONTAINER_TOOL_FOUND) local_conda_channel = os.path.abspath(args.local_conda_channel) if not os.path.exists(local_conda_channel): raise OpenCEError(Error.INCORRECT_INPUT_PATHS) if not os.path.exists(os.path.join(local_conda_channel, TEMP_FILES)): os.mkdir(os.path.join(local_conda_channel, TEMP_FILES)) for conda_env_file in parse_arg_list(args.conda_env_files): conda_env_file = os.path.abspath(conda_env_file) if not os.path.exists(conda_env_file): raise OpenCEError(Error.INCORRECT_INPUT_PATHS) # Copy the conda environment file into the TEMP_FILES dir inside local # conda channel with a new name and modify it conda_env_runtime_filename = os.path.splitext( os.path.basename(conda_env_file))[0] + '-runtime.yaml' conda_env_runtime_file = os.path.join(local_conda_channel, TEMP_FILES, conda_env_runtime_filename) create_copy(conda_env_file, conda_env_runtime_file) utils.replace_conda_env_channels(conda_env_runtime_file, r'file:.*', "file:/{}".format(TARGET_DIR)) image_version = utils.get_open_ce_version(conda_env_file) image_name = build_image(args.local_conda_channel, os.path.basename(conda_env_runtime_file), args.container_tool, image_version, args.container_build_args) print("Docker image with name {} is built successfully.".format( image_name)) cleanup(local_conda_channel)
def test_modified_file_removed(mocker): ''' Make sure the copied conda env file was deleted afterwards ''' open_ce_version = utils.get_open_ce_version(TEST_CONDA_ENV_FILE) intended_image_name = build_image.REPO_NAME + ":" + open_ce_version + TEST_IMAGE_NAME_SUFFIX container_tool = utils.DEFAULT_CONTAINER_TOOL mocker.patch( 'os.system', return_value=0, side_effect=(lambda x: helpers.validate_cli( x, expect= ["{} build".format(container_tool), "-t " + intended_image_name]))) arg_strings = [ "build", build_image.COMMAND, "--local_conda_channel", TEST_LOCAL_CONDA_CHANNEL_DIR, "--conda_env_file", TEST_CONDA_ENV_FILE ] opence._main(arg_strings) assert not os.path.exists( os.path.join(TEST_LOCAL_CONDA_CHANNEL_DIR, build_image.TEMP_FILES, "test-conda-env-runtime.yaml"))