Add unit tests for guestagent.volume.py

- more clean up on test_manager.py
- Migrate guestagent tests to unittest folder
- modify guestagent.manager.py so it recognize '_'

Bug # 1090139

Change-Id: I718a5212cd07c8bdb4e71796ba384574ce8275e9
This commit is contained in:
ruiyuan-shen 2013-01-15 11:26:42 -08:00
parent 4eb7e34ca9
commit 49bf821843
8 changed files with 200 additions and 43 deletions

View File

@ -2,7 +2,7 @@ from reddwarf.guestagent import dbaas
from reddwarf.guestagent import volume
from reddwarf.openstack.common import log as logging
from reddwarf.openstack.common import periodic_task
from reddwarf.openstack.common.gettextutils import _
LOG = logging.getLogger(__name__)
MYSQL_BASE_DIR = "/var/lib/mysql"

View File

@ -1,13 +0,0 @@
# Copyright 2011 OpenStack LLC
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

View File

@ -18,10 +18,7 @@ from reddwarf.guestagent import volume
import testtools
from mock import Mock, MagicMock
from proboscis import test
@test(groups=["dbaas.guestagent.dbaas"])
class GuestAgentManagerTest(testtools.TestCase):
def setUp(self):
@ -93,9 +90,6 @@ class GuestAgentManagerTest(testtools.TestCase):
def test_prepare_device_path_false(self):
self._prepare_dynamic(has_device_path=False)
def test_prepare_mysql_installed(self):
self._prepare_dynamic(has_device_path=False)
def test_prepare_mysql_not_installed(self):
self._prepare_dynamic(is_mysql_installed=False)
@ -113,13 +107,13 @@ class GuestAgentManagerTest(testtools.TestCase):
self._setUp_MySqlAppStatus_get()
dbaas.MySqlAppStatus.begin_mysql_install = MagicMock()
origin_format = volume.VolumeDevice.format
volume.VolumeDevice.format = MagicMock()
if is_mysql_installed:
self._prepare_mysql_is_installed()
else:
self._prepare_mysql_is_not_installed()
origin_is_installed, origin_stop_mysql, origin_migrate_data =\
self._prepare_mysql_is_installed(is_mysql_installed)
origin_mount = volume.VolumeDevice.mount
volume.VolumeDevice.mount = MagicMock()
dbaas.MySqlApp.start_mysql = MagicMock()
@ -148,13 +142,20 @@ class GuestAgentManagerTest(testtools.TestCase):
self.assertEqual(1, Manager.create_database.call_count)
self.assertEqual(1, Manager.create_user.call_count)
def _prepare_mysql_is_installed(self):
dbaas.MySqlApp.is_installed = MagicMock(return_value=True)
volume.VolumeDevice.format = origin_format
volume.VolumeDevice.migrate_data = origin_migrate_data
dbaas.MySqlApp.is_installed = origin_is_installed
dbaas.MySqlApp.stop_mysql = origin_stop_mysql
volume.VolumeDevice.mount = origin_mount
def _prepare_mysql_is_installed(self, is_installed=True):
origin_is_installed = dbaas.MySqlApp.is_installed
origin_stop_mysql = dbaas.MySqlApp.stop_mysql
origin_migrate_data = volume.VolumeDevice.migrate_data
dbaas.MySqlApp.is_installed = MagicMock(return_value=is_installed)
dbaas.MySqlApp.stop_mysql = MagicMock()
volume.VolumeDevice.migrate_data = MagicMock()
def _prepare_mysql_is_not_installed(self):
dbaas.MySqlApp.is_installed = MagicMock(return_value=False)
return origin_is_installed, origin_stop_mysql, origin_migrate_data
def test_restart(self):
self._setUp_MySqlAppStatus_get()

View File

