Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ELisA tests #120

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions elisa-logbook/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.venv/
elisa_client_api/
27 changes: 27 additions & 0 deletions elisa-logbook/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Elisa Logbook Microservice
## To test the microservice
On a machine at CERN:
```bash
git clone https://github.com/DUNE-DAQ/microservices.git # Clone this repository
cd microservices/elisa-logbook
git clone https://github.com/DUNE-DAQ/elisa_client_api.git # Clone the elisa_client_api repository
python3 -m venv .venv # Create a virtual environment
source .venv/bin/activate # Activate the virtual environment
pip install -r microservices/dockerfiles/requirements.txt # Install the dependencies
pip install ./elisa_client_api # Install the elisa_client_api
pip install pytest # Install pytest
USERNAME=something PASSWORD=something python -m pytest -s --system pdsp -k test_elisa_microservice_locally
# After running the tests:
deactivate
```
You need to provide the service account username and password for the ELisA logbook. Ask Pierre Lasorak for the credentials.


If you don't want to be spamming the ELisA logbook too much, you can run a subset of tests for example:
```bash
python -m pytest -k test_elisa_microservice_conf
```
or choose a specific system:
```bash
python -m pytest --system pddp -k test_elisa_microservice_locally
```
10 changes: 10 additions & 0 deletions elisa-logbook/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

def pytest_addoption(parser):
parser.addoption(
'--system',
action="store",
type=str,
nargs='+',
default=['npvd', 'pdsp', 'pddp'],
help="Name of the system(s) to test"
)
1 change: 1 addition & 0 deletions elisa-logbook/credmgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def __init__(self, service:str, username:str, password:str, realm:str):
self.username = username
self.password = password
self.realm = realm
self.log = logging.getLogger(self.__class__.__name__)

def generate_cern_sso_cookie(self, website, kerberos_directory, output_directory):
env = {'KRB5CCNAME': f'DIR:{kerberos_directory}'}
Expand Down
4 changes: 2 additions & 2 deletions elisa-logbook/logbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ def new_message():
@app.route('/v1/elisaLogbook/reply_to_message/', methods=["PUT"])
@auth.login_required
def reply_to_message():
if request.json.get('body', "") == "" or request.json.get('title', "") == "" or request.json.get('command', "") == "" or request.json.get('author', "") == "" or request.json.get('id', "") == "":
if request.json.get('body', "") == "" or request.json.get('command', "") == "" or request.json.get('author', "") == "" or request.json.get('id', "") == "":
resp = make_response(
jsonify(
response = "Body, title, command, author or id cannot be empty!",
response = "Body, command, author or id cannot be empty!",
sent_data = request.json
)
)
Expand Down
218 changes: 218 additions & 0 deletions elisa-logbook/test_elisa_microservice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
import getpass
import json
import multiprocessing
import os
import pytest
import requests
import socket
import time

from elisa import ElisaLogbook
from credmgr import CERNSessionHandler, credentials


@pytest.fixture
def elisa_logbook_config():
with open('elisaconf.json', 'r') as f:
return json.load(f)


@pytest.fixture
def elisa_microservice_config():
with open(os.path.expanduser('~/.drunc.json'), 'r') as f:
return json.load(f)


@pytest.fixture
def elisa_microservice_local_config(): # a configuration to start the microservice locally
with open(os.path.expanduser('~/.drunc.json'), 'r') as f:
data = json.load(f)
updated_data = {}
port = 61263
for key, values in data['elisa_configuration'].items():
values['socket'] = f'http://0.0.0.0:{port}'
port += 1
updated_data[key] = values
data['elisa_configuration'] = updated_data
return data


@pytest.fixture
def systems(request):
return request.config.getoption("--system")


@pytest.fixture
def cern_session():
user_var = os.getenv("USERNAME").strip("\n")
pass_var = os.getenv("PASSWORD").strip("\n")
credentials.add_login("elisa", user_var, pass_var, "CERN.CH")
return CERNSessionHandler(user_var)


@pytest.fixture()
def local_microservice(elisa_microservice_local_config):

def run_app(which_system):
# Configure app for testing

socket = elisa_microservice_local_config['elisa_configuration'][which_system]['socket']
port = int(socket.split(':')[-1])
host = ":".join(socket.split(':')[:-1])

