blob: a56f8374af912bb239cfc14f32e2c76620df1630 [file] [log] [blame]
Matthew Treinish7b015822014-01-21 18:15:39 +00001# 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
15import time
16
17import mock
18
19from tempest.common import waiters
20from tempest import exceptions
Yaroslav Lobankovdb4a2e12015-11-28 20:04:54 +030021from tempest.services.volume.base import base_volumes_client
Matthew Treinishffad78a2016-04-16 14:39:52 -040022from tempest.tests import base
Jordan Pittier0e53b612016-03-03 14:23:17 +010023import tempest.tests.utils as utils
Matthew Treinish7b015822014-01-21 18:15:39 +000024
25
26class TestImageWaiters(base.TestCase):
27 def setUp(self):
28 super(TestImageWaiters, self).setUp()
29 self.client = mock.MagicMock()
30 self.client.build_timeout = 1
31 self.client.build_interval = 1
32
33 def test_wait_for_image_status(self):
Ken'ichi Ohmichi5d410762015-05-22 01:10:03 +000034 self.client.show_image.return_value = ({'status': 'active'})
Matthew Treinish7b015822014-01-21 18:15:39 +000035 start_time = int(time.time())
36 waiters.wait_for_image_status(self.client, 'fake_image_id', 'active')
37 end_time = int(time.time())
38 # Ensure waiter returns before build_timeout
39 self.assertTrue((end_time - start_time) < 10)
40
Yaroslav Lobankov2fea4052016-04-19 15:05:57 +030041 def test_wait_for_image_status_timeout(self):
Jordan Pittier0e53b612016-03-03 14:23:17 +010042 time_mock = self.patch('time.time')
43 time_mock.side_effect = utils.generate_timeout_series(1)
44
Ken'ichi Ohmichi5d410762015-05-22 01:10:03 +000045 self.client.show_image.return_value = ({'status': 'saving'})
Matthew Treinish7b015822014-01-21 18:15:39 +000046 self.assertRaises(exceptions.TimeoutException,
47 waiters.wait_for_image_status,
48 self.client, 'fake_image_id', 'active')
49
Yaroslav Lobankov2fea4052016-04-19 15:05:57 +030050 def test_wait_for_image_status_error_on_image_create(self):
Ken'ichi Ohmichi5d410762015-05-22 01:10:03 +000051 self.client.show_image.return_value = ({'status': 'ERROR'})
Matthew Treinish7b015822014-01-21 18:15:39 +000052 self.assertRaises(exceptions.AddImageException,
53 waiters.wait_for_image_status,
54 self.client, 'fake_image_id', 'active')
Matt Riedemannf77e7dc2015-08-10 16:39:39 -070055
56 @mock.patch.object(time, 'sleep')
57 def test_wait_for_volume_status_error_restoring(self, mock_sleep):
58 # Tests that the wait method raises VolumeRestoreErrorException if
59 # the volume status is 'error_restoring'.
Yaroslav Lobankovdb4a2e12015-11-28 20:04:54 +030060 client = mock.Mock(spec=base_volumes_client.BaseVolumesClient,
Matt Riedemannf77e7dc2015-08-10 16:39:39 -070061 build_interval=1)
John Warren6177c9e2015-08-19 20:00:17 +000062 volume1 = {'volume': {'status': 'restoring-backup'}}
63 volume2 = {'volume': {'status': 'error_restoring'}}
Matt Riedemannf77e7dc2015-08-10 16:39:39 -070064 mock_show = mock.Mock(side_effect=(volume1, volume2))
65 client.show_volume = mock_show
66 volume_id = '7532b91e-aa0a-4e06-b3e5-20c0c5ee1caa'
67 self.assertRaises(exceptions.VolumeRestoreErrorException,
68 waiters.wait_for_volume_status,
69 client, volume_id, 'available')
70 mock_show.assert_has_calls([mock.call(volume_id),
71 mock.call(volume_id)])
72 mock_sleep.assert_called_once_with(1)