Statistics
| Branch: | Tag: | Revision:

root / test / py / ganeti.utils.storage_unittest.py @ a09639d1

History | View | Annotate | Download (4.1 kB)

1
#!/usr/bin/python
2
#
3

    
4
# Copyright (C) 2013 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

    
21

    
22
"""Script for unittesting the ganeti.utils.storage module"""
23

    
24
import mock
25

    
26
import unittest
27

    
28
from ganeti import constants
29
from ganeti.utils import storage
30

    
31
import testutils
32

    
33

    
34
class TestGetStorageUnitForDiskTemplate(unittest.TestCase):
35

    
36
  def setUp(self):
37
    self._default_vg_name = "some_vg_name"
38
    self._cluster = mock.Mock()
39
    self._cluster.file_storage_dir = "my/file/storage/dir"
40
    self._cluster.shared_file_storage_dir = "my/shared/file/storage/dir"
41
    self._cfg = mock.Mock()
42
    self._cfg.GetVGName = mock.Mock(return_value=self._default_vg_name)
43
    self._cfg.GetClusterInfo = mock.Mock(return_value=self._cluster)
44

    
45
  def testGetDefaultStorageUnitForDiskTemplateLvm(self):
46
    for disk_template in [constants.DT_DRBD8, constants.DT_PLAIN]:
47
      (storage_type, storage_key) = \
48
          storage._GetDefaultStorageUnitForDiskTemplate(self._cfg,
49
                                                        disk_template)
50
      self.assertEqual(storage_type, constants.ST_LVM_VG)
51
      self.assertEqual(storage_key, self._default_vg_name)
52

    
53
  def testGetDefaultStorageUnitForDiskTemplateFile(self):
54
    (storage_type, storage_key) = \
55
        storage._GetDefaultStorageUnitForDiskTemplate(self._cfg,
56
                                                      constants.DT_FILE)
57
    self.assertEqual(storage_type, constants.ST_FILE)
58
    self.assertEqual(storage_key, self._cluster.file_storage_dir)
59

    
60
  def testGetDefaultStorageUnitForDiskTemplateSharedFile(self):
61
    (storage_type, storage_key) = \
62
        storage._GetDefaultStorageUnitForDiskTemplate(self._cfg,
63
                                                      constants.DT_SHARED_FILE)
64
    self.assertEqual(storage_type, constants.ST_FILE)
65
    self.assertEqual(storage_key, self._cluster.shared_file_storage_dir)
66

    
67
  def testGetDefaultStorageUnitForDiskTemplateDiskless(self):
68
    (storage_type, storage_key) = \
69
        storage._GetDefaultStorageUnitForDiskTemplate(self._cfg,
70
                                                      constants.DT_DISKLESS)
71
    self.assertEqual(storage_type, constants.ST_DISKLESS)
72
    self.assertEqual(storage_key, None)
73

    
74

    
75
class TestGetStorageUnits(unittest.TestCase):
76

    
77
  def setUp(self):
78
    storage._GetDefaultStorageUnitForDiskTemplate = \
79
        mock.Mock(return_value=("foo", "bar"))
80
    self._cfg = mock.Mock()
81

    
82
  def testGetStorageUnits(self):
83
    disk_templates = constants.DTS_FILEBASED
84
    storage_units = storage.GetStorageUnits(self._cfg, disk_templates)
85
    self.assertEqual(len(storage_units), len(disk_templates))
86

    
87
  def testGetStorageUnitsLvm(self):
88
    disk_templates = [constants.DT_PLAIN, constants.DT_DRBD8]
89
    storage_units = storage.GetStorageUnits(self._cfg, disk_templates)
90
    self.assertEqual(len(storage_units), len(disk_templates))
91

    
92

    
93
class TestLookupSpaceInfoByStorageType(unittest.TestCase):
94

    
95
  def setUp(self):
96
    self._space_info = [
97
        {"type": st, "name": st + "_key", "storage_size": 0, "storage_free": 0}
98
        for st in constants.STORAGE_TYPES]
99

    
100
  def testValidLookup(self):
101
    query_type = constants.ST_LVM_PV
102
    result = storage.LookupSpaceInfoByStorageType(self._space_info, query_type)
103
    self.assertEqual(query_type, result["type"])
104

    
105
  def testNotInList(self):
106
    result = storage.LookupSpaceInfoByStorageType(self._space_info,
107
                                                  "non_existing_type")
108
    self.assertEqual(None, result)
109

    
110

    
111
if __name__ == "__main__":
112
  testutils.GanetiTestProgram()