def run_with_env(*args, **kwargs):
os.environ["HARDWARE"] = which_system
from logbook import app
app.config['TESTING'] = True
app.run(*args, **kwargs)

# Start Flask in a separate process
process = multiprocessing.Process(
target = run_with_env,
kwargs = {
'port': port,
'host': host.replace('http://', '').replace('https://', ''),
'debug': True,
}
)
process.daemon = True
process.start()

# Give the server a moment to start
time.sleep(1)
return process

return run_app


def test_elisa_configuration(
systems,
elisa_logbook_config):

for system in systems:
assert system in elisa_logbook_config

system_config = elisa_logbook_config[system]

assert 'connection' in system_config
assert 'website' in system_config
assert 'attributes' in system_config
messages_kind = ['start', 'stop', 'message']

for message_kind in messages_kind:
assert message_kind in system_config['attributes']

assert 'type' in system_config['attributes'][message_kind]
assert 'set_on_reply' in system_config['attributes'][message_kind]['type']
assert 'set_on_new_thread' in system_config['attributes'][message_kind]['type']
assert 'value' in system_config['attributes'][message_kind]['type']

assert ('RunControl_MessageType' in system_config['attributes'][message_kind] or
'Automatic_Message_Type' in system_config['attributes'][message_kind])

if 'RunControl_MessageType' in system_config['attributes'][message_kind]:
assert 'set_on_reply' in system_config['attributes'][message_kind]['RunControl_MessageType']
assert 'set_on_new_thread' in system_config['attributes'][message_kind]['RunControl_MessageType']
assert 'value' in system_config['attributes'][message_kind]['RunControl_MessageType']

elif 'Automatic_Message_Type' in system_config['attributes'][message_kind]:
assert 'set_on_reply' in system_config['attributes'][message_kind]['Automatic_Message_Type']
assert 'set_on_new_thread' in system_config['attributes'][message_kind]['Automatic_Message_Type']
assert 'value' in system_config['attributes'][message_kind]['Automatic_Message_Type']


def test_elisa_logbook(
systems,
elisa_logbook_config,
cern_session):

for system in systems:

elisa_logbook = ElisaLogbook(
elisa_logbook_config[system],
cern_session
)
assert elisa_logbook is not None
thread_id = elisa_logbook.start_new_thread(
"unit-test",
"test_elisa_logbook: Ignore this message",
"start",
getpass.getuser(),
['DAQ']
)
assert thread_id is not None
thread_id_response = elisa_logbook.reply(
"test_elisa_logbook: Ignore this message",
"stop",
getpass.getuser(),
['DAQ'],
thread_id
)
assert thread_id_response is not None


def test_elisa_microservice_conf(
systems,
elisa_microservice_config):

assert "elisa_configuration" in elisa_microservice_config

elisa_configuration = elisa_microservice_config["elisa_configuration"]

for system in systems:
assert system in elisa_configuration

system_config = elisa_configuration[system]

assert "socket" in system_config
assert "user" in system_config
assert "password" in system_config



def test_elisa_microservice_locally(
systems,
elisa_microservice_local_config,
local_microservice):

elisa_configuration = elisa_microservice_local_config["elisa_configuration"]

for system in systems:
lm = local_microservice(system)
system_config = elisa_configuration[system]
usvc_socket = system_config['socket'].replace('0.0.0.0', socket.gethostname())

response = requests.get(usvc_socket + '/')
response.raise_for_status()
assert response.status_code == 200

response = requests.post(
usvc_socket + '/v1/elisaLogbook/new_message/',
json={
'title': 'unit-test',
'body': 'test_elisa_microservice_locally: Ignore this message',
'command': 'start',
'author': getpass.getuser(),
'systems': ['DAQ']
},
auth=(system_config['user'], system_config['password'])
)
response.raise_for_status()
response_json = response.json()
assert response_json['response'] == "Message thread started successfully"
assert response_json['thread_id'] is not None
response = requests.put(
usvc_socket + '/v1/elisaLogbook/reply_to_message/',
json={
'id': response_json['thread_id'],
'body': 'test_elisa_microservice_locally: Ignore this message',
'command': 'stop',
'author': getpass.getuser(),
'systems': ['DAQ']
},
auth=(system_config['user'], system_config['password'])
)
response.raise_for_status()
response_json = response.json()
assert response_json['response'] == "Message thread replied successfully"

Loading