ganeti.utils.storage_unittest.py 4.84 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/python
#

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


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

import mock

import unittest

from ganeti import constants
from ganeti import objects
from ganeti.utils import storage

import testutils


class TestGetStorageUnitForDiskTemplate(unittest.TestCase):

  def setUp(self):
    self._default_vg_name = "some_vg_name"
39
40
    self._cluster = mock.Mock()
    self._cluster.file_storage_dir = "my/file/storage/dir"
41
    self._cluster.shared_file_storage_dir = "my/shared/file/storage/dir"
42
43
    self._cfg = mock.Mock()
    self._cfg.GetVGName = mock.Mock(return_value=self._default_vg_name)
44
    self._cfg.GetClusterInfo = mock.Mock(return_value=self._cluster)
45
46
47
48
49
50
51
52
53
54
55
56
57
58

  def testGetDefaultStorageUnitForDiskTemplateLvm(self):
    for disk_template in [constants.DT_DRBD8, constants.DT_PLAIN]:
      (storage_type, storage_key) = \
          storage._GetDefaultStorageUnitForDiskTemplate(self._cfg,
                                                        disk_template)
      self.assertEqual(storage_type, constants.ST_LVM_VG)
      self.assertEqual(storage_key, self._default_vg_name)

  def testGetDefaultStorageUnitForDiskTemplateFile(self):
    (storage_type, storage_key) = \
        storage._GetDefaultStorageUnitForDiskTemplate(self._cfg,
                                                      constants.DT_FILE)
    self.assertEqual(storage_type, constants.ST_FILE)
59
    self.assertEqual(storage_key, self._cluster.file_storage_dir)
60
61
62
63
64
65

  def testGetDefaultStorageUnitForDiskTemplateSharedFile(self):
    (storage_type, storage_key) = \
        storage._GetDefaultStorageUnitForDiskTemplate(self._cfg,
                                                      constants.DT_SHARED_FILE)
    self.assertEqual(storage_type, constants.ST_FILE)
66
    self.assertEqual(storage_key, self._cluster.shared_file_storage_dir)
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

  def testGetDefaultStorageUnitForDiskTemplateDiskless(self):
    (storage_type, storage_key) = \
        storage._GetDefaultStorageUnitForDiskTemplate(self._cfg,
                                                      constants.DT_DISKLESS)
    self.assertEqual(storage_type, constants.ST_DISKLESS)
    self.assertEqual(storage_key, None)

  def testGetDefaultStorageUnitForSpindles(self):
    (storage_type, storage_key) = \
        storage._GetDefaultStorageUnitForSpindles(self._cfg)
    self.assertEqual(storage_type, constants.ST_LVM_PV)
    self.assertEqual(storage_key, self._default_vg_name)


class TestGetStorageUnitsOfCluster(unittest.TestCase):

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

    self._cluster_cfg = objects.Cluster()
    self._enabled_disk_templates = \
        [constants.DT_DRBD8, constants.DT_PLAIN, constants.DT_FILE,
         constants.DT_SHARED_FILE]
    self._cluster_cfg.enabled_disk_templates = \
        self._enabled_disk_templates
    self._cfg = mock.Mock()
    self._cfg.GetClusterInfo = mock.Mock(return_value=self._cluster_cfg)
    self._cfg.GetVGName = mock.Mock(return_value="some_vg_name")

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

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


class TestLookupSpaceInfoByStorageType(unittest.TestCase):

  def setUp(self):
    self._space_info = [
        {"type": st, "name": st + "_key", "storage_size": 0, "storage_free": 0}
114
        for st in constants.STORAGE_TYPES]
115
116
117
118
119
120
121
122
123
124
125
126
127
128

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

  def testNotInList(self):
    result = storage.LookupSpaceInfoByStorageType(self._space_info,
                                                  "non_existing_type")
    self.assertEqual(None, result)


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