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 |
guo yunxian | ebb15f2 | 2016-11-01 21:03:35 +0800 | [diff] [blame] | 21 | from tempest.lib import exceptions as lib_exc |
lkuchlan | f53947e | 2016-09-15 10:37:57 +0300 | [diff] [blame] | 22 | from tempest.lib.services.volume.v2 import volumes_client |
Matthew Treinish | ffad78a | 2016-04-16 14:39:52 -0400 | [diff] [blame] | 23 | from tempest.tests import base |
Jordan Pittier | 0e53b61 | 2016-03-03 14:23:17 +0100 | [diff] [blame] | 24 | import tempest.tests.utils as utils |
Matthew Treinish | 7b01582 | 2014-01-21 18:15:39 +0000 | [diff] [blame] | 25 | |
| 26 | |
| 27 | class TestImageWaiters(base.TestCase): |
| 28 | def setUp(self): |
| 29 | super(TestImageWaiters, self).setUp() |
| 30 | self.client = mock.MagicMock() |
| 31 | self.client.build_timeout = 1 |
| 32 | self.client.build_interval = 1 |
| 33 | |
| 34 | def test_wait_for_image_status(self): |
Ken'ichi Ohmichi | 5d41076 | 2015-05-22 01:10:03 +0000 | [diff] [blame] | 35 | self.client.show_image.return_value = ({'status': 'active'}) |
Matthew Treinish | 7b01582 | 2014-01-21 18:15:39 +0000 | [diff] [blame] | 36 | start_time = int(time.time()) |
| 37 | waiters.wait_for_image_status(self.client, 'fake_image_id', 'active') |
| 38 | end_time = int(time.time()) |
| 39 | # Ensure waiter returns before build_timeout |
Béla Vancsics | 64862f7 | 2016-11-08 09:12:31 +0100 | [diff] [blame] | 40 | self.assertLess((end_time - start_time), 10) |
Matthew Treinish | 7b01582 | 2014-01-21 18:15:39 +0000 | [diff] [blame] | 41 | |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 42 | def test_wait_for_image_status_timeout(self): |
Jordan Pittier | 0e53b61 | 2016-03-03 14:23:17 +0100 | [diff] [blame] | 43 | time_mock = self.patch('time.time') |
| 44 | time_mock.side_effect = utils.generate_timeout_series(1) |
| 45 | |
Ken'ichi Ohmichi | 5d41076 | 2015-05-22 01:10:03 +0000 | [diff] [blame] | 46 | self.client.show_image.return_value = ({'status': 'saving'}) |
guo yunxian | ebb15f2 | 2016-11-01 21:03:35 +0800 | [diff] [blame] | 47 | self.assertRaises(lib_exc.TimeoutException, |
Matthew Treinish | 7b01582 | 2014-01-21 18:15:39 +0000 | [diff] [blame] | 48 | waiters.wait_for_image_status, |
| 49 | self.client, 'fake_image_id', 'active') |
| 50 | |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 51 | def test_wait_for_image_status_error_on_image_create(self): |
Ken'ichi Ohmichi | 5d41076 | 2015-05-22 01:10:03 +0000 | [diff] [blame] | 52 | self.client.show_image.return_value = ({'status': 'ERROR'}) |
Matthew Treinish | 7b01582 | 2014-01-21 18:15:39 +0000 | [diff] [blame] | 53 | self.assertRaises(exceptions.AddImageException, |
| 54 | waiters.wait_for_image_status, |
| 55 | self.client, 'fake_image_id', 'active') |
Matt Riedemann | f77e7dc | 2015-08-10 16:39:39 -0700 | [diff] [blame] | 56 | |
| 57 | @mock.patch.object(time, 'sleep') |
| 58 | def test_wait_for_volume_status_error_restoring(self, mock_sleep): |
| 59 | # Tests that the wait method raises VolumeRestoreErrorException if |
| 60 | # the volume status is 'error_restoring'. |
lkuchlan | f53947e | 2016-09-15 10:37:57 +0300 | [diff] [blame] | 61 | client = mock.Mock(spec=volumes_client.VolumesClient, |
Matt Riedemann | f77e7dc | 2015-08-10 16:39:39 -0700 | [diff] [blame] | 62 | build_interval=1) |
John Warren | 6177c9e | 2015-08-19 20:00:17 +0000 | [diff] [blame] | 63 | volume1 = {'volume': {'status': 'restoring-backup'}} |
| 64 | volume2 = {'volume': {'status': 'error_restoring'}} |
Matt Riedemann | f77e7dc | 2015-08-10 16:39:39 -0700 | [diff] [blame] | 65 | mock_show = mock.Mock(side_effect=(volume1, volume2)) |
| 66 | client.show_volume = mock_show |
| 67 | volume_id = '7532b91e-aa0a-4e06-b3e5-20c0c5ee1caa' |
| 68 | self.assertRaises(exceptions.VolumeRestoreErrorException, |
lkuchlan | 52d7b0d | 2016-11-07 20:53:19 +0200 | [diff] [blame] | 69 | waiters.wait_for_volume_resource_status, |
Matt Riedemann | f77e7dc | 2015-08-10 16:39:39 -0700 | [diff] [blame] | 70 | client, volume_id, 'available') |
| 71 | mock_show.assert_has_calls([mock.call(volume_id), |
| 72 | mock.call(volume_id)]) |
| 73 | mock_sleep.assert_called_once_with(1) |