Merge "Add unit test codes of instancemonitor"

This commit is contained in:
Jenkins 2017-02-10 07:49:27 +00:00 committed by Gerrit Code Review
commit 3caf57eaba
6 changed files with 204 additions and 0 deletions

View File

@ -0,0 +1,39 @@
# Copyright(c) 2016 Nippon Telegraph and Telephone Corporation
#
# 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.
class FakeLibvirtOpenReadOnly(object):
def domainEventRegisterAny(self, dom, eventID, cb, opaque):
return 1
def setKeepAlive(self, interval, count):
return None
def isAlive(self):
return 0
def domainEventDeregisterAny(self, cid):
return None
def close(self):
raise EnvironmentError("Test Exception.")
class FakeConnection(object):
def __init__(self):
class Ha(object):
def create_notification(self, type, hostname, generated_time,
payload):
return {}
self.ha = Ha()

View File

@ -0,0 +1,68 @@
# Copyright(c) 2016 Nippon Telegraph and Telephone Corporation
#
# 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 mock
import testtools
import eventlet
from openstack import connection
from openstack import profile
from oslo_utils import timeutils
from masakarimonitors.instancemonitor.libvirt_handler import callback
from masakarimonitors.tests.unit.instancemonitor import fakes
eventlet.monkey_patch(os=False)
class TestCallback(testtools.TestCase):
def setUp(self):
super(TestCallback, self).setUp()
@mock.patch.object(connection, 'Connection')
@mock.patch.object(profile.Profile, 'set_interface')
@mock.patch.object(profile.Profile, 'set_version')
@mock.patch.object(profile.Profile, 'set_region')
@mock.patch.object(profile.Profile, 'set_name')
@mock.patch.object(profile.Profile, '_add_service')
def test_vir_event_filter(self,
mock_add_service,
mock_set_name,
mock_set_region,
mock_set_version,
mock_set_interface,
mock_Connection):
obj = callback.Callback()
mock_add_service.return_value = None
mock_set_name.return_value = None
mock_set_region.return_value = None
mock_set_version.return_value = None
mock_set_interface.return_value = None
mock_Connection.return_value = fakes.FakeConnection()
eventID_val = 0
detail_val = 5
uuID = 'test_uuid'
noticeType = 'VM'
hostname = 'masakari-node'
currentTime = timeutils.utcnow()
obj.libvirt_event_callback(eventID_val,
detail_val,
uuID,
noticeType,
hostname,
currentTime)

View File

@ -0,0 +1,43 @@
# Copyright(c) 2016 Nippon Telegraph and Telephone Corporation
#
# 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 mock
import testtools
import eventlet
from masakarimonitors.instancemonitor.libvirt_handler import callback
from masakarimonitors.instancemonitor.libvirt_handler import eventfilter
eventlet.monkey_patch(os=False)
class TestEventFilter(testtools.TestCase):
def setUp(self):
super(TestEventFilter, self).setUp()
@mock.patch.object(callback.Callback, 'libvirt_event_callback')
def test_vir_event_filter(self,
mock_libvirt_event_callback):
obj = eventfilter.EventFilter()
mock_libvirt_event_callback.return_value = 0
eventID = 0
eventType = 5
detail = 5
uuID = 'test_uuid'
obj.vir_event_filter(eventID, eventType, detail, uuID)

View File

@ -0,0 +1,54 @@
# Copyright(c) 2016 Nippon Telegraph and Telephone Corporation
#
# 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 libvirt
import mock
import testtools
import eventlet
from masakarimonitors.instancemonitor import instance
from masakarimonitors.instancemonitor.libvirt_handler import eventfilter
from masakarimonitors.tests.unit.instancemonitor import fakes
eventlet.monkey_patch(os=False)
class TestInstancemonitorManager(testtools.TestCase):
def setUp(self):
super(TestInstancemonitorManager, self).setUp()
@mock.patch.object(libvirt, 'openReadOnly')
@mock.patch.object(eventfilter.EventFilter, 'vir_event_filter')
@mock.patch.object(instance.InstancemonitorManager,
'_vir_event_loop_native_start')
def test_main(self,
mock_vir_event_loop_native_start,
mock_vir_event_filter,
mock_openReadOnly):
obj = instance.InstancemonitorManager()
mock_vir_event_loop_native_start.return_value = None
mock_vir_event_filter.return_value = None
mock_openReadOnly.return_value = fakes.FakeLibvirtOpenReadOnly()
exception_flag = False
try:
obj.main()
except EnvironmentError:
exception_flag = True
self.assertTrue(exception_flag)