|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | + |
| 13 | +import mock |
| 14 | +import testtools |
| 15 | + |
| 16 | +from openstack.database.v1 import instance |
| 17 | + |
| 18 | +IDENTIFIER = 'IDENTIFIER' |
| 19 | +EXAMPLE = { |
| 20 | + 'flavor': '1', |
| 21 | + 'id': IDENTIFIER, |
| 22 | + 'links': '3', |
| 23 | + 'name': '4', |
| 24 | + 'status': '5', |
| 25 | + 'volume': '6', |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +class TestInstance(testtools.TestCase): |
| 30 | + |
| 31 | + def test_basic(self): |
| 32 | + sot = instance.Instance() |
| 33 | + self.assertEqual('instance', sot.resource_key) |
| 34 | + self.assertEqual('instances', sot.resources_key) |
| 35 | + self.assertEqual('/instances', sot.base_path) |
| 36 | + self.assertEqual('database', sot.service.service_type) |
| 37 | + self.assertTrue(sot.allow_create) |
| 38 | + self.assertTrue(sot.allow_retrieve) |
| 39 | + self.assertTrue(sot.allow_update) |
| 40 | + self.assertTrue(sot.allow_delete) |
| 41 | + self.assertTrue(sot.allow_list) |
| 42 | + |
| 43 | + def test_make_it(self): |
| 44 | + sot = instance.Instance(EXAMPLE) |
| 45 | + self.assertEqual(EXAMPLE['flavor'], sot.flavor) |
| 46 | + self.assertEqual(EXAMPLE['id'], sot.id) |
| 47 | + self.assertEqual(EXAMPLE['links'], sot.links) |
| 48 | + self.assertEqual(EXAMPLE['name'], sot.name) |
| 49 | + self.assertEqual(EXAMPLE['status'], sot.status) |
| 50 | + self.assertEqual(EXAMPLE['volume'], sot.volume) |
| 51 | + |
| 52 | + def test_enable_root_user(self): |
| 53 | + sot = instance.Instance(EXAMPLE) |
| 54 | + response = mock.Mock() |
| 55 | + response.body = {'user': {'name': 'root', 'password': 'foo'}} |
| 56 | + sess = mock.Mock() |
| 57 | + sess.post = mock.MagicMock() |
| 58 | + sess.post.return_value = response |
| 59 | + |
| 60 | + self.assertEqual(response.body['user'], sot.enable_root_user(sess)) |
| 61 | + |
| 62 | + url = ("instances/%s/root" % IDENTIFIER) |
| 63 | + sess.post.assert_called_with(url, service=sot.service) |
| 64 | + |
| 65 | + def test_is_root_enabled(self): |
| 66 | + sot = instance.Instance(EXAMPLE) |
| 67 | + response = mock.Mock() |
| 68 | + response.body = {'rootEnabled': True} |
| 69 | + sess = mock.Mock() |
| 70 | + sess.get = mock.MagicMock() |
| 71 | + sess.get.return_value = response |
| 72 | + |
| 73 | + self.assertEqual(True, sot.is_root_enabled(sess)) |
| 74 | + |
| 75 | + url = ("instances/%s/root" % IDENTIFIER) |
| 76 | + sess.get.assert_called_with(url, service=sot.service) |
| 77 | + |
| 78 | + def test_action_restart(self): |
| 79 | + sot = instance.Instance(EXAMPLE) |
| 80 | + response = mock.Mock() |
| 81 | + response.body = '' |
| 82 | + sess = mock.Mock() |
| 83 | + sess.post = mock.MagicMock() |
| 84 | + sess.post.return_value = response |
| 85 | + |
| 86 | + self.assertEqual(None, sot.restart(sess)) |
| 87 | + |
| 88 | + url = ("instances/%s/action" % IDENTIFIER) |
| 89 | + body = {'restart': {}} |
| 90 | + sess.post.assert_called_with(url, service=sot.service, json=body) |
| 91 | + |
| 92 | + def test_action_resize(self): |
| 93 | + sot = instance.Instance(EXAMPLE) |
| 94 | + response = mock.Mock() |
| 95 | + response.body = '' |
| 96 | + sess = mock.Mock() |
| 97 | + sess.post = mock.MagicMock() |
| 98 | + sess.post.return_value = response |
| 99 | + flavor = 'http://flavor/flav' |
| 100 | + |
| 101 | + self.assertEqual(None, sot.resize(sess, flavor)) |
| 102 | + |
| 103 | + url = ("instances/%s/action" % IDENTIFIER) |
| 104 | + body = {'resize': {'flavorRef': flavor}} |
| 105 | + sess.post.assert_called_with(url, service=sot.service, json=body) |
| 106 | + |
| 107 | + def test_action_resize_volume(self): |
| 108 | + sot = instance.Instance(EXAMPLE) |
| 109 | + response = mock.Mock() |
| 110 | + response.body = '' |
| 111 | + sess = mock.Mock() |
| 112 | + sess.post = mock.MagicMock() |
| 113 | + sess.post.return_value = response |
| 114 | + size = 4 |
| 115 | + |
| 116 | + self.assertEqual(None, sot.resize_volume(sess, size)) |
| 117 | + |
| 118 | + url = ("instances/%s/action" % IDENTIFIER) |
| 119 | + body = {'resize': {'volume': size}} |
| 120 | + sess.post.assert_called_with(url, service=sot.service, json=body) |
0 commit comments