summaryrefslogtreecommitdiffstats
path: root/apex/tests/test_apex_virtual_configure_vm.py
blob: 228e06d678223a3849d44874b3551ac4ea624d07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
##############################################################################
# Copyright (c) 2016 Dan Radez (dradez@redhat.com) (Red Hat)
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

import libvirt
import unittest

from mock import patch

from apex.virtual.configure_vm import generate_baremetal_macs
from apex.virtual.configure_vm import create_vm_storage
from apex.virtual.configure_vm import create_vm

from nose.tools import (
    assert_regexp_matches,
    assert_raises,
    assert_equal)


class TestVirtualConfigureVM(unittest.TestCase):
    @classmethod
    def setup_class(cls):
        """This method is run once for each class before any tests are run"""

    @classmethod
    def teardown_class(cls):
        """This method is run once for each class _after_ all tests are run"""

    def setup(self):
        """This method is run once before _each_ test method is executed"""

    def teardown(self):
        """This method is run once after _each_ test method is executed"""

    def test_generate_baremetal_macs(self):
        assert_regexp_matches(generate_baremetal_macs()[0],
                              '^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$')

    def test_generate_baremetal_macs_alot(self):
        assert_equal(len(generate_baremetal_macs(127)), 127)

    def test_generate_baremetal_macs_too_many(self):
        assert_raises(ValueError, generate_baremetal_macs, 128)

    @patch('apex.virtual.configure_vm.libvirt.open')
    def test_create_vm_storage(self, mock_libvirt_open):
        # setup mock
        conn = mock_libvirt_open.return_value
        pool = conn.storagePoolLookupByName.return_value
        pool.isActive.return_value = 0
        # execute
        create_vm_storage('test')

    @patch('apex.virtual.configure_vm.libvirt.open')
    def test_create_vm_storage_pool_none(self, mock_libvirt_open):
        # setup mock
        conn = mock_libvirt_open.return_value
        conn.storagePoolLookupByName.return_value = None
        # execute
        assert_raises(Exception, create_vm_storage, 'test')

    @patch('apex.virtual.configure_vm.libvirt.open')
    def test_create_vm_storage_libvirt_error(self, mock_libvirt_open):
        # setup mock
        conn = mock_libvirt_open.return_value
        pool = conn.storagePoolLookupByName.return_value
        pool.storageVolLookupByName.side_effect = libvirt.libvirtError('ermsg')
        # execute
        assert_raises(libvirt.libvirtError, create_vm_storage, 'test')

    @patch('apex.virtual.configure_vm.libvirt.open')
    def test_create_vm_storage_new_vol_none(self, mock_libvirt_open):
        # setup mock
        conn = mock_libvirt_open.return_value
        pool = conn.storagePoolLookupByName.return_value
        pool.createXML.return_value = None
        # execute
        assert_raises(Exception, create_vm_storage, 'test')

    @patch('apex.virtual.configure_vm.libvirt.open')
    @patch('apex.virtual.configure_vm.create_vm_storage')
    def test_create_vm(self, mock_create_vm_storage,
                       mock_libvirt_open):
        create_vm('test', 'image', default_network=True,
                  direct_boot=True, kernel_args='test', template_dir='./build')

    @patch('apex.virtual.configure_vm.libvirt.open')
    @patch('apex.virtual.configure_vm.create_vm_storage')
    def test_create_vm_x86_64(self, mock_create_vm_storage,
                              mock_libvirt_open):
        create_vm('test', 'image', arch='x86_64', template_dir='./build')

    @patch('apex.virtual.configure_vm.libvirt.open')
    @patch('apex.virtual.configure_vm.create_vm_storage')
    def test_create_vm_aarch64(self, mock_create_vm_storage,
                               mock_libvirt_open):
        create_vm('test', 'image', arch='aarch64', template_dir='./build')