Skip to content
Snippets Groups Projects
Commit 4ee9b567 authored by Nikolay Stanchev's avatar Nikolay Stanchev
Browse files

Added status flag for malformed configuration of the aggregator plus tests for this behaviour.

parent d14c26c7
No related branches found
No related tags found
No related merge requests found
...@@ -25,19 +25,23 @@ from pyramid.config import Configurator ...@@ -25,19 +25,23 @@ from pyramid.config import Configurator
from pyramid.settings import asbool from pyramid.settings import asbool
from clmcservice.views import AggregatorConfig, AggregatorController from clmcservice.views import AggregatorConfig, AggregatorController
from clmcservice.utilities import STATUS_ATTRIBUTE from clmcservice.utilities import RUNNING_FLAG, MALFORMED_FLAG
def main(global_config, **settings): def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.""" """
This function returns a Pyramid WSGI application.
"""
# a conversion is necessary so that the configuration values of the aggregator are stored with the right type instead of strings # a conversion is necessary so that the configuration values of the aggregator are stored with the right type instead of strings
aggregator_running = asbool(settings.get('aggregator_running', False)) aggregator_running = asbool(settings.get(RUNNING_FLAG, False))
settings[STATUS_ATTRIBUTE] = asbool(aggregator_running) settings[RUNNING_FLAG] = asbool(aggregator_running)
aggregator_report_period = int(settings.get('aggregator_report_period', 5)) aggregator_report_period = int(settings.get('aggregator_report_period', 5))
settings['aggregator_report_period'] = aggregator_report_period settings['aggregator_report_period'] = aggregator_report_period
settings[MALFORMED_FLAG] = False
config = Configurator(settings=settings) config = Configurator(settings=settings)
config.add_route('aggregator_config', '/aggregator/config') config.add_route('aggregator_config', '/aggregator/config')
......
...@@ -25,12 +25,12 @@ import pytest ...@@ -25,12 +25,12 @@ import pytest
from pyramid import testing from pyramid import testing
from pyramid.httpexceptions import HTTPBadRequest from pyramid.httpexceptions import HTTPBadRequest
from time import sleep from time import sleep
from clmcservice.utilities import CONFIG_ATTRIBUTES, PROCESS_ATTRIBUTE, STATUS_ATTRIBUTE from clmcservice.utilities import CONFIG_ATTRIBUTES, PROCESS_ATTRIBUTE, RUNNING_FLAG, MALFORMED_FLAG
import os import os
import signal import signal
class TestAggregator(object): class TestAggregatorAPI(object):
""" """
A pytest-implementation test for the aggregator API calls A pytest-implementation test for the aggregator API calls
""" """
...@@ -42,21 +42,20 @@ class TestAggregator(object): ...@@ -42,21 +42,20 @@ class TestAggregator(object):
""" """
self.config = testing.setUp() self.config = testing.setUp()
self.config.add_settings({'aggregator_running': False, 'aggregator_report_period': 5, self.config.add_settings({'aggregator_running': False, 'malformed': False, 'aggregator_report_period': 5,
'aggregator_database_name': 'E2EMetrics', 'aggregator_database_url': "http://172.40.231.51:8086"}) 'aggregator_database_name': 'E2EMetrics', 'aggregator_database_url': "http://172.40.231.51:8086"})
yield yield
testing.tearDown() testing.tearDown()
def test_GET(self): def test_GET_config(self):
""" """
Tests the GET method for the status of the aggregator. Tests the GET method for the configuration of the aggregator.
""" """
from clmcservice.views import AggregatorConfig # nested import so that importing the class view is part of the test itself from clmcservice.views import AggregatorConfig # nested import so that importing the class view is part of the test itself
assert not self.config.get_settings().get(STATUS_ATTRIBUTE), "Initially aggregator is not running."
assert self.config.get_settings().get('aggregator_report_period') == 5, "Initial report period is 5 seconds." assert self.config.get_settings().get('aggregator_report_period') == 5, "Initial report period is 5 seconds."
assert self.config.get_settings().get('aggregator_database_name') == 'E2EMetrics', "Initial database name the aggregator uses is E2EMetrics." assert self.config.get_settings().get('aggregator_database_name') == 'E2EMetrics', "Initial database name the aggregator uses is E2EMetrics."
assert self.config.get_settings().get('aggregator_database_url') == "http://172.40.231.51:8086", "Initial aggregator url is http://172.40.231.51:8086" assert self.config.get_settings().get('aggregator_database_url') == "http://172.40.231.51:8086", "Initial aggregator url is http://172.40.231.51:8086"
...@@ -68,7 +67,6 @@ class TestAggregator(object): ...@@ -68,7 +67,6 @@ class TestAggregator(object):
'aggregator_database_name': 'E2EMetrics', 'aggregator_database_name': 'E2EMetrics',
'aggregator_database_url': "http://172.40.231.51:8086"}, "Response must be a dictionary representing a JSON object with the correct configuration data of the aggregator." 'aggregator_database_url': "http://172.40.231.51:8086"}, "Response must be a dictionary representing a JSON object with the correct configuration data of the aggregator."
assert not self.config.get_settings().get(STATUS_ATTRIBUTE), "A GET request must not modify the aggregator configuration data."
assert self.config.get_settings().get('aggregator_report_period') == 5, "A GET request must not modify the aggregator configuration data." assert self.config.get_settings().get('aggregator_report_period') == 5, "A GET request must not modify the aggregator configuration data."
assert self.config.get_settings().get('aggregator_database_name') == 'E2EMetrics', "A GET request must not modify the aggregator configuration data." assert self.config.get_settings().get('aggregator_database_name') == 'E2EMetrics', "A GET request must not modify the aggregator configuration data."
assert self.config.get_settings().get('aggregator_database_url') == "http://172.40.231.51:8086", "A GET request must not modify the aggregator configuration data." assert self.config.get_settings().get('aggregator_database_url') == "http://172.40.231.51:8086", "A GET request must not modify the aggregator configuration data."
...@@ -91,30 +89,31 @@ class TestAggregator(object): ...@@ -91,30 +89,31 @@ class TestAggregator(object):
("{}", None), ("{}", None),
("{aggregator_running: true}", None), ("{aggregator_running: true}", None),
]) ])
def test_PUT(self, input_body, output_value): def test_PUT_config(self, input_body, output_value):
""" """
Tests the PUT method for the configuration of the aggregator Tests the PUT method for the configuration of the aggregator
:param input_body: the input form parameter :param input_body: the input body parameter
:param output_value: the expected output value, None for expecting an Exception :param output_value: the expected output value, None for expecting an Exception
""" """
from clmcservice.views import AggregatorConfig # nested import so that importing the class view is part of the test itself from clmcservice.views import AggregatorConfig # nested import so that importing the class view is part of the test itself
assert not self.config.get_settings().get(STATUS_ATTRIBUTE), "Initially aggregator is not running." assert not self.config.get_settings().get(RUNNING_FLAG), "Initially aggregator is not running."
assert self.config.get_settings().get('aggregator_report_period') == 5, "Initial report period is 5 seconds." assert self.config.get_settings().get('aggregator_report_period') == 5, "Initial report period is 5 seconds."
assert self.config.get_settings().get('aggregator_database_name') == 'E2EMetrics', "Initial database name the aggregator uses is E2EMetrics." assert self.config.get_settings().get('aggregator_database_name') == 'E2EMetrics', "Initial database name the aggregator uses is E2EMetrics."
assert self.config.get_settings().get('aggregator_database_url') == "http://172.40.231.51:8086", "Initial aggregator url is http://172.40.231.51:8086" assert self.config.get_settings().get('aggregator_database_url') == "http://172.40.231.51:8086", "Initial aggregator url is http://172.40.231.51:8086"
request = testing.DummyRequest() request = testing.DummyRequest()
request.body = input_body.encode(request.charset) request.body = input_body.encode(request.charset)
if output_value is not None: if output_value is not None:
response = AggregatorConfig(request).put() response = AggregatorConfig(request).put()
assert response == output_value, "Response of PUT request must include the new status of the aggregator" assert response == output_value, "Response of PUT request must include the new configuration of the aggregator"
for attribute in CONFIG_ATTRIBUTES: for attribute in CONFIG_ATTRIBUTES:
assert self.config.get_settings().get(attribute) == output_value.get(attribute), "Aggregator settings configuration is not updated." assert self.config.get_settings().get(attribute) == output_value.get(attribute), "Aggregator settings configuration is not updated."
assert not self.config.get_settings().get(RUNNING_FLAG), "Aggregator running status should not be updated after a configuration update."
else: else:
error_raised = False error_raised = False
try: try:
...@@ -126,12 +125,12 @@ class TestAggregator(object): ...@@ -126,12 +125,12 @@ class TestAggregator(object):
def test_start(self): def test_start(self):
""" """
Tests the case of starting the aggregator through an API call. Tests starting the aggregator through an API call.
""" """
from clmcservice.views import AggregatorController # nested import so that importing the class view is part of the test itself from clmcservice.views import AggregatorController # nested import so that importing the class view is part of the test itself
assert not self.config.get_settings().get(STATUS_ATTRIBUTE), "Initially aggregator is not running." assert not self.config.get_settings().get(RUNNING_FLAG), "Initially aggregator is not running."
assert self.config.get_settings().get(PROCESS_ATTRIBUTE) is None, "Initially no aggregator process is running." assert self.config.get_settings().get(PROCESS_ATTRIBUTE) is None, "Initially no aggregator process is running."
request = testing.DummyRequest() request = testing.DummyRequest()
...@@ -139,8 +138,8 @@ class TestAggregator(object): ...@@ -139,8 +138,8 @@ class TestAggregator(object):
request.body = input_body.encode(request.charset) request.body = input_body.encode(request.charset)
response = AggregatorController(request).put() response = AggregatorController(request).put()
assert response == {STATUS_ATTRIBUTE: True}, "The aggregator should have been started." assert response == {RUNNING_FLAG: True}, "The aggregator should have been started."
assert self.config.get_settings().get(STATUS_ATTRIBUTE), "The aggregator should have been started." assert self.config.get_settings().get(RUNNING_FLAG), "The aggregator should have been started."
assert self.config.get_settings().get(PROCESS_ATTRIBUTE) is not None, "Aggregator process should have been initialized." assert self.config.get_settings().get(PROCESS_ATTRIBUTE) is not None, "Aggregator process should have been initialized."
# kill the started process after the test is over # kill the started process after the test is over
...@@ -148,9 +147,13 @@ class TestAggregator(object): ...@@ -148,9 +147,13 @@ class TestAggregator(object):
os.kill(pid, signal.SIGTERM) os.kill(pid, signal.SIGTERM)
def test_stop(self): def test_stop(self):
"""
Tests stopping the aggregator through an API call.
"""
from clmcservice.views import AggregatorController # nested import so that importing the class view is part of the test itself from clmcservice.views import AggregatorController # nested import so that importing the class view is part of the test itself
assert not self.config.get_settings().get(STATUS_ATTRIBUTE), "Initially aggregator is not running." assert not self.config.get_settings().get(RUNNING_FLAG), "Initially aggregator is not running."
assert self.config.get_settings().get(PROCESS_ATTRIBUTE) is None, "Initially no aggregator process is running." assert self.config.get_settings().get(PROCESS_ATTRIBUTE) is None, "Initially no aggregator process is running."
# send a start request to trigger the aggregator # send a start request to trigger the aggregator
...@@ -166,8 +169,8 @@ class TestAggregator(object): ...@@ -166,8 +169,8 @@ class TestAggregator(object):
request.body = input_body.encode(request.charset) request.body = input_body.encode(request.charset)
response = AggregatorController(request).put() response = AggregatorController(request).put()
assert response == {STATUS_ATTRIBUTE: False}, "The aggregator should have been stopped." assert response == {RUNNING_FLAG: False}, "The aggregator should have been stopped."
assert not self.config.get_settings().get(STATUS_ATTRIBUTE), "The aggregator should have been stopped." assert not self.config.get_settings().get(RUNNING_FLAG), "The aggregator should have been stopped."
assert self.config.get_settings().get(PROCESS_ATTRIBUTE) is None, "Aggregator process should have been terminated." assert self.config.get_settings().get(PROCESS_ATTRIBUTE) is None, "Aggregator process should have been terminated."
sleep(2) # put a 2 seconds timeout so that the aggregator process can terminate sleep(2) # put a 2 seconds timeout so that the aggregator process can terminate
...@@ -178,14 +181,18 @@ class TestAggregator(object): ...@@ -178,14 +181,18 @@ class TestAggregator(object):
request.body = input_body.encode(request.charset) request.body = input_body.encode(request.charset)
response = AggregatorController(request).put() response = AggregatorController(request).put()
assert response == {STATUS_ATTRIBUTE: False}, "The aggregator should have been stopped." assert response == {RUNNING_FLAG: False}, "The aggregator should have been stopped."
assert not self.config.get_settings().get(STATUS_ATTRIBUTE), "The aggregator should have been stopped." assert not self.config.get_settings().get(RUNNING_FLAG), "The aggregator should have been stopped."
assert self.config.get_settings().get(PROCESS_ATTRIBUTE) is None, "Aggregator process should have been terminated." assert self.config.get_settings().get(PROCESS_ATTRIBUTE) is None, "Aggregator process should have been terminated."
def test_restart(self): def test_restart(self):
"""
Tests restarting the aggregator through an API call.
"""
from clmcservice.views import AggregatorController # nested import so that importing the class view is part of the test itself from clmcservice.views import AggregatorController # nested import so that importing the class view is part of the test itself
assert not self.config.get_settings().get(STATUS_ATTRIBUTE), "Initially aggregator is not running." assert not self.config.get_settings().get(RUNNING_FLAG), "Initially aggregator is not running."
assert self.config.get_settings().get(PROCESS_ATTRIBUTE) is None, "Initially no aggregator process is running." assert self.config.get_settings().get(PROCESS_ATTRIBUTE) is None, "Initially no aggregator process is running."
# test restarting the aggregator process when it is stopped # test restarting the aggregator process when it is stopped
...@@ -194,9 +201,9 @@ class TestAggregator(object): ...@@ -194,9 +201,9 @@ class TestAggregator(object):
request.body = input_body.encode(request.charset) request.body = input_body.encode(request.charset)
response = AggregatorController(request).put() response = AggregatorController(request).put()
assert response == {STATUS_ATTRIBUTE: True}, "The aggregator should have been restarted." assert response == {RUNNING_FLAG: True}, "The aggregator should have been restarted."
assert self.config.get_settings().get(STATUS_ATTRIBUTE), "The aggregator should have been restarted." assert self.config.get_settings().get(RUNNING_FLAG), "The aggregator should have been restarted."
assert self.config.get_settings().get('aggregator_process'), "The aggregator process should have been reinitialised." assert self.config.get_settings().get(PROCESS_ATTRIBUTE), "The aggregator process should have been reinitialised."
# test restarting the aggregator process when it is running # test restarting the aggregator process when it is running
request = testing.DummyRequest() request = testing.DummyRequest()
...@@ -204,9 +211,9 @@ class TestAggregator(object): ...@@ -204,9 +211,9 @@ class TestAggregator(object):
request.body = input_body.encode(request.charset) request.body = input_body.encode(request.charset)
response = AggregatorController(request).put() response = AggregatorController(request).put()
assert response == {STATUS_ATTRIBUTE: True}, "The aggregator should have been restarted." assert response == {RUNNING_FLAG: True}, "The aggregator should have been restarted."
assert self.config.get_settings().get(STATUS_ATTRIBUTE), "The aggregator should have been restarted." assert self.config.get_settings().get(RUNNING_FLAG), "The aggregator should have been restarted."
assert self.config.get_settings().get('aggregator_process'), "The aggregator process should have been reinitialised." assert self.config.get_settings().get(PROCESS_ATTRIBUTE), "The aggregator process should have been reinitialised."
# kill the started process after the test is over # kill the started process after the test is over
pid = request.registry.settings[PROCESS_ATTRIBUTE].pid pid = request.registry.settings[PROCESS_ATTRIBUTE].pid
...@@ -222,9 +229,13 @@ class TestAggregator(object): ...@@ -222,9 +229,13 @@ class TestAggregator(object):
'{}' '{}'
]) ])
def test_malformed_actions(self, input_body): def test_malformed_actions(self, input_body):
"""
Tests sending a malformed type of action to the aggregator through an API call.
"""
from clmcservice.views import AggregatorController # nested import so that importing the class view is part of the test itself from clmcservice.views import AggregatorController # nested import so that importing the class view is part of the test itself
assert not self.config.get_settings().get(STATUS_ATTRIBUTE), "Initially aggregator is not running." assert not self.config.get_settings().get(RUNNING_FLAG), "Initially aggregator is not running."
assert self.config.get_settings().get(PROCESS_ATTRIBUTE) is None, "Initially no aggregator process is running." assert self.config.get_settings().get(PROCESS_ATTRIBUTE) is None, "Initially no aggregator process is running."
# test restarting the aggregator process when it is running # test restarting the aggregator process when it is running
...@@ -239,3 +250,111 @@ class TestAggregator(object): ...@@ -239,3 +250,111 @@ class TestAggregator(object):
error_raised = True error_raised = True
assert error_raised assert error_raised
def test_GET_status(self):
"""
Tests the GET method for the status of the aggregator.
"""
from clmcservice.views import AggregatorController # nested import so that importing the class view is part of the test itself
assert not self.config.get_settings().get(RUNNING_FLAG), "Initially aggregator is not running."
assert self.config.get_settings().get(PROCESS_ATTRIBUTE) is None, "Initially no aggregator process is running."
request = testing.DummyRequest()
response = AggregatorController(request).get()
assert response == {'aggregator_running': False}, "Response must be a dictionary representing a JSON object with the correct status data of the aggregator."
assert not self.config.get_settings().get(RUNNING_FLAG), "A GET request must not modify the aggregator status flag."
assert self.config.get_settings().get(PROCESS_ATTRIBUTE) is None, "A GET request must not start the aggregator process."
# test status with malformed configuration
self.config.get_settings()[MALFORMED_FLAG] = True
self.config.get_settings()[RUNNING_FLAG] = True
request = testing.DummyRequest()
response = AggregatorController(request).get()
assert response == {'aggregator_running': True,
'malformed': True,
'comment': 'Aggregator is running in a malformed state - it uses an old version of the configuration. Please, restart it so that the updated configuration is used.'}, \
"Response must be a dictionary representing a JSON object with the correct status data of the aggregator."
assert self.config.get_settings().get(RUNNING_FLAG), "A GET request must not modify the aggregator status flag."
assert self.config.get_settings().get(MALFORMED_FLAG), "A GET request must not modify the aggregator malformed flag."
assert self.config.get_settings().get(PROCESS_ATTRIBUTE) is None, "A GET request must not start the aggregator process."
def test_malformed_flag_behaviour(self):
"""
Tests the behaviour of the malformed configuration flag of the aggregator when doing a sequence of API calls.
"""
from clmcservice.views import AggregatorController, AggregatorConfig # nested import so that importing the class view is part of the test itself
assert not self.config.get_settings().get(RUNNING_FLAG), "Initially aggregator is not running."
assert not self.config.get_settings().get(MALFORMED_FLAG), "Initially aggregator is not in a malformed state"
assert self.config.get_settings().get(PROCESS_ATTRIBUTE) is None, "Initially no aggregator process is running."
assert self.config.get_settings().get('aggregator_report_period') == 5, "Initial report period is 5 seconds."
assert self.config.get_settings().get('aggregator_database_name') == 'E2EMetrics', "Initial database name the aggregator uses is E2EMetrics."
assert self.config.get_settings().get('aggregator_database_url') == "http://172.40.231.51:8086", "Initial aggregator url is http://172.40.231.51:8086"
# start the aggregator with the default configuration
request = testing.DummyRequest()
input_body = '{"action": "start"}'
request.body = input_body.encode(request.charset)
response = AggregatorController(request).put()
assert response == {RUNNING_FLAG: True}, "The aggregator should have been started."
# update the configuration of the aggregator while it is running
config_body = '{"aggregator_report_period": 15, "aggregator_database_name": "E2EMetrics", "aggregator_database_url": "http://172.50.231.51:8086"}'
output_body = {'aggregator_report_period': 15, 'aggregator_database_name': "E2EMetrics", 'aggregator_database_url': "http://172.50.231.51:8086"}
request = testing.DummyRequest()
request.body = config_body.encode(request.charset)
response = AggregatorConfig(request).put()
assert response == output_body, "Response of PUT request must include the new configuration of the aggregator"
assert self.config.get_settings().get(RUNNING_FLAG), "The aggregator shouldn't be stopped when the configuration is updated."
assert self.config.get_settings().get(MALFORMED_FLAG), "The malformed flag should be set when the configuration is updated while the process is running."
assert self.config.get_settings().get(PROCESS_ATTRIBUTE) is not None, "The aggregator shouldn't be stopped when the configuration is updated."
# check that the malformed flag has been updated through a GET call
request = testing.DummyRequest()
response = AggregatorController(request).get()
assert response == {'aggregator_running': True,
'malformed': True,
'comment': 'Aggregator is running in a malformed state - it uses an old version of the configuration. Please, restart it so that the updated configuration is used.'}, \
"Response must be a dictionary representing a JSON object with the correct status data of the aggregator."
# restart the aggregator with the new configuration
request = testing.DummyRequest()
input_body = '{"action": "restart"}'
request.body = input_body.encode(request.charset)
response = AggregatorController(request).put()
assert response == {RUNNING_FLAG: True}, "The aggregator should have been restarted."
assert self.config.get_settings().get(RUNNING_FLAG), "The aggregator should have been restarted."
assert not self.config.get_settings().get(MALFORMED_FLAG), "The malformed flag should have been reset to False."
assert self.config.get_settings().get(PROCESS_ATTRIBUTE) is not None, "The aggregator should have been restarted."
# update the configuration again while the aggregator is running
config_body = '{"aggregator_report_period": 30, "aggregator_database_name": "E2EMetrics", "aggregator_database_url": "http://172.50.231.51:8086"}'
output_body = {'aggregator_report_period': 30, 'aggregator_database_name': "E2EMetrics", 'aggregator_database_url': "http://172.50.231.51:8086"}
request = testing.DummyRequest()
request.body = config_body.encode(request.charset)
response = AggregatorConfig(request).put()
assert response == output_body, "Response of PUT request must include the new configuration of the aggregator"
assert self.config.get_settings().get(RUNNING_FLAG), "The aggregator shouldn't be stopped when the configuration is updated."
assert self.config.get_settings().get(MALFORMED_FLAG), "The malformed flag should be set when the configuration is updated while the process is running."
assert self.config.get_settings().get(PROCESS_ATTRIBUTE) is not None, "The aggregator shouldn't be stopped when the configuration is updated."
# stop the aggregator - this should also reset the malformed status flag
# restart the aggregator with the new configuration
request = testing.DummyRequest()
input_body = '{"action": "stop"}'
request.body = input_body.encode(request.charset)
response = AggregatorController(request).put()
assert response == {RUNNING_FLAG: False}, "The aggregator should have been stopped."
assert not self.config.get_settings().get(RUNNING_FLAG), "The aggregator should have been stopped."
assert not self.config.get_settings().get(MALFORMED_FLAG), "The malformed flag should have been reset to False."
assert self.config.get_settings().get(PROCESS_ATTRIBUTE) is None, "The aggregator should have been stopped."
...@@ -24,21 +24,18 @@ ...@@ -24,21 +24,18 @@
from json import loads from json import loads
CONFIG_ATTRIBUTES = ('aggregator_report_period', 'aggregator_database_name', 'aggregator_database_url') CONFIG_ATTRIBUTES = ('aggregator_report_period', 'aggregator_database_name', 'aggregator_database_url') # all of the configuration attributes - to be used as dictionary keys
STATUS_ATTRIBUTE = 'aggregator_running' RUNNING_FLAG = 'aggregator_running' # Attribute for storing the flag, which shows whether the aggregator is running or not - to be used as a dictionary key
PROCESS_ATTRIBUTE = 'aggregator_process' PROCESS_ATTRIBUTE = 'aggregator_process' # Attribute for storing the process object of the aggregator - to be used as a dictionary key
# # URL regular expression used in the django framework # a 'malformed' running state of the aggregator is when the configuration is updated, but the aggregator is not restarted so it is running with an old version of the conf.
# url_regex = re.compile( MALFORMED_FLAG = 'malformed' # Attribute for storing the flag, which shows whether the aggregator is running in an malformed state or not - to be used as a dictionary key
# r'^(?:http|ftp)s?://' # http:// or https://
# r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain... # used to indicate a malformed configuration message
# r'localhost|' # localhost... COMMENT_ATTRIBUTE = 'comment'
# r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|' # ...or ipv4 COMMENT_VALUE = 'Aggregator is running in a malformed state - it uses an old version of the configuration. Please, restart it so that the updated configuration is used.'
# r'\[?[A-F0-9]*:[A-F0-9:]+\]?)' # ...or ipv6
# r'(?::\d+)?' # optional port
# r'(?:/?|[/?]\S+)$', re.IGNORECASE)
def validate_config_content(configuration): def validate_config_content(configuration):
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
from pyramid.view import view_defaults from pyramid.view import view_defaults
from pyramid.httpexceptions import HTTPBadRequest from pyramid.httpexceptions import HTTPBadRequest
from subprocess import Popen, DEVNULL from subprocess import Popen, DEVNULL
from clmcservice.utilities import validate_config_content, validate_action_content, CONFIG_ATTRIBUTES, STATUS_ATTRIBUTE, PROCESS_ATTRIBUTE from clmcservice.utilities import validate_config_content, validate_action_content, CONFIG_ATTRIBUTES, RUNNING_FLAG, PROCESS_ATTRIBUTE, MALFORMED_FLAG, COMMENT_ATTRIBUTE, COMMENT_VALUE
import os.path import os.path
...@@ -63,7 +63,7 @@ class AggregatorConfig(object): ...@@ -63,7 +63,7 @@ class AggregatorConfig(object):
:raises HTTPBadRequest: if request body is not a valid JSON for the configurator :raises HTTPBadRequest: if request body is not a valid JSON for the configurator
""" """
# old_config = {attribute: self.request.registry.settings.get(attribute) for attribute in CONFIG_ATTRIBUTES} old_config = {attribute: self.request.registry.settings.get(attribute) for attribute in CONFIG_ATTRIBUTES}
new_config = self.request.body.decode(self.request.charset) new_config = self.request.body.decode(self.request.charset)
try: try:
...@@ -72,9 +72,9 @@ class AggregatorConfig(object): ...@@ -72,9 +72,9 @@ class AggregatorConfig(object):
for attribute in CONFIG_ATTRIBUTES: for attribute in CONFIG_ATTRIBUTES:
self.request.registry.settings[attribute] = new_config.get(attribute) self.request.registry.settings[attribute] = new_config.get(attribute)
# if not equal_configurations(old_config, new_config): # if configuration is not already malformed, check whether the configuration is updated
# stop_aggregator() if not self.request.registry.settings[MALFORMED_FLAG]:
# start_aggregator(new_config) self.request.registry.settings[MALFORMED_FLAG] = old_config != new_config and self.request.registry.settings[RUNNING_FLAG]
return new_config return new_config
...@@ -106,7 +106,11 @@ class AggregatorController(object): ...@@ -106,7 +106,11 @@ class AggregatorController(object):
""" """
aggregator_data = self.request.registry.settings aggregator_data = self.request.registry.settings
config = {STATUS_ATTRIBUTE: aggregator_data.get(STATUS_ATTRIBUTE)} config = {RUNNING_FLAG: aggregator_data.get(RUNNING_FLAG)}
if aggregator_data[MALFORMED_FLAG] and aggregator_data[RUNNING_FLAG]:
config[MALFORMED_FLAG] = True
config[COMMENT_ATTRIBUTE] = COMMENT_VALUE
return config return config
...@@ -128,22 +132,24 @@ class AggregatorController(object): ...@@ -128,22 +132,24 @@ class AggregatorController(object):
action = content['action'] action = content['action']
if action == 'start': if action == 'start':
aggregator_started = self.request.registry.settings[STATUS_ATTRIBUTE] aggregator_started = self.request.registry.settings[RUNNING_FLAG]
if not aggregator_started: if not aggregator_started:
process = self.start_aggregator(config) process = self.start_aggregator(config)
self.request.registry.settings[STATUS_ATTRIBUTE] = True self.request.registry.settings[RUNNING_FLAG] = True
self.request.registry.settings[PROCESS_ATTRIBUTE] = process self.request.registry.settings[PROCESS_ATTRIBUTE] = process
elif action == 'stop': elif action == 'stop':
self.stop_aggregator(self.request.registry.settings.get(PROCESS_ATTRIBUTE)) self.stop_aggregator(self.request.registry.settings.get(PROCESS_ATTRIBUTE))
self.request.registry.settings[STATUS_ATTRIBUTE] = False self.request.registry.settings[RUNNING_FLAG] = False
self.request.registry.settings[PROCESS_ATTRIBUTE] = None self.request.registry.settings[PROCESS_ATTRIBUTE] = None
self.request.registry.settings[MALFORMED_FLAG] = False
elif action == 'restart': elif action == 'restart':
self.stop_aggregator(self.request.registry.settings.get(PROCESS_ATTRIBUTE)) self.stop_aggregator(self.request.registry.settings.get(PROCESS_ATTRIBUTE))
process = self.start_aggregator(config) process = self.start_aggregator(config)
self.request.registry.settings[STATUS_ATTRIBUTE] = True self.request.registry.settings[RUNNING_FLAG] = True
self.request.registry.settings[PROCESS_ATTRIBUTE] = process self.request.registry.settings[PROCESS_ATTRIBUTE] = process
self.request.registry.settings[MALFORMED_FLAG] = False
return {STATUS_ATTRIBUTE: self.request.registry.settings.get(STATUS_ATTRIBUTE)} return {RUNNING_FLAG: self.request.registry.settings.get(RUNNING_FLAG)}
except AssertionError: except AssertionError:
raise HTTPBadRequest('Bad request content - must be in JSON format: {"action": value}, where value is "start", "stop" or "restart".') raise HTTPBadRequest('Bad request content - must be in JSON format: {"action": value}, where value is "start", "stop" or "restart".')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment