def test_build_container(self, authfile, is_flatpak, x86_build_dir): options = [ f"--tag={X86_UNIQUE_IMAGE}", "--no-cache", "--pull-always", ] if is_flatpak: options.append("--squash-all") else: options.append("--squash") options.append("--build-arg=REMOTE_SOURCES=unpacked_remote_sources") if authfile: options.append(f"--authfile={authfile}") expect_cmd = [ "/usr/bin/podman", "--remote", "--connection=connection-name", "build", *options, str(x86_build_dir.path), ] mock_popen(0, ["starting the build\n", "finished successfully\n"], expect_cmd=expect_cmd) podman_remote = PodmanRemote("connection-name", registries_authfile=authfile) output_lines = podman_remote.build_container( build_dir=x86_build_dir, build_args=BUILD_ARGS, dest_tag=X86_UNIQUE_IMAGE, squash_all=is_flatpak ) assert list(output_lines) == ["starting the build\n", "finished successfully\n"]
def test_setup_for_fails(self): (flexmock(subprocess).should_receive("check_output").and_raise( subprocess.CalledProcessError(1, ["podman", "..."], output=b'something went wrong'))) err_msg = "Failed to set up podman-remote connection: something went wrong" with pytest.raises(BuildTaskError, match=err_msg): PodmanRemote.setup_for(X86_LOCKED_RESOURCE)
def test_push_container_fails(self): (flexmock(retries).should_receive("run_cmd").and_raise( subprocess.CalledProcessError(1, 'some command'))) podman_remote = PodmanRemote("connection-name") err_msg = r"Push failed \(rc=1\). Check the logs for more details." with pytest.raises(PushError, match=err_msg): podman_remote.push_container(X86_UNIQUE_IMAGE)
def mock_podman_remote(self, mock_locked_resource, mock_dockercfg_path) -> PodmanRemote: podman_remote = PodmanRemote( connection_name=mock_locked_resource.host.hostname) (flexmock(PodmanRemote).should_receive("setup_for").with_args( mock_locked_resource, registries_authfile=mock_dockercfg_path).and_return(podman_remote)) return podman_remote
def test_push_container(self, authfile, insecure): expect_cmd = [ "/usr/bin/podman", "--remote", "--connection=connection-name", "push", "--format=v2s2", str(X86_UNIQUE_IMAGE), ] if authfile: expect_cmd.insert(-1, f"--authfile={authfile}") if insecure: expect_cmd.insert(-1, "--tls-verify=false") flexmock(retries).should_receive("run_cmd").with_args(expect_cmd).once() podman_remote = PodmanRemote("connection-name", registries_authfile=authfile) podman_remote.push_container(X86_UNIQUE_IMAGE, insecure=insecure)
def test_build_container_fails(self, output_lines, expect_err_line, x86_build_dir): mock_popen(1, output_lines) podman_remote = PodmanRemote("connection-name") returned_lines = podman_remote.build_container( build_dir=x86_build_dir, build_args=BUILD_ARGS, dest_tag=X86_UNIQUE_IMAGE, squash_all=False, ) for expect_line in output_lines: assert next(returned_lines) == expect_line err_msg = rf"Build failed \(rc=1\). {re.escape(expect_err_line)}" with pytest.raises(BuildProcessError, match=err_msg): next(returned_lines)
def test_setup_for(self): resource = X86_LOCKED_RESOURCE expect_cmd = [ "/usr/bin/podman", "system", "connection", "add", "--identity=/workspace/ws-remote-host-auth/remote-host-auth", "--socket-path=/run/user/2022/podman/podman.sock", PIPELINE_RUN_NAME, "ssh://[email protected]", ] (flexmock(subprocess).should_receive("check_output").with_args( expect_cmd, stderr=subprocess.STDOUT).once()) podman_remote = PodmanRemote.setup_for(resource) assert podman_remote._connection_name == PIPELINE_RUN_NAME
def test_get_image_size(self, x86_task_params, error, error_msg): expect_cmd = [ "/usr/bin/podman", "--remote", "--connection=connection-name", "image", "inspect", str(X86_UNIQUE_IMAGE), ] if error == IndexError: inspect_json = json.dumps([]) elif error == JSONDecodeError: inspect_json = 'invalid json' else: inspect_json = json.dumps( [ { "Id": "750037c05cfe1857e16500167c7c217658e15eb9bc6283020cfb3524c93d1240", "Digest": "sha256:1fcb4b8b5d3fcdba78119734db03328fb3b8463bbcc83e1dda3e4" "ffb0ffe3b34", "Size": 158641422, } ] ) if error != InspectError: ( flexmock(retries) .should_receive("run_cmd") .with_args(expect_cmd).once() .and_return(inspect_json) ) else: ( flexmock(retries) .should_receive("run_cmd") .and_raise(subprocess.CalledProcessError(1, "some command")) ) podman_remote = PodmanRemote("connection-name") if error: # all errors are re-raised as InspectError with pytest.raises(InspectError, match=error_msg): podman_remote.get_image_size(X86_UNIQUE_IMAGE) else: podman_remote.get_image_size(X86_UNIQUE_IMAGE)