From 7a4ea704a82ca62a0cffe37d6e25d39336818ff7 Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Tue, 30 Sep 2014 15:54:49 +0000 Subject: [PATCH 01/18] STATIC_URL needed for LiveServerTestCase django.test.LiveServerTestCase errors out if STATIC_URL is not defined in settings. --- adagios/settings.py | 1 + 1 file changed, 1 insertion(+) diff --git a/adagios/settings.py b/adagios/settings.py index 46d1736aa..d71fb07ac 100644 --- a/adagios/settings.py +++ b/adagios/settings.py @@ -34,6 +34,7 @@ ) MANAGERS = ADMINS +STATIC_URL = "/media/" DATABASES = { 'default': { From 8f3f229df943f9a811f00a25e6067251c2304d4a Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Tue, 30 Sep 2014 15:55:26 +0000 Subject: [PATCH 02/18] Splinter based browser testing added Adds tests using selenium and actual browsers using webdriver. TEST_SPLINTER environment variable enables this type of testing. Test cases added: /status - check that Status Overview, Network Parents returns a number. /status/services - clicks multi select box and selects all services. Confirms that all services are selected. --- adagios/status/tests.py | 76 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/adagios/status/tests.py b/adagios/status/tests.py index 9feb31efc..fd5e8bd50 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 @@ -102,7 +103,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) @@ -158,3 +159,76 @@ def test_get(self): self.assertTrue(len(result) == 1) self.assertTrue('rta' in result[0]['metrics']) self.assertTrue('packetloss' in result[0]['metrics']) + + +class SplinterTestCase(LiveServerTestCase): + browser = None + environment = None + + + @classmethod + def setUpClass(cls): + super(SplinterTestCase, cls).setUpClass() + + if 'TEST_SPLINTER' not in os.environ: + cls.enable = False + return + import splinter + cls.enable = True + 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() + + cls.factory = RequestFactory() + + cls.browser = splinter.Browser() + + @classmethod + def tearDownClass(cls): + super(SplinterTestCase, cls).tearDownClass() + if cls.enable: + cls.browser.quit() + cls.environment.terminate() + + + def test_network_parents(self): + """Status Overview, Network Parents should show an integer""" + if not self.enable: + return + self.browser.visit(self.live_server_url + "/status") + + # Second link is Network Parents in overview + self.assertEqual(self.browser.find_link_by_href( + "/status/parents")[1].html.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""" + + if not self.enable: + return + + self.browser.visit(self.live_server_url + "/status/services") + + self.browser.find_by_xpath("//input[@class='select_many']").first.click() + self.browser.find_by_xpath("//a[@class='select_all']").first.click() + + # Get all statustable rows + status_table_rows = self.browser.find_by_xpath( + "//table[contains(@class, 'statustable')]" + ).first.find_by_xpath("//tbody/tr[contains(@class, 'mainrow')]") + + # Sub-select non-selected + for row in status_table_rows: + self.assertTrue(row.has_class("row_selected"), + "Non selected row found after selecting all: " + \ + row.text) From 3e0de0ff832d15255081ee4a092924ea0d696211 Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Tue, 30 Sep 2014 23:22:43 +0000 Subject: [PATCH 03/18] Support for arguments to splinter --- adagios/status/tests.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/adagios/status/tests.py b/adagios/status/tests.py index fd5e8bd50..7e9bb764c 100644 --- a/adagios/status/tests.py +++ b/adagios/status/tests.py @@ -183,9 +183,17 @@ def setUpClass(cls): cls.environment.start() cls.livestatus = cls.environment.get_livestatus() - cls.factory = RequestFactory() + splinter_args = {} + for key, value in os.environ.iteritems(): + if key.startswith("TEST_SPLINTER_") is False: + continue + key = key.replace("TEST_SPLINTER_", "").lower() + splinter_args[key] = value + + if 'url' in splinter_args: + splinter_args['driver_name'] = 'remote' - cls.browser = splinter.Browser() + cls.browser = splinter.Browser(**splinter_args) @classmethod def tearDownClass(cls): From ba23c608e99eb0b418638580c7a21967803a2bab Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Wed, 1 Oct 2014 16:06:14 +0000 Subject: [PATCH 04/18] est for too many top_alert_producers --- adagios/status/tests.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/adagios/status/tests.py b/adagios/status/tests.py index 7e9bb764c..32defe0b9 100644 --- a/adagios/status/tests.py +++ b/adagios/status/tests.py @@ -240,3 +240,21 @@ def test_services_select_all(self): self.assertTrue(row.has_class("row_selected"), "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""" + if not self.enable: + return + + self.browser.visit(self.live_server_url + "/status") + + top_alert_table_rows = self.browser.find_by_xpath( + "//table[@id='top_alert_producers']/tbody/tr" + ) + + count = 0 + for row in top_alert_table_rows: + if 'display' not in row['style']: + count += 1 + + self.assertTrue(count <= 3, "Top alert producers returns too many rows") From 026ed3c3b697185e88fec8e20daa71ea514a0019 Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Tue, 21 Oct 2014 10:40:14 +0000 Subject: [PATCH 05/18] Move away from splinter to selenium --- adagios/status/tests.py | 57 +++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/adagios/status/tests.py b/adagios/status/tests.py index 5f4eaab73..243bc1698 100644 --- a/adagios/status/tests.py +++ b/adagios/status/tests.py @@ -32,6 +32,13 @@ import adagios.utils import simplejson as json +try: + from selenium import webdriver + from selenium.webdriver.common.by import By + enable_selenium = True +except ImportError: + enable_selenium = False + class LiveStatusTestCase(unittest.TestCase): @@ -174,19 +181,19 @@ def test_get(self): self.assertTrue('packetloss' in result[0]['metrics']) -class SplinterTestCase(LiveServerTestCase): - browser = None +class SeleniumTestCase(LiveServerTestCase): + driver = None environment = None @classmethod def setUpClass(cls): - super(SplinterTestCase, cls).setUpClass() + super(SeleniumTestCase, cls).setUpClass() - if 'TEST_SPLINTER' not in os.environ: + if enable_selenium is False: cls.enable = False return - import splinter + cls.enable = True cls.nagios_config = adagios.settings.nagios_config cls.environment = adagios.utils.FakeAdagiosEnvironment() @@ -196,23 +203,13 @@ def setUpClass(cls): cls.environment.start() cls.livestatus = cls.environment.get_livestatus() - splinter_args = {} - for key, value in os.environ.iteritems(): - if key.startswith("TEST_SPLINTER_") is False: - continue - key = key.replace("TEST_SPLINTER_", "").lower() - splinter_args[key] = value - - if 'url' in splinter_args: - splinter_args['driver_name'] = 'remote' - - cls.browser = splinter.Browser(**splinter_args) + cls.driver = webdriver.Firefox() @classmethod def tearDownClass(cls): - super(SplinterTestCase, cls).tearDownClass() + super(SeleniumTestCase, cls).tearDownClass() if cls.enable: - cls.browser.quit() + cls.driver.close() cls.environment.terminate() @@ -220,11 +217,11 @@ def test_network_parents(self): """Status Overview, Network Parents should show an integer""" if not self.enable: return - self.browser.visit(self.live_server_url + "/status") + self.driver.get(self.live_server_url + "/status") # Second link is Network Parents in overview - self.assertEqual(self.browser.find_link_by_href( - "/status/parents")[1].html.isdigit(), True) + 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 @@ -238,19 +235,19 @@ def test_services_select_all(self): if not self.enable: return - self.browser.visit(self.live_server_url + "/status/services") + self.driver.get(self.live_server_url + "/status/services") - self.browser.find_by_xpath("//input[@class='select_many']").first.click() - self.browser.find_by_xpath("//a[@class='select_all']").first.click() + 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.browser.find_by_xpath( + status_table_rows = self.driver.find_element_by_xpath( "//table[contains(@class, 'statustable')]" - ).first.find_by_xpath("//tbody/tr[contains(@class, 'mainrow')]") + ).find_elements(By.XPATH, "//tbody/tr[contains(@class, 'mainrow')]") # Sub-select non-selected for row in status_table_rows: - self.assertTrue(row.has_class("row_selected"), + self.assertTrue('row_selected' in row.get_attribute('class'), "Non selected row found after selecting all: " + \ row.text) @@ -259,15 +256,15 @@ def test_status_overview_top_alert_producers(self): if not self.enable: return - self.browser.visit(self.live_server_url + "/status") + self.driver.get(self.live_server_url + "/status") - top_alert_table_rows = self.browser.find_by_xpath( + 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['style']: + if 'display' not in row.get_attribute('style'): count += 1 self.assertTrue(count <= 3, "Top alert producers returns too many rows") From a1a7f175eae63a9aed381b8d2fa9f09bcb0c7ddf Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Tue, 21 Oct 2014 10:45:46 +0000 Subject: [PATCH 06/18] Skip selenium tests properly when not installed --- adagios/status/tests.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/adagios/status/tests.py b/adagios/status/tests.py index 243bc1698..8be6feb0f 100644 --- a/adagios/status/tests.py +++ b/adagios/status/tests.py @@ -191,10 +191,8 @@ def setUpClass(cls): super(SeleniumTestCase, cls).setUpClass() if enable_selenium is False: - cls.enable = False return - cls.enable = True cls.nagios_config = adagios.settings.nagios_config cls.environment = adagios.utils.FakeAdagiosEnvironment() cls.environment.create_minimal_environment() @@ -208,21 +206,21 @@ def setUpClass(cls): @classmethod def tearDownClass(cls): super(SeleniumTestCase, cls).tearDownClass() - if cls.enable: + if enable_selenium: cls.driver.close() cls.environment.terminate() + @unittest.skipUnless(enable_selenium, 'Requires selenium') def test_network_parents(self): """Status Overview, Network Parents should show an integer""" - if not self.enable: - return 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) + @unittest.skipUnless(enable_selenium, 'Requires selenium') def test_services_select_all(self): """Loads services list and tries to select everything @@ -232,9 +230,6 @@ def test_services_select_all(self): Look for statustable rows Assert that all rows are checked""" - if not self.enable: - return - self.driver.get(self.live_server_url + "/status/services") self.driver.find_element_by_xpath("//input[@class='select_many']").click() @@ -251,10 +246,9 @@ def test_services_select_all(self): "Non selected row found after selecting all: " + \ row.text) + @unittest.skipUnless(enable_selenium, 'Requires selenium') def test_status_overview_top_alert_producers(self): """Check the top alert producers part of status overview""" - if not self.enable: - return self.driver.get(self.live_server_url + "/status") From dda19a54f0b64d30e0f294000bc61253667a22c2 Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Mon, 27 Oct 2014 21:35:57 +0000 Subject: [PATCH 07/18] Move SeleniumTestCase to utils.py, re-usability SeleniumTestCase can be used by many components withing adagios and should not live within status/ --- adagios/status/tests.py | 47 ++++------------------------------------- adagios/utils.py | 37 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 43 deletions(-) diff --git a/adagios/status/tests.py b/adagios/status/tests.py index 8be6feb0f..bee1efade 100644 --- a/adagios/status/tests.py +++ b/adagios/status/tests.py @@ -32,13 +32,6 @@ import adagios.utils import simplejson as json -try: - from selenium import webdriver - from selenium.webdriver.common.by import By - enable_selenium = True -except ImportError: - enable_selenium = False - class LiveStatusTestCase(unittest.TestCase): @@ -181,46 +174,15 @@ def test_get(self): self.assertTrue('packetloss' in result[0]['metrics']) -class SeleniumTestCase(LiveServerTestCase): - driver = None - environment = None - - - @classmethod - def setUpClass(cls): - super(SeleniumTestCase, cls).setUpClass() - - if enable_selenium is False: - return - - 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() - - cls.driver = webdriver.Firefox() - - @classmethod - def tearDownClass(cls): - super(SeleniumTestCase, cls).tearDownClass() - if enable_selenium: - cls.driver.close() - cls.environment.terminate() - - - @unittest.skipUnless(enable_selenium, 'Requires selenium') +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, + self.assertEqual(self.driver.find_elements(self.By.XPATH, "//a[@href='/status/parents']")[1].text.isdigit(), True) - @unittest.skipUnless(enable_selenium, 'Requires selenium') def test_services_select_all(self): """Loads services list and tries to select everything @@ -238,7 +200,7 @@ def test_services_select_all(self): # 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')]") + ).find_elements(self.By.XPATH, "//tbody/tr[contains(@class, 'mainrow')]") # Sub-select non-selected for row in status_table_rows: @@ -246,13 +208,12 @@ def test_services_select_all(self): "Non selected row found after selecting all: " + \ row.text) - @unittest.skipUnless(enable_selenium, 'Requires selenium') 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, + top_alert_table_rows = self.driver.find_elements(self.By.XPATH, "//table[@id='top_alert_producers']/tbody/tr" ) diff --git a/adagios/utils.py b/adagios/utils.py index 3f361008b..4917710d2 100644 --- a/adagios/utils.py +++ b/adagios/utils.py @@ -26,6 +26,8 @@ import adagios.settings import os import pynag.Utils.misc +from django.test import LiveServerTestCase +from django.utils import unittest from django.utils.translation import ugettext as _ @@ -170,3 +172,38 @@ def terminate(self): self.restore_adagios_global_variables() super(FakeAdagiosEnvironment, self).terminate() + +class SeleniumTestCase(LiveServerTestCase): + driver = None + environment = None + + + + @classmethod + def setUpClass(cls): + super(SeleniumTestCase, cls).setUpClass() + + try: + from selenium import webdriver + from selenium.webdriver.common.by import By + + except ImportError: + raise unittest.SkipTest("No selenium installed") + + 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() + cls.By = By + + cls.driver = webdriver.Firefox() + + @classmethod + def tearDownClass(cls): + super(SeleniumTestCase, cls).tearDownClass() + cls.driver.close() + cls.environment.terminate() + From e7ad2df638ac52930825a97e2516a33707c4cc50 Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Mon, 27 Oct 2014 23:28:52 +0000 Subject: [PATCH 08/18] Only run LiveServerTestCase if selenium is there --- adagios/utils.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/adagios/utils.py b/adagios/utils.py index 4917710d2..51480dea0 100644 --- a/adagios/utils.py +++ b/adagios/utils.py @@ -181,8 +181,6 @@ class SeleniumTestCase(LiveServerTestCase): @classmethod def setUpClass(cls): - super(SeleniumTestCase, cls).setUpClass() - try: from selenium import webdriver from selenium.webdriver.common.by import By @@ -190,6 +188,8 @@ def setUpClass(cls): 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() @@ -197,13 +197,12 @@ def setUpClass(cls): cls.environment.update_adagios_global_variables() cls.environment.start() cls.livestatus = cls.environment.get_livestatus() - cls.By = By cls.driver = webdriver.Firefox() @classmethod def tearDownClass(cls): super(SeleniumTestCase, cls).tearDownClass() - cls.driver.close() cls.environment.terminate() + cls.driver.close() From b0faf9a21746000d33b0f6f564ba0aa7cb99996e Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Mon, 27 Oct 2014 23:29:40 +0000 Subject: [PATCH 09/18] Add selenium tests for objectbrowser --- adagios/objectbrowser/tests.py | 46 ++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/adagios/objectbrowser/tests.py b/adagios/objectbrowser/tests.py index ba8b752fc..fa7063642 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) > 1, + "No table rows in objecttable") _TEST_SERVICE = """ define service { From 03f495723d015db7a7b0193bae733dc6ee2cc25c Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Tue, 28 Oct 2014 00:06:35 +0000 Subject: [PATCH 10/18] Add missing import of By --- adagios/status/tests.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/adagios/status/tests.py b/adagios/status/tests.py index bee1efade..aff4f5eab 100644 --- a/adagios/status/tests.py +++ b/adagios/status/tests.py @@ -32,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): @@ -180,7 +185,7 @@ def test_network_parents(self): self.driver.get(self.live_server_url + "/status") # Second link is Network Parents in overview - self.assertEqual(self.driver.find_elements(self.By.XPATH, + self.assertEqual(self.driver.find_elements(By.XPATH, "//a[@href='/status/parents']")[1].text.isdigit(), True) def test_services_select_all(self): @@ -200,7 +205,7 @@ def test_services_select_all(self): # Get all statustable rows status_table_rows = self.driver.find_element_by_xpath( "//table[contains(@class, 'statustable')]" - ).find_elements(self.By.XPATH, "//tbody/tr[contains(@class, 'mainrow')]") + ).find_elements(By.XPATH, "//tbody/tr[contains(@class, 'mainrow')]") # Sub-select non-selected for row in status_table_rows: @@ -213,7 +218,7 @@ def test_status_overview_top_alert_producers(self): self.driver.get(self.live_server_url + "/status") - top_alert_table_rows = self.driver.find_elements(self.By.XPATH, + top_alert_table_rows = self.driver.find_elements(By.XPATH, "//table[@id='top_alert_producers']/tbody/tr" ) From 57f1c1547c5a70da381921afe0212beb71716b98 Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Tue, 28 Oct 2014 00:06:56 +0000 Subject: [PATCH 11/18] Allow testserver to run on multiple ports --- adagios/settings.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/adagios/settings.py b/adagios/settings.py index 260404532..e67c8c662 100644 --- a/adagios/settings.py +++ b/adagios/settings.py @@ -307,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' From 6fac22afecae3e0b882ce7593ea3b80f71535584 Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Tue, 28 Oct 2014 00:07:15 +0000 Subject: [PATCH 12/18] Use same selenium session across unit test files --- adagios/utils.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/adagios/utils.py b/adagios/utils.py index 51480dea0..3e6772c84 100644 --- a/adagios/utils.py +++ b/adagios/utils.py @@ -31,6 +31,7 @@ from django.utils.translation import ugettext as _ +SELENIUM_DRIVER = None def wait(object_type, WaitObject, WaitCondition, WaitTrigger, **kwargs): livestatus = adagios.status.utils.livestatus(None) @@ -177,14 +178,11 @@ class SeleniumTestCase(LiveServerTestCase): driver = None environment = None - - @classmethod def setUpClass(cls): + global SELENIUM_DRIVER try: from selenium import webdriver - from selenium.webdriver.common.by import By - except ImportError: raise unittest.SkipTest("No selenium installed") @@ -198,11 +196,17 @@ def setUpClass(cls): cls.environment.start() cls.livestatus = cls.environment.get_livestatus() - cls.driver = webdriver.Firefox() + if not SELENIUM_DRIVER: + SELENIUM_DRIVER = webdriver.Firefox() + + cls.driver = SELENIUM_DRIVER @classmethod def tearDownClass(cls): - super(SeleniumTestCase, cls).tearDownClass() + # Exit browser when all tests are done + import atexit + atexit.register(cls.driver.close) + cls.environment.terminate() - cls.driver.close() + super(SeleniumTestCase, cls).tearDownClass() From 07f174a1e561acce48edec6edd8a2079d6cbd656 Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Tue, 28 Oct 2014 00:38:16 +0000 Subject: [PATCH 13/18] Test hooking up travis-ci with saucelabs --- .travis.yml | 6 ++++++ adagios/utils.py | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) 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/utils.py b/adagios/utils.py index 3e6772c84..09f8e8ceb 100644 --- a/adagios/utils.py +++ b/adagios/utils.py @@ -197,7 +197,20 @@ def setUpClass(cls): cls.livestatus = cls.environment.get_livestatus() if not SELENIUM_DRIVER: - SELENIUM_DRIVER = webdriver.Firefox() + if 'TRAVIS' in os.environ: + capabilities = {} + capabilities["build"] = os.environ["TRAVIS_BUILD_NUMBER"] + capabilities["tags"] = [os.environ["TRAVIS_PYTHON_VERSION"], "CI"] + capabilities["tunnel-identifier"] = os.environ["TRAVIS_JOB_NUMBER"] + + username = os.environ["SAUCE_USERNAME"] + access_key = os.environ["SAUCE_ACCESS_KEY"] + + hub_url = "%s:%s@localhost:4445" % (username, access_key) + SELENIUM_DRIVER = webdriver.Remote(desired_capabilities=capabilities, command_executor="http://%s/wd/hub" % hub_url) + else: + SELENIUM_DRIVER = webdriver.Firefox() + cls.driver = SELENIUM_DRIVER From 7840a0d6258e1b7820986eb0d95db00746144367 Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Tue, 28 Oct 2014 01:10:22 +0000 Subject: [PATCH 14/18] More work on travis saucelabs integration --- adagios/utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/adagios/utils.py b/adagios/utils.py index 09f8e8ceb..7e01d6653 100644 --- a/adagios/utils.py +++ b/adagios/utils.py @@ -198,10 +198,12 @@ def setUpClass(cls): if not SELENIUM_DRIVER: if 'TRAVIS' in os.environ: - capabilities = {} + 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"] From 849e4e3c8637707fc75a96984dad6e14cd64853c Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Tue, 28 Oct 2014 01:23:53 +0000 Subject: [PATCH 15/18] Set hub_url to saucelabs --- adagios/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adagios/utils.py b/adagios/utils.py index 7e01d6653..7b39436d7 100644 --- a/adagios/utils.py +++ b/adagios/utils.py @@ -208,8 +208,8 @@ def setUpClass(cls): username = os.environ["SAUCE_USERNAME"] access_key = os.environ["SAUCE_ACCESS_KEY"] - hub_url = "%s:%s@localhost:4445" % (username, access_key) - SELENIUM_DRIVER = webdriver.Remote(desired_capabilities=capabilities, command_executor="http://%s/wd/hub" % hub_url) + 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() From ea3a855f03930e3c2cac72436be47d42e1cd5af0 Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Tue, 28 Oct 2014 01:34:58 +0000 Subject: [PATCH 16/18] Make driver only exit once --- adagios/utils.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/adagios/utils.py b/adagios/utils.py index 7b39436d7..47f26a821 100644 --- a/adagios/utils.py +++ b/adagios/utils.py @@ -28,6 +28,7 @@ import pynag.Utils.misc from django.test import LiveServerTestCase from django.utils import unittest +import atexit from django.utils.translation import ugettext as _ @@ -212,16 +213,15 @@ def setUpClass(cls): 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.close) + cls.driver = SELENIUM_DRIVER @classmethod def tearDownClass(cls): - # Exit browser when all tests are done - import atexit - atexit.register(cls.driver.close) - cls.environment.terminate() super(SeleniumTestCase, cls).tearDownClass() From 0db0d208bf24553204a497f2c0b14fe215306e38 Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Tue, 28 Oct 2014 01:35:12 +0000 Subject: [PATCH 17/18] Look for more than zero, failed on 1 host --- adagios/objectbrowser/tests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adagios/objectbrowser/tests.py b/adagios/objectbrowser/tests.py index fa7063642..dc6a0f0e2 100644 --- a/adagios/objectbrowser/tests.py +++ b/adagios/objectbrowser/tests.py @@ -570,8 +570,8 @@ def test_hosts_loading(self): except TimeoutException: self.assertTrue(False, "Timed out waiting for host table to load") - self.assertTrue(len(host_table_rows) > 1, - "No table rows in objecttable") + self.assertTrue(len(host_table_rows) > 0, + "No table rows in host-table") _TEST_SERVICE = """ define service { From 82f6b8299daad1defdc23dad7183d8380491094f Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Tue, 28 Oct 2014 01:38:08 +0000 Subject: [PATCH 18/18] Call quit instead of close This completely closes webdriver --- adagios/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adagios/utils.py b/adagios/utils.py index 47f26a821..bd13d1953 100644 --- a/adagios/utils.py +++ b/adagios/utils.py @@ -214,7 +214,7 @@ def setUpClass(cls): else: SELENIUM_DRIVER = webdriver.Firefox() # Exit browser when all tests are done - atexit.register(SELENIUM_DRIVER.close) + atexit.register(SELENIUM_DRIVER.quit)