diff --git a/.travis.yml b/.travis.yml index 3c84c095f..e5309d230 100644 --- a/.travis.yml +++ b/.travis.yml @@ -35,6 +35,7 @@ install: - pip install $DJANGO_VERSION - pip install simplejson - pip install paramiko + - pip install selenium - pip install https://github.com/pynag/pynag/zipball/master - python setup.py build - python setup.py install @@ -59,3 +60,8 @@ install: - sudo chmod -R 777 /var/lib/nagios3 notifications: email: false +addons: + sauce_connect: + username: tommi + access_key: + secure: "GZUO7uiH0Q5/oXAn3CiDjDo9/CeNTeRPI2z1uERAOX6aK0D6XnSpn+ot/KXYN90c/spPqyi4e6MdgfFxuZrt7IK7cMpxEPYYjbB9+vFbODaVvo6/1J3SxpcGJtn/bciEF4bTiEcmL4gfuc6+5FedocvFIpAmHuANOzfv1B26F0o=" diff --git a/adagios/objectbrowser/tests.py b/adagios/objectbrowser/tests.py index ba8b752fc..dc6a0f0e2 100644 --- a/adagios/objectbrowser/tests.py +++ b/adagios/objectbrowser/tests.py @@ -32,6 +32,14 @@ pynag.Model.cfg_file = adagios.settings.nagios_config +try: + from selenium.webdriver.common.by import By + from selenium.webdriver.support.ui import WebDriverWait + from selenium.webdriver.support import expected_conditions as EC + from selenium.common.exceptions import TimeoutException +except ImportError: + # selenium tests are skipped if selenium is not available + pass class TestObjectBrowser(unittest.TestCase): @@ -526,6 +534,44 @@ def test_set_prefix(self): field.set_prefix('') self.assertEqual('', field.get_prefix()) +class SeleniumObjectBrowserTestCase(adagios.utils.SeleniumTestCase): + def test_contacts_loading(self): + """Test if contacts under configure loads""" + self.driver.get(self.live_server_url + "/objectbrowser/#contact-tab_tab") + + wait = WebDriverWait(self.driver, 10) + + try: + # Get all host rows + contact_table_rows = wait.until( + EC.presence_of_all_elements_located(( + By.XPATH, + "//table[contains(@id, 'contact-table')]/tbody/tr")) + ) + except TimeoutException: + self.assertTrue(False, "Timed out waiting for contact table to load") + + self.assertTrue(len(contact_table_rows) > 0, + "No table rows in contact table") + + def test_hosts_loading(self): + """Test if hosts under configure loads""" + self.driver.get(self.live_server_url + "/objectbrowser") + + wait = WebDriverWait(self.driver, 10) + + try: + # Get all host rows + host_table_rows = wait.until( + EC.presence_of_all_elements_located(( + By.XPATH, + "//table[contains(@id, 'host-table')]/tbody/tr")) + ) + except TimeoutException: + self.assertTrue(False, "Timed out waiting for host table to load") + + self.assertTrue(len(host_table_rows) > 0, + "No table rows in host-table") _TEST_SERVICE = """ define service { diff --git a/adagios/settings.py b/adagios/settings.py index 943abc7ed..e67c8c662 100644 --- a/adagios/settings.py +++ b/adagios/settings.py @@ -34,6 +34,7 @@ ) MANAGERS = ADMINS +STATIC_URL = "/media/" DATABASES = { 'default': { @@ -306,3 +307,6 @@ def get_random_string(length, stringset=string.ascii_letters + string.digits + s 'theme': THEME_DEFAULT, 'refresh_rate': refresh_rate } + +# Allow tests to run server on multiple ports +os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = 'localhost:8000-9000' diff --git a/adagios/status/tests.py b/adagios/status/tests.py index 7239fa042..aff4f5eab 100644 --- a/adagios/status/tests.py +++ b/adagios/status/tests.py @@ -24,6 +24,7 @@ import pynag.Parsers import os from django.test.client import RequestFactory +from django.test import LiveServerTestCase import adagios.status import adagios.status.utils import adagios.status.graphite @@ -31,6 +32,11 @@ import adagios.utils import simplejson as json +try: + from selenium.webdriver.common.by import By +except ImportError: + pass + class LiveStatusTestCase(unittest.TestCase): @@ -103,7 +109,7 @@ def test_status_detail(self): tmp = self.loadPage('/status/detail?contactgroup_name=admins') self.assertTrue('nagiosadmin' in tmp.content) - + def testStateHistory(self): request = self.factory.get('/status/state_history') adagios.status.views.state_history(request) @@ -171,3 +177,54 @@ def test_get(self): self.assertTrue(len(result) == 1) self.assertTrue('rta' in result[0]['metrics']) self.assertTrue('packetloss' in result[0]['metrics']) + + +class SeleniumStatusTestCase(adagios.utils.SeleniumTestCase): + def test_network_parents(self): + """Status Overview, Network Parents should show an integer""" + self.driver.get(self.live_server_url + "/status") + + # Second link is Network Parents in overview + self.assertEqual(self.driver.find_elements(By.XPATH, + "//a[@href='/status/parents']")[1].text.isdigit(), True) + + def test_services_select_all(self): + """Loads services list and tries to select everything + + Flow: + Load http:///status/services + Click select all + Look for statustable rows + Assert that all rows are checked""" + + self.driver.get(self.live_server_url + "/status/services") + + self.driver.find_element_by_xpath("//input[@class='select_many']").click() + self.driver.find_element_by_xpath("//a[@class='select_all']").click() + + # Get all statustable rows + status_table_rows = self.driver.find_element_by_xpath( + "//table[contains(@class, 'statustable')]" + ).find_elements(By.XPATH, "//tbody/tr[contains(@class, 'mainrow')]") + + # Sub-select non-selected + for row in status_table_rows: + self.assertTrue('row_selected' in row.get_attribute('class'), + "Non selected row found after selecting all: " + \ + row.text) + + def test_status_overview_top_alert_producers(self): + """Check the top alert producers part of status overview""" + + self.driver.get(self.live_server_url + "/status") + + top_alert_table_rows = self.driver.find_elements(By.XPATH, + "//table[@id='top_alert_producers']/tbody/tr" + ) + + count = 0 + for row in top_alert_table_rows: + if 'display' not in row.get_attribute('style'): + count += 1 + + self.assertTrue(count <= 3, "Top alert producers returns too many rows") diff --git a/adagios/utils.py b/adagios/utils.py index 3f361008b..bd13d1953 100644 --- a/adagios/utils.py +++ b/adagios/utils.py @@ -26,9 +26,13 @@ import adagios.settings import os import pynag.Utils.misc +from django.test import LiveServerTestCase +from django.utils import unittest +import atexit from django.utils.translation import ugettext as _ +SELENIUM_DRIVER = None def wait(object_type, WaitObject, WaitCondition, WaitTrigger, **kwargs): livestatus = adagios.status.utils.livestatus(None) @@ -170,3 +174,54 @@ def terminate(self): self.restore_adagios_global_variables() super(FakeAdagiosEnvironment, self).terminate() + +class SeleniumTestCase(LiveServerTestCase): + driver = None + environment = None + + @classmethod + def setUpClass(cls): + global SELENIUM_DRIVER + try: + from selenium import webdriver + except ImportError: + raise unittest.SkipTest("No selenium installed") + + super(SeleniumTestCase, cls).setUpClass() + + cls.nagios_config = adagios.settings.nagios_config + cls.environment = adagios.utils.FakeAdagiosEnvironment() + cls.environment.create_minimal_environment() + cls.environment.configure_livestatus() + cls.environment.update_adagios_global_variables() + cls.environment.start() + cls.livestatus = cls.environment.get_livestatus() + + if not SELENIUM_DRIVER: + if 'TRAVIS' in os.environ: + capabilities = webdriver.DesiredCapabilities.CHROME + capabilities["build"] = os.environ["TRAVIS_BUILD_NUMBER"] + capabilities["tags"] = [os.environ["TRAVIS_PYTHON_VERSION"], "CI"] + capabilities["tunnel-identifier"] = os.environ["TRAVIS_JOB_NUMBER"] + capabilities['platform'] = "Windows 8.1" + capabilities['version'] = "31" + + username = os.environ["SAUCE_USERNAME"] + access_key = os.environ["SAUCE_ACCESS_KEY"] + + hub_url = "%s:%s@ondemand.saucelabs.com/wd/hub" % (username, access_key) + SELENIUM_DRIVER = webdriver.Remote(desired_capabilities=capabilities, command_executor="http://%s" % hub_url) + else: + SELENIUM_DRIVER = webdriver.Firefox() + # Exit browser when all tests are done + atexit.register(SELENIUM_DRIVER.quit) + + + + cls.driver = SELENIUM_DRIVER + + @classmethod + def tearDownClass(cls): + cls.environment.terminate() + super(SeleniumTestCase, cls).tearDownClass() +