Skip to content

Commit

Permalink
Merge b81655d into fd3ac3b
Browse files Browse the repository at this point in the history
  • Loading branch information
mohierf committed Mar 20, 2017
2 parents fd3ac3b + b81655d commit cefb6c9
Show file tree
Hide file tree
Showing 18 changed files with 103 additions and 80 deletions.
23 changes: 5 additions & 18 deletions alignak_backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1991,24 +1991,11 @@ def index(path):


def main():
"""
Called when this module is started from shell
"""
try:
print("--------------------------------------------------------------------------------")
print("%s, listening on %s:%d" % (
manifest['name'], app.config.get('HOST', '127.0.0.1'), app.config.get('PORT', 8090)
))
print("--------------------------------------------------------------------------------")
app.run(
host=settings.get('HOST', '127.0.0.1'),
port=settings.get('PORT', 5000),
debug=settings.get('DEBUG', False)
)
except Exception as e:
print("Application run failed, exception: %s / %s" % (type(e), str(e)))
print("Back trace of this kill: %s" % traceback.format_exc())
sys.exit(1)
"""Called when this module is started from Python shell"""
print("--------------------------------------------------------------------------------")
print("Running `python app.py` is no more supported. Use 'alignak-backend' shell script.")
print("--------------------------------------------------------------------------------")
sys.exit(1)

if __name__ == "__main__":
main()
25 changes: 21 additions & 4 deletions alignak_backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,31 @@
"""
Main
"""
from alignak_backend.app import app
from __future__ import print_function

from alignak_backend.app import app, manifest


def main():
"""Main function to run the alignak backend
This function is used by the `alignak-backend` script installed with setup.py.
It is intended to run the backend in a single process mode. This mode is not
recommended for production but may be useful in development to avoir setting-up
an uWSGI server.
"""
Main function
"""
app.run()
try:
host = app.config.get('HOST', '127.0.0.1')
if not host:
host = '0.0.0.0'
port = app.config.get('PORT', 5000)
url = "http://%s:%d" % (host, port)
print("--------------------------------------------------------------------------------")
print("%s, listening on %s" % (manifest['name'], url))
print("--------------------------------------------------------------------------------")
app.run(host=host, port=port, debug=app.config.get('DEBUG', False))
except Exception as e:
print("Application run failed, exception: %s / %s" % (type(e), str(e)))

if __name__ == "__main__": # pragma: no cover
main()
4 changes: 2 additions & 2 deletions alignak_backend/models/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ def get_schema():
Schema structure of this resource
host/service define the concerned host/service for this history event. If the service is null
then the vent is an host event. When posting an history the backend will value the host_name
then the event is an host event. When posting an history the backend will value the host_name
and service_name fields automatically, this to keep human readable information even when the
host/service do not exist anymore in the backend or if host/service _id got changed (host
delated and re-created).
deleted and re-created).
The type field defines the event type (see the type definition for the allowed events)
Expand Down
2 changes: 1 addition & 1 deletion test/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def setUpClass(cls):
)
assert exit_code == 0

cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignakbackend:app',
cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignak_backend.app:app',
'--socket', '0.0.0.0:5000',
'--protocol=http', '--enable-threads', '--pidfile',
'/tmp/uwsgi.pid'])
Expand Down
2 changes: 1 addition & 1 deletion test/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def setUpClass(cls):
)
assert exit_code == 0

cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignakbackend:app',
cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignak_backend.app:app',
'--socket', '0.0.0.0:5000',
'--protocol=http', '--enable-threads', '--pidfile',
'/tmp/uwsgi.pid'])
Expand Down
2 changes: 1 addition & 1 deletion test/test_grafana.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def setUpClass(cls):
)
assert exit_code == 0

cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignakbackend:app',
cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignak_backend.app:app',
'--socket', '0.0.0.0:5000',
'--protocol=http', '--enable-threads', '--pidfile',
'/tmp/uwsgi.pid', '--logto=/tmp/alignak_backend.log'])
Expand Down
2 changes: 1 addition & 1 deletion test/test_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def setUpClass(cls):
)
assert exit_code == 0

cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignakbackend:app',
cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignak_backend.app:app',
'--socket', '0.0.0.0:5000',
'--protocol=http', '--enable-threads', '--pidfile',
'/tmp/uwsgi.pid'])
Expand Down
2 changes: 1 addition & 1 deletion test/test_hook_livesynthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def setUpClass(cls):
)
assert exit_code == 0

cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignakbackend:app',
cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignak_backend.app:app',
'--socket', '0.0.0.0:5000',
'--protocol=http', '--enable-threads', '--pidfile',
'/tmp/uwsgi.pid'])
Expand Down
2 changes: 1 addition & 1 deletion test/test_hook_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def setUpClass(cls):
)
assert exit_code == 0

cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignakbackend:app',
cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignak_backend.app:app',
'--socket', '0.0.0.0:5000',
'--protocol=http', '--enable-threads', '--pidfile',
'/tmp/uwsgi.pid'])
Expand Down
2 changes: 1 addition & 1 deletion test/test_keep_default_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def setUpClass(cls):
)
assert exit_code == 0

cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignakbackend:app',
cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignak_backend.app:app',
'--socket', '0.0.0.0:5000',
'--protocol=http', '--enable-threads', '--pidfile',
'/tmp/uwsgi.pid'])
Expand Down
2 changes: 1 addition & 1 deletion test/test_livesynthesis_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def setUpClass(cls):
)
assert exit_code == 0

cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignakbackend:app',
cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignak_backend.app:app',
'--socket', '0.0.0.0:5000',
'--protocol=http', '--enable-threads', '--pidfile',
'/tmp/uwsgi.pid'])
Expand Down
2 changes: 1 addition & 1 deletion test/test_logcheckresult.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def setUpClass(cls):
)
assert exit_code == 0

cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignakbackend:app',
cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignak_backend.app:app',
'--socket', '0.0.0.0:5000',
'--protocol=http', '--enable-threads', '--pidfile',
'/tmp/uwsgi.pid'])
Expand Down
2 changes: 1 addition & 1 deletion test/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def setUpClass(cls):
)
assert exit_code == 0

cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignakbackend:app',
cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignak_backend.app:app',
'--socket', '0.0.0.0:5000',
'--protocol=http', '--enable-threads', '--pidfile',
'/tmp/uwsgi.pid'])
Expand Down
2 changes: 1 addition & 1 deletion test/test_overall_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def setUpClass(cls):
)
assert exit_code == 0

cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignakbackend:app',
cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignak_backend.app:app',
'--socket', '0.0.0.0:5000',
'--protocol=http', '--enable-threads', '--pidfile',
'/tmp/uwsgi.pid'])
Expand Down
103 changes: 61 additions & 42 deletions test/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
This test check is backend is running without error
"""

