1 ##############################################################################
2 # Copyright (c) 2016 Dan Radez (dradez@redhat.com) (Red Hat)
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 ##############################################################################
13 from mock import patch
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
21 from nose.tools import (
23 assert_regexp_matches,
28 class TestVirtualUtils(unittest.TestCase):
31 """This method is run once for each class before any tests are run"""
34 def teardown_class(cls):
35 """This method is run once for each class _after_ all tests are run"""
38 """This method is run once before _each_ test method is executed"""
41 """This method is run once after _each_ test method is executed"""
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)
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'/>
53 assert_equal(get_virt_ip(), '1.2.3.4')
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)
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)
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),
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'])
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})
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')
89 @patch('apex.virtual.utils.subprocess.check_output')
90 def test_virt_customize_file_not_found(self, mock_subprocess):
91 assert_raises(FileNotFoundError,
93 [{'--operation': 'arg'}], 'target')
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,
101 [{'--operation': 'arg'}], 'target')