-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_sync_keystone.py
More file actions
152 lines (127 loc) · 4.54 KB
/
test_sync_keystone.py
File metadata and controls
152 lines (127 loc) · 4.54 KB
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
import uuid
from contextlib import nullcontext
from unittest.mock import MagicMock
import pytest
from openstack.connection import Connection
from pytest_lazy_fixtures import lf
from understack_workflows.main.sync_keystone import Event
from understack_workflows.main.sync_keystone import argument_parser
from understack_workflows.main.sync_keystone import do_action
from understack_workflows.main.sync_keystone import handle_project_delete
@pytest.fixture
def mock_pynautobot_api(mocker):
mock_client = MagicMock(name="MockPynautobotApi")
mock_devices = MagicMock()
mock_devices.filter.return_value = []
mock_devices.update.return_value = True
mock_client.dcim.devices = mock_devices
mock_tenants = MagicMock()
mock_tenants.get.return_value = None
mock_tenants.delete.return_value = True
mock_client.tenancy.tenants = mock_tenants
mocker.patch(
"understack_workflows.main.sync_keystone.pynautobot.api",
return_value=mock_client,
)
return mock_client
@pytest.mark.parametrize(
"arg_list,context,expected_id",
[
(["identity.project.created", ""], pytest.raises(SystemExit), None),
(["identity.project.created", "http"], pytest.raises(SystemExit), None),
(
["identity.project.created", lf("project_id")],
nullcontext(),
lf("project_id"),
),
(
[
"identity.project.created",
lf("project_id"),
],
nullcontext(),
lf("project_id"),
),
(
["identity.project.created", lf("project_id")],
nullcontext(),
lf("project_id"),
),
],
)
def test_parse_object_id(arg_list, context, expected_id):
parser = argument_parser()
with context:
args = parser.parse_args([str(arg) for arg in arg_list])
assert args.object == expected_id
def test_create_project(
os_conn,
mock_pynautobot_api,
project_id: uuid.UUID,
domain_id: uuid.UUID,
):
ret = do_action(os_conn, mock_pynautobot_api, Event.ProjectCreate, project_id)
os_conn.identity.get_project.assert_any_call(project_id.hex)
os_conn.identity.get_domain.assert_any_call(domain_id.hex)
assert ret == 0
def test_update_project(
os_conn,
mock_pynautobot_api,
project_id: uuid.UUID,
domain_id: uuid.UUID,
):
ret = do_action(os_conn, mock_pynautobot_api, Event.ProjectUpdate, project_id)
os_conn.identity.get_project.assert_any_call(project_id.hex)
os_conn.identity.get_domain.assert_any_call(domain_id.hex)
assert ret == 0
def test_update_project_domain_skipped(
os_conn,
mock_pynautobot_api,
domain_id: uuid.UUID,
):
"""Test that domains are skipped during update events."""
ret = do_action(os_conn, mock_pynautobot_api, Event.ProjectUpdate, domain_id)
# Should fetch the project to check if it's a domain
os_conn.identity.get_project.assert_called_once_with(domain_id.hex)
# Should NOT call get_domain or create/update tenant since it's a domain
os_conn.identity.get_domain.assert_not_called()
mock_pynautobot_api.tenancy.tenants.get.assert_not_called()
mock_pynautobot_api.tenancy.tenants.create.assert_not_called()
assert ret == 0
def test_delete_project(
os_conn,
mock_pynautobot_api,
project_id: uuid.UUID,
):
ret = do_action(os_conn, mock_pynautobot_api, Event.ProjectDelete, project_id)
assert ret == 0
@pytest.mark.parametrize(
"tenant_exists, expect_delete_call, expect_unmap_call",
[
(False, False, False), # Tenant does NOT exist
(True, True, True), # Tenant exists
],
)
def test_handle_project_delete(
mocker, mock_pynautobot_api, tenant_exists, expect_delete_call, expect_unmap_call
):
project_id = uuid.uuid4()
tenant_obj = MagicMock()
mock_pynautobot_api.tenancy.tenants.get.return_value = (
tenant_obj if tenant_exists else None
)
mock_unmap_devices = mocker.patch(
"understack_workflows.main.sync_keystone._unmap_tenant_from_devices"
)
conn_mock: Connection = MagicMock(spec=Connection)
ret = handle_project_delete(conn_mock, mock_pynautobot_api, project_id)
assert ret == 0
mock_pynautobot_api.tenancy.tenants.get.assert_called_once_with(id=project_id)
if tenant_exists:
mock_unmap_devices.assert_called_once_with(
tenant_id=project_id, nautobot=mock_pynautobot_api
)
tenant_obj.delete.assert_called_once()
else:
mock_unmap_devices.assert_not_called()
tenant_obj.delete.assert_not_called()