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 (186 lines) | stat: -rw-r--r-- 8,504 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
"""
    SoftLayer.tests.CLI.modules.cdn_tests
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

from SoftLayer.CLI import exceptions
from SoftLayer import testing


class CdnTests(testing.TestCase):

    def test_list_accounts(self):
        result = self.run_command(['cdn', 'list'])

        self.assert_no_fail(result)
        self.assertEqual(json.loads(result.output),
                         [{'cname': 'cdnakauuiet7s6u6.cdnedge.bluemix.net',
                           'domain': 'test.example.com',
                           'origin': '1.1.1.1',
                           'status': 'CNAME_CONFIGURATION',
                           'unique_id': '11223344',
                           'vendor': 'akamai'}]
                         )

    @mock.patch('SoftLayer.utils.days_to_datetime')
    def test_detail_account(self, mock_now):
        mock_now.return_value = datetime.datetime(2020, 1, 1)
        result = self.run_command(['cdn', 'detail', '--history=30', '1245'])

        self.assert_no_fail(result)
        api_results = json.loads(result.output)
        self.assertEqual(api_results['hit_ratio'], '2.0 %')
        self.assertEqual(api_results['total_bandwidth'], '1.0 GB')
        self.assertEqual(api_results['total_hits'], 3)
        self.assertEqual(api_results['hostname'], 'test.example.com')
        self.assertEqual(api_results['protocol'], 'HTTP')

    def test_purge_content(self):
        result = self.run_command(['cdn', 'purge', '1234',
                                   '/article/file.txt'])

        self.assert_no_fail(result)

    def test_list_origins(self):
        result = self.run_command(['cdn', 'origin-list', '1234'])

        self.assert_no_fail(result)
        self.assertEqual(json.loads(result.output), [{'HTTP Port': 80,
                                                      'Origin': '10.10.10.1',
                                                      'Path': '/example',
                                                      'Status': 'RUNNING'},
                                                     {'HTTP Port': 80,
                                                      'Origin': '10.10.10.1',
                                                      'Path': '/example1',
                                                      'Status': 'RUNNING'}])

    def test_add_origin_server(self):
        result = self.run_command(
            ['cdn', 'origin-add', '-t', 'server', '-H=test.example.com', '-p', 80, '-o', 'web', '-c=include-all',
             '1234', '10.10.10.1', '/example/videos2'])

        self.assert_no_fail(result)

    def test_add_origin_server_dynamic(self):
        result = self.run_command(
            ['cdn', 'origin-add', '-t', 'server', '-H=test.example.com', '-s', 81, '-o', 'dynamic', '-c=include-all',
             '-P', 'HTTPS', '-d', 'abc.html', '-g', True, '-i', True, '1234', '10.10.10.1', '/example/videos2', ])

        self.assert_no_fail(result)

    def test_add_origin_storage(self):
        result = self.run_command(['cdn', 'origin-add', '-t', 'storage', '-b=test-bucket', '-H=test.example.com',
                                   '-p', 80, '-o', 'web', '-c=include-all', '1234', '10.10.10.1', '/example/videos2'])

        self.assert_no_fail(result)

    def test_add_origin_storage_dynamic(self):
        result = self.run_command(['cdn', 'origin-add', '-t', 'storage', '-b=test-bucket', '-H=test.example.com',
                                   '-s', 81, '-o', 'dynamic', '-c=include-all', '1234', '10.10.10.1',
                                   '/example/videos2', '-g', True, '-i', True])

        self.assert_no_fail(result)

    def test_add_origin_without_storage(self):
        result = self.run_command(['cdn', 'origin-add', '-t', 'storage', '-H=test.example.com', '-p', 80,
                                   '-P', 'HTTPS', '-o', 'web', '-c=include-all',
                                   '1234', '10.10.10.1', '/example/videos2'])

        self.assertEqual(result.exit_code, 2)
        self.assertIsInstance(result.exception, exceptions.ArgumentError)

    def test_add_origin_storage_with_file_extensions(self):
        result = self.run_command(
            ['cdn', 'origin-add', '-t', 'storage', '-b=test-bucket', '-e', 'jpg', '-H=test.example.com', '-p', 80,
             '-o', 'web', '-c=include-all', '1234', '10.10.10.1', '/example/videos2'])

        self.assert_no_fail(result)

    def test_add_origin_storage_with_file_extensions_dynamic(self):
        result = self.run_command(
            ['cdn', 'origin-add', '-t', 'storage', '-b=test-bucket', '-e', 'jpg', '-H=test.example.com', '-s', 81,
             '-P', 'HTTPS', '-o', 'dynamic', '-d', 'abc.html', '-g', True, '-i', True,
             '-c=include-all', '1234', '10.10.10.1', '/example/videos2',
             ])

        self.assert_no_fail(result)

    def test_remove_origin(self):
        result = self.run_command(['cdn', 'origin-remove', '1234',
                                   '/example1'])

        self.assert_no_fail(result)
        self.assertEqual(result.output, "Origin with path /example1 has been deleted\n")

    def test_edit_header(self):
        result = self.run_command(['cdn', 'edit', 'test.example.com', '--header=www.test.com'])
        self.assert_no_fail(result)
        header_result = json.loads(result.output)
        self.assertEqual('www.test.com', header_result['Header'])

    def test_edit_http_port(self):
        result = self.run_command(['cdn', 'edit', 'test.example.com', '--http-port=83'])
        self.assert_no_fail(result)
        header_result = json.loads(result.output)
        self.assertEqual(83, header_result['Http Port'])

    def test_edit_respect_headers(self):
        result = self.run_command(['cdn', 'edit', 'test.example.com', '--respect-headers=1'])
        self.assert_no_fail(result)
        header_result = json.loads(result.output)
        self.assertEqual(True, header_result['Respect Headers'])

    def test_edit_cache(self):
        result = self.run_command(['cdn', 'edit', 'test.example.com', '--cache', 'include-specified',
                                   '--cache', 'test'])
        self.assert_no_fail(result)
        header_result = json.loads(result.output)
        self.assertEqual('include: test', header_result['Cache key optimization'])

    def test_edit_cache_by_uniqueId(self):
        result = self.run_command(['cdn', 'edit', '11223344', '--cache', 'include-specified', '--cache', 'test'])
        self.assert_no_fail(result)
        header_result = json.loads(result.output)
        self.assertEqual('include: test', header_result['Cache key optimization'])

    def test_delete_cdn(self):
        result = self.run_command(['cdn', 'delete', '123456'])
        self.assert_no_fail(result)
        self.assertIn("Cdn with uniqueId: 123456 was deleted.", result.output)

    def test_create_cdn(self):
        result = self.run_command(['cdn', 'create', '--hostname', 'www.example.com',
                                  '--origin', '123.123.123.123', '--http', '80'])
        self.assert_no_fail(result)
        self.assertIn("CDN Unique ID", result.output)
        self.assertIn("354034879028850", result.output)
        self.assertIn("Hostname", result.output)
        self.assertIn("test.com", result.output)
        self.assertIn("header", result.output)
        self.assertIn("header.test.com", result.output)
        self.assertIn("Http Port", result.output)
        self.assertIn("80", result.output)
        self.assertIn("Path", result.output)
        self.assertIn("/*", result.output)

    def test_create_cdn_without_hostname(self):
        result = self.run_command(['cdn', 'create'])
        self.assertEqual(2, result.exit_code)
        print(result.output)
        self.assertIn("Error: Missing option '--hostname'.", result.output)

    def test_create_cdn_without_origin(self):
        result = self.run_command(['cdn', 'create', '--hostname', 'www.example.com'])
        self.assertEqual(2, result.exit_code)
        print(result.output)
        self.assertIn("Error: Missing option '--origin'.", result.output)

    def test_create_cdn_without_http_or_https(self):
        result = self.run_command(['cdn', 'create', '--hostname', 'www.example.com', '--origin', '123.123.123.123'])
        self.assertEqual(2, result.exit_code)
        self.assertIn("Is needed http or https options", result.exception.message)