File: cdn_tests.py

package info (click to toggle)
python-softlayer 6.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,508 kB
  • sloc: python: 57,195; makefile: 133; xml: 97; sh: 59
file content (187 lines) | stat: -rw-r--r-- 7,552 bytes parent folder | download
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
"""
    SoftLayer.tests.managers.cdn_tests
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    :license: MIT, see LICENSE for more details.
"""
import datetime
from unittest import mock as mock

from SoftLayer import fixtures
from SoftLayer.managers import cdn
from SoftLayer import testing


class CDNTests(testing.TestCase):

    def set_up(self):
        self.cdn_client = cdn.CDNManager(self.client)

    def test_list_accounts(self):
        self.cdn_client.list_cdn()
        self.assert_called_with('SoftLayer_Network_CdnMarketplace_Configuration_Mapping',
                                'listDomainMappings')

    def test_detail_cdn(self):
        self.cdn_client.get_cdn("12345")

        args = ("12345",)
        self.assert_called_with('SoftLayer_Network_CdnMarketplace_Configuration_Mapping',
                                'listDomainMappingByUniqueId',
                                args=args)

    @mock.patch('SoftLayer.utils.days_to_datetime')
    def test_detail_usage_metric(self, mock_now):
        mock_now.return_value = datetime.datetime(2020, 1, 1)
        self.cdn_client.get_usage_metrics(12345, history=30, frequency="aggregate")

        args = (12345,
                self.cdn_client.start_data,
                self.cdn_client.end_date,
                "aggregate")
        self.assert_called_with('SoftLayer_Network_CdnMarketplace_Metrics',
                                'getMappingUsageMetrics',
                                args=args)

    # Does this still work in 2038 ? https://github.com/softlayer/softlayer-python/issues/1764 for context
    @mock.patch('SoftLayer.utils.days_to_datetime')
    def test_detail_usage_metric_future(self, mock_now):
        mock_now.return_value = datetime.datetime(2040, 1, 1)
        self.assertRaises(
            OverflowError,
            self.cdn_client.get_usage_metrics, 12345, history=30, frequency="aggregate"
        )

    def test_get_origins(self):
        self.cdn_client.get_origins("12345")
        self.assert_called_with('SoftLayer_Network_CdnMarketplace_Configuration_Mapping_Path',
                                'listOriginPath')

    def test_add_origin(self):
        self.cdn_client.add_origin("12345", "10.10.10.1", "/example/videos", dynamic_path="abc.html",
                                   origin_type="server", header="test.example.com", https_port=81,
                                   protocol='https', optimize_for="dynamic", compression=True,
                                   prefetching=True, cache_query="include all")

        args = ({
            'uniqueId': "12345",
            'origin': '10.10.10.1',
            'path': '/example/videos',
            'originType': 'HOST_SERVER',
            'header': 'test.example.com',
            'httpPort': 80,
            'httpsPort': 81,
            'protocol': 'HTTPS',
            'performanceConfiguration': 'Dynamic content acceleration',
            'cacheKeyQueryRule': "include all",
            'dynamicContentAcceleration': {
                'detectionPath': "/abc.html",
                'prefetchEnabled': True,
                'mobileImageCompressionEnabled': True
            }
        },)
        self.assert_called_with('SoftLayer_Network_CdnMarketplace_Configuration_Mapping_Path',
                                'createOriginPath',
                                args=args)

    def test_add_origin_with_bucket_and_file_extension(self):
        self.cdn_client.add_origin("12345", "10.10.10.1", "/example/videos", dynamic_path="abc.html",
                                   origin_type="server", header="test.example.com", https_port=81,
                                   protocol='https', optimize_for="dynamic", compression=True,
                                   prefetching=True, cache_query="include all")

        args = ({
            'uniqueId': "12345",
            'origin': '10.10.10.1',
            'path': '/example/videos',
            'originType': 'HOST_SERVER',
            'header': 'test.example.com',
            'httpPort': 80,
            'httpsPort': 81,
            'protocol': 'HTTPS',
            'performanceConfiguration': 'Dynamic content acceleration',
            'cacheKeyQueryRule': "include all",
            'dynamicContentAcceleration': {
                'detectionPath': "/abc.html",
                'prefetchEnabled': True,
                'mobileImageCompressionEnabled': True
            }
        },)
        self.assert_called_with('SoftLayer_Network_CdnMarketplace_Configuration_Mapping_Path',
                                'createOriginPath',
                                args=args)

    def test_remove_origin(self):
        self.cdn_client.remove_origin("12345", "/example1")

        args = ("12345",
                "/example1")
        self.assert_called_with('SoftLayer_Network_CdnMarketplace_Configuration_Mapping_Path',
                                'deleteOriginPath',
                                args=args)

    def test_purge_content(self):
        self.cdn_client.purge_content("12345", "/example1")

        args = ("12345",
                "/example1")
        self.assert_called_with('SoftLayer_Network_CdnMarketplace_Configuration_Cache_Purge',
                                'createPurge',
                                args=args)

    def test_cdn_edit(self):
        identifier = '11223344'
        header = 'www.test.com'
        result = self.cdn_client.edit(identifier, header=header)

        self.assertEqual(fixtures.SoftLayer_Network_CdnMarketplace_Configuration_Mapping.
                         updateDomainMapping, result)

        self.assert_called_with(
            'SoftLayer_Network_CdnMarketplace_Configuration_Mapping',
            'updateDomainMapping',
            args=({
                'uniqueId': '11223344',
                'originType': 'HOST_SERVER',
                'protocol': 'HTTP',
                'path': '/',
                'vendorName': 'akamai',
                'cname': 'cdnakauuiet7s6u6.cdnedge.bluemix.net',
                'domain': 'test.example.com',
                'httpPort': 80,
                'header': 'www.test.com',
                'origin': '1.1.1.1'
            },)
        )

    def test_cdn_instance_by_hostname(self):
        hostname = 'test.example.com'
        result = self.cdn_client._get_ids_from_hostname(hostname)
        expected_result = fixtures.SoftLayer_Network_CdnMarketplace_Configuration_Mapping.listDomainMappings
        self.assertEqual(expected_result[0]['uniqueId'], result[0])
        self.assert_called_with(
            'SoftLayer_Network_CdnMarketplace_Configuration_Mapping',
            'listDomainMappings',)

    def test_delete_cdn(self):
        uniqueId = "123465"
        self.cdn_client.delete_cdn(uniqueId)

        args = (uniqueId,)

        self.assert_called_with('SoftLayer_Network_CdnMarketplace_Configuration_Mapping',
                                'deleteDomainMapping',
                                args=args)

    def test_create_cdn(self):
        hostname = "test.com"
        origin = "123.123.123.123"
        origin_type = "server"
        http = 80
        newCdn = ({"domain": hostname, "origin": origin, "originType": "HOST_SERVER",
                  "vendorName": "akamai", "httpPort": http, "protocol": "HTTP"},)
        self.cdn_client.create_cdn(hostname, origin, origin_type, http)

        self.assert_called_with('SoftLayer_Network_CdnMarketplace_Configuration_Mapping',
                                'createDomainMapping',
                                args=newCdn)