4 # Copyright (C) 2012 Google Inc.
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.
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.
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
22 """Script for testing ganeti.storage"""
28 from ganeti import constants
29 from ganeti import utils
30 from ganeti import compat
31 from ganeti import errors
32 from ganeti import storage
37 class TestVGReduce(testutils.GanetiTestCase):
39 LIST_CMD = storage.LvmVgStorage.LIST_COMMAND
40 VGREDUCE_CMD = storage.LvmVgStorage.VGREDUCE_COMMAND
42 def _runCmd(self, cmd, **kwargs):
43 if not self.run_history:
44 self.fail("Empty run results")
45 exp_cmd, result = self.run_history.pop(0)
46 self.assertEqual(cmd, exp_cmd)
49 def testOldVersion(self):
50 lvmvg = storage.LvmVgStorage()
51 stdout = self._ReadTestData("vgreduce-removemissing-2.02.02.txt")
52 vgs_fail = self._ReadTestData("vgs-missing-pvs-2.02.02.txt")
54 ([self.VGREDUCE_CMD, "--removemissing", self.VGNAME],
55 utils.RunResult(0, None, stdout, "", "", None, None)),
56 ([self.LIST_CMD, "--noheadings", "--nosuffix", self.VGNAME],
57 utils.RunResult(0, None, "", "", "", None, None)),
59 lvmvg._RemoveMissing(self.VGNAME, _runcmd_fn=self._runCmd)
60 self.assertEqual(self.run_history, [])
61 for ecode, out in [(1, ""), (0, vgs_fail)]:
63 ([self.VGREDUCE_CMD, "--removemissing", self.VGNAME],
64 utils.RunResult(0, None, stdout, "", "", None, None)),
65 ([self.LIST_CMD, "--noheadings", "--nosuffix", self.VGNAME],
66 utils.RunResult(ecode, None, out, "", "", None, None)),
68 self.assertRaises(errors.StorageError, lvmvg._RemoveMissing, self.VGNAME,
69 _runcmd_fn=self._runCmd)
70 self.assertEqual(self.run_history, [])
72 def testNewVersion(self):
73 lvmvg = storage.LvmVgStorage()
74 stdout1 = self._ReadTestData("vgreduce-removemissing-2.02.66-fail.txt")
75 stdout2 = self._ReadTestData("vgreduce-removemissing-2.02.66-ok.txt")
76 vgs_fail = self._ReadTestData("vgs-missing-pvs-2.02.66.txt")
77 # first: require --fail, check that it's used
79 ([self.VGREDUCE_CMD, "--removemissing", self.VGNAME],
80 utils.RunResult(0, None, stdout1, "", "", None, None)),
81 ([self.VGREDUCE_CMD, "--removemissing", "--force", self.VGNAME],
82 utils.RunResult(0, None, stdout2, "", "", None, None)),
83 ([self.LIST_CMD, "--noheadings", "--nosuffix", self.VGNAME],
84 utils.RunResult(0, None, "", "", "", None, None)),
86 lvmvg._RemoveMissing(self.VGNAME, _runcmd_fn=self._runCmd)
87 self.assertEqual(self.run_history, [])
88 # second: make sure --fail is not used if not needed
90 ([self.VGREDUCE_CMD, "--removemissing", self.VGNAME],
91 utils.RunResult(0, None, stdout2, "", "", None, None)),
92 ([self.LIST_CMD, "--noheadings", "--nosuffix", self.VGNAME],
93 utils.RunResult(0, None, "", "", "", None, None)),
95 lvmvg._RemoveMissing(self.VGNAME, _runcmd_fn=self._runCmd)
96 self.assertEqual(self.run_history, [])
97 # third: make sure we error out if vgs doesn't find the volume
98 for ecode, out in [(1, ""), (0, vgs_fail)]:
100 ([self.VGREDUCE_CMD, "--removemissing", self.VGNAME],
101 utils.RunResult(0, None, stdout1, "", "", None, None)),
102 ([self.VGREDUCE_CMD, "--removemissing", "--force", self.VGNAME],
103 utils.RunResult(0, None, stdout2, "", "", None, None)),
104 ([self.LIST_CMD, "--noheadings", "--nosuffix", self.VGNAME],
105 utils.RunResult(ecode, None, out, "", "", None, None)),
107 self.assertRaises(errors.StorageError, lvmvg._RemoveMissing, self.VGNAME,
108 _runcmd_fn=self._runCmd)
109 self.assertEqual(self.run_history, [])
112 if __name__ == "__main__":
113 testutils.GanetiTestProgram()