workerpool: Preserve task number when deferring
[ganeti-local] / test / cfgupgrade_unittest.py
index 06a6f2c..4d38b26 100755 (executable)
@@ -1,7 +1,7 @@
 #!/usr/bin/python
 #
 
-# Copyright (C) 2010 Google Inc.
+# Copyright (C) 2010, 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
@@ -32,13 +32,17 @@ from ganeti import constants
 from ganeti import utils
 from ganeti import errors
 from ganeti import serializer
+from ganeti import netutils
 
 import testutils
 
 
-def _RunUpgrade(path, dry_run, no_verify):
+def _RunUpgrade(path, dry_run, no_verify, ignore_hostname=True):
   cmd = [sys.executable, "%s/tools/cfgupgrade" % testutils.GetSourceDir(),
-         "--debug", "--force", "--path=%s" % path]
+         "--debug", "--force", "--path=%s" % path, "--confdir=%s" % path]
+
+  if ignore_hostname:
+    cmd.append("--ignore-hostname")
   if dry_run:
     cmd.append("--dry-run")
   if no_verify:
@@ -62,6 +66,8 @@ class TestCfgupgrade(unittest.TestCase):
     self.known_hosts_path = utils.PathJoin(self.tmpdir, "known_hosts")
     self.confd_hmac_path = utils.PathJoin(self.tmpdir, "hmac.key")
     self.cds_path = utils.PathJoin(self.tmpdir, "cluster-domain-secret")
+    self.ss_master_node_path = utils.PathJoin(self.tmpdir, "ssconf_master_node")
+    self.file_storage_paths = utils.PathJoin(self.tmpdir, "file-storage-paths")
 
   def tearDown(self):
     shutil.rmtree(self.tmpdir)
@@ -72,12 +78,43 @@ class TestCfgupgrade(unittest.TestCase):
   def _CreateValidConfigDir(self):
     utils.WriteFile(self.noded_cert_path, data="")
     utils.WriteFile(self.known_hosts_path, data="")
+    utils.WriteFile(self.ss_master_node_path,
+                    data="node.has.another.name.example.net")
 
   def testNoConfigDir(self):
     self.assertFalse(utils.ListVisibleFiles(self.tmpdir))
     self.assertRaises(Exception, _RunUpgrade, self.tmpdir, False, True)
     self.assertRaises(Exception, _RunUpgrade, self.tmpdir, True, True)
 
+  def testWrongHostname(self):
+    self._CreateValidConfigDir()
+
+    utils.WriteFile(self.config_path, data=serializer.DumpJson({
+      "version": constants.CONFIG_VERSION,
+      "cluster": {},
+      "instances": {},
+      }))
+
+    hostname = netutils.GetHostname().name
+    assert hostname != utils.ReadOneLineFile(self.ss_master_node_path)
+
+    self.assertRaises(Exception, _RunUpgrade, self.tmpdir, False, True,
+                      ignore_hostname=False)
+
+  def testCorrectHostname(self):
+    self._CreateValidConfigDir()
+
+    utils.WriteFile(self.config_path, data=serializer.DumpJson({
+      "version": constants.CONFIG_VERSION,
+      "cluster": {},
+      "instances": {},
+      }))
+
+    utils.WriteFile(self.ss_master_node_path,
+                    data="%s\n" % netutils.GetHostname().name)
+
+    _RunUpgrade(self.tmpdir, False, True, ignore_hostname=False)
+
   def testInconsistentConfig(self):
     self._CreateValidConfigDir()
     # There should be no "config_version"
@@ -86,6 +123,7 @@ class TestCfgupgrade(unittest.TestCase):
       "cluster": {
         "config_version": 0,
         },
+      "instances": {},
       }
     utils.WriteFile(self.config_path, data=serializer.DumpJson(cfg))
     self.assertRaises(Exception, _RunUpgrade, self.tmpdir, False, True)
@@ -96,10 +134,20 @@ class TestCfgupgrade(unittest.TestCase):
     utils.WriteFile(self.config_path, data=serializer.DumpJson({}))
     self.assertRaises(Exception, _RunUpgrade, self.tmpdir, False, True)
 
-  def _TestSimpleUpgrade(self, from_version, dry_run):
+  def _TestSimpleUpgrade(self, from_version, dry_run,
+                         file_storage_dir=None,
+                         shared_file_storage_dir=None):
+    cluster = {}
+
+    if file_storage_dir:
+      cluster["file_storage_dir"] = file_storage_dir
+    if shared_file_storage_dir:
+      cluster["shared_file_storage_dir"] = shared_file_storage_dir
+
     cfg = {
       "version": from_version,
-      "cluster": {},
+      "cluster": cluster,
+      "instances": {},
       }
     self._CreateValidConfigDir()
     utils.WriteFile(self.config_path, data=serializer.DumpJson(cfg))
@@ -127,12 +175,16 @@ class TestCfgupgrade(unittest.TestCase):
   def testRapiUsers(self):
     self.assertFalse(os.path.exists(self.rapi_users_path))
     self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
+    self.assertFalse(os.path.exists(os.path.dirname(self.rapi_users_path)))
 
     utils.WriteFile(self.rapi_users_path_pre24, data="some user\n")
     self._TestSimpleUpgrade(constants.BuildVersion(2, 3, 0), False)
 
+    self.assertTrue(os.path.isdir(os.path.dirname(self.rapi_users_path)))
     self.assert_(os.path.islink(self.rapi_users_path_pre24))
     self.assert_(os.path.isfile(self.rapi_users_path))
+    self.assertEqual(os.readlink(self.rapi_users_path_pre24),
+                     self.rapi_users_path)
     for path in [self.rapi_users_path, self.rapi_users_path_pre24]:
       self.assertEqual(utils.ReadFile(path), "some user\n")
 
@@ -146,6 +198,8 @@ class TestCfgupgrade(unittest.TestCase):
 
     self.assert_(os.path.islink(self.rapi_users_path_pre24))
     self.assert_(os.path.isfile(self.rapi_users_path))
+    self.assertEqual(os.readlink(self.rapi_users_path_pre24),
+                     self.rapi_users_path)
     for path in [self.rapi_users_path, self.rapi_users_path_pre24]:
       self.assertEqual(utils.ReadFile(path), "other user\n")
 
@@ -153,16 +207,118 @@ class TestCfgupgrade(unittest.TestCase):
     self.assertFalse(os.path.exists(self.rapi_users_path))
     self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
 
+    os.mkdir(os.path.dirname(self.rapi_users_path))
     os.symlink(self.rapi_users_path, self.rapi_users_path_pre24)
-    utils.WriteFile(self.rapi_users_path_pre24, data="hello world\n")
+    utils.WriteFile(self.rapi_users_path, data="hello world\n")
 
     self._TestSimpleUpgrade(constants.BuildVersion(2, 2, 0), False)
 
-    self.assert_(os.path.isfile(self.rapi_users_path))
+    self.assert_(os.path.isfile(self.rapi_users_path) and
+                 not os.path.islink(self.rapi_users_path))
     self.assert_(os.path.islink(self.rapi_users_path_pre24))
+    self.assertEqual(os.readlink(self.rapi_users_path_pre24),
+                     self.rapi_users_path)
+    for path in [self.rapi_users_path, self.rapi_users_path_pre24]:
+      self.assertEqual(utils.ReadFile(path), "hello world\n")
+
+  def testRapiUsersExistingTarget(self):
+    self.assertFalse(os.path.exists(self.rapi_users_path))
+    self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
+
+    os.mkdir(os.path.dirname(self.rapi_users_path))
+    utils.WriteFile(self.rapi_users_path, data="other user\n")
+    utils.WriteFile(self.rapi_users_path_pre24, data="hello world\n")
+
+    self.assertRaises(Exception, self._TestSimpleUpgrade,
+                      constants.BuildVersion(2, 2, 0), False)
+
+    for path in [self.rapi_users_path, self.rapi_users_path_pre24]:
+      self.assert_(os.path.isfile(path) and not os.path.islink(path))
+    self.assertEqual(utils.ReadFile(self.rapi_users_path), "other user\n")
+    self.assertEqual(utils.ReadFile(self.rapi_users_path_pre24),
+                     "hello world\n")
+
+  def testRapiUsersDryRun(self):
+    self.assertFalse(os.path.exists(self.rapi_users_path))
+    self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
+
+    utils.WriteFile(self.rapi_users_path_pre24, data="some user\n")
+    self._TestSimpleUpgrade(constants.BuildVersion(2, 3, 0), True)
+
+    self.assertFalse(os.path.isdir(os.path.dirname(self.rapi_users_path)))
+    self.assertTrue(os.path.isfile(self.rapi_users_path_pre24) and
+                    not os.path.islink(self.rapi_users_path_pre24))
+    self.assertFalse(os.path.exists(self.rapi_users_path))
+
+  def testRapiUsers24AndAboveDryRun(self):
+    self.assertFalse(os.path.exists(self.rapi_users_path))
+    self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
+
+    os.mkdir(os.path.dirname(self.rapi_users_path))
+    utils.WriteFile(self.rapi_users_path, data="other user\n")
+    self._TestSimpleUpgrade(constants.BuildVersion(2, 3, 0), True)
+
+    self.assertTrue(os.path.isfile(self.rapi_users_path) and
+                    not os.path.islink(self.rapi_users_path))
+    self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
+    self.assertEqual(utils.ReadFile(self.rapi_users_path), "other user\n")
+
+  def testRapiUsersExistingSymlinkDryRun(self):
+    self.assertFalse(os.path.exists(self.rapi_users_path))
+    self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
+
+    os.mkdir(os.path.dirname(self.rapi_users_path))
+    os.symlink(self.rapi_users_path, self.rapi_users_path_pre24)
+    utils.WriteFile(self.rapi_users_path, data="hello world\n")
+
+    self._TestSimpleUpgrade(constants.BuildVersion(2, 2, 0), True)
+
+    self.assertTrue(os.path.islink(self.rapi_users_path_pre24))
+    self.assertTrue(os.path.isfile(self.rapi_users_path) and
+                    not os.path.islink(self.rapi_users_path))
+    self.assertEqual(os.readlink(self.rapi_users_path_pre24),
+                     self.rapi_users_path)
     for path in [self.rapi_users_path, self.rapi_users_path_pre24]:
       self.assertEqual(utils.ReadFile(path), "hello world\n")
 
+  def testFileStoragePathsDryRun(self):
+    self.assertFalse(os.path.exists(self.file_storage_paths))
+
+    self._TestSimpleUpgrade(constants.BuildVersion(2, 6, 0), True,
+                            file_storage_dir=self.tmpdir,
+                            shared_file_storage_dir="/tmp")
+
+    self.assertFalse(os.path.exists(self.file_storage_paths))
+
+  def testFileStoragePathsBoth(self):
+    self.assertFalse(os.path.exists(self.file_storage_paths))
+
+    self._TestSimpleUpgrade(constants.BuildVersion(2, 6, 0), False,
+                            file_storage_dir=self.tmpdir,
+                            shared_file_storage_dir="/tmp")
+
+    lines = utils.ReadFile(self.file_storage_paths).splitlines()
+    self.assertTrue(lines.pop(0).startswith("# "))
+    self.assertTrue(lines.pop(0).startswith("# cfgupgrade"))
+    self.assertEqual(lines.pop(0), self.tmpdir)
+    self.assertEqual(lines.pop(0), "/tmp")
+    self.assertFalse(lines)
+    self.assertEqual(os.stat(self.file_storage_paths).st_mode & 0777,
+                     0600, msg="Wrong permissions")
+
+  def testFileStoragePathsSharedOnly(self):
+    self.assertFalse(os.path.exists(self.file_storage_paths))
+
+    self._TestSimpleUpgrade(constants.BuildVersion(2, 5, 0), False,
+                            file_storage_dir=None,
+                            shared_file_storage_dir=self.tmpdir)
+
+    lines = utils.ReadFile(self.file_storage_paths).splitlines()
+    self.assertTrue(lines.pop(0).startswith("# "))
+    self.assertTrue(lines.pop(0).startswith("# cfgupgrade"))
+    self.assertEqual(lines.pop(0), self.tmpdir)
+    self.assertFalse(lines)
+
   def testUpgradeFrom_2_0(self):
     self._TestSimpleUpgrade(constants.BuildVersion(2, 0, 0), False)
 
@@ -175,6 +331,15 @@ class TestCfgupgrade(unittest.TestCase):
   def testUpgradeFrom_2_3(self):
     self._TestSimpleUpgrade(constants.BuildVersion(2, 3, 0), False)
 
+  def testUpgradeFrom_2_4(self):
+    self._TestSimpleUpgrade(constants.BuildVersion(2, 4, 0), False)
+
+  def testUpgradeFrom_2_5(self):
+    self._TestSimpleUpgrade(constants.BuildVersion(2, 5, 0), False)
+
+  def testUpgradeFrom_2_6(self):
+    self._TestSimpleUpgrade(constants.BuildVersion(2, 6, 0), False)
+
   def testUpgradeCurrent(self):
     self._TestSimpleUpgrade(constants.CONFIG_VERSION, False)
 
@@ -190,6 +355,15 @@ class TestCfgupgrade(unittest.TestCase):
   def testUpgradeDryRunFrom_2_3(self):
     self._TestSimpleUpgrade(constants.BuildVersion(2, 3, 0), True)
 
+  def testUpgradeDryRunFrom_2_4(self):
+    self._TestSimpleUpgrade(constants.BuildVersion(2, 4, 0), True)
+
+  def testUpgradeDryRunFrom_2_5(self):
+    self._TestSimpleUpgrade(constants.BuildVersion(2, 5, 0), True)
+
+  def testUpgradeDryRunFrom_2_6(self):
+    self._TestSimpleUpgrade(constants.BuildVersion(2, 6, 0), True)
+
   def testUpgradeCurrentDryRun(self):
     self._TestSimpleUpgrade(constants.CONFIG_VERSION, True)