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