Debian Codegraph @

ansible_collections/amazon/aws/tests/unit/plugins/module_utils/s3/test_endpoints.py

raw
1 # -*- coding: utf-8 -*-
2 #
3 # (c) 2021 Red Hat Inc.
4 #
5 # This file is part of Ansible
6 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
7
8 from unittest.mock import patch
9
10 import pytest
11
12 from ansible_collections.amazon.aws.plugins.module_utils import s3
13
14 mod_urlparse = "ansible_collections.amazon.aws.plugins.module_utils.s3.urlparse"
15
16
17 class UrlInfo:
18 def __init__(self, scheme=None, hostname=None, port=None):
19 self.hostname = hostname
20 self.scheme = scheme
21 self.port = port
22
23
24 @patch(mod_urlparse)
25 def test_is_fakes3_with_none_arg(m_urlparse):
26 m_urlparse.side_effect = SystemExit(1)
27 result = s3.is_fakes3(None)
28 assert not result
29 m_urlparse.assert_not_called()
30
31
32 @pytest.mark.parametrize(
33 "url,scheme,result",
34 [
35 ("https://test-s3.amazon.com", "https", False),
36 ("fakes3://test-s3.amazon.com", "fakes3", True),
37 ("fakes3s://test-s3.amazon.com", "fakes3s", True),
38 ],
39 )
40 @patch(mod_urlparse)
41 def test_is_fakes3(m_urlparse, url, scheme, result):
42 m_urlparse.return_value = UrlInfo(scheme=scheme)
43 assert result == s3.is_fakes3(url)
44 m_urlparse.assert_called_with(url)
45
46
47 @pytest.mark.parametrize(
48 "url,urlinfo,endpoint",
49 [
50 (
51 "fakes3://test-s3.amazon.com",
52 {"scheme": "fakes3", "hostname": "test-s3.amazon.com"},
53 {"endpoint": "http://test-s3.amazon.com:80", "use_ssl": False},
54 ),
55 (
56 "fakes3://test-s3.amazon.com:8080",
57 {"scheme": "fakes3", "hostname": "test-s3.amazon.com", "port": 8080},
58 {"endpoint": "http://test-s3.amazon.com:8080", "use_ssl": False},
59 ),
60 (
61 "fakes3s://test-s3.amazon.com",
62 {"scheme": "fakes3s", "hostname": "test-s3.amazon.com"},
63 {"endpoint": "https://test-s3.amazon.com:443", "use_ssl": True},
64 ),
65 (
66 "fakes3s://test-s3.amazon.com:9096",
67 {"scheme": "fakes3s", "hostname": "test-s3.amazon.com", "port": 9096},
68 {"endpoint": "https://test-s3.amazon.com:9096", "use_ssl": True},
69 ),
70 ],
71 )
72 @patch(mod_urlparse)
73 def test_parse_fakes3_endpoint(m_urlparse, url, urlinfo, endpoint):
74 m_urlparse.return_value = UrlInfo(**urlinfo)
75 result = s3.parse_fakes3_endpoint(url)
76 assert endpoint == result
77 m_urlparse.assert_called_with(url)
78
79
80 @pytest.mark.parametrize(
81 "url,scheme,use_ssl",
82 [
83 ("https://test-s3-ceph.amazon.com", "https", True),
84 ("http://test-s3-ceph.amazon.com", "http", False),
85 ],
86 )
87 @patch(mod_urlparse)
88 def test_parse_ceph_endpoint(m_urlparse, url, scheme, use_ssl):
89 m_urlparse.return_value = UrlInfo(scheme=scheme)
90 result = s3.parse_ceph_endpoint(url)
91 assert result == {"endpoint": url, "use_ssl": use_ssl}
92 m_urlparse.assert_called_with(url)