~ubuntu-branches/ubuntu/vivid/ironic/vivid-updates

« back to all changes in this revision

Viewing changes to ironic/tests/test_image_service.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2015-04-17 09:28:31 UTC
  • mfrom: (1.2.7)
  • Revision ID: package-import@ubuntu.com-20150417092831-wu2awfbqomnzpeim
Tags: 2015.1~rc1-0ubuntu1
* New upstream milestone release:
  - d/control: Align with upstream dependencies
  - d/p/fix-requirements.patch: Dropped no longer needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
        self.service = image_service.HttpImageService()
31
31
        self.href = 'http://127.0.0.1:12345/fedora.qcow2'
32
32
 
33
 
    @mock.patch.object(requests, 'head')
 
33
    @mock.patch.object(requests, 'head', autospec=True)
34
34
    def test_validate_href(self, head_mock):
35
35
        response = head_mock.return_value
36
36
        response.status_code = 200
45
45
                          self.service.validate_href,
46
46
                          self.href)
47
47
 
48
 
    @mock.patch.object(requests, 'head')
 
48
    @mock.patch.object(requests, 'head', autospec=True)
49
49
    def test_validate_href_error_code(self, head_mock):
50
50
        head_mock.return_value.status_code = 400
51
51
        self.assertRaises(exception.ImageRefValidationFailed,
52
52
                          self.service.validate_href, self.href)
53
53
        head_mock.assert_called_once_with(self.href)
54
54
 
55
 
    @mock.patch.object(requests, 'head')
 
55
    @mock.patch.object(requests, 'head', autospec=True)
56
56
    def test_validate_href_error(self, head_mock):
57
57
        head_mock.side_effect = requests.ConnectionError()
58
58
        self.assertRaises(exception.ImageRefValidationFailed,
59
59
                          self.service.validate_href, self.href)
60
60
        head_mock.assert_called_once_with(self.href)
61
61
 
62
 
    @mock.patch.object(requests, 'head')
 
62
    @mock.patch.object(requests, 'head', autospec=True)
63
63
    def test_show(self, head_mock):
64
64
        head_mock.return_value.status_code = 200
65
65
        result = self.service.show(self.href)
66
66
        head_mock.assert_called_with(self.href)
67
67
        self.assertEqual({'size': 1, 'properties': {}}, result)
68
68
 
69
 
    @mock.patch.object(requests, 'head')
 
69
    @mock.patch.object(requests, 'head', autospec=True)
70
70
    def test_show_no_content_length(self, head_mock):
71
71
        head_mock.return_value.status_code = 200
72
72
        head_mock.return_value.headers = {}
74
74
                          self.service.show, self.href)
75
75
        head_mock.assert_called_with(self.href)
76
76
 
77
 
    @mock.patch.object(shutil, 'copyfileobj')
78
 
    @mock.patch.object(requests, 'get')
 
77
    @mock.patch.object(shutil, 'copyfileobj', autospec=True)
 
78
    @mock.patch.object(requests, 'get', autospec=True)
79
79
    def test_download_success(self, req_get_mock, shutil_mock):
80
80
        response_mock = req_get_mock.return_value
81
81
        response_mock.status_code = 200
88
88
        )
89
89
        req_get_mock.assert_called_once_with(self.href, stream=True)
90
90
 
