ganeti.storage.container_unittest.py 4.49 KB
Newer Older
Iustin Pop's avatar
Iustin Pop committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/usr/bin/python
#

# Copyright (C) 2012 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.


22
"""Script for testing ganeti.storage.container"""
Iustin Pop's avatar
Iustin Pop committed
23 24 25 26 27 28 29 30 31

import re
import unittest
import random

from ganeti import constants
from ganeti import utils
from ganeti import compat
from ganeti import errors
32
from ganeti.storage import container
Iustin Pop's avatar
Iustin Pop committed
33 34 35 36 37 38

import testutils


class TestVGReduce(testutils.GanetiTestCase):
  VGNAME = "xenvg"
39 40
  LIST_CMD = container.LvmVgStorage.LIST_COMMAND
  VGREDUCE_CMD = container.LvmVgStorage.VGREDUCE_COMMAND
Iustin Pop's avatar
Iustin Pop committed
41 42 43 44 45 46 47 48 49

  def _runCmd(self, cmd, **kwargs):
    if not self.run_history:
      self.fail("Empty run results")
    exp_cmd, result = self.run_history.pop(0)
    self.assertEqual(cmd, exp_cmd)
    return result

  def testOldVersion(self):
50
    lvmvg = container.LvmVgStorage()
51 52
    stdout = testutils.ReadTestData("vgreduce-removemissing-2.02.02.txt")
    vgs_fail = testutils.ReadTestData("vgs-missing-pvs-2.02.02.txt")
Iustin Pop's avatar
Iustin Pop committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    self.run_history = [
      ([self.VGREDUCE_CMD, "--removemissing", self.VGNAME],
       utils.RunResult(0, None, stdout, "", "", None, None)),
      ([self.LIST_CMD, "--noheadings", "--nosuffix", self.VGNAME],
       utils.RunResult(0, None, "", "", "", None, None)),
      ]
    lvmvg._RemoveMissing(self.VGNAME, _runcmd_fn=self._runCmd)
    self.assertEqual(self.run_history, [])
    for ecode, out in [(1, ""), (0, vgs_fail)]:
      self.run_history = [
        ([self.VGREDUCE_CMD, "--removemissing", self.VGNAME],
         utils.RunResult(0, None, stdout, "", "", None, None)),
        ([self.LIST_CMD, "--noheadings", "--nosuffix", self.VGNAME],
         utils.RunResult(ecode, None, out, "", "", None, None)),
        ]
      self.assertRaises(errors.StorageError, lvmvg._RemoveMissing, self.VGNAME,
                        _runcmd_fn=self._runCmd)
      self.assertEqual(self.run_history, [])

  def testNewVersion(self):
73
    lvmvg = container.LvmVgStorage()
74 75 76
    stdout1 = testutils.ReadTestData("vgreduce-removemissing-2.02.66-fail.txt")
    stdout2 = testutils.ReadTestData("vgreduce-removemissing-2.02.66-ok.txt")
    vgs_fail = testutils.ReadTestData("vgs-missing-pvs-2.02.66.txt")
Iustin Pop's avatar
Iustin Pop committed
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
    # first: require --fail, check that it's used
    self.run_history = [
      ([self.VGREDUCE_CMD, "--removemissing", self.VGNAME],
       utils.RunResult(0, None, stdout1, "", "", None, None)),
      ([self.VGREDUCE_CMD, "--removemissing", "--force", self.VGNAME],
       utils.RunResult(0, None, stdout2, "", "", None, None)),
      ([self.LIST_CMD, "--noheadings", "--nosuffix", self.VGNAME],
       utils.RunResult(0, None, "", "", "", None, None)),
      ]
    lvmvg._RemoveMissing(self.VGNAME, _runcmd_fn=self._runCmd)
    self.assertEqual(self.run_history, [])
    # second: make sure --fail is not used if not needed
    self.run_history = [
      ([self.VGREDUCE_CMD, "--removemissing", self.VGNAME],
       utils.RunResult(0, None, stdout2, "", "", None, None)),
      ([self.LIST_CMD, "--noheadings", "--nosuffix", self.VGNAME],
       utils.RunResult(0, None, "", "", "", None, None)),
      ]
    lvmvg._RemoveMissing(self.VGNAME, _runcmd_fn=self._runCmd)
    self.assertEqual(self.run_history, [])
    # third: make sure we error out if vgs doesn't find the volume
    for ecode, out in [(1, ""), (0, vgs_fail)]:
      self.run_history = [
        ([self.VGREDUCE_CMD, "--removemissing", self.VGNAME],
         utils.RunResult(0, None, stdout1, "", "", None, None)),
        ([self.VGREDUCE_CMD, "--removemissing", "--force", self.VGNAME],
         utils.RunResult(0, None, stdout2, "", "", None, None)),
        ([self.LIST_CMD, "--noheadings", "--nosuffix", self.VGNAME],
         utils.RunResult(ecode, None, out, "", "", None, None)),
        ]
      self.assertRaises(errors.StorageError, lvmvg._RemoveMissing, self.VGNAME,
                        _runcmd_fn=self._runCmd)
      self.assertEqual(self.run_history, [])


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