Commit 200ae4f7 by Chif Gergő

Patch openstack cennection with MagicMock, fix tests, remove duplicated test files

parent e01d873c
Pipeline #801 failed with stage
in 39 seconds
class ImageManager: class ImageManager:
def __init__(self) -> None:
super().__init__()
def upload_file(self, name, path, format): def upload_file(self, name, path, format):
raise NotImplementedError raise NotImplementedError
......
import unittest import unittest
from interface.image.Image import Image from interface_openstack.interface.image.image import Image
class TestImage(unittest.TestCase): class TestImage(unittest.TestCase):
...@@ -11,7 +11,3 @@ class TestImage(unittest.TestCase): ...@@ -11,7 +11,3 @@ class TestImage(unittest.TestCase):
self.assertEqual(image.id, 'id') self.assertEqual(image.id, 'id')
self.assertEqual(image.name, 'name') self.assertEqual(image.name, 'name')
self.assertEqual(image.format, 'format') self.assertEqual(image.format, 'format')
if __name__ == '__main__':
unittest.main()
import unittest import unittest
from interface.storage.Snapshot import Snapshot from interface_openstack.interface.storage.snapshot import Snapshot
class TestSnapshot(unittest.TestCase): class TestSnapshot(unittest.TestCase):
...@@ -13,7 +13,3 @@ class TestSnapshot(unittest.TestCase): ...@@ -13,7 +13,3 @@ class TestSnapshot(unittest.TestCase):
self.assertEqual(snapshot.size, 1) self.assertEqual(snapshot.size, 1)
self.assertEqual(snapshot.status, 'status') self.assertEqual(snapshot.status, 'status')
self.assertEqual(snapshot.created_at, 'created_at') self.assertEqual(snapshot.created_at, 'created_at')
if __name__ == '__main__':
unittest.main()
import unittest import unittest
from unittest.mock import MagicMock, create_autospec from unittest.mock import MagicMock, patch
from implementation.vm.instance import OSVirtualMachineManager from interface_openstack.implementation.vm.instance import OSVirtualMachineManager
import openstack
servers = [ servers = [
MagicMock( MagicMock(
...@@ -18,46 +17,64 @@ servers = [ ...@@ -18,46 +17,64 @@ servers = [
) )
] ]
fake_auth = {
class MockOpenStackCompute(MagicMock): "auth_url": "",
def setUp(self): "project_id": "",
self.compute = MagicMock() "project_name": "",
self.compute.create_server = MagicMock(return_value=servers[0]) "user_domain_name": "",
self.compute.find_flavor = MagicMock(return_value=MagicMock(id="uuid1")) "user_domain_id": "",
self.compute.find_image = MagicMock(return_value=MagicMock(id="uuid2")) "project_domain_id": "",
self.compute.wait_for_server = MagicMock(return_value=True) "username": "",
self.compute.list_servers = MagicMock(return_value=servers) "password": "",
"region_name": "",
self.compute.get_server = create_autospec() "interface": "",
"identity_api_version": "",
}
class InstanceCreateTestCase(unittest.TestCase): class InstanceCreateTestCase(unittest.TestCase):
def setUp(self):
self.conn = MockOpenStackCompute()
self.conn.setUp()
self.manager = OSVirtualMachineManager(self.conn) @patch('openstack.connect')
def setUp(self, mock_connect):
self.compute = MagicMock()
mock_connect().compute = self.compute
self.manager = OSVirtualMachineManager(auth=fake_auth)
def tearDown(self): def tearDown(self):
pass pass
def test_create_from_template(self): def test_create_from_template(self):
instance = self.manager.create_vm_from_template('test1', # Mock
'imageid', self.compute.create_server = MagicMock(return_value=servers[0])
'flavorid', self.compute.find_flavor = MagicMock(return_value=MagicMock(id="uuid1"))
['networkid1']) self.compute.find_image = MagicMock(return_value=MagicMock(id="uuid2"))
self.conn.compute.create_server.assert_called()
self.assertEqual(instance.name, servers[0].name) # Execute
instance_id = self.manager.create_vm_from_template('test1',
'imageid',
'flavorid',
['networkid1'])
# Assert
self.manager.openstack.compute.create_server.assert_called()
self.assertEqual(instance_id, servers[0].id)
def test_create_from_template_params(self): def test_create_from_template_params(self):
# Mock
self.compute.create_server = MagicMock(return_value=servers[0])
self.compute.find_flavor = MagicMock(return_value=MagicMock(id="uuid1"))
self.compute.find_image = MagicMock(return_value=MagicMock(id="uuid2"))
# Execute
self.manager.create_vm_from_template('test', 'imageid', 'flavorid', self.manager.create_vm_from_template('test', 'imageid', 'flavorid',
['networkid1']) ['networkid1'])
self.conn.compute.create_server.assert_called_with( # Assert
self.manager.openstack.compute.create_server.assert_called_with(
name='test', name='test',
flavorRef="uuid1", flavorRef="uuid1",
imageRef="uuid2", imageRef="uuid2",
networks=['networkid1'], networks=['networkid1'],
min_count=1) )
def test_suspend(self): def test_suspend(self):
self.compute.suspend_server() self.manager.openstack.compute.suspend_server()
import unittest import unittest
from interface.storage.Volume import Volume from interface_openstack.interface.storage.volume import Volume
class TestVolume(unittest.TestCase): class TestVolume(unittest.TestCase):
...@@ -14,7 +14,3 @@ class TestVolume(unittest.TestCase): ...@@ -14,7 +14,3 @@ class TestVolume(unittest.TestCase):
self.assertEqual(volume.bootable, False) self.assertEqual(volume.bootable, False)
self.assertEqual(volume.status, 'status') self.assertEqual(volume.status, 'status')
self.assertEqual(volume.created_at, 'created_at') self.assertEqual(volume.created_at, 'created_at')
if __name__ == '__main__':
unittest.main()
import unittest
from interface.image.Image import Image
class TestImage(unittest.TestCase):
def test_fields(self):
image = Image('id', 'name', 'format')
self.assertEqual(image.id, 'id')
self.assertEqual(image.name, 'name')
self.assertEqual(image.format, 'format')
if __name__ == '__main__':
unittest.main()
import unittest
from interface.storage.Snapshot import Snapshot
class TestSnapshot(unittest.TestCase):
def test_fields(self):
snapshot = Snapshot('id', 'volume_id', 1, 'status', 'created_at')
self.assertEqual(snapshot.id, 'id')
self.assertEqual(snapshot.volume_id, 'volume_id')
self.assertEqual(snapshot.size, 1)
self.assertEqual(snapshot.status, 'status')
self.assertEqual(snapshot.created_at, 'created_at')
if __name__ == '__main__':
unittest.main()
import unittest
from interface.storage.Volume import Volume
class TestVolume(unittest.TestCase):
def test_fields(self):
volume = Volume('id', 'image_id', 1, False, 'status', 'created_at')
self.assertEqual(volume.id, 'id')
self.assertEqual(volume.image_id, 'image_id')
self.assertEqual(volume.size, 1)
self.assertEqual(volume.bootable, False)
self.assertEqual(volume.status, 'status')
self.assertEqual(volume.created_at, 'created_at')
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment