Bulk-removal of SSH keys of normal nodes
authorHelga Velroyen <helgav@google.com>
Fri, 20 Nov 2015 09:41:12 +0000 (10:41 +0100)
committerHelga Velroyen <helgav@google.com>
Thu, 17 Dec 2015 08:13:00 +0000 (09:13 +0100)
This patch adds a unit test for bulk-removing
normal nodes. Besides that, it fixes a small
bug that surfaced with that test.

Signed-off-by: Helga Velroyen <helgav@google.com>
Reviewed-by: Lisa Velden <velden@google.com>

lib/backend.py
test/py/ganeti.backend_unittest.py

index 16d3146..d4756be 100644 (file)
@@ -1927,8 +1927,8 @@ def RemoveNodeSshKeyBulk(node_list,
   master_node = ssconf_store.GetMasterNode()
   ssh_port_map = ssconf_store.GetSshPortMap()
 
+  all_keys_to_remove = {}
   if from_authorized_keys or from_public_keys:
-    all_keys_to_remove = {}
     for node_info in node_list:
       if node_info.name == master_node and not keys_to_remove:
         raise errors.SshUpdateError("Cannot remove the master node's keys.")
index 31c3973..f9f0065 100755 (executable)
@@ -1478,6 +1478,37 @@ class TestAddRemoveGenerateNodeSshKey(testutils.GanetiTestCase):
     self.assertEqual(1,
         len(self._ssh_file_manager.GetAuthorizedKeysOfNode(node_name)))
 
+  def testRemoveNormalNodeBulk(self):
+    node_list = []
+    key_map = {}
+    for node_name, (node_uuid, node_key, _, _, _) in \
+        self._ssh_file_manager.GetAllNormalNodes()[:3]:
+      node_list.append(backend.SshRemoveNodeInfo(uuid=node_uuid,
+                                                 name=node_name,
+                                                 from_authorized_keys=False,
+                                                 from_public_keys=False,
+                                                 clear_authorized_keys=True,
+                                                 clear_public_keys=True))
+      key_map[node_name] = node_key
+
+    backend.RemoveNodeSshKeyBulk(node_list,
+                                 self._master_candidate_uuids,
+                                 self._potential_master_candidates,
+                                 pub_key_file=self._pub_key_file,
+                                 ssconf_store=self._ssconf_mock,
+                                 noded_cert_file=self.noded_cert_file,
+                                 run_cmd_fn=self._run_cmd_mock)
+
+    for node_info in node_list:
+      self._ssh_file_manager.AssertNoNodeHasPublicKey(
+          node_info.uuid, key_map[node_info.name])
+      self._ssh_file_manager.AssertNodeSetOnlyHasAuthorizedKey(
+          [node_info.name], key_map[node_info.name])
+      self.assertEqual(0,
+          len(self._ssh_file_manager.GetPublicKeysOfNode(node_info.name)))
+      self.assertEqual(1,
+          len(self._ssh_file_manager.GetAuthorizedKeysOfNode(node_info.name)))
+
   def testDemoteMasterCandidateToPotentialMasterCandidate(self):
     node_name, node_info = self._ssh_file_manager.GetAllMasterCandidates()[0]
     self._ssh_file_manager.SetOrAddNode(