91
 
    @mock.patch.object(requests, 'get',
 
91
    @mock.patch.object(requests, 'get', autospec=True,
92
92
                       side_effect=requests.ConnectionError())
93
93
    def test_download_fail_connerror(self, req_get_mock):
94
94
        file_mock = mock.Mock(spec=file)
95
95
        self.assertRaises(exception.ImageDownloadFailed,
96
96
                          self.service.download, self.href, file_mock)
97
97
 
98
 
    @mock.patch.object(shutil, 'copyfileobj')
99
 
    @mock.patch.object(requests, 'get')
 
98
    @mock.patch.object(shutil, 'copyfileobj', autospec=True)
 
99
    @mock.patch.object(requests, 'get', autospec=True)
100
100
    def test_download_fail_ioerror(self, req_get_mock, shutil_mock):
101
101
        response_mock = req_get_mock.return_value
102
102
        response_mock.status_code = 200
115
115
        self.href = 'file:///home/user/image.qcow2'
116
116
        self.href_path = '/home/user/image.qcow2'
117
117
 
118
 
    @mock.patch.object(os.path, 'isfile', return_value=True)
 
118
    @mock.patch.object(os.path, 'isfile', return_value=True, autospec=True)
119
119
    def test_validate_href(self, path_exists_mock):
120
120
        self.service.validate_href(self.href)
121
121
        path_exists_mock.assert_called_once_with(self.href_path)
122
122
 
123
 
    @mock.patch.object(os.path, 'isfile', return_value=False)
 
123
    @mock.patch.object(os.path, 'isfile', return_value=False, autospec=True)
124
124
    def test_validate_href_path_not_found_or_not_file(self, path_exists_mock):
125
125
        self.assertRaises(exception.ImageRefValidationFailed,
126
126
                          self.service.validate_href, self.href)
127
127
        path_exists_mock.assert_called_once_with(self.href_path)
128
128
 
129
 
    @mock.patch.object(os.path, 'getsize', return_value=42)
130
 
    @mock.patch.object(image_service.FileImageService, 'validate_href')
 
129
    @mock.patch.object(os.path, 'getsize', return_value=42, autospec=True)
 
130
    @mock.patch.object(image_service.FileImageService, 'validate_href',
 
131
                       autospec=True)
131
132
    def test_show(self, _validate_mock, getsize_mock):
132
133
        _validate_mock.return_value = self.href_path
133
134
        result = self.service.show(self.href)
134
135
        getsize_mock.assert_called_once_with(self.href_path)
135
 
        _validate_mock.assert_called_once_with(self.href)
 
136
        _validate_mock.assert_called_once_with(mock.ANY, self.href)
136
137
        self.assertEqual({'size': 42, 'properties': {}}, result)
137
138
 
138
 
    @mock.patch.object(os, 'link')
139
 
    @mock.patch.object(os, 'remove')
140
 
    @mock.patch.object(os, 'access', return_value=True)
141
 
    @mock.patch.object(os, 'stat')
142
 
    @mock.patch.object(image_service.FileImageService, 'validate_href')
 
139
    @mock.patch.object(os, 'link', autospec=True)
 
140
    @mock.patch.object(os, 'remove', autospec=True)
 
141
    @mock.patch.object(os, 'access', return_value=True, autospec=True)
 
142
    @mock.patch.object(os, 'stat', autospec=True)
 
143
    @mock.patch.object(image_service.FileImageService, 'validate_href',
 
144
                       autospec=True)
143
145
    def test_download_hard_link(self, _validate_mock, stat_mock, access_mock,
144
146
                                remove_mock, link_mock):
145
147
        _validate_mock.return_value = self.href_path
147
149
        file_mock = mock.Mock(spec=file)
148
150
        file_mock.name = 'file'
149
151
        self.service.download(self.href, file_mock)
150
 
        _validate_mock.assert_called_once_with(self.href)
 
152
        _validate_mock.assert_called_once_with(mock.ANY, self.href)
151
153
        self.assertEqual(2, stat_mock.call_count)
152
154
        access_mock.assert_called_once_with(self.href_path, os.R_OK | os.W_OK)
153
155
        remove_mock.assert_called_once_with('file')
154
156
        link_mock.assert_called_once_with(self.href_path, 'file')
155
157
 
156
 
    @mock.patch.object(sendfile, 'sendfile')
157
 
    @mock.patch.object(os.path, 'getsize', return_value=42)
158
 
    @mock.patch.object(__builtin__, 'open')
159
 
    @mock.patch.object(os, 'access', return_value=False)
160
 
    @mock.patch.object(os, 'stat')
161
 
    @mock.patch.object(image_service.FileImageService, 'validate_href')
 
158
    @mock.patch.object(sendfile, 'sendfile', autospec=True)
 
159
    @mock.patch.object(os.path, 'getsize', return_value=42, autospec=True)
 
160
    @mock.patch.object(__builtin__, 'open', autospec=True)
 
161
    @mock.patch.object(os, 'access', return_value=False, autospec=True)
 
162
    @mock.patch.object(os, 'stat', autospec=True)
 
163
    @mock.patch.object(image_service.FileImageService, 'validate_href',
 
164
                       autospec=True)
162
165
    def test_download_copy(self, _validate_mock, stat_mock, access_mock,
163
166
                           open_mock, size_mock, copy_mock):
164
167
        _validate_mock.return_value = self.href_path
167
170
        input_mock = mock.MagicMock(spec=file)
168
171
        open_mock.return_value = input_mock
169
172
        self.service.download(self.href, file_mock)
170
 
        _validate_mock.assert_called_once_with(self.href)
 
173
        _validate_mock.assert_called_once_with(mock.ANY, self.href)
171
174
        self.assertEqual(2, stat_mock.call_count)
172
175
        access_mock.assert_called_once_with(self.href_path, os.R_OK | os.W_OK)
173
176
        copy_mock.assert_called_once_with(file_mock.fileno(),
175
178
                                          0, 42)
176
179
        size_mock.assert_called_once_with(self.href_path)
177
180
 
178
 
    @mock.patch.object(os, 'remove', side_effect=OSError)
179
 
    @mock.patch.object(os, 'access', return_value=True)
180
 
    @mock.patch.object(os, 'stat')
181
 
    @mock.patch.object(image_service.FileImageService, 'validate_href')
 
181
    @mock.patch.object(os, 'remove', side_effect=OSError, autospec=True)
 
182
    @mock.patch.object(os, 'access', return_value=True, autospec=True)
 
183
    @mock.patch.object(os, 'stat', autospec=True)
 
184
    @mock.patch.object(image_service.FileImageService, 'validate_href',
 
185
                       autospec=True)
182
186
    def test_download_hard_link_fail(self, _validate_mock, stat_mock,
183
187
                                     access_mock, remove_mock):
184
188
        _validate_mock.return_value = self.href_path
187
191
        file_mock.name = 'file'
188
192
        self.assertRaises(exception.ImageDownloadFailed,
189
193
                          self.service.download, self.href, file_mock)
190
 
        _validate_mock.assert_called_once_with(self.href)
 
194
        _validate_mock.assert_called_once_with(mock.ANY, self.href)
191
195
        self.assertEqual(2, stat_mock.call_count)
192
196
        access_mock.assert_called_once_with(self.href_path, os.R_OK | os.W_OK)
193
197
 
194
 
    @mock.patch.object(sendfile, 'sendfile', side_effect=OSError)
195
 
    @mock.patch.object(os.path, 'getsize', return_value=42)
196
 
    @mock.patch.object(__builtin__, 'open')
197
 
    @mock.patch.object(os, 'access', return_value=False)
198
 
    @mock.patch.object(os, 'stat')
199
 
    @mock.patch.object(image_service.FileImageService, 'validate_href')
 
198
    @mock.patch.object(sendfile, 'sendfile', side_effect=OSError,
 
199
                       autospec=True)
 
200
    @mock.patch.object(os.path, 'getsize', return_value=42, autospec=True)
 
201
    @mock.patch.object(__builtin__, 'open', autospec=True)
 
202
    @mock.patch.object(os, 'access', return_value=False, autospec=True)
 
203
    @mock.patch.object(os, 'stat', autospec=True)
 
204
    @mock.patch.object(image_service.FileImageService, 'validate_href',
 
205
                       autospec=True)
200
206
    def test_download_copy_fail(self, _validate_mock, stat_mock, access_mock,
201
207
                                open_mock, size_mock, copy_mock):
202
208
        _validate_mock.return_value = self.href_path
206
212
        open_mock.return_value = input_mock
207
213
        self.assertRaises(exception.ImageDownloadFailed,
208
214
                          self.service.download, self.href, file_mock)
209
 
        _validate_mock.assert_called_once_with(self.href)
 
215
        _validate_mock.assert_called_once_with(mock.ANY, self.href)
210
216
        self.assertEqual(2, stat_mock.call_count)
211
217
        access_mock.assert_called_once_with(self.href_path, os.R_OK | os.W_OK)
212
218
        size_mock.assert_called_once_with(self.href_path)
215
221
class ServiceGetterTestCase(base.TestCase):
216
222
 
217
223
    @mock.patch.object(glance_v1_service.GlanceImageService, '__init__',
218
 
                       return_value=None)
 
224
                       return_value=None, autospec=True)
