def test_host_delete(self): """Delete the newly created hosts""" datamgr = DataManager(alignak_webui.app.app, session=self.session) # Count hosts count = datamgr.count_objects('host') print("Host count: %s" % count) # Get host and service in the backend host = datamgr.get_host({'where': {'name': 'pi1'}}) assert datamgr.delete_object('service', host) is False assert datamgr.delete_object('host', host) is True # Count hosts (one less!) new_count = datamgr.count_objects('host') print("Host count: %s" % new_count) assert new_count == count - 1 # Get host and service in the backend host = datamgr.get_host({'where': {'name': 'pi2'}}) assert datamgr.delete_object('service', host) is False assert datamgr.delete_object('host', host) is True # Count hosts (one less!) new_count = datamgr.count_objects('host') print("Host count: %s" % new_count) assert new_count == count - 2
def test_host_delete(self): """Delete the newly created hosts""" datamgr = DataManager(alignak_webui.app.app, session=self.session) # Count hosts count = datamgr.count_objects('host') print("Host count: %s" % count) # Get host and service in the backend host = datamgr.get_host({'where': {'name': 'pi1'}}) assert datamgr.delete_object('service', host) is False assert datamgr.delete_object('host', host) is True # Count hosts (one less!) new_count = datamgr.count_objects('host') print("Host count: %s" % new_count) assert new_count == count - 1 # Get host and service in the backend host = datamgr.get_host({'where': {'name': 'pi2'}}) assert datamgr.delete_object('service', host) is False assert datamgr.delete_object('host', host) is True # Count hosts (one less!) new_count = datamgr.count_objects('host') print("Host count: %s" % new_count) assert new_count == count - 2
class TestRelations(unittest2.TestCase): def setUp(self): print("setting up ...") self.dmg = DataManager(alignak_webui.app.app) print('Data manager', self.dmg) # Initialize and do not load assert self.dmg.user_login('admin', 'admin', load=False) def tearDown(self): print("tearing down ...") # Logout self.dmg.reset(logout=True) def test_relation_host_command(self): """ Datamanager objects get - host/command relation """ print("--- test Item") # Get main realm self.dmg.get_realm({'where': {'name': 'All'}}) # Get main TP self.dmg.get_timeperiod({'where': {'name': '24x7'}}) # Get host host = self.dmg.get_host({'where': {'name': 'localhost'}}) print(host.__dict__) print(host.check_period) assert isinstance(host.check_command, Command) assert host.check_command def test_relation_host_service(self): """ Datamanager objects get - host/services relation """ print("--- test Item") # Get main realm self.dmg.get_realm({'where': {'name': 'All'}}) # Get main TP self.dmg.get_timeperiod({'where': {'name': '24x7'}}) # Get host host = self.dmg.get_host({'where': {'name': 'localhost'}}) print("Host: ", host.__dict__) # Get services of this host service = self.dmg.get_service( {'where': { 'name': 'Shinken2-broker', 'host_name': host.id }}) print("Services: ", service)
def test_host_submit_form(self): """Edit and submit host edition form""" datamgr = DataManager(alignak_webui.app.app, session=self.session) # Get host and service in the backend host = datamgr.get_host({'where': {'name': 'alignak_glpi'}}) services = datamgr.get_host_services(host) service = services[0] print('Enable edition mode') response = self.app.post('/edition_mode', params={'state': 'on'}) session = response.request.environ['beaker.session'] # edition_mode is defined and activated in the session... assert 'edition_mode' in session assert True == session['edition_mode'] assert response.json == {'edition_mode': True, 'message': 'Edition mode enabled'} print('get page /host_form (edition mode) - with an host id') response = self.app.get('/host_form/%s' % host.id) response.mustcontain( '''<div id="form_host">''', '''<form role="form" data-element="%s" class="element_form " method="post" action="/host_form/%s">''' % ( host.id, host.id), '''$('form[data-element="%s"]').on("submit", function (evt) {''' % host.id ) print('Post form updates') data = { 'name': host.name, 'alias': "Alias edited" } response = self.app.post('/host_form/%s' % host.id, params=data) assert response.json == { "_message": "host 'alignak_glpi' updated", "alias": "Alias edited" }
class TestLoadCreate(unittest2.TestCase): def setUp(self): self.dmg = DataManager(alignak_webui.app.app) print('Data manager', self.dmg) def test_3_1_load(self): """ Datamanager load """ print('test load as admin') # Initialize and load ... no reset assert self.dmg.user_login('admin', 'admin') result = self.dmg.load() print("Result:", result) # self.assertEqual(result, 0) # No new objects created ... # Initialize and load ... with reset result = self.dmg.load(reset=True) print("Result:", result) # Must not have loaded any objects ... behavior changed, no more objects loading on login # self.assertEqual(result, 0) def test_3_3_get_errors(self): """ Datamanager objects get errors """ print('test get errors') # Initialize and load ... no reset assert self.dmg.user_login('admin', 'admin') result = self.dmg.load() print("Result:", result) assert result == 0 # No new objects created ... # Get elements error item = self.dmg.get_user('unknown') assert not item item = self.dmg.get_userrestrictrole('unknown') assert not item item = self.dmg.get_realm('unknown') assert not item item = self.dmg.get_host('unknown') assert not item item = self.dmg.get_hostgroup('unknown') assert not item item = self.dmg.get_hostdependency('unknown') assert not item item = self.dmg.get_service('unknown') assert not item item = self.dmg.get_servicegroup('unknown') assert not item item = self.dmg.get_servicedependency('unknown') assert not item item = self.dmg.get_command('unknown') assert not item item = self.dmg.get_history('unknown') assert not item item = self.dmg.get_logcheckresult('unknown') assert not item item = self.dmg.get_timeperiod('unknown') assert not item
def test_host_form(self): """View host edition form""" # Get host in the backend datamgr = DataManager(alignak_webui.app.app, session=self.session) host = datamgr.get_host({'where': {'name': 'alignak_glpi'}}) print('get page /host_form (reading mode)') response = self.app.get('/host_form/%s' % host.id) response.mustcontain( '''<div id="form_host">''', '''<form role="form" data-element="%s" class="element_form " >''' % host.id, no=[ '''$('form[data-element="%s"]').on("submit", function (evt) {''' % host.id ]) print('Enable edition mode') response = self.app.post('/edition_mode', params={'state': 'on'}) session = response.request.environ['beaker.session'] # edition_mode is defined and activated in the session... assert 'edition_mode' in session assert True == session['edition_mode'] assert response.json == { 'edition_mode': True, 'message': 'Edition mode enabled' } print('get page /host_form (edition mode) - with an host id') response = self.app.get('/host_form/%s' % host.id) response.mustcontain( '''<div id="form_host">''', '''<form role="form" data-element="%s" class="element_form " method="post" action="/host_form/%s">''' % (host.id, host.id), '''$('form[data-element="%s"]').on("submit", function (evt) {''' % host.id) print('get page /host_form (edition mode) - with an host name') response = self.app.get('/host_form/%s' % host.name) response.mustcontain( '''<div id="form_host">''', '''<form role="form" data-element="%s" class="element_form " method="post" action="/host_form/%s">''' % (host.id, host.id), '''$('form[data-element="%s"]').on("submit", function (evt) {''' % host.id) print('get page /host_form (edition mode) - for a new host') response = self.app.get('/host_form/unknown_host') response.mustcontain( '''<div id="form_host">''', '''<form role="form" data-element="None" class="element_form " method="post" action="/host_form/None">''', '''<h4>You are creating a new host.</h4>''', '''$('form[data-element="None"]').on("submit", function (evt) {''')
def test_host_form(self): """View host edition form""" # Get host in the backend datamgr = DataManager(alignak_webui.app.app, session=self.session) host = datamgr.get_host({'where': {'name': 'alignak_glpi'}}) print('get page /host_form (reading mode)') response = self.app.get('/host_form/%s' % host.id) response.mustcontain( '''<div id="form_host">''', '''<form role="form" data-element="%s" class="element_form " >''' % host.id, no = [ '''$('form[data-element="%s"]').on("submit", function (evt) {''' % host.id ] ) print('Enable edition mode') response = self.app.post('/edition_mode', params={'state': 'on'}) session = response.request.environ['beaker.session'] # edition_mode is defined and activated in the session... assert 'edition_mode' in session assert True == session['edition_mode'] assert response.json == {'edition_mode': True, 'message': 'Edition mode enabled'} print('get page /host_form (edition mode) - with an host id') response = self.app.get('/host_form/%s' % host.id) response.mustcontain( '''<div id="form_host">''', '''<form role="form" data-element="%s" class="element_form " method="post" action="/host_form/%s">''' % ( host.id, host.id), '''$('form[data-element="%s"]').on("submit", function (evt) {''' % host.id ) print('get page /host_form (edition mode) - with an host name') response = self.app.get('/host_form/%s' % host.name) response.mustcontain( '''<div id="form_host">''', '''<form role="form" data-element="%s" class="element_form " method="post" action="/host_form/%s">''' % ( host.id, host.id), '''$('form[data-element="%s"]').on("submit", function (evt) {''' % host.id ) print('get page /host_form (edition mode) - for a new host') response = self.app.get('/host_form/unknown_host') response.mustcontain( '''<div id="form_host">''', '''<form role="form" data-element="None" class="element_form " method="post" action="/host_form/None">''', '''<h4>You are creating a new host.</h4>''', '''$('form[data-element="None"]').on("submit", function (evt) {''' )
def test_host_submit_form(self): """Edit and submit host edition form""" datamgr = DataManager(alignak_webui.app.app, session=self.session) # Get host and service in the backend host = datamgr.get_host({'where': {'name': 'alignak_glpi'}}) services = datamgr.get_host_services(host) service = services[0] print('Enable edition mode') response = self.app.post('/edition_mode', params={'state': 'on'}) session = response.request.environ['beaker.session'] # edition_mode is defined and activated in the session... assert 'edition_mode' in session assert True == session['edition_mode'] assert response.json == { 'edition_mode': True, 'message': 'Edition mode enabled' } print('get page /host_form (edition mode) - with an host id') response = self.app.get('/host_form/%s' % host.id) response.mustcontain( '''<div id="form_host">''', '''<form role="form" data-element="%s" class="element_form " method="post" action="/host_form/%s">''' % (host.id, host.id), '''$('form[data-element="%s"]').on("submit", function (evt) {''' % host.id) print('Post form updates') data = {'name': host.name, 'alias': "Alias edited"} response = self.app.post('/host_form/%s' % host.id, params=data) assert response.json == { "_message": "host 'alignak_glpi' updated", "alias": "Alias edited" }
def test_command(self): """ Actions - command""" print('test command') print('get page /command/form/add') response = self.app.get('/command/form/add') response.mustcontain( '<form class="form-horizontal" data-item="command" data-action="add" ' ) print('get page /command/parameters - bad parameters') response = self.app.get('/command/parameters', status=409) assert response.json == {'error': "the command 'None' does not exist"} response = self.app.get( '/command/parameters?command=fake&elements_type=host', status=409) assert response.json == {'error': "the command 'fake' does not exist"} response = self.app.get( '/command/parameters?command=process_host_check_result&elements_type=fake', status=409) assert response.json == { 'error': "the plugin for 'fake' is not existing or not installed" } print('get page /command/parameters') response = self.app.get( '/command/parameters?elements_type=host&command=process_host_check_result' ) expected = { "ls_state_id": { "allowed": { "0": "Up", "1": "Down (1)", "2": "Not used (2)", "3": "Not used (3)", "4": "Unreachable" }, "allowed_0": "Up", "allowed_1": "Down (1)", "allowed_2": "Not used (2)", "allowed_3": "Not used (3)", "allowed_4": "Unreachable", "comment": "Current state identifier. O: UP, 1: DOWN, 2/3: NOT USED, 4: UNREACHABLE", "default": 3, "title": "State identifier", "editable": True, "hidden": True, "type": "integer" }, "ls_output": { "default": "Check output from WebUI", "type": "string", "title": "Output", "editable": True, "comment": "Last check output" }, "ls_long_output": { "default": "", "type": "string", "title": "Long output", "editable": True, "visible": False, "comment": "Last check long output" }, "ls_perf_data": { "default": "", "type": "string", "title": "Performance data", "editable": True, "visible": False, "comment": "Last check performance data" } } assert expected == response.json # Current user is admin session = response.request.environ['beaker.session'] assert 'current_user' in session and session['current_user'] assert session['current_user'].get_username() == 'admin' # Data manager datamgr = DataManager(alignak_webui.app.app, session=session) # Get host and user in the backend host = datamgr.get_host({'where': {'name': 'localhost'}}) user = datamgr.get_user({'where': {'name': 'admin'}}) # ------------------------------------------- # Add a command # Missing or invalid parameters! data = { # "command": "test", "elements_type": 'host', "element_id": host.id } response = self.app.post('/command/add', data, status=400) print(response) # Unknown command data = { "command": "test", "elements_type": 'host', "element_id": host.id } response = self.app.post('/command/add', data, status=400) # Missing command parameter data = { "command": "process_host_check_result", "elements_type": 'host', "element_id": host.id } response = self.app.post('/command/add', data, status=400) # Command for an host data = { "command": "process_host_check_result", "elements_type": 'host', "element_id": host.id, "ls_state_id": '0', "ls_output": "New output...", "ls_long_output": "", "ls_perf_data": "", } response = self.app.post('/command/add', data, status=409) # As of #193... assert response.json['status'] == "ko" assert response.json[ 'message'] == "Failed sending a command for localhost. "
def test_recheck(self): """ Actions - recheck""" print('test recheck') print('get page /recheck/form/add') response = self.app.get('/recheck/form/add') response.mustcontain( '<form class="form-horizontal" data-item="recheck" data-action="add" ' ) # Current user is admin session = response.request.environ['beaker.session'] assert 'current_user' in session and session['current_user'] assert session['current_user'].get_username() == 'admin' # Data manager datamgr = DataManager(alignak_webui.app.app, session=session) # Get host, user and realm in the backend host = datamgr.get_host({'where': {'name': 'localhost'}}) user = datamgr.get_user({'where': {'name': 'admin'}}) # ------------------------------------------- # Add a recheck # Missing livestate_id! data = { "host": host.id, "service": None, "sticky": True, "persistent": True, "notify": True, "comment": "User comment", } response = self.app.post('/recheck/add', data, status=400) # Recheck an host data = { "elements_type": 'host', "element_id": host.id, "comment": "User comment", } response = self.app.post('/recheck/add', data) assert response.json['status'] == "ok" assert response.json['message'] == "Check request sent for localhost. " # Recheck a service service = datamgr.get_service( {'where': { 'host': host.id, 'name': 'Cpu' }}) data = { "elements_type": 'service', "element_id": service.id, "comment": "User comment", } response = self.app.post('/recheck/add', data) assert response.json['status'] == "ok" assert response.json[ 'message'] == "Check request sent for localhost/Cpu. " # Recheck several services service1 = datamgr.get_service( {'where': { 'host': host.id, 'name': 'Cpu' }}) service2 = datamgr.get_service( {'where': { 'host': host.id, 'name': 'Memory' }}) data = { "action": "add", "elements_type": 'service', "element_id": [service1.id, service2.id, 'test'], "comment": "User comment", } response = self.app.post('/recheck/add', data) assert response.json['status'] == "ok" assert response.json[ 'message'] == "Check request sent for localhost/Cpu. Check request sent for localhost/Memory. service element test does not exist. "
def test_downtime(self): """ Actions - downtime""" print('test actions') print('get page /downtime/form/add') response = self.app.get('/downtime/form/add') response.mustcontain( '<form class="form-horizontal" data-item="downtime" data-action="add"' ) # Current user is admin session = response.request.environ['beaker.session'] assert 'current_user' in session and session['current_user'] assert session['current_user'].get_username() == 'admin' # Data manager datamgr = DataManager(alignak_webui.app.app, session=session) # Get host, user and realm in the backend host = datamgr.get_host({'where': {'name': 'localhost'}}) user = datamgr.get_user({'where': {'name': 'admin'}}) now = datetime.utcnow() later = now + timedelta(days=2, hours=4, minutes=3, seconds=12) now = timegm(now.timetuple()) later = timegm(later.timetuple()) # ------------------------------------------- # Add an downtime # Missing livestate_id! data = { "action": "add", "host": host.id, "service": None, "start_time": now, "end_time": later, "fixed": False, 'duration': 86400, "comment": "User comment", } self.app.post('/downtime/add', data, status=400) # downtime an host data = { "action": "add", "element_id": host.id, "start_time": now, "end_time": later, "fixed": False, 'duration': 86400, "comment": "User comment", } response = self.app.post('/downtime/add', data) assert response.json['status'] == "ok" assert response.json['message'] == \ "Downtime sent for localhost. " # downtime a service service = datamgr.get_service( {'where': { 'host': host.id, 'name': 'Cpu' }}) data = { "action": "add", "elements_type": 'service', "element_id": service.id, "start_time": now, "end_time": later, "fixed": False, 'duration': 86400, "comment": "User comment", } response = self.app.post('/downtime/add', data) assert response.json['status'] == "ok" assert response.json['message'] == \ "Downtime sent for localhost/Cpu. " # downtime several services service1 = datamgr.get_service( {'where': { 'host': host.id, 'name': 'Cpu' }}) service2 = datamgr.get_service( {'where': { 'host': host.id, 'name': 'Memory' }}) data = { "action": "add", "elements_type": 'service', "element_id": [service1.id, service2.id, 'test'], "start_time": now, "end_time": later, "fixed": False, 'duration': 86400, "comment": "User comment", } response = self.app.post('/downtime/add', data) assert response.json['status'] == "ok" assert response.json['message'] == \ "Downtime sent for localhost/Cpu. " \ "Downtime sent for localhost/Memory. " \ "service element test does not exist. "
def test_acknowledge(self): """ Actions - acknowledge""" print('test actions') print('get page /acknowledge/form/add') response = self.app.get('/acknowledge/form/add') response.mustcontain( '<form class="form-horizontal" data-item="acknowledge" data-action="add" ' ) # Get Data manager in the session session = response.request.environ['beaker.session'] assert 'current_user' in session and session['current_user'] assert session['current_user'].get_username() == 'admin' datamgr = DataManager(alignak_webui.app.app, session=session) # Get host and user in the backend host = datamgr.get_host({'where': {'name': 'localhost'}}) user = datamgr.get_user({'where': {'name': 'admin'}}) # ------------------------------------------- # Add an acknowledge # Missing element_id! data = { "elements_type": "host", "element_id": host.id, "service": None, "sticky": True, "persistent": True, "notify": True, "comment": "User comment", } self.app.post('/acknowledge/add', data, status=400) # Acknowledge an host data = { "action": "add", "elements_type": 'host', # Default value, can be omitted ... "element_id": host.id, "sticky": True, "persistent": True, "notify": True, "comment": "User comment", } response = self.app.post('/acknowledge/add', data) assert response.json['status'] == "ok" assert response.json['message'] == \ "Acknowledge sent for localhost. " # Acknowledge a service service = datamgr.get_service( {'where': { 'host': host.id, 'name': 'Cpu' }}) data = { "action": "add", "elements_type": 'service', "element_id": service.id, "sticky": True, "persistent": True, "notify": True, "comment": "User comment", } response = self.app.post('/acknowledge/add', data) assert response.json['status'] == "ok" assert response.json['message'] == \ "Acknowledge sent for localhost/Cpu. " # Acknowledge several services service1 = datamgr.get_service( {'where': { 'host': host.id, 'name': 'Cpu' }}) service2 = datamgr.get_service( {'where': { 'host': host.id, 'name': 'Memory' }}) data = { "action": "add", "elements_type": 'service', "element_id": [service1.id, service2.id], "sticky": True, "persistent": True, "notify": True, "comment": "User comment", } response = self.app.post('/acknowledge/add', data) assert response.json['status'] == "ok" assert response.json['message'] == \ "Acknowledge sent for localhost/Cpu. " \ "Acknowledge sent for localhost/Memory. " \ # Acknowledge several services service1 = datamgr.get_service( {'where': { 'host': host.id, 'name': 'Cpu' }}) service2 = datamgr.get_service( {'where': { 'host': host.id, 'name': 'Memory' }}) data = { "action": "add", "elements_type": 'service', "element_id": [service1.id, service2.id, 'test'], "sticky": True, "persistent": True, "notify": True, "comment": "User comment", } response = self.app.post('/acknowledge/add', data) assert response.json['status'] == "ok" assert response.json['message'] == \ "Acknowledge sent for localhost/Cpu. " \ "Acknowledge sent for localhost/Memory. " \ "service element test does not exist. "
def test_service_widgets(self): """External - service widgets""" print('allowed service widgets external access') # Log in to get Data manager in the session response = self.app.get('/login') response.mustcontain('<form role="form" method="post" action="/login">') print('login accepted - go to home page') response = self.app.post('/login', {'username': '******', 'password': '******'}) # Redirected twice: /login -> / -> /dashboard ! redirected_response = response.follow() redirected_response = redirected_response.follow() redirected_response.mustcontain('<div id="livestate">') session = redirected_response.request.environ['beaker.session'] assert 'current_user' in session and session['current_user'] assert session['current_user'].get_username() == 'admin' datamgr = DataManager(alignak_webui.app.app, session=session) # Get host and service in the backend host = datamgr.get_host({'where': {'name': 'KNM-Shinken'}}) print("Host: %s" % host) services = datamgr.get_host_services(host) print("Services: %s" % services) # service = datamgr.get_service({'where': {'name': 'http', 'host': host.id}}) service = services[0] print("Service: %s" % service) # assert False # Get external service widget - no widget identifier self.app.authorization = ('Basic', ('admin', 'admin')) response = self.app.get( '/external/service/%s' % service.id, status=409 ) response.mustcontain('<div><h1>Missing service widget name.</h1><p>You must provide a widget name</p></div>') # Get external service widget - unknown widget self.app.authorization = ('Basic', ('admin', 'admin')) response = self.app.get( '/external/service/%s/unknown' % service.id, status=409 ) response.mustcontain('<div><h1>Unknown required widget: unknown.</h1><p>The required widget is not available.</p></div>') # Service information # Get external service widget self.app.authorization = ('Basic', ('admin', 'admin')) response = self.app.get( '/external/service/%s/information?page' % service.id ) response.mustcontain( '<!DOCTYPE html>', '<html lang="en">', '<body>', '<section>', '<div id="wd_panel_information" class="panel panel-default alignak_webui_widget embedded">', '<!-- Service information widget -->', '</section>', '</body>' ) # Get external service widget, no page parameter self.app.authorization = ('Basic', ('admin', 'admin')) response = self.app.get( '/external/service/%s/information' % service.id ) response.mustcontain( '<div id="wd_panel_information" class="panel panel-default alignak_webui_widget embedded">', '<!-- Service information widget -->', ) # service configuration # Get external service widget self.app.authorization = ('Basic', ('admin', 'admin')) response = self.app.get( '/external/service/%s/configuration?page' % service.id ) response.mustcontain( '<!DOCTYPE html>', '<html lang="en">', '<body>', '<section>', '<div id="wd_panel_configuration" class="panel panel-default alignak_webui_widget embedded">', '<!-- Service configuration widget -->', '</section>', '</body>' ) # Get external service widget, no page parameter self.app.authorization = ('Basic', ('admin', 'admin')) response = self.app.get( '/external/service/%s/configuration' % service.id ) response.mustcontain( '<div id="wd_panel_configuration" class="panel panel-default alignak_webui_widget embedded">', '<!-- Service configuration widget -->', ) # Service timeline # Get external service widget self.app.authorization = ('Basic', ('admin', 'admin')) response = self.app.get( '/external/service/%s/timeline?page' % service.id ) response.mustcontain( '<!DOCTYPE html>', '<html lang="en">', '<body>', '<section>', '<div id="wd_panel_timeline" class="panel panel-default alignak_webui_widget embedded">', '<!-- Service timeline widget -->', '</section>', '</body>', '<div class="alert alert-info">', '<p>No available history events.</p>', '</div>' ) # Get external service widget, no page parameter self.app.authorization = ('Basic', ('admin', 'admin')) response = self.app.get( '/external/service/%s/timeline' % service.id ) response.mustcontain( '<div id="wd_panel_timeline" class="panel panel-default alignak_webui_widget embedded">', '<!-- Service timeline widget -->', ) # service metrics # Get external service widget self.app.authorization = ('Basic', ('admin', 'admin')) response = self.app.get( '/external/service/%s/metrics?page' % service.id ) response.mustcontain( '<!DOCTYPE html>', '<html lang="en">', '<body>', '<section>', '<div id="wd_panel_metrics" class="panel panel-default alignak_webui_widget embedded">', '<!-- Service metrics widget -->', '</section>', '</body>' ) # Get external service widget, no page parameter self.app.authorization = ('Basic', ('admin', 'admin')) response = self.app.get( '/external/service/%s/metrics' % service.id ) response.mustcontain( '<div id="wd_panel_metrics" class="panel panel-default alignak_webui_widget embedded">', '<!-- Service metrics widget -->', )
def test_host_new_host(self): """Create a new host edition form""" datamgr = DataManager(alignak_webui.app.app, session=self.session) # Get realm in the backend realm = datamgr.get_realm({'where': {'name': 'All'}}) # Get host template in the backend template = datamgr.get_host({'where': {'name': 'generic-host', '_is_template': True}}) print('Enable edition mode') response = self.app.post('/edition_mode', params={'state': 'on'}) session = response.request.environ['beaker.session'] # edition_mode is defined and activated in the session... assert 'edition_mode' in session assert True == session['edition_mode'] assert response.json == {'edition_mode': True, 'message': 'Edition mode enabled'} # Count hosts count = datamgr.count_objects('host') print("Host count: %s" % count) print('get page /host_form (edition mode) - for a new host') response = self.app.get('/host_form/unknown_host') response.mustcontain( '''<div id="form_host">''', '''<form role="form" data-element="None" class="element_form " method="post" action="/host_form/None">''', '''<h4>You are creating a new host.</h4>''', '''$('form[data-element="None"]').on("submit", function (evt) {''' ) # A name is enough to create a new host print('Host creation - missing name') data = { "_is_template": False, } response = self.app.post('/host_form/None', params=data) print(response.json) assert response.json == { '_is_template': False, '_message': 'host creation failed!', '_errors': [''] } # A name is enough to create a new host print('Host creation without template') data = { "_is_template": False, 'name': "New host", 'alias': "Friendly name" } response = self.app.post('/host_form/None', params=data) # Returns the new item _id new_host_id = response.json['_id'] resp = response.json resp.pop('_id') assert resp == { "_message": "New host created", "_realm": realm.id, "_is_template": False, "name": "New host", 'alias': "Friendly name" } # Count hosts (one more!) new_count = datamgr.count_objects('host') print("Host count: %s" % new_count) assert new_count == count + 1 # Get the new host in the backend host = datamgr.get_host({'where': {'name': 'New host'}}) assert host assert host.id == new_host_id assert host.name == "New host" assert host.alias == "Friendly name" print('Host creation with a template') data = { "_is_template": False, "_templates": [template.id], 'name': "New host 2", 'alias': "Friendly name 2" } response = self.app.post('/host_form/None', params=data) # Returns the new item _id new_host_id = response.json['_id'] resp = response.json resp.pop('_id') assert resp == { "_message": "New host created", "_realm": realm.id, "_is_template": False, "_templates": [template.id], "name": "New host 2", 'alias': "Friendly name 2" } # Count hosts (one more!) new_count = datamgr.count_objects('host') print("Host count: %s" % new_count) assert new_count == count + 2 # Get the new host in the backend host = datamgr.get_host({'where': {'name': 'New host 2'}}) assert host assert host.id == new_host_id assert host.name == "New host 2" assert host.alias == "Friendly name 2"
def test_host_new_host(self): """Create a new host edition form""" datamgr = DataManager(alignak_webui.app.app, session=self.session) # Get realm in the backend realm = datamgr.get_realm({'where': {'name': 'All'}}) # Get host template in the backend template = datamgr.get_host( {'where': { 'name': 'generic-host', '_is_template': True }}) print('Enable edition mode') response = self.app.post('/edition_mode', params={'state': 'on'}) session = response.request.environ['beaker.session'] # edition_mode is defined and activated in the session... assert 'edition_mode' in session assert True == session['edition_mode'] assert response.json == { 'edition_mode': True, 'message': 'Edition mode enabled' } # Count hosts count = datamgr.count_objects('host') print("Host count: %s" % count) print('get page /host_form (edition mode) - for a new host') response = self.app.get('/host_form/unknown_host') response.mustcontain( '''<div id="form_host">''', '''<form role="form" data-element="None" class="element_form " method="post" action="/host_form/None">''', '''<h4>You are creating a new host.</h4>''', '''$('form[data-element="None"]').on("submit", function (evt) {''') # A name is enough to create a new host print('Host creation - missing name') data = { "_is_template": False, } response = self.app.post('/host_form/None', params=data) print(response.json) assert response.json == { '_is_template': False, '_message': 'host creation failed!', '_errors': [''] } # A name is enough to create a new host print('Host creation without template') data = { "_is_template": False, 'name': "New host", 'alias': "Friendly name" } response = self.app.post('/host_form/None', params=data) # Returns the new item _id new_host_id = response.json['_id'] resp = response.json resp.pop('_id') assert resp == { "_message": "New host created", "_realm": realm.id, "_is_template": False, "name": "New host", 'alias': "Friendly name" } # Count hosts (one more!) new_count = datamgr.count_objects('host') print("Host count: %s" % new_count) assert new_count == count + 1 # Get the new host in the backend host = datamgr.get_host({'where': {'name': 'New host'}}) assert host assert host.id == new_host_id assert host.name == "New host" assert host.alias == "Friendly name" print('Host creation with a template') data = { "_is_template": False, "_templates": [template.id], 'name': "New host 2", 'alias': "Friendly name 2" } response = self.app.post('/host_form/None', params=data) # Returns the new item _id new_host_id = response.json['_id'] resp = response.json resp.pop('_id') assert resp == { "_message": "New host created", "_realm": realm.id, "_is_template": False, "_templates": [template.id], "name": "New host 2", 'alias': "Friendly name 2" } # Count hosts (one more!) new_count = datamgr.count_objects('host') print("Host count: %s" % new_count) assert new_count == count + 2 # Get the new host in the backend host = datamgr.get_host({'where': {'name': 'New host 2'}}) assert host assert host.id == new_host_id assert host.name == "New host 2" assert host.alias == "Friendly name 2"
class TestHosts(unittest2.TestCase): def setUp(self): print("setting up ...") self.dmg = DataManager(alignak_webui.app.app) print('Data manager', self.dmg) # Initialize and do not load assert self.dmg.user_login('admin', 'admin', load=False) def tearDown(self): print("tearing down ...") # Logout self.dmg.reset(logout=True) def test_hosts(self): """ Datamanager objects get - hosts """ print("Get all hosts") # Get main realm self.dmg.get_realm({'where': {'name': 'All'}}) # Get main TP self.dmg.get_timeperiod({'where': {'name': '24x7'}}) # Get all hosts hosts = self.dmg.get_hosts() assert 13 == len(hosts) print("---") for host in hosts: print("Got host: %s" % host) # Get all hosts (really all...) hosts = self.dmg.get_hosts(all_elements=True) assert 13 == len(hosts) print("---") for host in hosts: print("Got host: %s" % host) # Get all hosts (with all embedded relations) hosts = self.dmg.get_hosts(embedded=True) assert 13 == len(hosts) print("---") for host in hosts: print("Got host: %s" % host) # Get all hosts templates hosts = self.dmg.get_hosts(template=True) assert 28 == len(hosts) print("---") for host in hosts: print("Got host template: %s" % host) # Get one host hosts = self.dmg.get_hosts({'where': {'name': 'alignak_glpi'}}) assert 1 == len(hosts) print("---") for host in hosts: print("Got host: %s" % host) assert hosts[0].name == 'alignak_glpi' # Get one host host = self.dmg.get_host({'where': {'name': 'alignak_glpi'}}) assert host.name == 'alignak_glpi' # Get one host host = self.dmg.get_host(host._id) assert host.name == 'alignak_glpi' assert host.status == 'UNREACHABLE' # Get host services services = self.dmg.get_host_services({'where': {'name': 'unknown'}}) assert services == -1 services = self.dmg.get_host_services( {'where': { 'name': 'alignak_glpi' }}) print("---") service_name = '' for service in services: print("Got service: %s" % service) service_name = service['name'] assert len(services) > 1 services = self.dmg.get_host_services( {'where': { 'name': 'alignak_glpi' }}, search={'where': { 'name': service_name }}) services = self.dmg.get_host_services(host) assert len(services) > 1 # Get host overall state (state, status) = self.dmg.get_host_overall_state(host) print("Host overall state: %s %s" % (state, status)) assert state == 3 assert status == 'warning' def test_host(self): """ Datamanager objects get - host """ print("--- test Item") # Get main realm self.dmg.get_realm({'where': {'name': 'All'}}) # Get main TP self.dmg.get_timeperiod({'where': {'name': '24x7'}}) # Get host host = self.dmg.get_host({'where': {'name': 'localhost'}}) print(host.__dict__) print(host.check_period) assert isinstance(host.check_command, Command) assert host.check_command