aboutsummaryrefslogtreecommitdiffstats
path: root/src/onaptests/steps/simulator/cds_mockserver.py
blob: 9fc4162c2ab81816e92a7001bbb147f96792ad4d (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
# http://www.apache.org/licenses/LICENSE-2.0
"""CDS mockserver registration module."""

import time

import requests
from onapsdk.configuration import settings

from onaptests.steps.base import BaseStep
from onaptests.steps.instantiate.msb_k8s import CreateInstanceStep
from onaptests.utils.exceptions import OnapTestException


class CdsMockserverCnfConfigureStep(BaseStep):
    """Configure cds mockserver expectations."""

    def __init__(self, cleanup: bool = False) -> None:
        """Initialize step.

        Substeps:
            - CreateInstanceStep.
        """
        super().__init__(cleanup=cleanup)
        self.add_step(CreateInstanceStep(cleanup=cleanup))

    @property
    def description(self) -> str:
        """Step description."""
        return "Configure cds-mockserver."

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

    @BaseStep.store_state
    def execute(self) -> None:
        """Create mockserver expectations.

        Use settings values:
         - CDS_MOCKSERVER_EXPECTATIONS.
        """
        super().execute()
        time.sleep(60)  # Wait for mockserver
        for expectation in settings.CDS_MOCKSERVER_EXPECTATIONS:
            try:
                response = requests.put(
                    "http://portal.api.simpledemo.onap.org:30726/mockserver/expectation",
                    json={
                        "httpRequest": {
                            "method": expectation["method"],
                            "path": expectation["path"]
                        },
                        "httpResponse": {
                            "body": expectation["response"]
                        }
                    }
                )
                response.raise_for_status()
            except (requests.ConnectionError, requests.HTTPError) as http_error:
                self._logger.debug(f"Can't register cds-mockserver expectation: {str(http_error)}")
                raise OnapTestException("CDS mockserver not configured")