Commit e1cdc179 by Belákovics Ádám

Try methods in main.py, start write tests

parent 503b5c02
Pipeline #710 failed with stage
in 38 seconds
# This file is for testing the openstack api access # This file is for testing the openstack api access
from implementation.vm.instance import OSVirtualMachineManager from implementation.vm.instance import OSVirtualMachineManager
from interface.vm.resources import BlockDeviceMapping, Flavor
import openstack import openstack
# openstack.enable_logging(debug=True) # openstack.enable_logging(debug=True)
conn = openstack.connect(cloud='openstack') conn = openstack.connect(cloud='openstack')
block_dev_map = BlockDeviceMapping(
boot_index=0,
uuid="da51253f-867c-472d-8ce0-81e7b7126d60",
source_type="image",
volume_size=10,
destination_type="volume",
delete_on_termination=True,
disk_bus="scsi"
)
networks = [{"uuid": "c03d0d4b-413e-4cc6-9ebe-c0b5ca0dac3a"}]
interface = OSVirtualMachineManager(conn) interface = OSVirtualMachineManager(conn)
resource = {"name": "m1.tiny", "ram": 1024, "cpu": 2}
print('#'*40) print('#'*40)
print(resource)
# interface.create_vm_from_template('new_server', resource, "cirros-0.4.0-x86_64-disk") # interface.create_vm_from_template('new_server', resource, "cirros-0.4.0-x86_64-disk")
# interface.stop_vm('a6bc504f-a422-4492-b429-e5dad2df12f4') # interface.stop_vm('a6bc504f-a422-4492-b429-e5dad2df12f4')
server = interface.get_vm("New test") # server = interface.get_vm("8e94c162-f8ed-4872-b9e5-50bf33040b5b")
# print(interface.delete_flavor("flavorTest"))
for flavor in interface.list_flavors():
print(flavor.JSON())
# print(interface.create_base_vm("BaseTest", "2", networks, block_dev_map))
# print(interface.convert_server_to_instance(server).JSON())
print(interface.convert_server_to_instance(server).JSON()) # print(interface.openstack.compute.get_server("8e94c162-f8ed-4872-b9e5-50bf33040b5b"))
print(interface.get_status("Uborka Test")) # print(server.JSON())
print(interface.get_vnc_console("8e94c162-f8ed-4872-b9e5-50bf33040b5b")) # print(interface.get_vnc_console("8e94c162-f8ed-4872-b9e5-50bf33040b5b"))
# print(interface.convert_server_to_instance(server)) # print(interface.convert_server_to_instance(server))
...@@ -37,4 +56,3 @@ print(interface.get_vnc_console("8e94c162-f8ed-4872-b9e5-50bf33040b5b")) ...@@ -37,4 +56,3 @@ print(interface.get_vnc_console("8e94c162-f8ed-4872-b9e5-50bf33040b5b"))
# interface.wake_up_vm(server.id) # interface.wake_up_vm(server.id)
# interface.destroy_vm(server.id) # interface.destroy_vm(server.id)
# conn.compute.change_server_password(server, "root")
import unittest
from unittest.mock import MagicMock
from implementation.vm.instance import OSVirtualMachineManager
from interface.vm.resources import Instance
servers = [
MagicMock(
id="test1",
name="test1",
flavorRef="flav1",
imageRef="image1",
networks= [{"uuid": "network1"}],
terminated_at="20200320-15-31",
launched_at="20190510-13-22",
disks=["disk1"],
status="ACTIVE",
addresses=[{"mac": "12345678AB", "ipv4": "1.1.1.1"}]
)
]
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)
class InstanceCreateTestCase(unittest.TestCase):
def setUp(self):
self.conn = MockOpenStackCompute()
self.conn.setUp()
self.manager = OSVirtualMachineManager(self.conn)
def tearDown(self):
pass
def test_create_from_template(self):
instance = self.manager.create_vm_from_template('test', 'imageid', 'flavorid',
['networkid1'])
self.conn.compute.create_server.assert_called()
def test_create_from_template_params(self):
self.manager.create_vm_from_template('test', 'imageid', 'flavorid',
['networkid1'])
self.conn.compute.create_server.assert_called_with(
name='test',
flavorRef="uuid1",
imageRef="uuid2",
networks=['networkid1'],
min_count=1)
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