Debian Codegraph @

ansible_collections/community/aws/tests/unit/plugins/modules/test_directconnect_confirm_connection.py

raw
1 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
2
3 from unittest.mock import call
4 from unittest.mock import patch
5
6 import pytest
7
8 try:
9 from botocore.exceptions import ClientError
10 except ImportError:
11 pass
12
13 from ansible_collections.amazon.aws.plugins.module_utils.botocore import HAS_BOTO3
14
15 from ansible_collections.community.aws.plugins.modules import directconnect_confirm_connection
16 from ansible_collections.community.aws.tests.unit.plugins.modules.utils import AnsibleExitJson
17 from ansible_collections.community.aws.tests.unit.plugins.modules.utils import AnsibleFailJson
18 from ansible_collections.community.aws.tests.unit.plugins.modules.utils import ModuleTestCase
19 from ansible_collections.community.aws.tests.unit.plugins.modules.utils import set_module_args
20
21 if not HAS_BOTO3:
22 pytestmark = pytest.mark.skip(
23 "test_directconnect_confirm_connection.py requires the `boto3` and `botocore` modules"
24 )
25
26
27 @patch("ansible_collections.amazon.aws.plugins.module_utils.core.HAS_BOTO3", new=True)
28 @patch.object(directconnect_confirm_connection.AnsibleAWSModule, "client")
29 class TestAWSDirectConnectConfirmConnection(ModuleTestCase):
30 def test_missing_required_parameters(self, *args):
31 set_module_args({})
32 with self.assertRaises(AnsibleFailJson) as exec_info:
33 directconnect_confirm_connection.main()
34
35 result = exec_info.exception.args[0]
36 assert result["failed"] is True
37 assert "name" in result["msg"]
38 assert "connection_id" in result["msg"]
39
40 def test_get_by_connection_id(self, mock_client):
41 mock_client.return_value.describe_connections.return_value = {
42 "connections": [
43 {
44 "connectionState": "requested",
45 "connectionId": "dxcon-fgq9rgot",
46 "location": "EqSe2",
47 "connectionName": "ansible-test-connection",
48 "bandwidth": "1Gbps",
49 "ownerAccount": "123456789012",
50 "region": "us-west-2",
51 }
52 ]
53 }
54 set_module_args({"connection_id": "dxcon-fgq9rgot"})
55 with self.assertRaises(AnsibleExitJson) as exec_info:
56 directconnect_confirm_connection.main()
57
58 result = exec_info.exception.args[0]
59 assert result["changed"] is False
60 assert result["connection_state"] == "requested"
61 mock_client.return_value.describe_connections.assert_has_calls([call(connectionId="dxcon-fgq9rgot")])
62 mock_client.return_value.confirm_connection.assert_not_called()
63
64 def test_get_by_name(self, mock_client):
65 mock_client.return_value.describe_connections.return_value = {
66 "connections": [
67 {
68 "connectionState": "requested",
69 "connectionId": "dxcon-fgq9rgot",
70 "location": "EqSe2",
71 "connectionName": "ansible-test-connection",
72 "bandwidth": "1Gbps",
73 "ownerAccount": "123456789012",
74 "region": "us-west-2",
75 }
76 ]
77 }
78 set_module_args({"name": "ansible-test-connection"})
79 with self.assertRaises(AnsibleExitJson) as exec_info:
80 directconnect_confirm_connection.main()
81
82 result = exec_info.exception.args[0]
83 assert result["changed"] is False
84 assert result["connection_state"] == "requested"
85 mock_client.return_value.describe_connections.assert_has_calls([call(), call(connectionId="dxcon-fgq9rgot")])
86 mock_client.return_value.confirm_connection.assert_not_called()
87
88 def test_missing_connection_id(self, mock_client):
89 mock_client.return_value.describe_connections.side_effect = ClientError(
90 {"Error": {"Code": "ResourceNotFoundException"}}, "DescribeConnection"
91 )
92 set_module_args({"connection_id": "dxcon-aaaabbbb"})
93 with self.assertRaises(AnsibleFailJson) as exec_info:
94 directconnect_confirm_connection.main()
95
96 result = exec_info.exception.args[0]
97 assert result["failed"] is True
98 mock_client.return_value.describe_connections.assert_has_calls([call(connectionId="dxcon-aaaabbbb")])
99
100 def test_missing_name(self, mock_client):
101 mock_client.return_value.describe_connections.return_value = {
102 "connections": [
103 {
104 "connectionState": "requested",
105 "connectionId": "dxcon-fgq9rgot",
106 "location": "EqSe2",
107 "connectionName": "ansible-test-connection",
108 "bandwidth": "1Gbps",
109 "ownerAccount": "123456789012",
110 "region": "us-west-2",
111 }
112 ]
113 }
114 set_module_args({"name": "foobar"})
115 with self.assertRaises(AnsibleFailJson) as exec_info:
116 directconnect_confirm_connection.main()
117
118 result = exec_info.exception.args[0]
119 assert result["failed"] is True
120 mock_client.return_value.describe_connections.assert_has_calls([call()])
121
122 def test_confirm(self, mock_client):
123 mock_client.return_value.describe_connections.return_value = {
124 "connections": [
125 {
126 "connectionState": "ordering",
127 "connectionId": "dxcon-fgq9rgot",
128 "location": "EqSe2",
129 "connectionName": "ansible-test-connection",
130 "bandwidth": "1Gbps",
131 "ownerAccount": "123456789012",
132 "region": "us-west-2",
133 }
134 ]
135 }
136 mock_client.return_value.confirm_connection.return_value = [{}]
137 set_module_args({"connection_id": "dxcon-fgq9rgot"})
138 with self.assertRaises(AnsibleExitJson) as exec_info:
139 directconnect_confirm_connection.main()
140
141 result = exec_info.exception.args[0]
142 assert result["changed"] is True
143 mock_client.return_value.describe_connections.assert_has_calls(
144 [
145 call(connectionId="dxcon-fgq9rgot"),
146 call(connectionId="dxcon-fgq9rgot"),
147 call(connectionId="dxcon-fgq9rgot"),
148 ]
149 )
150 mock_client.return_value.confirm_connection.assert_called_once_with(connectionId="dxcon-fgq9rgot")