def _stop_container(self, tolerate_err=False):
        """
        Stop running container using available container platform.

        :param tolerate_err: Parameter for set up handling of the exceptions
        :type tolerate_err: bool
        """
        run_cmd(
            f"{self._container_platform} stop {self._base_container_name}_{self._port}",
            tolerate_err=tolerate_err,
        )
 def _get_container_platform(self):
     """Search for available container platform."""
     if self._container_platform is None:
         docker_terminal_result = run_cmd("command -v docker")
         podman_terminal_result = run_cmd("command -v podman")
         if "/podman" in podman_terminal_result:
             self._container_platform = "podman"
         elif "/docker" in docker_terminal_result:
             self._container_platform = "docker"
         else:
             raise exceptions.OIIInspectorError(
                 "Docker or Podman is needed to be installed on the platform"
             )
Example #3
0
def test_run_cmd_fail(mocked_popen):
    mock_process.returncode = 1
    mocked_popen.return_value = mock_process
    with pytest.raises(RuntimeError):
        output = run_cmd("ls")
        assert output == "success"
    mocked_popen.assert_called_once_with(["ls"], stdout=-1, stderr=-1)
    def _serve_index_registry_at_port(self, port, wait_time):
        """
        Start an image registry service at a specified port.

        :param str int port: port to start the service on.
        :param wait_time: time to wait before checking if the service is initialized.
        :return: object of the running Popen process.
        :rtype: Popen
        :raises OIIInspectorError: if the process has failed to initialize too many times, or an unexpected
        error occurred.
        :raises AddressAlreadyInUse: if the specified port is already being used by another service.
        """
        cmd = (
            f"{self._container_platform} run --name={self._base_container_name}_{port} "
            f"-p={port}:{port} {self._image_address}")
        rpc_proc = subprocess.Popen(
            shlex.split(cmd),
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            universal_newlines=True,
        )
        start_time = time.time()
        while time.time() - start_time < wait_time:
            time.sleep(1)
            ret = rpc_proc.poll()
            # process has terminated
            if ret is not None:
                stderr = rpc_proc.stderr.read()
                if "address already in use" in stderr:
                    raise exceptions.AddressAlreadyInUse(
                        f"Port {port} is already used by a different service")
                elif ("the container name" in stderr
                      and "is already in use by" in stderr):
                    raise exceptions.AddressAlreadyInUse(
                        f"Port {port} is already used by service with name: {self._base_container_name}_{port}"
                    )
                else:
                    raise exceptions.OIIInspectorError(
                        f"Command {cmd} has failed with error {stderr}".format(
                            cmd="".join(cmd), stderr=stderr))

            # query the service to see if it has started
            try:
                output = run_cmd(
                    f"grpcurl -plaintext localhost:{port} list api.Registry")
            except RuntimeError:
                output = ""

            if ("api.Registry.ListBundles"
                    in output) or ("api.Registry.ListPackages" in output):
                log.debug("Started the command {cmd}".format(cmd="".join(cmd)))
                log.info("Index registry service has been initialized.")
                return rpc_proc

        rpc_proc.kill()
        raise exceptions.OIIInspectorError(
            "Index registry has not been initialized")
def use_container_manager(image_address, api_address, call_argument=None):
    """
    Handle usage of ContainerManager.

    :param str image_address: Address of the image, to be observed
    :param str api_address: API address to be accessed
    :param str call_argument: Arguments for specification of the query
    """
    with ContainerManager(image_address) as image_manager:
        image_manager.start_container()
        local_image_address = image_manager.local_address_of_image
        if call_argument is None:
            command_to_call = (
                f"grpcurl -plaintext {local_image_address} api.Registry/{api_address}"
            )
        else:
            command_to_call = f"grpcurl -plaintext -d {call_argument} {local_image_address} api.Registry/{api_address}"
        result = run_cmd(command_to_call)
    return result
 def _pull_image(self):
     """Pull the image with available container platform."""
     run_cmd(f"{self._container_platform} pull {self._image_address}")
Example #7
0
def test_run_cmd_pass(mocked_popen):
    mock_process.returncode = 0
    mocked_popen.return_value = mock_process
    output = run_cmd("ls")
    assert output == "success"
    mocked_popen.assert_called_once_with(["ls"], stdout=-1, stderr=-1)