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.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
22 from nose.tools import (
24 assert_regexp_matches,
29 class TestVirtualUtils(unittest.TestCase):
32 """This method is run once for each class before any tests are run"""
35 def teardown_class(cls):
36 """This method is run once for each class _after_ all tests are run"""
39 """This method is run once before _each_ test method is executed"""
42 """This method is run once after _each_ test method is executed"""
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)
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'/>
54 assert_equal(get_virt_ip(), '1.2.3.4')
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)
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)
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),
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'])
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,
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})
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})
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')
107 @patch('apex.virtual.utils.subprocess.check_output')
108 def test_virt_customize_file_not_found(self, mock_subprocess):
109 assert_raises(FileNotFoundError,
111 [{'--operation': 'arg'}], 'target')
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,
119 [{'--operation': 'arg'}], 'target')