aboutsummaryrefslogtreecommitdiffstats
path: root/ice_validator/app_tests/preload_tests/test_vnfapi.py
blob: 312c418a2503ed30629cb40e75e01138460f851f (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# -*- coding: utf8 -*-
# ============LICENSE_START====================================================
# org.onap.vvp/validation-scripts
# ===================================================================
# Copyright © 2019 AT&T Intellectual Property. All rights reserved.
# ===================================================================
#
# Unless otherwise specified, all software contained herein is licensed
# under the Apache License, Version 2.0 (the "License");
# you may not use this software except in compliance with the License.
# You may obtain a copy of the License at
#
#             http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
#
# Unless otherwise specified, all documentation contained herein is licensed
# under the Creative Commons License, Attribution 4.0 Intl. (the "License");
# you may not use this documentation except in compliance with the License.
# You may obtain a copy of the License at
#
#             https://creativecommons.org/licenses/by/4.0/
#
# Unless required by applicable law or agreed to in writing, documentation
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ============LICENSE_END============================================
import tempfile
from pathlib import Path
from shutil import rmtree

import pytest

from app_tests.preload_tests.test_grapi import load_json
from preload.environment import EnvironmentFileDataSource
from preload.model import Vnf, get_heat_templates
from preload_vnfapi import VnfApiPreloadGenerator
from tests.helpers import load_yaml, first

THIS_DIR = Path(__file__).parent
SAMPLE_HEAT_DIR = THIS_DIR / "sample_heat"


def load_module(base_dir, name):
    path = Path(str(base_dir / "vnfapi" / name))
    assert path.exists(), "{} does not exist".format(path)
    return load_yaml(str(path))


@pytest.fixture(scope="session")
def session_dir(request):
    # Temporary directory that gets deleted at the session
    # pytest tmpdir doesn't support a non-function scoped temporary directory
    session_dir = Path(tempfile.mkdtemp())
    request.addfinalizer(lambda: rmtree(session_dir))
    return session_dir


@pytest.fixture(scope="session")
def preload(pytestconfig, session_dir):
    # Generate the preloads for testing
    def fake_getoption(opt, default=None):
        return [SAMPLE_HEAT_DIR.as_posix()] if opt == "template_dir" else None

    pytestconfig.getoption = fake_getoption
    templates = get_heat_templates(pytestconfig)
    vnf = Vnf(templates)
    datasource = EnvironmentFileDataSource(THIS_DIR / "sample_env")
    generator = VnfApiPreloadGenerator(vnf, session_dir, datasource)
    generator.generate()
    return session_dir


@pytest.fixture(scope="session")
def base(preload):
    return load_module(preload, "base.json")


@pytest.fixture(scope="session")
def incremental(preload):
    return load_module(preload, "incremental.json")


def test_base_azs(base):
    az = base["input"]["vnf-topology-information"]["vnf-assignments"][
        "availability-zones"
    ]
    assert az == [
        {"availability-zone": "VALUE FOR: availability_zone_0"},
        {"availability-zone": "VALUE FOR: availability_zone_1"},
    ]


def test_base_networks(base):
    nets = base["input"]["vnf-topology-information"]["vnf-assignments"]["vnf-networks"]
    assert nets == [
        {
            "network-role": "oam",
            "network-name": "VALUE FOR: network name for oam_net_id",
            "subnet-name": "VALUE FOR: name for oam_subnet_id",
        },
        {"network-role": "ha", "network-name": "VALUE FOR: network name for ha_net_id"},
        {
            "network-role": "ctrl",
            "network-name": "VALUE FOR: network name for ctrl_net_id",
            "subnet-name": "VALUE FOR: name for ctrl_subnet_id",
        },
    ]


def test_base_vm_types(base):
    vms = base["input"]["vnf-topology-information"]["vnf-assignments"]["vnf-vms"]
    vm_types = {vm["vm-type"] for vm in vms}
    assert vm_types == {"db", "svc", "mgmt", "lb"}
    db = first(vms, lambda v: v["vm-type"] == "db")
    assert db == {
        "vm-type": "db",
        "vm-count": 2,
        "vm-names": {"vm-name": ["VALUE FOR: db_name_0", "VALUE FOR: db_name_1"]},
        "vm-networks": [
            {
                "network-role": "oam",
                "network-role-tag": "oam",
                "ip-count": 2,
                "ip-count-ipv6": 0,
                "floating-ip": "",
                "floating-ip-v6": "",
                "network-ips": [
                    {"ip-address": "VALUE FOR: db_oam_ip_0"},
                    {"ip-address": "VALUE FOR: db_oam_ip_1"},
                ],
                "network-ips-v6": [],
                "network-macs": [],
                "interface-route-prefixes": [],
                "use-dhcp": "N",
            },
            {
                "network-role": "ha",
                "network-role-tag": "ha",
                "ip-count": 0,
                "ip-count-ipv6": 0,
                "floating-ip": "VALUE FOR: db_ha_floating_ip",
                "floating-ip-v6": "VALUE FOR: db_ha_floating_v6_ip",
                "network-ips": [],
                "network-ips-v6": [],
                "network-macs": [],
                "interface-route-prefixes": [],
                "use-dhcp": "Y",
            },
        ],
    }


def test_base_parameters(base):
    params = base["input"]["vnf-topology-information"]["vnf-parameters"]
    assert params == []


def test_incremental(incremental):
    az = incremental["input"]["vnf-topology-information"]["vnf-assignments"][
        "availability-zones"
    ]
    assert isinstance(az, list)
    assert len(az) == 1
    assert az[0] == {"availability-zone": "VALUE FOR: availability_zone_0"}


def test_incremental_networks(incremental):
    nets = incremental["input"]["vnf-topology-information"]["vnf-assignments"][
        "vnf-networks"
    ]
    assert isinstance(nets, list)
    assert len(nets) == 1
    assert nets[0]["network-role"] == "ha"


def test_preload_env_population(preload):
    base_path = THIS_DIR / "sample_env/preloads/vnfapi/base_incomplete.json"
    data = load_json(base_path)
    azs = data["input"]["vnf-topology-information"]["vnf-assignments"][
        "availability-zones"
    ]
    assert azs == [{"availability-zone": "az0"}, {"availability-zone": "az1"}]