Modify gnt-node add to call external script
[ganeti-local] / test / ganeti.config_unittest.py
index 36188fa..32a8cb8 100755 (executable)
@@ -1,7 +1,7 @@
 #!/usr/bin/python
 #
 
-# Copyright (C) 2006, 2007 Google Inc.
+# Copyright (C) 2006, 2007, 2010 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
@@ -29,11 +29,15 @@ import tempfile
 import os.path
 import socket
 
-from ganeti import errors
-from ganeti import constants
+from ganeti import bootstrap
 from ganeti import config
+from ganeti import constants
+from ganeti import errors
 from ganeti import objects
 from ganeti import utils
+from ganeti import netutils
+
+import testutils
 
 
 class TestConfigRunner(unittest.TestCase):
@@ -41,6 +45,7 @@ class TestConfigRunner(unittest.TestCase):
   def setUp(self):
     fd, self.cfg_file = tempfile.mkstemp()
     os.close(fd)
+    self._init_cluster(self.cfg_file)
 
   def tearDown(self):
     try:
@@ -55,8 +60,8 @@ class TestConfigRunner(unittest.TestCase):
 
   def _init_cluster(self, cfg):
     """Initializes the cfg object"""
-    me = utils.HostInfo()
-    ip = constants.LOCALHOST_IP_ADDRESS
+    me = netutils.Hostname()
+    ip = constants.IP4_ADDRESS_LOCALHOST
 
     cluster_config = objects.Cluster(
       serial_no=1,
@@ -64,27 +69,32 @@ class TestConfigRunner(unittest.TestCase):
       highest_used_port=(constants.FIRST_DRBD_PORT - 1),
       mac_prefix="aa:00:00",
       volume_group_name="xenvg",
-      default_bridge=constants.DEFAULT_BRIDGE,
+      drbd_usermode_helper="/bin/true",
+      nicparams={constants.PP_DEFAULT: constants.NICC_DEFAULTS},
       tcpudp_port_pool=set(),
-      hypervisor=constants.HT_FAKE,
+      enabled_hypervisors=[constants.HT_FAKE],
       master_node=me.name,
       master_ip="127.0.0.1",
       master_netdev=constants.DEFAULT_BRIDGE,
       cluster_name="cluster.local",
       file_storage_dir="/tmp",
+      uid_pool=[],
       )
 
     master_node_config = objects.Node(name=me.name,
                                       primary_ip=me.ip,
-                                      secondary_ip=ip)
+                                      secondary_ip=ip,
+                                      serial_no=1,
+                                      master_candidate=True)
 
-    cfg.InitConfig(constants.CONFIG_VERSION,
-                   cluster_config, master_node_config)
+    bootstrap.InitConfig(constants.CONFIG_VERSION,
+                         cluster_config, master_node_config, self.cfg_file)
 
   def _create_instance(self):
     """Create and return an instance object"""
-    inst = objects.Instance(name="test.example.com", disks=[],
-                            disk_template=constants.DT_DISKLESS)
+    inst = objects.Instance(name="test.example.com", disks=[], nics=[],
+                            disk_template=constants.DT_DISKLESS,
+                            primary_node=self._get_object().GetMasterNode())
     return inst
 
   def testEmpty(self):
@@ -94,7 +104,6 @@ class TestConfigRunner(unittest.TestCase):
   def testInit(self):
     """Test initialize the config file"""
     cfg = self._get_object()
-    self._init_cluster(cfg)
     self.failUnlessEqual(1, len(cfg.GetNodeList()))
     self.failUnlessEqual(0, len(cfg.GetInstanceList()))
 
@@ -104,16 +113,15 @@ class TestConfigRunner(unittest.TestCase):
     # construct a fake cluster object
     fake_cl = objects.Cluster()
     # fail if we didn't read the config
-    self.failUnlessRaises(errors.ProgrammerError, cfg.Update, fake_cl)
+    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_cl, None)
 
-    self._init_cluster(cfg)
     cl = cfg.GetClusterInfo()
     # first pass, must not fail
-    cfg.Update(cl)
+    cfg.Update(cl, None)
     # second pass, also must not fail (after the config has been written)
-    cfg.Update(cl)
+    cfg.Update(cl, None)
     # but the fake_cl update should still fail
-    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_cl)
+    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_cl, None)
 
   def testUpdateNode(self):
     """Test updates on one node object"""
@@ -121,16 +129,17 @@ class TestConfigRunner(unittest.TestCase):
     # construct a fake node
     fake_node = objects.Node()
     # fail if we didn't read the config
-    self.failUnlessRaises(errors.ProgrammerError, cfg.Update, fake_node)
+    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_node,
+                          None)
 
-    self._init_cluster(cfg)
     node = cfg.GetNodeInfo(cfg.GetNodeList()[0])
     # first pass, must not fail
-    cfg.Update(node)
+    cfg.Update(node, None)
     # second pass, also must not fail (after the config has been written)
-    cfg.Update(node)
+    cfg.Update(node, None)
     # but the fake_node update should still fail
-    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_node)
+    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_node,
+                          None)
 
   def testUpdateInstance(self):
     """Test updates on one instance object"""
@@ -139,18 +148,37 @@ class TestConfigRunner(unittest.TestCase):
     inst = self._create_instance()
     fake_instance = objects.Instance()
     # fail if we didn't read the config
-    self.failUnlessRaises(errors.ProgrammerError, cfg.Update, fake_instance)
+    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_instance,
+                          None)
 
-    self._init_cluster(cfg)
-    cfg.AddInstance(inst)
+    cfg.AddInstance(inst, "my-job")
     instance = cfg.GetInstanceInfo(cfg.GetInstanceList()[0])
     # first pass, must not fail
-    cfg.Update(instance)
+    cfg.Update(instance, None)
     # second pass, also must not fail (after the config has been written)
-    cfg.Update(instance)
+    cfg.Update(instance, None)
     # but the fake_instance update should still fail
-    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_instance)
+    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_instance,
+                          None)
+
+  def testNICParameterSyntaxCheck(self):
+    """Test the NIC's CheckParameterSyntax function"""
+    mode = constants.NIC_MODE
+    link = constants.NIC_LINK
+    m_bridged = constants.NIC_MODE_BRIDGED
+    m_routed = constants.NIC_MODE_ROUTED
+    CheckSyntax = objects.NIC.CheckParameterSyntax
+
+    CheckSyntax(constants.NICC_DEFAULTS)
+    CheckSyntax({mode: m_bridged, link: 'br1'})
+    CheckSyntax({mode: m_routed, link: 'default'})
+    self.assertRaises(errors.ConfigurationError,
+                      CheckSyntax, {mode: '000invalid', link: 'any'})
+    self.assertRaises(errors.ConfigurationError,
+                      CheckSyntax, {mode: m_bridged, link: None})
+    self.assertRaises(errors.ConfigurationError,
+                      CheckSyntax, {mode: m_bridged, link: ''})
 
 
 if __name__ == '__main__':
-  unittest.main()
+  testutils.GanetiTestProgram()