from __future__ import print_function

import os
import time
import shlex
Expand All @@ -12,56 +14,73 @@
import unittest2


class TestRun(unittest2.TestCase):
"""
This class test if backend is available after start it (start without errors)
"""
class TestStart(unittest2.TestCase):
"""Test the different application start mode"""
def test_start_application_uwsgi(self):
""" Start alignak backend with uwsgi"""

@classmethod
def setUpClass(cls):
"""
This method:
* delete mongodb database
* start the backend with uwsgi
* log in the backend and get the token
* get the realm
os.getcwd()
print("Launching application with UWSGI ...")

:return: None
"""
# Set test mode for Alignak backend
os.environ['TEST_ALIGNAK_BACKEND'] = '1'
os.environ['ALIGNAK_BACKEND_MONGO_DBNAME'] = 'alignak-backend-test'
test_dir = os.path.dirname(os.path.realpath(__file__))
print("Dir: %s" % test_dir)

# Delete used mongo DBs
exit_code = subprocess.call(
shlex.split(
'mongo %s --eval "db.dropDatabase()"' % os.environ['ALIGNAK_BACKEND_MONGO_DBNAME'])
print("Starting Alignak Backend...")
subprocess.Popen(
shlex.split('uwsgi --plugin python -w alignak_backend.app:app --socket 0.0.0.0:5000 '
'--protocol=http --enable-threads --pidfile /tmp/uwsgi.pid')
)
assert exit_code == 0

cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignakbackend:app',
'--socket', '0.0.0.0:5000',
'--protocol=http', '--enable-threads', '--pidfile',
'/tmp/uwsgi.pid'])
time.sleep(3)
time.sleep(1)

cls.endpoint = 'http://127.0.0.1:5000'
headers = {'Content-Type': 'application/json'}
params = {'username': 'admin', 'password': 'admin'}

@classmethod
def tearDownClass(cls):
"""
Kill uwsgi
# Log onto the backend
response = requests.post('http://127.0.0.1:5000/login', json=params, headers=headers)
resp = response.json()
assert resp['token']

:return: None
"""
subprocess.call(['uwsgi', '--stop', '/tmp/uwsgi.pid'])
time.sleep(2)

def test_send_to_backend_livehost(self):
"""
Test if backend is up and answer
def test_start_application(self):
""" Start application stand alone"""
print('Launching application in dev mode')

dir_path = os.path.dirname(os.path.realpath(__file__))
os.chdir(os.path.join(dir_path, "../alignak_backend"))
print("Launching application default...")
process = subprocess.Popen(
shlex.split('python ../alignak_backend/main.py'),
stdout=subprocess.PIPE
)
print('PID = ', process.pid)
# it use little time to start
time.sleep(3)

headers = {'Content-Type': 'application/json'}
params = {'username': 'admin', 'password': 'admin'}

# Log onto the backend
response = requests.post('http://127.0.0.1:5000/login', json=params, headers=headers)
resp = response.json()
assert resp['token']

time.sleep(1)

print("Killing application ...")
process.kill()
os.chdir(dir_path)

:return: None
"""
r = requests.get(self.endpoint + '/docs')
self.assertEqual(r.status_code, 200)
def test_start_application_old(self):
""" Start application old mode"""
print('Start application old mode')

dir_path = os.path.dirname(os.path.realpath(__file__))
os.chdir(os.path.join(dir_path, "../alignak_backend"))
print("Launching application default...")
exit_code = subprocess.call(
shlex.split('python ../alignak_backend/app.py')
)
assert exit_code == 1
os.chdir(dir_path)
2 changes: 1 addition & 1 deletion test/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def setUpClass(cls):
)
assert exit_code == 0

cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignakbackend:app',
cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignak_backend.app:app',
'--socket', '0.0.0.0:5000',
'--protocol=http', '--enable-threads', '--pidfile',
'/tmp/uwsgi.pid'])
Expand Down
2 changes: 1 addition & 1 deletion test/test_user_uipreferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def setUpClass(cls):
)
assert exit_code == 0

cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignakbackend:app',
cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignak_backend.app:app',
'--socket', '0.0.0.0:5000',
'--protocol=http', '--enable-threads', '--pidfile',
'/tmp/uwsgi.pid'])
Expand Down
2 changes: 1 addition & 1 deletion test/test_userrestrict.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def setUpClass(cls):
)
assert exit_code == 0

cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignakbackend:app',
cls.p = subprocess.Popen(['uwsgi', '--plugin', 'python', '-w', 'alignak_backend.app:app',
'--socket', '0.0.0.0:5000',
'--protocol=http', '--enable-threads', '--pidfile',
'/tmp/uwsgi.pid'])
Expand Down

0 comments on commit cefb6c9

Please sign in to comment.