1 ##############################################################################
2 # Copyright (c) 2016 Dan Radez (dradez@redhat.com) (Red Hat)
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
13 from mock import patch
15 from apex.virtual.configure_vm import generate_baremetal_macs
16 from apex.virtual.configure_vm import create_vm_storage
17 from apex.virtual.configure_vm import create_vm
19 from nose.tools import (
20 assert_regexp_matches,
25 class TestVirtualConfigureVM(unittest.TestCase):
28 """This method is run once for each class before any tests are run"""
31 def teardown_class(cls):
32 """This method is run once for each class _after_ all tests are run"""
35 """This method is run once before _each_ test method is executed"""
38 """This method is run once after _each_ test method is executed"""
40 def test_generate_baremetal_macs(self):
41 assert_regexp_matches(generate_baremetal_macs()[0],
42 '^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$')
44 def test_generate_baremetal_macs_alot(self):
45 assert_equal(len(generate_baremetal_macs(127)), 127)
47 def test_generate_baremetal_macs_too_many(self):
48 assert_raises(ValueError, generate_baremetal_macs, 128)
50 @patch('apex.virtual.configure_vm.libvirt.open')
51 def test_create_vm_storage(self, mock_libvirt_open):
53 conn = mock_libvirt_open.return_value
54 pool = conn.storagePoolLookupByName.return_value
55 pool.isActive.return_value = 0
57 create_vm_storage('test')
59 @patch('apex.virtual.configure_vm.libvirt.open')
60 def test_create_vm_storage_pool_none(self, mock_libvirt_open):
62 conn = mock_libvirt_open.return_value
63 conn.storagePoolLookupByName.return_value = None
65 assert_raises(Exception, create_vm_storage, 'test')
67 @patch('apex.virtual.configure_vm.libvirt.open')
68 def test_create_vm_storage_libvirt_error(self, mock_libvirt_open):
70 conn = mock_libvirt_open.return_value
71 pool = conn.storagePoolLookupByName.return_value
72 pool.storageVolLookupByName.side_effect = libvirt.libvirtError('ermsg')
74 assert_raises(libvirt.libvirtError, create_vm_storage, 'test')
76 @patch('apex.virtual.configure_vm.libvirt.open')
77 def test_create_vm_storage_new_vol_none(self, mock_libvirt_open):
79 conn = mock_libvirt_open.return_value
80 pool = conn.storagePoolLookupByName.return_value
81 pool.createXML.return_value = None
83 assert_raises(Exception, create_vm_storage, 'test')
85 @patch('apex.virtual.configure_vm.libvirt.open')
86 @patch('apex.virtual.configure_vm.create_vm_storage')
87 def test_create_vm(self, mock_create_vm_storage,
89 create_vm('test', 'image', default_network=True,
90 direct_boot=True, kernel_args='test', template_dir='./build')
92 @patch('apex.virtual.configure_vm.libvirt.open')
93 @patch('apex.virtual.configure_vm.create_vm_storage')
94 def test_create_vm_x86_64(self, mock_create_vm_storage,
96 create_vm('test', 'image', arch='x86_64', template_dir='./build')
98 @patch('apex.virtual.configure_vm.libvirt.open')
99 @patch('apex.virtual.configure_vm.create_vm_storage')
100 def test_create_vm_aarch64(self, mock_create_vm_storage,
102 create_vm('test', 'image', arch='aarch64', template_dir='./build')