def test_command(self, mock_arg, mock_svn_separator, mock_time_separator, mock_socket): subj = Backup() for command, method, has_src_dirs, has_dst_dirs in ( ('full', subj.full, True, True), ('dump', subj.dump, True, True), ('clone', subj.clone, False, True), ('git', subj.git, True, False), ('checks', subj.checks, False, True), ('any', subj.help, False, False), (str(uid()), subj.help, False, False)): for empty in (False, True): with self.subTest(command=command, empty=empty): mock_time_separator.reset_mock() mock_svn_separator.reset_mock() subj.commands = {} src_dirs = ['src%s-%s' % (i, uid()) for i in uid_range()] dest_dirs = ['dst%s-%s' % (i, uid()) for i in uid_range()] num = uid() command_line = [command] if has_src_dirs: command_line.append(','.join(src_dirs)) if has_dst_dirs: command_line.append(','.join(dest_dirs)) command_line.append(str(num)) mock_arg.side_effect = command_line hostname = 'hostname%s' % uid() smtp_host = 'smtp_host%s' % uid() subj.config = {} if empty else { 'hostname': hostname, 'smtp_host': smtp_host } mock_socket.gethostname.return_value = hostname if empty else None commands = {} if not has_dst_dirs: dest_dirs = [] else: for dst in dest_dirs: commands[dst] = [] result = subj.command() self.assertEqual(result, method) self.assertEqual(subj.hostname, hostname) self.assertEqual(subj.smtp_host, None if empty else smtp_host) self.assertTrue( isinstance(subj.time_separator, TimeSeparator)) mock_time_separator.assert_called_once_with( num if command == 'full' or command == 'clone' else None) mock_svn_separator.assert_called_once_with() self.assertEqual(subj.separators[0], subj.time_separator) self.assertTrue( isinstance(subj.separators[1], SvnSeparator)) self.assertEqual(len(subj.separators), 2) self.assertEqual(subj.src_dirs, src_dirs if has_src_dirs else ['']) self.assertEqual(subj.dest_dirs, dest_dirs if has_dst_dirs else []) self.assertEqual(subj.commands, commands)
def test_send_errors(self, mime_text, smtp_ssl): for thrown in [None, BaseException(), Exception(), IOError()]: for empty in (False, True): with self.subTest(thrown=thrown, empty=empty): for m in (mime_text, smtp_ssl): m.reset_mock() subj = Backup() errors = [] if empty else [ 'err-%s' % uid(), 'err-%s' % uid() ] subj.errors = errors hostname = 'hostname-%s' % uid() subj.hostname = hostname subj.smtp_host = 'smtp_host-%s' % uid() port = uid() user = '******' % uid() password = '******' % uid() from_address = 'from-%s' % uid() to_address = 'to-%s' % uid() subj.config = { 'smtp_port': port, 'smtp_user': user, 'smtp_password': password, 'fromaddr': from_address, 'toaddrs': to_address } server = Mock() smtp_ssl.return_value = server message = MagicMock() def send_message(msg): self.assertEqual(msg, message) server.login.assert_called_once_with(user, password) mime_text.assert_called_once_with( '\n'.join(errors), 'plain', 'utf-8') message.__setitem__.assert_has_calls([ call('Subject', "backup error: " + hostname), call('From', from_address), call('To', to_address) ]) server.quit.assert_not_called() if thrown: raise thrown mime_text.return_value = message server.send_message.side_effect = send_message # noinspection PyBroadException try: subj.send_errors() except BaseException as e: if e != thrown: raise e if not empty: server.send_message.assert_called_once_with(message) server.quit.assert_called_once_with() else: smtp_ssl.assert_not_called() server.login.assert_not_called() server.quit.assert_not_called()