Statistics
| Branch: | Tag: | Revision:

root / test / py / ganeti.utils.storage_unittest.py @ 683335b3

History | View | Annotate | Download (4.7 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 import errors
30
from ganeti import objects
31
from ganeti import pathutils
32
from ganeti.utils import storage
33

    
34
import testutils
35

    
36

    
37
class TestGetStorageUnitForDiskTemplate(unittest.TestCase):
38

    
39
  def setUp(self):
40
    self._default_vg_name = "some_vg_name"
41
    self._cfg = mock.Mock()
42
    self._cfg.GetVGName = mock.Mock(return_value=self._default_vg_name)
43

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

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

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

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

    
73
  def testGetDefaultStorageUnitForSpindles(self):
74
    (storage_type, storage_key) = \
75
        storage._GetDefaultStorageUnitForSpindles(self._cfg)
76
    self.assertEqual(storage_type, constants.ST_LVM_PV)
77
    self.assertEqual(storage_key, self._default_vg_name)
78

    
79

    
80
class TestGetStorageUnitsOfCluster(unittest.TestCase):
81

    
82
  def setUp(self):
83
    storage._GetDefaultStorageUnitForDiskTemplate = \
84
        mock.Mock(return_value=("foo", "bar"))
85

    
86
    self._cluster_cfg = objects.Cluster()
87
    self._enabled_disk_templates = \
88
        [constants.DT_DRBD8, constants.DT_PLAIN, constants.DT_FILE,
89
         constants.DT_SHARED_FILE]
90
    self._cluster_cfg.enabled_disk_templates = \
91
        self._enabled_disk_templates
92
    self._cfg = mock.Mock()
93
    self._cfg.GetClusterInfo = mock.Mock(return_value=self._cluster_cfg)
94
    self._cfg.GetVGName = mock.Mock(return_value="some_vg_name")
95

    
96
  def testGetStorageUnitsOfCluster(self):
97
    storage_units = storage.GetStorageUnitsOfCluster(self._cfg)
98
    self.assertEqual(len(storage_units), len(self._enabled_disk_templates))
99

    
100
  def testGetStorageUnitsOfClusterWithSpindles(self):
101
    storage_units = storage.GetStorageUnitsOfCluster(
102
        self._cfg, include_spindles=True)
103
    self.assertEqual(len(storage_units), len(self._enabled_disk_templates) + 1)
104
    self.assertTrue(constants.ST_LVM_PV in [st for (st, sk) in storage_units])
105

    
106

    
107
class TestLookupSpaceInfoByStorageType(unittest.TestCase):
108

    
109
  def setUp(self):
110
    self._space_info = [
111
        {"type": st, "name": st + "_key", "storage_size": 0, "storage_free": 0}
112
        for st in constants.VALID_STORAGE_TYPES]
113

    
114
  def testValidLookup(self):
115
    query_type = constants.ST_LVM_PV
116
    result = storage.LookupSpaceInfoByStorageType(self._space_info, query_type)
117
    self.assertEqual(query_type, result["type"])
118

    
119
  def testNotInList(self):
120
    result = storage.LookupSpaceInfoByStorageType(self._space_info,
121
                                                  "non_existing_type")
122
    self.assertEqual(None, result)
123

    
124

    
125
if __name__ == "__main__":
126
  testutils.GanetiTestProgram()