Always update OVS to 2.9.2
[apex.git] / apex / tests / test_apex_virtual_configure_vm.py
1 ##############################################################################
2 # Copyright (c) 2016 Dan Radez (dradez@redhat.com) (Red Hat)
3 #
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 ##############################################################################
9
10 import libvirt
11 import unittest
12
13 from mock import patch
14
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
18
19 from nose.tools import (
20     assert_regexp_matches,
21     assert_raises,
22     assert_equal)
23
24
25 class TestVirtualConfigureVM(unittest.TestCase):
26     @classmethod
27     def setup_class(cls):
28         """This method is run once for each class before any tests are run"""
29
30     @classmethod
31     def teardown_class(cls):
32         """This method is run once for each class _after_ all tests are run"""
33
34     def setup(self):
35         """This method is run once before _each_ test method is executed"""
36
37     def teardown(self):
38         """This method is run once after _each_ test method is executed"""
39
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})$')
43
44     def test_generate_baremetal_macs_alot(self):
45         assert_equal(len(generate_baremetal_macs(127)), 127)
46
47     def test_generate_baremetal_macs_too_many(self):
48         assert_raises(ValueError, generate_baremetal_macs, 128)
49
50     @patch('apex.virtual.configure_vm.libvirt.open')
51     def test_create_vm_storage(self, mock_libvirt_open):
52         # setup mock
53         conn = mock_libvirt_open.return_value
54         pool = conn.storagePoolLookupByName.return_value
55         pool.isActive.return_value = 0
56         # execute
57         create_vm_storage('test')
58
59     @patch('apex.virtual.configure_vm.libvirt.open')
60     def test_create_vm_storage_pool_none(self, mock_libvirt_open):
61         # setup mock
62         conn = mock_libvirt_open.return_value
63         conn.storagePoolLookupByName.return_value = None
64         # execute
65         assert_raises(Exception, create_vm_storage, 'test')
66
67     @patch('apex.virtual.configure_vm.libvirt.open')
68     def test_create_vm_storage_libvirt_error(self, mock_libvirt_open):
69         # setup mock
70         conn = mock_libvirt_open.return_value
71         pool = conn.storagePoolLookupByName.return_value
72         pool.storageVolLookupByName.side_effect = libvirt.libvirtError('ermsg')
73         # execute
74         assert_raises(libvirt.libvirtError, create_vm_storage, 'test')
75
76     @patch('apex.virtual.configure_vm.libvirt.open')
77     def test_create_vm_storage_new_vol_none(self, mock_libvirt_open):
78         # setup mock
79         conn = mock_libvirt_open.return_value
80         pool = conn.storagePoolLookupByName.return_value
81         pool.createXML.return_value = None
82         # execute
83         assert_raises(Exception, create_vm_storage, 'test')
84
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,
88                        mock_libvirt_open):
89         create_vm('test', 'image', default_network=True,
90                   direct_boot=True, kernel_args='test', template_dir='./build')
91
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,
95                               mock_libvirt_open):
96         create_vm('test', 'image', arch='x86_64', template_dir='./build')
97
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,
101                                mock_libvirt_open):
102         create_vm('test', 'image', arch='aarch64', template_dir='./build')