Fixes ensuring VBMCs are actually running
[apex.git] / apex / tests / test_apex_virtual_utils.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 subprocess
11 import unittest
12
13 from mock import patch
14
15 from apex.virtual.exceptions import ApexVirtualException
16 from apex.virtual.utils import DEFAULT_VIRT_IP
17 from apex.virtual.utils import get_virt_ip
18 from apex.virtual.utils import generate_inventory
19 from apex.virtual.utils import host_setup
20 from apex.virtual.utils import virt_customize
21
22 from nose.tools import (
23     assert_is_instance,
24     assert_regexp_matches,
25     assert_raises,
26     assert_equal)
27
28
29 class TestVirtualUtils(unittest.TestCase):
30     @classmethod
31     def setup_class(cls):
32         """This method is run once for each class before any tests are run"""
33
34     @classmethod
35     def teardown_class(cls):
36         """This method is run once for each class _after_ all tests are run"""
37
38     def setup(self):
39         """This method is run once before _each_ test method is executed"""
40
41     def teardown(self):
42         """This method is run once after _each_ test method is executed"""
43
44     @patch('apex.virtual.utils.subprocess.check_output')
45     def test_get_virt_ip(self, mock_subprocess):
46         mock_subprocess.return_value = '<xml></xml>'
47         assert_equal(get_virt_ip(), DEFAULT_VIRT_IP)
48
49     @patch('apex.virtual.utils.subprocess.check_output')
50     def test_get_virt_ip_not_default(self, mock_subprocess):
51         mock_subprocess.return_value = '''<xml>
52 <ip address='1.2.3.4' netmask='255.255.255.0'/>
53 </xml>'''
54         assert_equal(get_virt_ip(), '1.2.3.4')
55
56     @patch('apex.virtual.utils.subprocess.check_output')
57     def test_get_virt_ip_raises(self, mock_subprocess):
58         mock_subprocess.side_effect = subprocess.CalledProcessError(1, 'cmd')
59         assert_equal(get_virt_ip(), DEFAULT_VIRT_IP)
60
61     @patch('apex.virtual.utils.common_utils')
62     def test_generate_inventory(self, mock_common_utils):
63         assert_is_instance(generate_inventory('target_file'), dict)
64
65     @patch('apex.virtual.utils.common_utils')
66     def test_generate_inventory_ha_enabled(self, mock_common_utils):
67         assert_is_instance(generate_inventory('target_file', ha_enabled=True),
68                            dict)
69
70     @patch('apex.virtual.utils.get_virt_ip')
71     @patch('apex.virtual.utils.subprocess.check_output')
72     @patch('apex.virtual.utils.iptc')
73     @patch('apex.virtual.utils.subprocess.check_call')
74     @patch('apex.virtual.utils.vbmc_lib')
75     def test_host_setup(self, mock_vbmc_lib, mock_subprocess, mock_iptc,
76                         mock_check_output, mock_get_virt_ip):
77         mock_get_virt_ip.return_value = '192.168.122.1'
78         mock_check_output.return_value = b'blah |dummy \nstatus | running'
79         host_setup({'test': 2468})
80         mock_subprocess.assert_called_with(['vbmc', 'start', 'test'])
81
82     @patch('apex.virtual.utils.get_virt_ip')
83     @patch('apex.virtual.utils.subprocess.check_output')
84     @patch('apex.virtual.utils.iptc')
85     @patch('apex.virtual.utils.subprocess.check_call')
86     @patch('apex.virtual.utils.vbmc_lib')
87     def test_host_setup_vbmc_fails(self, mock_vbmc_lib, mock_subprocess,
88                                    mock_iptc, mock_check_output,
89                                    mock_get_virt_ip):
90         mock_get_virt_ip.return_value = '192.168.122.1'
91         mock_check_output.return_value = b'blah |dummy \nstatus | stopped'
92         assert_raises(ApexVirtualException, host_setup, {'test': 2468})
93
94     @patch('apex.virtual.utils.iptc')
95     @patch('apex.virtual.utils.subprocess.check_call')
96     @patch('apex.virtual.utils.vbmc_lib')
97     def test_host_setup_raise_called_process_error(self, mock_vbmc_lib,
98                                                    mock_subprocess, mock_iptc):
99         mock_subprocess.side_effect = subprocess.CalledProcessError(1, 'cmd')
100         assert_raises(subprocess.CalledProcessError, host_setup, {'tst': 2468})
101
102     @patch('apex.virtual.utils.os.path')
103     @patch('apex.virtual.utils.subprocess.check_output')
104     def test_virt_customize(self, mock_subprocess, mock_os_path):
105         virt_customize([{'--operation': 'arg'}], 'target')
106
107     @patch('apex.virtual.utils.subprocess.check_output')
108     def test_virt_customize_file_not_found(self, mock_subprocess):
109         assert_raises(FileNotFoundError,
110                       virt_customize,
111                       [{'--operation': 'arg'}], 'target')
112
113     @patch('apex.virtual.utils.os.path')
114     @patch('apex.virtual.utils.subprocess.check_output')
115     def test_virt_customize_raises(self, mock_subprocess, mock_os_path):
116         mock_subprocess.side_effect = subprocess.CalledProcessError(1, 'cmd')
117         assert_raises(subprocess.CalledProcessError,
118                       virt_customize,
119                       [{'--operation': 'arg'}], 'target')