Commit ced004bb authored by Federico Sismondi's avatar Federico Sismondi

added unitestest for messages lib + jenkins file for the jobs

parent c9b04b62
Pipeline #1761 failed with stage
in 0 seconds
properties([[$class: 'GitLabConnectionProperty', gitLabConnection: 'figitlab']])
if(env.JOB_NAME =~ 'utils/'){
node('sudo'){
env.AMQP_URL="amqp://guest:guest@localhost/"
env.AMQP_EXCHANGE="amq.topic"
stage("Clone repo and submodules"){
checkout scm
sh '''
git submodule update --init
tree .
'''
}
stage ("Environment dependencies"){
withEnv(["DEBIAN_FRONTEND=noninteractive"]){
sh '''
sudo apt-get clean
sudo apt-get update
sudo apt-get upgrade -y -qq
sudo apt-get install --fix-missing -y -qq python-dev python-pip python-setuptools
sudo apt-get install --fix-missing -y -qq python3-dev python3-pip python3-setuptools
sudo apt-get install --fix-missing -y -qq build-essential
sudo apt-get install --fix-missing -y -qq libyaml-dev
sudo apt-get install --fix-missing -y -qq libssl-dev openssl
sudo apt-get install --fix-missing -y -qq libffi-dev
sudo apt-get install --fix-missing -y -qq curl tree netcat
sudo apt-get install --fix-missing -y -qq rabbitmq-server
sudo apt-get install --fix-missing -y -qq supervisor
sudo apt-get install --fix-missing -y -qq make
echo restarting rmq server and app
sudo rabbitmq-server -detached || true
sudo rabbitmqctl stop_app || true
sudo rabbitmqctl start_app || true
'''
}
}
stage("Testing Tool dependencies"){
gitlabCommitStatus("Testing Tool dependencies"){
withEnv(["DEBIAN_FRONTEND=noninteractive"]){
sh '''
echo installing python dependencies...
python3 -m pip -qq install -r requirements.txt
'''
}
}
}
stage("unittesting modules"){
gitlabCommitStatus("unittesting modules"){
sh '''
echo $AMQP_URL
pwd
python3 -m pytest -p no:cacheprovider tests
'''
}
}
}
}
# -*- coding: utf-8 -*-
# !/usr/bin/env python3
import unittest
import pika
import os
from messages import *
from event_bus_utils import publish_message
import logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
logger = logging.getLogger(__name__)
logging.getLogger('pika').setLevel(logging.WARNING)
AMQP_EXCHANGE = str(os.environ['AMQP_EXCHANGE'])
AMQP_URL = str(os.environ['AMQP_URL'])
class MessagesLibraryTests(unittest.TestCase):
"""
python3 -m unittest tests/test_messages.py -vvv
"""
def setUp(self):
# let's collect all the messages classes
self.set_of_collected_messages = {v for (k, v) in globals().items() if 'Msg' in k}
self.non_serializable_messages_types = {MsgReply, MsgErrorReply}
self.serializable_messages_types = self.set_of_collected_messages - self.non_serializable_messages_types
# amqp stuff
self.conn = pika.BlockingConnection(pika.URLParameters(AMQP_URL))
self.channel = self.conn.channel()
# message validation queue
self.queue_for_post_validation = '%s::queue_for_post_validation' % self.__class__.__name__
# lets' first clean up the queue
self.channel.queue_delete(queue=self.queue_for_post_validation)
self.channel.queue_declare(queue=self.queue_for_post_validation, auto_delete=True)
# subscribe to all messages in the bus
self.channel.queue_bind(exchange=AMQP_EXCHANGE, queue=self.queue_for_post_validation, routing_key='#')
self.channel.basic_qos(prefetch_count=1)
def tearDown(self):
self.conn.close()
def test_serialize_and_deserialize_messages_on_bus(self):
self.serialize_and_publish_all_messages()
self.consume_and_deserialize_messages()
def serialize_and_publish_all_messages(self):
for i in self.serializable_messages_types:
try:
m = i()
except Exception as e:
self.fail('%s found, while serializing %s' % (e, str(i)))
logging.info("publishing message type %s" % str(type(m)))
publish_message(
self.conn,
m
)
def consume_and_deserialize_messages(self):
time.sleep(1)
message_count = 0
method, props, body = self.channel.basic_get(self.queue_for_post_validation)
while method:
logging.info('got a message')
message_count += 1
self.channel.basic_ack(method.delivery_tag)
# load w/o properties
message = Message.load(
json_body=body.decode('utf-8'),
routing_key=method.routing_key,
properties=None,
)
# load w/ properties
props_dict = {
'content_type': props.content_type,
'delivery_mode': props.delivery_mode,
'correlation_id': props.correlation_id,
'reply_to': props.reply_to,
'message_id': props.message_id,
'timestamp': props.timestamp,
'user_id': props.user_id,
'app_id': props.app_id,
}
# load w/o properties
message = Message.load(
json_body=body.decode('utf-8'),
routing_key=method.routing_key,
properties=props_dict,
)
logging.info(repr(message))
method, props, body = self.channel.basic_get(self.queue_for_post_validation)
if len(self.serializable_messages_types) != message_count:
self.fail('expecting %s messages, but got %s' % (len(self.serializable_messages_types), message_count))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment