aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_cloud_region_resource.py
blob: e9e49744bb6e72a15a14b24f49fd70be2cd2b6e6 (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
"""
   Copyright 2022 Deutsche Telekom AG

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file 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.
"""
from pathlib import Path
from unittest.mock import MagicMock, patch, PropertyMock

from onapsdk.aai.cloud_infrastructure.complex import Complex

from onap_data_provider.resources.cloud_region_resource import (
    CloudRegion,
    CloudRegionResource,
)
from onapsdk.exceptions import ResourceNotFound

CLOUD_REGION_DATA = {
    "cloud-owner": "test",
    "cloud-region-id": "test",
    "orchestration-disabled": True,
    "in-maint": False,
}

CLOUD_REGION_K8S_TYPE = {
    "cloud-region-id": "k8s-test",
    "cloud-owner": "k8s-test",
    "orchestration-disabled": True,
    "in-maint": False,
    "cloud-type": "k8s",
    "kube-config": Path(Path(__file__).parent, "test-kube-config"),
}


@patch("onap_data_provider.resources.cloud_region_resource.CloudRegion.get_by_id")
def test_cloud_region_resource_cloud_region(mock_cloud_region_get_by_id):
    mock_cloud_region_get_by_id.side_effect = ResourceNotFound
    cloud_region_resource = CloudRegionResource(CLOUD_REGION_DATA)
    assert cloud_region_resource.cloud_region is None

    mock_cloud_region_get_by_id.side_effect = None
    mock_cloud_region_get_by_id.return_value = 1
    assert cloud_region_resource.cloud_region == 1


@patch(
    "onap_data_provider.resources.cloud_region_resource.CloudRegionResource.cloud_region",
    new_callable=PropertyMock,
)
def test_cloud_region_resource_exists(mock_cloud_region):
    mock_cloud_region.return_value = None
    cloud_region_resource = CloudRegionResource(CLOUD_REGION_DATA)
    assert cloud_region_resource.exists is False
    mock_cloud_region.return_value = 1  # Anything but not None
    assert cloud_region_resource.exists is True


@patch(
    "onap_data_provider.resources.cloud_region_resource.CloudRegionResource.exists",
    new_callable=PropertyMock,
)
@patch("onap_data_provider.resources.cloud_region_resource.CloudRegion.create")
def test_cloud_region_create(mock_cloud_region_create, mock_exists):

    cloud_region_resource = CloudRegionResource(CLOUD_REGION_DATA)
    assert cloud_region_resource.data == CLOUD_REGION_DATA

    mock_exists.return_value = False
    cloud_region_resource.create()
    assert mock_cloud_region_create.called_once_with(
        cloud_owner="test",
        cloud_region_id="test",
        orchestration_disabled=True,
        in_maint=False,
    )

    mock_exists.reset_mock()
    mock_cloud_region_create.reset_mock()

    mock_exists.return_value = True
    cloud_region_resource.create()
    mock_cloud_region_create.assert_not_called()


@patch(
    "onap_data_provider.resources.cloud_region_resource.CloudRegionResource.cloud_region",
    new_callable=PropertyMock,
)
@patch("onap_data_provider.resources.cloud_region_resource.Complex.get_by_physical_location_id")
def test_cloud_region_resource_link_to_complex(
    mock_complex_get_by_physical_location_id, mock_cloud_region_property
):
    mock_cloud_region_property.return_value.complex = MagicMock()
    cloud_region_resource = CloudRegionResource(CLOUD_REGION_DATA)
    cloud_region_resource._link_to_complex("test")
    mock_complex_get_by_physical_location_id.assert_not_called()

    mock_cloud_region_property.return_value.complex = None
    mock_complex_get_by_physical_location_id.side_effect = ResourceNotFound
    cloud_region_resource._link_to_complex("test")
    mock_cloud_region_property.return_value.link_to_complex.assert_not_called()

    mock_complex_get_by_physical_location_id.side_effect = None
    mock_complex_get_by_physical_location_id.return_value = Complex("test")
    cloud_region_resource._link_to_complex("test")
    mock_cloud_region_property.return_value.link_to_complex.assert_called_once()


@patch(
    "onap_data_provider.resources.cloud_region_resource.CloudRegionResource.cloud_region",
    new_callable=PropertyMock,
)
def test_cloud_region_resource_create_availability_zones(mock_cloud_region_property):
    cloud_region_resource = CloudRegionResource(CLOUD_REGION_DATA)
    cloud_region_resource.data["availability-zones"] = [
        {"availability-zone-name": "testzone1", "hypervisor-type": "OpenStackTest"}
    ]
    cloud_region_resource.create()
    mock_cloud_region_property.return_value.add_availability_zone.assert_called_once()


@patch(
    "onap_data_provider.resources.cloud_region_resource.CloudRegionResource.exists",
    new_callable=PropertyMock,
)
@patch("onap_data_provider.resources.cloud_region_resource.ConnectivityInfo")
@patch("onap_data_provider.resources.cloud_region_resource.CloudRegion.create")
@patch("onap_data_provider.resources.cloud_region_resource.CloudRegion.complex")
@patch("onap_data_provider.resources.cloud_region_resource.SoDbAdapter.add_cloud_site")
def test_cloud_region_k8s_type(
    mock_add_cloud_site,
    _,
    mock_cloud_region_create,
    mock_connectivity_info,
    mock_exists,
):
    mock_exists.return_value = False
    mock_cloud_region_create.return_value = CloudRegion(
        cloud_owner=CLOUD_REGION_K8S_TYPE["cloud-owner"],
        cloud_region_id=CLOUD_REGION_K8S_TYPE["cloud-region-id"],
        orchestration_disabled=CLOUD_REGION_K8S_TYPE["orchestration-disabled"],
        in_maint=CLOUD_REGION_K8S_TYPE["in-maint"],
        cloud_type=CLOUD_REGION_K8S_TYPE["cloud-type"],
    )
    cloud_region_resource = CloudRegionResource(CLOUD_REGION_K8S_TYPE)
    cloud_region_resource.create()
    mock_connectivity_info.get_connectivity_info_by_region_id.assert_called_once_with(
        CLOUD_REGION_K8S_TYPE["cloud-region-id"]
    )
    mock_add_cloud_site.assert_called_once()

    mock_connectivity_info.get_connectivity_info_by_region_id.side_effect = (
        ResourceNotFound
    )
    cloud_region_resource.create()
    mock_connectivity_info.create.assert_called_once()