summaryrefslogtreecommitdiffstats
path: root/apex/tests/test_apex_virtual_utils.py
blob: a9eb78dd3d374437f0bb82f2e314c28cf23c7875 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
##############################################################################
# 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 subprocess
import unittest

from mock import patch

from apex.virtual.exceptions import ApexVirtualException
from apex.virtual.utils import DEFAULT_VIRT_IP
from apex.virtual.utils import get_virt_ip
from apex.virtual.utils import generate_inventory
from apex.virtual.utils import host_setup
from apex.virtual.utils import virt_customize

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


class TestVirtualUtils(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"""

    @patch('apex.virtual.utils.subprocess.check_output')
    def test_get_virt_ip(self, mock_subprocess):
        mock_subprocess.return_value = '<xml></xml>'
        assert_equal(get_virt_ip(), DEFAULT_VIRT_IP)

    @patch('apex.virtual.utils.subprocess.check_output')
    def test_get_virt_ip_not_default(self, mock_subprocess):
        mock_subprocess.return_value = '''<xml>
<ip address='1.2.3.4' netmask='255.255.255.0'/>
</xml>'''
        assert_equal(get_virt_ip(), '1.2.3.4')

    @patch('apex.virtual.utils.subprocess.check_output')
    def test_get_virt_ip_raises(self, mock_subprocess):
        mock_subprocess.side_effect = subprocess.CalledProcessError(1, 'cmd')
        assert_equal(get_virt_ip(), DEFAULT_VIRT_IP)

    @patch('apex.virtual.utils.common_utils')
    def test_generate_inventory(self, mock_common_utils):
        assert_is_instance(generate_inventory('target_file'), dict)

    @patch('apex.virtual.utils.common_utils')
    def test_generate_inventory_ha_enabled(self, mock_common_utils):
        assert_is_instance(generate_inventory('target_file', ha_enabled=True),
                           dict)

    @patch('apex.virtual.utils.get_virt_ip')
    @patch('apex.virtual.utils.subprocess.check_output')
    @patch('apex.virtual.utils.iptc')
    @patch('apex.virtual.utils.subprocess.check_call')
    @patch('apex.virtual.utils.vbmc_lib')
    def test_host_setup(self, mock_vbmc_lib, mock_subprocess, mock_iptc,
                        mock_check_output, mock_get_virt_ip):
        mock_get_virt_ip.return_value = '192.168.122.1'
        mock_check_output.return_value = b'blah |dummy \nstatus | running'
        host_setup({'test': 2468})
        mock_subprocess.assert_called_with(['vbmc', 'start', 'test'])

    @patch('apex.virtual.utils.get_virt_ip')
    @patch('apex.virtual.utils.subprocess.check_output')
    @patch('apex.virtual.utils.iptc')
    @patch('apex.virtual.utils.subprocess.check_call')
    @patch('apex.virtual.utils.vbmc_lib')
    def test_host_setup_vbmc_fails(self, mock_vbmc_lib, mock_subprocess,
                                   mock_iptc, mock_check_output,
                                   mock_get_virt_ip):
        mock_get_virt_ip.return_value = '192.168.122.1'
        mock_check_output.return_value = b'blah |dummy \nstatus | stopped'
        assert_raises(ApexVirtualException, host_setup, {'test': 2468})

    @patch('apex.virtual.utils.iptc')
    @patch('apex.virtual.utils.subprocess.check_call')
    @patch('apex.virtual.utils.vbmc_lib')
    def test_host_setup_raise_called_process_error(self, mock_vbmc_lib,
                                                   mock_subprocess, mock_iptc):
        mock_subprocess.side_effect = subprocess.CalledProcessError(1, 'cmd')
        assert_raises(subprocess.CalledProcessError, host_setup, {'tst': 2468})

    @patch('apex.virtual.utils.os.path')
    @patch('apex.virtual.utils.subprocess.check_output')
    def test_virt_customize(self, mock_subprocess, mock_os_path):
        virt_customize([{'--operation': 'arg'}], 'target')

    @patch('apex.virtual.utils.subprocess.check_output')
    def test_virt_customize_file_not_found(self, mock_subprocess):
        assert_raises(FileNotFoundError,
                      virt_customize,
                      [{'--operation': 'arg'}], 'target')

    @patch('apex.virtual.utils.os.path')
    @patch('apex.virtual.utils.subprocess.check_output')
    def test_virt_customize_raises(self, mock_subprocess, mock_os_path):
        mock_subprocess.side_effect = subprocess.CalledProcessError(1, 'cmd')
        assert_raises(subprocess.CalledProcessError,
                      virt_customize,
                      [{'--operation': 'arg'}], 'target')