def _create_node(self, node_index, container=None): node = DockerNode(parent_cluster=self, container=container, ssh_login_info=dict( hostname=None, user=self.node_container_user, key_file=self.node_container_key_file), base_logdir=self.logdir, node_prefix=self.node_prefix, node_index=node_index) if container is None: ContainerManager.build_container_image(node, "node") ContainerManager.run_container( node, "node", seed_ip=self.nodes[0].public_ip_address if node_index else None) ContainerManager.wait_for_status(node, "node", status="running") ContainerManager.ssh_copy_id(node, "node", self.node_container_user, self.node_container_key_file) node.init() return node
def test_ssh_copy_id(self): with self.subTest("Copy SSH pub key to non-existent container"): self.assertRaises(NotFound, ContainerManager.ssh_copy_id, self.node, "c2", user="******", key_file="/root/.ssh/id_rsa") with self.subTest("Copy SSH pub key"): self.container.exec_run = Mock() self.container.exec_run.return_value.exit_code = 0 with patch("paramiko.rsakey.RSAKey.from_private_key_file") as rsa_key_mock: rsa_key_mock.return_value.get_base64.return_value = "0123456789" ContainerManager.ssh_copy_id(self.node, "c1", user=sentinel.ssh_user, key_file="~/lala.key") rsa_key_mock.assert_called_once_with(os.path.expanduser("~/lala.key")) rsa_key_mock.return_value.get_base64.assert_called_once_with() self.container.exec_run.assert_called_once() # pylint: disable=unsubscriptable-object; disable this message for .call_args[] self.assertIn(" 0123456789 ", " ".join(self.container.exec_run.call_args[0][0])) self.assertEqual(self.container.exec_run.call_args[1]["user"], sentinel.ssh_user) with self.subTest("Check exec_run failure"): self.container.exec_run.reset_mock() self.container.exec_run.return_value.exit_code = 126 self.container.exec_run.return_value.output = b"blah" with patch("paramiko.rsakey.RSAKey.from_private_key_file") as rsa_key_mock: self.assertRaisesRegex(DockerException, "blah", ContainerManager.ssh_copy_id, self.node, "c1", user=sentinel.ssh_user, key_file="lala.key")