def test_build_command(exec_env_definition_file): content = {'version': 1} path = exec_env_definition_file(content=content) aee = AnsibleBuilder(filename=path, tag='my-custom-image') command = aee.build_command assert 'build' and 'my-custom-image' in command aee = AnsibleBuilder(filename=path, build_context='foo/bar/path', container_runtime='docker') command = aee.build_command assert 'foo/bar/path' in command assert 'foo/bar/path/Dockerfile' in " ".join(command)
def test_definition_syntax_error(self, data_dir): path = os.path.join(data_dir, 'definition_files/bad.yml') with pytest.raises(DefinitionError) as error: AnsibleBuilder(filename=path) assert 'An error occured while parsing the definition file:' in str(error.value.args[0])
def test_file_not_found_error(good_exec_env_definition_path, tmpdir): path = "exec_env.txt" with pytest.raises(DefinitionError) as error: AnsibleBuilder(filename=path) assert "Could not detect 'exec_env.txt' file in this directory.\nUse -f to specify a different location." in str(error.value.args[0])
def test_galaxy_requirements(exec_env_definition_file, galaxy_requirements_file, path_spec): galaxy_requirements_content = { 'collections': [{ 'name': 'geerlingguy.php_roles', 'version': '0.9.3', 'source': 'https://galaxy.ansible.com' }] } galaxy_requirements_path = galaxy_requirements_file( galaxy_requirements_content) exec_env_content = { 'version': 1, 'dependencies': { 'galaxy': str(galaxy_requirements_path) if path_spec == 'absolute' else '../galaxy/requirements.yml' } } exec_env_path = exec_env_definition_file(content=exec_env_content) aee = AnsibleBuilder(filename=exec_env_path) aee.create() with open(aee.containerfile.path) as f: content = f.read() assert 'ADD requirements.yml' in content
def test_galaxy_requirements(exec_env_definition_file, galaxy_requirements_file, path_spec, tmpdir): galaxy_requirements_content = { 'collections': [{ 'name': 'geerlingguy.php_roles', 'version': '0.9.3', 'source': 'https://galaxy.ansible.com' }] } galaxy_requirements_path = galaxy_requirements_file( galaxy_requirements_content) exec_env_content = { 'version': 1, 'dependencies': { 'galaxy': str(galaxy_requirements_path) if path_spec == 'absolute' else '../galaxy/requirements.yml' } } exec_env_path = exec_env_definition_file(content=exec_env_content) aee = AnsibleBuilder(filename=exec_env_path, build_context=tmpdir.mkdir('bc')) aee.build() with open(aee.containerfile.path) as f: content = f.read() assert f'ADD {CONTEXT_BUILD_OUTPUTS_DIR}/requirements.yml' in content
def test_build_command(exec_env_definition_file, tmpdir): content = {'version': 1} path = exec_env_definition_file(content=content) aee = AnsibleBuilder(filename=path, tag='my-custom-image') command = aee.build_command() assert 'build' and 'my-custom-image' in command context_path = str(tmpdir.mkdir('exec_env')) aee = AnsibleBuilder(filename=path, build_context=context_path, container_runtime='docker') command = aee.build_command() assert context_path in command assert 'exec_env/Dockerfile' in " ".join(command)
def test_base_image(exec_env_definition_file): content = {'version': 1} path = exec_env_definition_file(content=content) aee = AnsibleBuilder(filename=path) aee.create() with open(aee.containerfile.path) as f: content = f.read() assert 'ansible-runner' in content aee = AnsibleBuilder(filename=path, base_image='my-custom-image') aee.create() with open(aee.containerfile.path) as f: content = f.read() assert 'my-custom-image' in content
def test_base_image_via_definition_file(exec_env_definition_file, tmpdir): content = {'version': 1, 'base_image': 'my-other-custom-image'} path = exec_env_definition_file(content=content) aee = AnsibleBuilder(filename=path, build_context=tmpdir.mkdir('bc')) aee.build() with open(aee.containerfile.path) as f: content = f.read() assert 'my-other-custom-image' in content
def test_base_image_via_build_args(exec_env_definition_file, tmpdir): content = {'version': 1} path = exec_env_definition_file(content=content) aee = AnsibleBuilder(filename=path, build_context=tmpdir.mkdir('bc')) aee.build() with open(aee.containerfile.path) as f: content = f.read() assert 'ansible-runner' in content aee = AnsibleBuilder( filename=path, build_args={'ANSIBLE_RUNNER_IMAGE': 'my-custom-image'}, build_context=tmpdir.mkdir('bc2') ) aee.build() with open(aee.containerfile.path) as f: content = f.read() assert 'ANSIBLE_RUNNER_IMAGE' in content # TODO: should we make user value default?
def test_nested_galaxy_file(data_dir, tmp_path): if not os.path.exists('test/data/nested-galaxy.yml'): pytest.skip('Test is only valid when ran from ansible-builder root') AnsibleBuilder(filename='test/data/nested-galaxy.yml', build_context=tmp_path).build() req_in_bc = tmp_path.joinpath(constants.user_content_subfolder, 'requirements.yml') assert req_in_bc.exists() req_original = pathlib.Path('test/data/foo/requirements.yml') assert req_in_bc.read_text() == req_original.read_text()
def test_ansible_config_for_galaxy(exec_env_definition_file, tmpdir): if not os.path.exists('test/data/ansible-test.cfg'): pytest.skip('Test is only valid when ran from ansible-builder root') ansible_config_path = 'test/data/ansible-test.cfg' content = {'version': 1, 'ansible_config': ansible_config_path} path = exec_env_definition_file(content=content) aee = AnsibleBuilder(filename=path, build_context=tmpdir.mkdir('bc')) aee.build() with open(aee.containerfile.path) as f: content = f.read() assert f'ADD {CONTEXT_BUILD_OUTPUTS_DIR}/ansible.cfg ~/.ansible.cfg' in content
def test_nested_galaxy_file(data_dir, tmpdir): if not os.path.exists('test/data/nested-galaxy.yml'): pytest.skip('Test is only valid when ran from ansible-builder root') bc_folder = str(tmpdir) AnsibleBuilder(filename='test/data/nested-galaxy.yml', build_context=bc_folder).build() req_in_bc = os.path.join(bc_folder, constants.user_content_subfolder, 'requirements.yml') assert os.path.exists(req_in_bc) req_original = 'test/data/foo/requirements.yml' with open(req_in_bc, 'r') as f_in_bc: with open(req_original, 'r') as f_in_def: assert f_in_bc.read() == f_in_def.read()
def test_use_dockerfile_with_podman(exec_env_definition_file, tmpdir): path = exec_env_definition_file(content={'version': 1}) aee = AnsibleBuilder( filename=path, build_context=tmpdir.mkdir('bc'), container_runtime='podman', output_filename='Dockerfile' ) aee.build() assert aee.containerfile.path.endswith('Dockerfile') with open(aee.containerfile.path) as f: content = f.read() assert 'FROM' in content
def test_ansible_config_for_galaxy(exec_env_definition_file, tmp_path): if not os.path.exists('test/data/ansible-test.cfg'): pytest.skip('Test is only valid when ran from ansible-builder root') ansible_config_path = 'test/data/ansible-test.cfg' content = {'version': 1, 'ansible_config': ansible_config_path} path = exec_env_definition_file(content=content) aee = AnsibleBuilder(filename=path, build_context=tmp_path.joinpath('bc')) aee.build() with open(aee.containerfile.path) as f: content = f.read() assert f'ADD {constants.user_content_subfolder}/ansible.cfg ~/.ansible.cfg' in content
def test_base_image_via_definition_file_build_arg(exec_env_definition_file, tmp_path): content = { 'version': 1, 'build_arg_defaults': { 'EE_BASE_IMAGE': 'my-other-custom-image' } } path = exec_env_definition_file(content=content) aee = AnsibleBuilder(filename=path, build_context=tmp_path.joinpath('bc')) aee.build() with open(aee.containerfile.path) as f: content = f.read() assert 'EE_BASE_IMAGE=my-other-custom-image' in content
def test_definition_version_missing(exec_env_definition_file): path = exec_env_definition_file(content={}) aee = AnsibleBuilder(filename=path) with pytest.raises(ValueError): aee.version
def test_definition_version(exec_env_definition_file): path = exec_env_definition_file(content={'version': 1}) aee = AnsibleBuilder(filename=path) assert aee.version == '1'
def prepare(args): args = parse_args(args) return AnsibleBuilder(**vars(args))