|
| 1 | +# Copyright 2016, Google, Inc. |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | + |
| 14 | +import main |
| 15 | +import mock |
| 16 | + |
| 17 | + |
| 18 | +@mock.patch('main.requests') |
| 19 | +def test_wait_for_maintenance(requests_mock): |
| 20 | + # Response 1 is a host maintenance event. |
| 21 | + response1_mock = mock.Mock() |
| 22 | + response1_mock.status_code = 200 |
| 23 | + response1_mock.text = 'MIGRATE_ON_HOST_MAINTENANCE' |
| 24 | + response1_mock.headers = {'etag': 1} |
| 25 | + # Response 2 is the end of the event. |
| 26 | + response2_mock = mock.Mock() |
| 27 | + response2_mock.status_code = 200 |
| 28 | + response2_mock.text = 'NONE' |
| 29 | + response2_mock.headers = {'etag': 2} |
| 30 | + # Response 3 is a 503 |
| 31 | + response3_mock = mock.Mock() |
| 32 | + response3_mock.status_code = 503 |
| 33 | + |
| 34 | + requests_mock.get.side_effect = [ |
| 35 | + response1_mock, response2_mock, response3_mock, response2_mock, |
| 36 | + StopIteration()] |
| 37 | + |
| 38 | + callback_mock = mock.Mock() |
| 39 | + |
| 40 | + try: |
| 41 | + main.wait_for_maintenance(callback_mock) |
| 42 | + except StopIteration: |
| 43 | + pass |
| 44 | + |
| 45 | + assert callback_mock.call_count == 2 |
| 46 | + assert callback_mock.call_args_list[0][0] == (True,) |
| 47 | + assert callback_mock.call_args_list[1][0] == (False,) |
0 commit comments