Refactor storage of runtime exclusive storage flag in QA
authorMichael Hanselmann <hansmi@google.com>
Tue, 5 Feb 2013 12:48:25 +0000 (13:48 +0100)
committerMichael Hanselmann <hansmi@google.com>
Fri, 8 Feb 2013 13:35:31 +0000 (14:35 +0100)
This is a follow-up for “qa_config: Remove exclusive storage flag from
config”. Instead of storing the flag in a module-level variable it is
now stored within the new QA configuration class and unit tests are
provided. Wrappers in “qa_config” maintain the existing interface.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Helga Velroyen <helgav@google.com>

Makefile.am
qa/qa_config.py
test/data/qa-minimal-nodes-instances-only.json [new file with mode: 0644]
test/py/qa.qa_config_unittest.py

index b1cbdac..7a06d12 100644 (file)
@@ -1035,6 +1035,7 @@ TEST_FILES = \
        test/data/proc_drbd83_sync.txt \
        test/data/proc_drbd83_sync_want.txt \
        test/data/proc_drbd83_sync_krnl2.6.39.txt \
+       test/data/qa-minimal-nodes-instances-only.json \
        test/data/sys_drbd_usermode_helper.txt \
        test/data/vgreduce-removemissing-2.02.02.txt \
        test/data/vgreduce-removemissing-2.02.66-fail.txt \
index 9b50efb..7ccbfdd 100644 (file)
@@ -36,10 +36,6 @@ import qa_error
 _INSTANCE_CHECK_KEY = "instance-check"
 _ENABLED_HV_KEY = "enabled-hypervisors"
 
-#: Cluster-wide run-time value of the exclusive storage flag
-_exclusive_storage = None
-
-
 #: QA configuration (L{_QaConfig})
 _config = None
 
@@ -51,6 +47,9 @@ class _QaConfig(object):
     """
     self._data = data
 
+    #: Cluster-wide run-time value of the exclusive storage flag
+    self._exclusive_storage = None
+
   @classmethod
   def Load(cls, filename):
     """Loads a configuration file and produces a configuration object.
@@ -159,6 +158,29 @@ class _QaConfig(object):
     """
     return self.GetEnabledHypervisors()[0]
 
+  def SetExclusiveStorage(self, value):
+    """Set the expected value of the C{exclusive_storage} flag for the cluster.
+
+    """
+    self._exclusive_storage = bool(value)
+
+  def GetExclusiveStorage(self):
+    """Get the expected value of the C{exclusive_storage} flag for the cluster.
+
+    """
+    value = self._exclusive_storage
+    assert value is not None
+    return value
+
+  def IsTemplateSupported(self, templ):
+    """Is the given disk template supported by the current configuration?
+
+    """
+    if self.GetExclusiveStorage():
+      return templ in constants.DTS_EXCL_STORAGE
+    else:
+      return True
+
 
 def Load(path):
   """Loads the passed configuration file.
@@ -337,31 +359,24 @@ def SetInstanceTemplate(inst, template):
 
 
 def SetExclusiveStorage(value):
-  """Set the expected value of the exclusive_storage flag for the cluster.
+  """Wrapper for L{_QaConfig.SetExclusiveStorage}.
 
   """
-  global _exclusive_storage # pylint: disable=W0603
-
-  _exclusive_storage = bool(value)
+  return GetConfig().SetExclusiveStorage(value)
 
 
 def GetExclusiveStorage():
-  """Get the expected value of the exclusive_storage flag for the cluster.
+  """Wrapper for L{_QaConfig.GetExclusiveStorage}.
 
   """
-  val = _exclusive_storage
-  assert val is not None
-  return val
+  return GetConfig().GetExclusiveStorage()
 
 
 def IsTemplateSupported(templ):
-  """Is the given disk template supported by the current configuration?
+  """Wrapper for L{_QaConfig.GetExclusiveStorage}.
 
   """
-  if GetExclusiveStorage():
-    return templ in constants.DTS_EXCL_STORAGE
-  else:
-    return True
+  return GetConfig().IsTemplateSupported(templ)
 
 
 def AcquireNode(exclude=None):
diff --git a/test/data/qa-minimal-nodes-instances-only.json b/test/data/qa-minimal-nodes-instances-only.json
new file mode 100644 (file)
index 0000000..38b51b5
--- /dev/null
@@ -0,0 +1,46 @@
+{
+  "name": "xen-test-qa-minimal-nodes-instances-only",
+
+  "disk": ["1G", "512M"],
+  "disk-growth": ["2G", "768M"],
+
+  "nodes": [
+    {
+      "# Master node": null,
+      "primary": "xen-test-0",
+      "secondary": "192.0.2.1"
+    },
+
+    {
+      "primary": "xen-test-1",
+      "secondary": "192.0.2.2"
+    },
+
+    {
+      "primary": "xen-test-2",
+      "secondary": "192.0.2.3"
+    },
+
+    {
+      "primary": "xen-test-3",
+      "secondary": "192.0.2.4"
+    }
+  ],
+
+  "instances": [
+    {
+      "name": "xen-test-inst1",
+      "nic.mac/0": "AA:00:00:11:11:11"
+    },
+    {
+      "name": "xen-test-inst2",
+      "nic.mac/0": "AA:00:00:22:22:22"
+    }
+  ],
+
+  "tests": {
+    "default": false
+  },
+
+  "# vim: set syntax=javascript :": null
+}
index 2755719..710b706 100755 (executable)
@@ -252,5 +252,26 @@ class TestQaConfigWithSampleConfig(unittest.TestCase):
     self.assertEqual(self.config.GetMasterNode(), self.config["nodes"][0])
 
 
+class TestQaConfig(unittest.TestCase):
+  def setUp(self):
+    filename = \
+      testutils.TestDataFilename("qa-minimal-nodes-instances-only.json")
+
+    self.config = qa_config._QaConfig.Load(filename)
+
+  def testExclusiveStorage(self):
+    self.assertRaises(AssertionError, self.config.GetExclusiveStorage)
+
+    for value in [False, True, 0, 1, 30804, ""]:
+      self.config.SetExclusiveStorage(value)
+      self.assertEqual(self.config.GetExclusiveStorage(), bool(value))
+
+      for template in constants.DISK_TEMPLATES:
+        if value and template not in constants.DTS_EXCL_STORAGE:
+          self.assertFalse(self.config.IsTemplateSupported(template))
+        else:
+          self.assertTrue(self.config.IsTemplateSupported(template))
+
+
 if __name__ == "__main__":
   testutils.GanetiTestProgram()