def setUp(self): call_command('init', password='******', tester=True) self.tester = settings.TESTER self.user = authenticate(username=self.tester, password=self.tester) self.ucf = UserConf(self.user) self.mqtypes = self.guess_mq_type(self.user) self.mqtype = self.mqtypes[0]
class TestRabbitMQ(TestCase, MQTestMixin): def setUp(self): call_command('init', password='******', tester=True) tester = settings.TESTER self.user = authenticate(username=tester, password=tester) self.mqtype = MQTypes.RabbitMQ self.ucf = UserConf(self.user) conf = MQClientFactory.get_confs(self.mqtype, self.user) self.mq = MQClientFactory.create_connection(self.mqtype, conf) self.mq.connect() def tearDown(self): self.mq.disconnect() def test_send_to_queue(self): msg = 'my first message.' producer = self.mq.create_producer() producer.produce(msg, queue=self.ucf.get(ConfKeys[self.mqtype].queue_out)) self.mq.create_consumer() self.assertEqual(True, True) def test_send_to_exchange(self): msg = 'my second message to topic & key.' producer = self.mq.create_producer() producer.produce(msg, topic=self.ucf.get(ConfKeys[self.mqtype].topic_out), key=self.ucf.get(ConfKeys[self.mqtype].key_out)) self.assertEqual(True, True)
def set_configs(user, tokens): ucf = UserConf(user=user) opts = [] for token in tokens: token = token.strip() if not token or token.startswith('#') or token.startswith(';'): continue opt = token.split('=') if len(opt) != 2: return ' Invalid syntax around "%s"' % token opt[0] = opt[0].strip() opt[1] = opt[1].strip() opts.append(opt) for opt in opts: is_valid = False for confs in ConfKeys.values(): if opt[0] in confs.values(): is_valid = True break if is_valid: ucf.set(opt[0], opt[1]) else: sys.stderr.write(' "%s" is not a valid config name.\r\n' % opt[0]) return ' Configuration modified for %s.' % user.username
def setUp(self): call_command('init', password='******', tester=True) tester = settings.TESTER self.user = authenticate(username=tester, password=tester) self.mqtype = MQTypes.RabbitMQ self.ucf = UserConf(self.user) conf = MQClientFactory.get_confs(self.mqtype, self.user) self.mq = MQClientFactory.create_connection(self.mqtype, conf) self.mq.connect()
def guess_mq_type(user): mqtypes = [] ucf = UserConf(user=user) for section, confkeys in ConfKeys.items(): if section == 'General': continue v = ucf.get(confkeys.host) if v: mqtypes.append(section) if len(mqtypes) > 0: return mqtypes else: raise Exception('You must specify at least one MQ setting.')
def test_init_tester(self): pwd = '234567' t = settings.TESTER email = 'anonymous@localhost' config_file = InitCommand.default_tester_config call_command('init', password=pwd, tester=True) tester = authenticate(username=t, password=t) self.assertIsNotNone(tester) self.assertTrue(tester.is_active) self.assertFalse(tester.is_superuser) self.assertFalse(tester.is_staff) self.assertEqual(tester.email, email) dummy_section = '_' ucf = UserConf(tester) cf = configparser.ConfigParser() with open(config_file, 'r') as f: config_string = '[%s]\n' % dummy_section + f.read() cf.read_string(config_string) for opt in ucf.all(): v = cf.get(dummy_section, opt.name) self.assertEqual(v, opt.value)
class TestCLISend(TestCase, MQTestMixin): def setUp(self): call_command('init', password='******', tester=True) self.tester = settings.TESTER self.user = authenticate(username=self.tester, password=self.tester) self.ucf = UserConf(self.user) self.mqtypes = self.guess_mq_type(self.user) self.mqtype = self.mqtypes[0] def tearDown(self): pass def test_errors(self): name = pwd = self.tester mqtype = self.mqtype data = 'PyQueuer unittest sends message.' # miss arguments with self.assertRaises(CommandError) as err: call_command('send', user=name, password=pwd) self.assertIn('arguments are required', str(err.exception)) with self.assertRaises(CommandError) as err: call_command('send', user=name, password=pwd, queue='test') self.assertIn('arguments are required', str(err.exception)) with self.assertRaises(CommandError) as err: call_command('send', user=name, password=pwd, topic='test', type=mqtype) self.assertIn('arguments are required', str(err.exception)) # miss MQ type (even with incorrect arguments) rt = call_command('send', 'a=b', user=name, password=pwd) self.assertEqual(rt, 'You must specify MQ type.') # miss data or file rt = call_command('send', 'a=b', user=name, password=pwd, type=mqtype) self.assertEqual(rt, 'You must specify either --data or --file for reading message.') # miss data or file (even with incorrect MQ type) rt = call_command('send', 'a=b', user=name, password=pwd, type=mqtype.lower()) self.assertEqual(rt, 'You must specify either --data or --file for reading message.') # incorrect MQ type (even only lower case) with self.assertRaises(RuntimeError) as err: call_command('send', 'a=b', user=name, password=pwd, type=mqtype.lower(), data=data) self.assertIn('Unsupported MQ type', str(err.exception)) def test_send(self): if MQTypes.RabbitMQ in self.mqtypes: self._send_rabbit() if MQTypes.Kafka in self.mqtypes: self._send_kafka() def _send_rabbit(self): mqtype = MQTypes.RabbitMQ queue = self.ucf.get(ConfKeys[mqtype].queue_out) topic = self.ucf.get(ConfKeys[mqtype].topic_out) key = self.ucf.get(ConfKeys[mqtype].key_out) data = '{ "sender": "pyqueuer", "mq":"RabbitMQ", "desc":"unittest"}' file = os.path.sep.join([settings.BASE_DIR, 'requirements.txt']) # send data to exchange rt = call_command('send', 'topic=%s' % topic, 'key=%s' % key, user=self.tester, password=self.tester, type=mqtype, data=data) self.assertIn('Message sent', rt) # send file to queue rt = call_command('send', 'queue=%s' % queue, user=self.tester, password=self.tester, type=mqtype, file=file) self.assertIn('Message sent', rt) def _send_kafka(self): mqtype = MQTypes.Kafka topic = self.ucf.get(ConfKeys[mqtype].topic_out) data = '{ "sender": "pyqueuer", "mq":"Kafka", "desc":"unittest"}' file = os.path.sep.join([settings.BASE_DIR, 'requirements.txt']) # send data to topic rt = call_command('send', 'topic=%s' % topic, user=self.tester, password=self.tester, type=mqtype, data=data) self.assertIn('Message sent', rt) # send file to topic rt = call_command('send', 'topic=%s' % topic, user=self.tester, password=self.tester, type=mqtype, file=file) self.assertIn('Message sent', rt)
def handle(self, *args, **options): name = options['user'] pwd = options['password'] show_list = options['list'] conf = options['conf'] key = options['get'] imp = options['import'] expt = options['export'] config_file = options['config_file'] if not config_file: config_file = '%s_config.ini' % name if not name: name = input(' Please enter your user name:') if not pwd: pwd = getpass.getpass( ' Please enter the password for user "%s" :' % name) user = authenticate(username=name, password=pwd) if user and user.is_active: pass else: # sys.stderr.write('You are not authorized with given user name and password.\r\n') return ' ou are not authorized with given user name and password.' ucf = UserConf(user=user) sb = [] if show_list: # show config list if conf: print(' Ignore all options due to "--list" specified.') for opt in ucf.all(): sb.append('%s=%s' % (opt.name, opt.value)) return os.linesep.join(sb) elif conf: # set configurations return self.set_configs(user=user, tokens=conf) elif key: # get value of an config by given name return ucf.get(key) elif imp: # import from file. try: with open(config_file, 'rt') as f: return self.set_configs(user=user, tokens=f.readlines()) except (FileNotFoundError, PermissionError, FileExistsError): return ' File is not accessible.' elif expt: # export to file try: with open(config_file, 'wt') as f: for opt in ucf.all(): f.write('%s=%s' % (opt.name, opt.value)) f.write(os.linesep) except (FileNotFoundError, PermissionError, FileExistsError): return ' File is not accessible.' else: # sys.stderr.write('Please specify arguments such as --list or --get.\r\n') return ' Please specify arguments such as --list, --get and so on.' return ''