Matthew Treinish | 7b01582 | 2014-01-21 18:15:39 +0000 | [diff] [blame] | 1 | # Copyright 2014 IBM Corp. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | import time |
| 16 | |
| 17 | import mock |
| 18 | |
| 19 | from tempest.common import waiters |
| 20 | from tempest import exceptions |
Matt Riedemann | f77e7dc | 2015-08-10 16:39:39 -0700 | [diff] [blame] | 21 | from tempest.services.volume.json import volumes_client |
Matthew Treinish | 7b01582 | 2014-01-21 18:15:39 +0000 | [diff] [blame] | 22 | from tempest.tests import base |
| 23 | |
| 24 | |
| 25 | class TestImageWaiters(base.TestCase): |
| 26 | def setUp(self): |
| 27 | super(TestImageWaiters, self).setUp() |
| 28 | self.client = mock.MagicMock() |
| 29 | self.client.build_timeout = 1 |
| 30 | self.client.build_interval = 1 |
| 31 | |
| 32 | def test_wait_for_image_status(self): |
Ken'ichi Ohmichi | 5d41076 | 2015-05-22 01:10:03 +0000 | [diff] [blame] | 33 | self.client.show_image.return_value = ({'status': 'active'}) |
Matthew Treinish | 7b01582 | 2014-01-21 18:15:39 +0000 | [diff] [blame] | 34 | start_time = int(time.time()) |
| 35 | waiters.wait_for_image_status(self.client, 'fake_image_id', 'active') |
| 36 | end_time = int(time.time()) |
| 37 | # Ensure waiter returns before build_timeout |
| 38 | self.assertTrue((end_time - start_time) < 10) |
| 39 | |
| 40 | def test_wait_for_image_status_timeout(self): |
Ken'ichi Ohmichi | 5d41076 | 2015-05-22 01:10:03 +0000 | [diff] [blame] | 41 | self.client.show_image.return_value = ({'status': 'saving'}) |
Matthew Treinish | 7b01582 | 2014-01-21 18:15:39 +0000 | [diff] [blame] | 42 | self.assertRaises(exceptions.TimeoutException, |
| 43 | waiters.wait_for_image_status, |
| 44 | self.client, 'fake_image_id', 'active') |
| 45 | |
| 46 | def test_wait_for_image_status_error_on_image_create(self): |
Ken'ichi Ohmichi | 5d41076 | 2015-05-22 01:10:03 +0000 | [diff] [blame] | 47 | self.client.show_image.return_value = ({'status': 'ERROR'}) |
Matthew Treinish | 7b01582 | 2014-01-21 18:15:39 +0000 | [diff] [blame] | 48 | self.assertRaises(exceptions.AddImageException, |
| 49 | waiters.wait_for_image_status, |
| 50 | self.client, 'fake_image_id', 'active') |
Matt Riedemann | f77e7dc | 2015-08-10 16:39:39 -0700 | [diff] [blame] | 51 | |
| 52 | @mock.patch.object(time, 'sleep') |
| 53 | def test_wait_for_volume_status_error_restoring(self, mock_sleep): |
| 54 | # Tests that the wait method raises VolumeRestoreErrorException if |
| 55 | # the volume status is 'error_restoring'. |
| 56 | client = mock.Mock(spec=volumes_client.BaseVolumesClient, |
| 57 | build_interval=1) |
John Warren | 6177c9e | 2015-08-19 20:00:17 +0000 | [diff] [blame^] | 58 | volume1 = {'volume': {'status': 'restoring-backup'}} |
| 59 | volume2 = {'volume': {'status': 'error_restoring'}} |
Matt Riedemann | f77e7dc | 2015-08-10 16:39:39 -0700 | [diff] [blame] | 60 | mock_show = mock.Mock(side_effect=(volume1, volume2)) |
| 61 | client.show_volume = mock_show |
| 62 | volume_id = '7532b91e-aa0a-4e06-b3e5-20c0c5ee1caa' |
| 63 | self.assertRaises(exceptions.VolumeRestoreErrorException, |
| 64 | waiters.wait_for_volume_status, |
| 65 | client, volume_id, 'available') |
| 66 | mock_show.assert_has_calls([mock.call(volume_id), |
| 67 | mock.call(volume_id)]) |
| 68 | mock_sleep.assert_called_once_with(1) |