Debian Codegraph @

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

raw
1 #
2 # (c) 2016 Michael De La Rue
3 #
4 # This file is part of Ansible
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 # Make coding more python3-ish
8 from __future__ import absolute_import
9 from __future__ import division
10 from __future__ import print_function
11
12 __metaclass__ = type
13
14 import sys
15
16 import pytest
17
18 from ansible_collections.amazon.aws.plugins.module_utils import modules as aws_modules
19 from ansible_collections.amazon.aws.plugins.module_utils.botocore import HAS_BOTO3
20
21 import ansible_collections.community.aws.plugins.modules.api_gateway as agw
22 from ansible_collections.community.aws.tests.unit.plugins.modules.utils import set_module_args
23
24 if not HAS_BOTO3:
25 pytestmark = pytest.mark.skip("test_api_gateway.py requires the `boto3` and `botocore` modules")
26
27
28 exit_return_dict = {}
29
30
31 def fake_exit_json(self, **kwargs):
32 """store the kwargs given to exit_json rather than putting them out to stdout"""
33 global exit_return_dict
34 exit_return_dict = kwargs
35 sys.exit(0)
36
37
38 def test_upload_api(monkeypatch):
39 class FakeConnection:
40 def put_rest_api(self, *args, **kwargs):
41 assert kwargs["body"] == "the-swagger-text-is-fake"
42 return {"msg": "success!"}
43
44 def return_fake_connection(*args, **kwargs):
45 return FakeConnection()
46
47 # Because it's imported into the aws_modules namespace we need to override
48 # it there, even though the function itself lives in module_utils.botocore
49 monkeypatch.setattr(aws_modules, "boto3_conn", return_fake_connection)
50 monkeypatch.setattr(aws_modules.AnsibleAWSModule, "exit_json", fake_exit_json)
51
52 set_module_args(
53 {
54 "api_id": "fred",
55 "state": "present",
56 "swagger_text": "the-swagger-text-is-fake",
57 "region": "mars-north-1",
58 "_ansible_tmpdir": "/tmp/ansibl-abcdef",
59 }
60 )
61 with pytest.raises(SystemExit):
62 agw.main()
63 assert exit_return_dict["changed"]
64
65
66 def test_warn_if_region_not_specified():
67 set_module_args(
68 {
69 "name": "api_gateway",
70 "state": "present",
71 "runtime": "python2.7",
72 "role": "arn:aws:iam::123456789012:role/lambda_basic_execution",
73 "handler": "lambda_python.my_handler",
74 }
75 )
76 with pytest.raises(SystemExit):
77 print(agw.main())