File: image_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 (233 lines) | stat: -rw-r--r-- 8,859 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"""
    SoftLayer.tests.managers.image_tests
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    :license: MIT, see LICENSE for more details.
"""

import SoftLayer
from SoftLayer import exceptions
from SoftLayer import testing

IMAGE_SERVICE = 'SoftLayer_Virtual_Guest_Block_Device_Template_Group'


class ImageTests(testing.TestCase):

    def set_up(self):
        self.image = SoftLayer.ImageManager(self.client)
        self.vgbdtg = self.client['']
        self.account = self.client['Account']

    def test_get_image(self):
        result = self.image.get_image(100)

        self.assertEqual(result['id'], 100)
        self.assertEqual(result['name'], 'test_image')
        self.assertEqual(result['accountId'], 1234)
        self.assert_called_with(IMAGE_SERVICE, 'getObject', identifier=100)

    def test_delete_image(self):
        self.image.delete_image(100)

        self.assert_called_with(IMAGE_SERVICE, 'deleteObject', identifier=100)

    def test_list_private_images(self):
        # Casting to list() to force the iter_call generator to run
        results = list(self.image.list_private_images())
        self.assertEqual(len(results), 2)
        self.assert_called_with('SoftLayer_Account', 'getPrivateBlockDeviceTemplateGroups')

    def test_list_private_images_with_filters(self):
        # Casting to list() to force the iter_call generator to run
        results = list(self.image.list_private_images(guid='0FA9ECBD-CF7E-4A1F-1E36F8D27C2B', name='name'))

        _filter = {
            'privateBlockDeviceTemplateGroups': {
                'globalIdentifier': {
                    'operation': '_= 0FA9ECBD-CF7E-4A1F-1E36F8D27C2B'},
                'name': {'operation': '_= name'}}
        }
        self.assertEqual(len(results), 2)
        self.assert_called_with('SoftLayer_Account', 'getPrivateBlockDeviceTemplateGroups', filter=_filter)

    def test_list_public_images(self):
        # Casting to list() to force the iter_call generator to run
        results = list(self.image.list_public_images())
        self.assertEqual(len(results), 2)
        self.assert_called_with(IMAGE_SERVICE, 'getPublicImages')

    def test_list_public_images_with_filters(self):
        # Casting to list() to force the iter_call generator to run
        results = list(self.image.list_public_images(guid='0FA9ECBD-CF7E-4A1F-1E36F8D27C2B', name='name'))
        self.assertEqual(len(results), 2)
        _filter = {
            'globalIdentifier': {
                'operation': '_= 0FA9ECBD-CF7E-4A1F-1E36F8D27C2B'},
            'name': {'operation': '_= name'}
        }

        self.assert_called_with(IMAGE_SERVICE, 'getPublicImages', filter=_filter)

    def test_resolve_ids_guid(self):
        result = self.image.resolve_ids('3C1F3C68-0B67-4F5E-8907-D0FC84BF3F12')

        self.assertEqual(['3C1F3C68-0B67-4F5E-8907-D0FC84BF3F12'], result)

    def test_resolve_ids_name_public(self):
        public_mock = self.set_mock(IMAGE_SERVICE, 'getPublicImages')
        public_mock.return_value = [{'id': 100}]
        private_mock = self.set_mock('SoftLayer_Account',
                                     'getPrivateBlockDeviceTemplateGroups')
        private_mock.return_value = []

        self.account.getPrivateBlockDeviceTemplateGroups.return_value = []

        result = self.image.resolve_ids('image_name')
        self.assertEqual([100], result)

    def test_resolve_ids_name_private(self):
        public_mock = self.set_mock(IMAGE_SERVICE, 'getPublicImages')
        public_mock.return_value = []
        private_mock = self.set_mock('SoftLayer_Account',
                                     'getPrivateBlockDeviceTemplateGroups')
        private_mock.return_value = [{'id': 100}]

        result = self.image.resolve_ids('private_image_name')
        self.assertEqual([100], result)

    def test_resolve_ids_not_found(self):
        public_mock = self.set_mock(IMAGE_SERVICE, 'getPublicImages')
        public_mock.return_value = []
        private_mock = self.set_mock('SoftLayer_Account',
                                     'getPrivateBlockDeviceTemplateGroups')
        private_mock.return_value = []

        result = self.image.resolve_ids('unknown_name')
        self.assertEqual([], result)

    def test_edit_tags(self):
        # Test updating tags
        self.image.edit(1, tag="tag1,tag2")

        self.assert_called_with(IMAGE_SERVICE, 'setTags',
                                identifier=1,
                                args=("tag1,tag2",))

    def test_edit_blank(self):
        # Test a blank edit
        result = self.image.edit(1)

        self.assertEqual(result, False)
        self.assertEqual(self.calls(IMAGE_SERVICE, 'setTags'), [])

    def test_edit_full(self):
        # Finally test a full edit
        self.image.edit(1, name='abc', note='xyz')
        self.assert_called_with(IMAGE_SERVICE, 'editObject',
                                identifier=1,
                                args=({'name': 'abc', 'note': 'xyz'},))

    def test_import_image(self):
        self.image.import_image_from_uri(name='test_image',
                                         note='testimage',
                                         uri='someuri',
                                         os_code='UBUNTU_LATEST')

        self.assert_called_with(
            IMAGE_SERVICE,
            'createFromExternalSource',
            args=({'name': 'test_image',
                   'note': 'testimage',
                   'uri': 'someuri',
                   'operatingSystemReferenceCode': 'UBUNTU_LATEST'},))

    def test_import_image_cos(self):
        self.image.import_image_from_uri(name='test_image',
                                         note='testimage',
                                         uri='cos://some_uri',
                                         os_code='UBUNTU_LATEST',
                                         ibm_api_key='some_ibm_key',
                                         root_key_crn='some_root_key_crn',
                                         wrapped_dek='some_dek',
                                         cloud_init=False,
                                         byol=False,
                                         is_encrypted=False
                                         )

        self.assert_called_with(
            IMAGE_SERVICE,
            'createFromIcos',
            args=({'name': 'test_image',
                   'note': 'testimage',
                   'operatingSystemReferenceCode': 'UBUNTU_LATEST',
                   'uri': 'cos://some_uri',
                   'ibmApiKey': 'some_ibm_key',
                   'crkCrn': 'some_root_key_crn',
                   'wrappedDek': 'some_dek',
                   'cloudInit': False,
                   'byol': False,
                   'isEncrypted': False
                   },))

    def test_export_image(self):
        self.image.export_image_to_uri(1234, 'someuri')

        self.assert_called_with(
            IMAGE_SERVICE,
            'copyToExternalSource',
            args=({'uri': 'someuri'},),
            identifier=1234)

    def test_export_image_cos(self):
        self.image.export_image_to_uri(1234,
                                       'cos://someuri',
                                       ibm_api_key='someApiKey')

        self.assert_called_with(
            IMAGE_SERVICE,
            'copyToIcos',
            args=({'uri': 'cos://someuri', 'ibmApiKey': 'someApiKey'},),
            identifier=1234)

    def test_add_locations_image(self):
        locations = ['ams01']
        self.image.add_locations(100, locations)

        self.assert_called_with(IMAGE_SERVICE, 'addLocations', identifier=100)

    def test_add_locations_fail(self):
        locations = ['test']
        self.assertRaises(
            exceptions.SoftLayerError,
            self.image.add_locations,
            100,
            locations
        )

    def test_remove_locations_image(self):
        locations = ['ams01']
        self.image.remove_locations(100, locations)

        self.assert_called_with(IMAGE_SERVICE, 'removeLocations', identifier=100)

    def test_get_locations_id_fails(self):
        locations = ['test']
        self.assertRaises(
            exceptions.SoftLayerError,
            self.image.get_locations_list,
            100,
            locations
        )

    def test_share_image(self):
        result = self.image.share_image(image_id=123456, account_id=654321)

        self.assert_called_with(IMAGE_SERVICE, 'permitSharingAccess', identifier=123456, args=(654321,))
        self.assertEqual(True, result)

    def test_deny_share_image(self):
        result = self.image.deny_share_image(image_id=123456, account_id=654321)

        self.assert_called_with(IMAGE_SERVICE, 'denySharingAccess', identifier=123456, args=(654321,))
        self.assertEqual(True, result)