@ -16,14 +16,11 @@ import testtools
from mock import Mock, MagicMock
from reddwarf.guestagent import models
from reddwarf.common import utils
from reddwarf.db import sqlalchemy
from reddwarf.db.sqlalchemy import api as dbapi
from reddwarf.db import models as dbmodels
from proboscis import test
from datetime import datetime
@test(groups=["dbaas.guestagent.dbaas"])
class AgentHeartBeatTest(testtools.TestCase):
def setUp(self):
super(AgentHeartBeatTest, self).setUp()
@ -33,7 +30,7 @@ class AgentHeartBeatTest(testtools.TestCase):
def test_create(self):
utils.generate_uuid = Mock()
sqlalchemy.api.save = MagicMock(
dbapi.save = MagicMock(
return_value=dbmodels.DatabaseModelBase)
dbmodels.DatabaseModelBase.is_valid = Mock(return_value=True)
models.AgentHeartBeat.create()
@ -46,7 +43,7 @@ class AgentHeartBeatTest(testtools.TestCase):
dbmodels.DatabaseModelBase = Mock
dbmodels.get_db_api = MagicMock(
return_value=dbmodels.DatabaseModelBase)
sqlalchemy.api.save = Mock()
dbapi.save = Mock()
dbmodels.DatabaseModelBase.is_valid = Mock(return_value=True)
self.heartBeat = models.AgentHeartBeat()
self.heartBeat.save()

View File

@ -14,10 +14,8 @@
import testtools
from reddwarf.guestagent import query
from proboscis import test
@test(groups=["dbaas.guestagent.dbaas"])
class QueryTest(testtools.TestCase):
def setUp(self):
super(QueryTest, self).setUp()

View File

@ -15,10 +15,8 @@
import testtools
from mock import Mock, MagicMock
from reddwarf.guestagent import service
from proboscis import test
@test(groups=["dbaas.guestagent.dbaas"])
class ServiceTest(testtools.TestCase):
def setUp(self):
super(ServiceTest, self).setUp()

View File

@ -0,0 +1,180 @@
# Copyright 2012 OpenStack LLC
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License
import os
import testtools
import pexpect
from mock import Mock, MagicMock
from reddwarf.guestagent import volume
from reddwarf.common import utils
def _setUp_fake_spawn(return_val=0):
fake_spawn = pexpect.spawn('echo')
fake_spawn.expect = Mock(return_value=return_val)
pexpect.spawn = Mock(return_value=fake_spawn)
return fake_spawn
class VolumeDeviceTest(testtools.TestCase):
def setUp(self):
super(VolumeDeviceTest, self).setUp()
self.volumeDevice = volume.VolumeDevice('/dev/vdb')
def tearDown(self):
super(VolumeDeviceTest, self).tearDown()
def test_migrate_data(self):
origin_execute = utils.execute
utils.execute = Mock()
origin_tmp_mount = self.volumeDevice._tmp_mount
origin_unmount = self.volumeDevice.unmount
self.volumeDevice._tmp_mount = MagicMock()
self.volumeDevice.unmount = MagicMock()
self.volumeDevice.migrate_data('/')
self.assertEqual(2, utils.execute.call_count)
self.assertEqual(1, self.volumeDevice._tmp_mount.call_count)
self.assertEqual(1, self.volumeDevice.unmount.call_count)
utils.execute = origin_execute
self.volumeDevice._tmp_mount = origin_tmp_mount
self.volumeDevice.unmount = origin_unmount
def test__check_device_exists(self):
origin_execute = utils.execute
utils.execute = Mock()
self.volumeDevice._check_device_exists()
self.assertEqual(1, utils.execute.call_count)
utils.execute = origin_execute
def test__check_format(self):
fake_spawn = _setUp_fake_spawn()
self.volumeDevice._check_format()
self.assertEqual(1, fake_spawn.expect.call_count)
def test__check_format_2(self):
fake_spawn = _setUp_fake_spawn(return_val=1)
self.assertRaises(IOError, self.volumeDevice._check_format)
def test__format(self):
fake_spawn = _setUp_fake_spawn()
self.volumeDevice._format()
self.assertEqual(1, fake_spawn.expect.call_count)
self.assertEqual(1, pexpect.spawn.call_count)
def test_format(self):
origin_check_device_exists = self.volumeDevice._check_device_exists
origin_format = self.volumeDevice._format
origin_check_format = self.volumeDevice._check_format
self.volumeDevice._check_device_exists = MagicMock()
self.volumeDevice._check_format = MagicMock()
self.volumeDevice._format = MagicMock()
self.volumeDevice.format()
self.assertEqual(1, self.volumeDevice._check_device_exists.call_count)
self.assertEqual(1, self.volumeDevice._format.call_count)
self.assertEqual(1, self.volumeDevice._check_format.call_count)
self.volumeDevice._check_device_exists = origin_check_device_exists
self.volumeDevice._format = origin_format
self.volumeDevice._check_format = origin_check_format
def test_mount(self):
origin_ = volume.VolumeMountPoint.mount
volume.VolumeMountPoint.mount = Mock()
origin_write_to_fstab = volume.VolumeMountPoint.write_to_fstab
volume.VolumeMountPoint.write_to_fstab = Mock()
self.volumeDevice.mount(Mock)
self.assertEqual(1, volume.VolumeMountPoint.mount.call_count)
self.assertEqual(1, volume.VolumeMountPoint.write_to_fstab.call_count)
volume.VolumeMountPoint.mount = origin_
volume.VolumeMountPoint.write_to_fstab = origin_write_to_fstab
def test_resize_fs(self):
origin_check_device_exists = self.volumeDevice._check_device_exists
origin_execute = utils.execute
utils.execute = Mock()
self.volumeDevice._check_device_exists = MagicMock()
self.volumeDevice.resize_fs()
self.assertEqual(1, self.volumeDevice._check_device_exists.call_count)
self.assertEqual(1, utils.execute.call_count)
self.volumeDevice._check_device_exists = origin_check_device_exists
utils.execute = origin_execute
def test__tmp_mount(self):
origin_ = volume.VolumeMountPoint.mount
volume.VolumeMountPoint.mount = Mock()
self.volumeDevice._tmp_mount(Mock)
self.assertEqual(1, volume.VolumeMountPoint.mount.call_count)
volume.VolumeMountPoint.mount = origin_
def test_unmount_positive(self):
self._test_unmount()
def test_unmount_negative(self):
self._test_unmount(False)
def _test_unmount(self, positive=True):
origin_ = os.path.exists
os.path.exists = MagicMock(return_value=positive)
fake_spawn = _setUp_fake_spawn()
self.volumeDevice.unmount()
COUNT = 1
if not positive:
COUNT = 0
self.assertEqual(COUNT, fake_spawn.expect.call_count)
os.path.exists = origin_
class VolumeMountPointTest(testtools.TestCase):
def setUp(self):
super(VolumeMountPointTest, self).setUp()
self.volumeMountPoint = volume.VolumeMountPoint('/mnt/device',
'/dev/vdb')
def tearDown(self):
super(VolumeMountPointTest, self).tearDown()
def test_mount(self):
origin_ = os.path.exists
os.path.exists = MagicMock(return_value=False)
fake_spawn = _setUp_fake_spawn()
os.makedirs = MagicMock()
self.volumeMountPoint.mount()
self.assertEqual(1, os.path.exists.call_count)
self.assertEqual(1, os.makedirs.call_count)
self.assertEqual(1, fake_spawn.expect.call_count)
os.path.exists = origin_
def test_write_to_fstab(self):
origin_execute = utils.execute
utils.execute = Mock()
open = MagicMock()
self.volumeMountPoint.write_to_fstab()
self.assertEqual(5, utils.execute.call_count)
utils.execute = origin_execute

View File

@ -123,9 +123,5 @@ if __name__=="__main__":
from reddwarf.tests.api.mgmt import admin_required
from reddwarf.tests.api.mgmt import instances
from reddwarf.tests.api.mgmt import storage
from reddwarf.tests.guestagent import test_manager
from reddwarf.tests.guestagent import test_service
from reddwarf.tests.guestagent import test_query
from reddwarf.tests.guestagent import test_models
proboscis.TestProgram().run_and_exit()