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:
def __init__(self) -> None:
super().__init__()
def upload_file(self, name, path, format):
raise NotImplementedError
......
import unittest
from interface.image.Image import Image
from interface_openstack.interface.image.image import Image
class TestImage(unittest.TestCase):
......@@ -11,7 +11,3 @@ class TestImage(unittest.TestCase):
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
from interface_openstack.interface.storage.snapshot import Snapshot
class TestSnapshot(unittest.TestCase):
......@@ -13,7 +13,3 @@ class TestSnapshot(unittest.TestCase):
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 unittest.mock import MagicMock, create_autospec
from implementation.vm.instance import OSVirtualMachineManager
import openstack
from unittest.mock import MagicMock, patch
from interface_openstack.implementation.vm.instance import OSVirtualMachineManager
servers = [
MagicMock(
......@@ -18,46 +17,64 @@ servers = [
)
]
class MockOpenStackCompute(MagicMock):
def setUp(self):
self.compute = MagicMock()
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"))
self.compute.wait_for_server = MagicMock(return_value=True)
self.compute.list_servers = MagicMock(return_value=servers)
self.compute.get_server = create_autospec()
fake_auth = {
"auth_url": "",
"project_id": "",
"project_name": "",
"user_domain_name": "",
"user_domain_id": "",
"project_domain_id": "",
"username": "",
"password": "",
"region_name": "",
"interface": "",
"identity_api_version": "",
}
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):
pass
def test_create_from_template(self):
instance = self.manager.create_vm_from_template('test1',
'imageid',
'flavorid',
['networkid1'])
self.conn.compute.create_server.assert_called()
self.assertEqual(instance.name, servers[0].name)
# 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
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):
# 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',
['networkid1'])
self.conn.compute.create_server.assert_called_with(
# Assert
self.manager.openstack.compute.create_server.assert_called_with(
name='test',
flavorRef="uuid1",
imageRef="uuid2",
networks=['networkid1'],
min_count=1)
)
def test_suspend(self):
self.compute.suspend_server()
self.manager.openstack.compute.suspend_server()
import unittest
from interface.storage.Volume import Volume
from interface_openstack.interface.storage.volume import Volume
class TestVolume(unittest.TestCase):
......@@ -14,7 +14,3 @@ class TestVolume(unittest.TestCase):
self.assertEqual(volume.bootable, False)
self.assertEqual(volume.status, 'status')
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