1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2011 Isaku Yamahata
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
18
from nova import exception
19
from nova import flags
21
from nova.virt import disk
22
from nova.virt import driver
27
class TestVirtDriver(test.TestCase):
28
def test_block_device(self):
29
swap = {'device_name': '/dev/sdb',
31
ephemerals = [{'num': 0,
32
'virtual_name': 'ephemeral0',
33
'device_name': '/dev/sdc1',
35
block_device_mapping = [{'mount_device': '/dev/sde',
36
'device_path': 'fake_device'}]
38
'root_device_name': '/dev/sda',
40
'ephemerals': ephemerals,
41
'block_device_mapping': block_device_mapping}
43
empty_block_device_info = {}
46
driver.block_device_info_get_root(block_device_info), '/dev/sda')
48
driver.block_device_info_get_root(empty_block_device_info), None)
50
driver.block_device_info_get_root(None), None)
53
driver.block_device_info_get_swap(block_device_info), swap)
54
self.assertEqual(driver.block_device_info_get_swap(
55
empty_block_device_info)['device_name'], None)
56
self.assertEqual(driver.block_device_info_get_swap(
57
empty_block_device_info)['swap_size'], 0)
59
driver.block_device_info_get_swap({'swap': None})['device_name'],
62
driver.block_device_info_get_swap({'swap': None})['swap_size'],
65
driver.block_device_info_get_swap(None)['device_name'], None)
67
driver.block_device_info_get_swap(None)['swap_size'], 0)
70
driver.block_device_info_get_ephemerals(block_device_info),
73
driver.block_device_info_get_ephemerals(empty_block_device_info),
76
driver.block_device_info_get_ephemerals(None),
79
def test_swap_is_usable(self):
80
self.assertFalse(driver.swap_is_usable(None))
81
self.assertFalse(driver.swap_is_usable({'device_name': None}))
82
self.assertFalse(driver.swap_is_usable({'device_name': '/dev/sdb',
84
self.assertTrue(driver.swap_is_usable({'device_name': '/dev/sdb',
88
class TestVirtDisk(test.TestCase):
89
def test_check_safe_path(self):
90
ret = disk._join_and_check_path_within_fs('/foo', 'etc',
92
self.assertEquals(ret, '/foo/etc/something.conf')
94
def test_check_unsafe_path(self):
95
self.assertRaises(exception.Invalid,
96
disk._join_and_check_path_within_fs,
97
'/foo', 'etc/../../../something.conf')
99
def test_inject_files_with_bad_path(self):
100
self.assertRaises(exception.Invalid,
101
disk._inject_file_into_fs,
102
'/tmp', '/etc/../../../../etc/passwd',