def test_new_deploy(self, _uuid, _os, _open): _os.path.expanduser.return_value = "/home/user" _os.path.join.side_effect = [ "/home/user/.ssh/key_filename", "/home/user/.ssh/config" ] _uuid.uuid1 = mock.Mock() _uuid.uuid1.return_value = "sampleuuid" _key_file, _ssh_config_file, _inventory_file = ( mock.MagicMock(spec=file), mock.MagicMock(spec=file), mock.MagicMock(spec=file), ) def open_side_effect(*args): arg2file = { "/home/user/.ssh/key_filename": _key_file, "/home/user/.ssh/config": _ssh_config_file, "./hosts": _inventory_file } return arg2file[args[0]] _open.side_effect = open_side_effect new_host(ip="123.456.78.9", ssh_secret_key="sample_secret_key") _os.path.expanduser.assert_called_once_with("~") _uuid.uuid1.assert_called_once_with() self.assertEqual( _os.path.join.call_args_list, [ mock.call("/home/user", ".ssh", "sampleuuid"), mock.call("/home/user", ".ssh", "config") ] ) _key_file.__enter__.return_value.write.assert_called_once_with( "sample_secret_key\r\n" ) _os.chmod.assert_called_once_with("/home/user/.ssh/key_filename", 0o600) _ssh_config_file.__enter__.return_value.write.assert_called_once_with( self.SSH_CONFIG_TEMPLATE.format( ip="123.456.78.9", key_filename="/home/user/.ssh/key_filename" ) ) _inventory_file.__enter__.return_value.write.assert_called_once_with( "123.456.78.9\r\n" )
def post(self): hostname = self.get_argument("hostname") ip = self.get_argument("ip") ssh_secret_key = self.get_argument("ssh_secret_key") new_host(ip=ip, ssh_secret_key=ssh_secret_key) _host_item = HostMeta(hostname=hostname, ip=ip) session.add(_host_item) session.commit() session.close() self.write({ _host_item.id: json.loads(_host_item.status) })