219
225
    def test_get_glance_image_service(self, glance_service_mock):
220
226
        image_href = 'image-uuid'
221
227
        image_service.get_image_service(image_href, context=self.context)
222
 
        glance_service_mock.assert_called_once_with(None, 1, self.context)
 
228
        glance_service_mock.assert_called_once_with(mock.ANY, None, 1,
 
229
                                                    self.context)
223
230
 
224
231
    @mock.patch.object(glance_v1_service.GlanceImageService, '__init__',
225
 
                       return_value=None)
 
232
                       return_value=None, autospec=True)
226
233
    def test_get_glance_image_service_url(self, glance_service_mock):
227
234
        image_href = 'glance://image-uuid'
228
235
        image_service.get_image_service(image_href, context=self.context)
229
 
        glance_service_mock.assert_called_once_with(None, 1, self.context)
 
236
        glance_service_mock.assert_called_once_with(mock.ANY, None, 1,
 
237
                                                    self.context)
230
238
 
231
239
    @mock.patch.object(image_service.HttpImageService, '__init__',
232
 
                       return_value=None)
 
240
                       return_value=None, autospec=True)
233
241
    def test_get_http_image_service(self, http_service_mock):
234
242
        image_href = 'http://127.0.0.1/image.qcow2'
235
243
        image_service.get_image_service(image_href)
236
244
        http_service_mock.assert_called_once_with()
237
245
 
238
246
    @mock.patch.object(image_service.HttpImageService, '__init__',
239
 
                       return_value=None)
 
247
                       return_value=None, autospec=True)
240
248
    def test_get_https_image_service(self, http_service_mock):
241
249
        image_href = 'https://127.0.0.1/image.qcow2'
242
250
        image_service.get_image_service(image_href)
243
251
        http_service_mock.assert_called_once_with()
244
252
 
245
253
    @mock.patch.object(image_service.FileImageService, '__init__',
246
 
                       return_value=None)
 
254
                       return_value=None, autospec=True)
247
255
    def test_get_file_image_service(self, local_service_mock):
248
256
        image_href = 'file:///home/user/image.qcow2'
249
257
        image_service.get_image_service(image_href)