aboutsummaryrefslogtreecommitdiffstats
path: root/src/onaptests/steps/wrapper/start.py
blob: 18ba5df0fb3edcd2879a27307d704759032ce733 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""Start simulators via simulators' API."""
from typing import Union, Optional, Dict
import requests
from onaptests.steps.base import BaseStep
from onaptests.utils.exceptions import TestConfigurationException

class SimulatorStartStep(BaseStep):
    """Basic operations on a docker container."""

    def __init__(self,  # pylint: disable=R0913
                 cleanup: bool = False,
                 https: bool = False,
                 host: str = None,
                 port: Union[int, str] = None,
                 endpoint: Optional[str] = "",
                 method: str = "GET",
                 data: Dict = None) -> None:
        """Prepare request data and details.

        Arguments:
            cleanup (bool):
                determines if cleanup action should be called.
                Defaults to False.
            https (bool): use https or http. Defaults to False.
            host (str): IP or hostname. Defaults to None.
            port (Union[int, str]): port number. Defaults to None.
            endpoint (str):
                additional endpoint if applicable.
                Defautls to "".
            method (str):
                GET or POST strings, case insensitive.
                Defaults tp GET.
            data (Dict):
                parameters, that request's post() or get() takes, besides url.
                For example, {"json": {}, ...}. Defaults to None.
        """
        if not host and not port:
            raise TestConfigurationException("Provide host and/or port.")

        super().__init__(cleanup=cleanup)

        default_port = "443" if https else "80"
        protocol = "https" if https else "http"
        endpoint = endpoint[1:] if endpoint.startswith("/") else endpoint

        self.method = method
        self.data = data if data else {}
        self.url = f"{protocol}://{host}:{port or default_port}/{endpoint}"

    @property
    def description(self) -> str:
        """Step description."""
        return "Send commands to the simulator application."

    @property
    def component(self) -> str:
        """Component name."""
        return "Environment"

    @BaseStep.store_state
    def execute(self) -> None:
        """Send a start command to the simulator application."""
        super().execute()
        requests.request(self.method.upper(), self.url, **self.data)