Skip to content

Commit 67c7014

Browse files
committed
Merge pull request #441 from opinkerfi/selenium_tests
Selenium tests
2 parents ac91cd0 + 82f6b82 commit 67c7014

File tree

5 files changed

+169
-1
lines changed

5 files changed

+169
-1
lines changed

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ install:
3535
- pip install $DJANGO_VERSION
3636
- pip install simplejson
3737
- pip install paramiko
38+
- pip install selenium
3839
- pip install https://github.com/pynag/pynag/zipball/master
3940
- python setup.py build
4041
- python setup.py install
@@ -59,3 +60,8 @@ install:
5960
- sudo chmod -R 777 /var/lib/nagios3
6061
notifications:
6162
email: false
63+
addons:
64+
sauce_connect:
65+
username: tommi
66+
access_key:
67+
secure: "GZUO7uiH0Q5/oXAn3CiDjDo9/CeNTeRPI2z1uERAOX6aK0D6XnSpn+ot/KXYN90c/spPqyi4e6MdgfFxuZrt7IK7cMpxEPYYjbB9+vFbODaVvo6/1J3SxpcGJtn/bciEF4bTiEcmL4gfuc6+5FedocvFIpAmHuANOzfv1B26F0o="

adagios/objectbrowser/tests.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232

3333
pynag.Model.cfg_file = adagios.settings.nagios_config
3434

35+
try:
36+
from selenium.webdriver.common.by import By
37+
from selenium.webdriver.support.ui import WebDriverWait
38+
from selenium.webdriver.support import expected_conditions as EC
39+
from selenium.common.exceptions import TimeoutException
40+
except ImportError:
41+
# selenium tests are skipped if selenium is not available
42+
pass
3543

3644
class TestObjectBrowser(unittest.TestCase):
3745

@@ -526,6 +534,44 @@ def test_set_prefix(self):
526534
field.set_prefix('')
527535
self.assertEqual('', field.get_prefix())
528536

537+
class SeleniumObjectBrowserTestCase(adagios.utils.SeleniumTestCase):
538+
def test_contacts_loading(self):
539+
"""Test if contacts under configure loads"""
540+
self.driver.get(self.live_server_url + "/objectbrowser/#contact-tab_tab")
541+
542+
wait = WebDriverWait(self.driver, 10)
543+
544+
try:
545+
# Get all host rows
546+
contact_table_rows = wait.until(
547+
EC.presence_of_all_elements_located((
548+
By.XPATH,
549+
"//table[contains(@id, 'contact-table')]/tbody/tr"))
550+
)
551+
except TimeoutException:
552+
self.assertTrue(False, "Timed out waiting for contact table to load")
553+
554+
self.assertTrue(len(contact_table_rows) > 0,
555+
"No table rows in contact table")
556+
557+
def test_hosts_loading(self):
558+
"""Test if hosts under configure loads"""
559+
self.driver.get(self.live_server_url + "/objectbrowser")
560+
561+
wait = WebDriverWait(self.driver, 10)
562+
563+
try:
564+
# Get all host rows
565+
host_table_rows = wait.until(
566+
EC.presence_of_all_elements_located((
567+
By.XPATH,
568+
"//table[contains(@id, 'host-table')]/tbody/tr"))
569+
)
570+
except TimeoutException:
571+
self.assertTrue(False, "Timed out waiting for host table to load")
572+
573+
self.assertTrue(len(host_table_rows) > 0,
574+
"No table rows in host-table")
529575

530576
_TEST_SERVICE = """
531577
define service {

adagios/settings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
)
3535

3636
MANAGERS = ADMINS
37+
STATIC_URL = "/media/"
3738

3839
DATABASES = {
3940
'default': {
@@ -306,3 +307,6 @@ def get_random_string(length, stringset=string.ascii_letters + string.digits + s
306307
'theme': THEME_DEFAULT,
307308
'refresh_rate': refresh_rate
308309
}
310+
311+
# Allow tests to run server on multiple ports
312+
os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = 'localhost:8000-9000'

adagios/status/tests.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,19 @@
2424
import pynag.Parsers
2525
import os
2626
from django.test.client import RequestFactory
27+
from django.test import LiveServerTestCase
2728
import adagios.status
2829
import adagios.status.utils
2930
import adagios.status.graphite
3031
import adagios.settings
3132
import adagios.utils
3233
import simplejson as json
3334

35+
try:
36+
from selenium.webdriver.common.by import By
37+
except ImportError:
38+
pass
39+
3440

3541
class LiveStatusTestCase(unittest.TestCase):
3642

@@ -103,7 +109,7 @@ def test_status_detail(self):
103109
tmp = self.loadPage('/status/detail?contactgroup_name=admins')
104110
self.assertTrue('nagiosadmin' in tmp.content)
105111

106-
112+
107113
def testStateHistory(self):
108114
request = self.factory.get('/status/state_history')
109115
adagios.status.views.state_history(request)
@@ -171,3 +177,54 @@ def test_get(self):
171177
self.assertTrue(len(result) == 1)
172178
self.assertTrue('rta' in result[0]['metrics'])
173179
self.assertTrue('packetloss' in result[0]['metrics'])
180+
181+
182+
class SeleniumStatusTestCase(adagios.utils.SeleniumTestCase):
183+
def test_network_parents(self):
184+
"""Status Overview, Network Parents should show an integer"""
185+
self.driver.get(self.live_server_url + "/status")
186+
187+
# Second link is Network Parents in overview
188+
self.assertEqual(self.driver.find_elements(By.XPATH,
189+
"//a[@href='/status/parents']")[1].text.isdigit(), True)
190+
191+
def test_services_select_all(self):
192+
"""Loads services list and tries to select everything
193+
194+
Flow:
195+
Load http://<url>/status/services
196+
Click select all
197+
Look for statustable rows
198+
Assert that all rows are checked"""
199+
200+
self.driver.get(self.live_server_url + "/status/services")
201+
202+
self.driver.find_element_by_xpath("//input[@class='select_many']").click()
203+
self.driver.find_element_by_xpath("//a[@class='select_all']").click()
204+
205+
# Get all statustable rows
206+
status_table_rows = self.driver.find_element_by_xpath(
207+
"//table[contains(@class, 'statustable')]"
208+
).find_elements(By.XPATH, "//tbody/tr[contains(@class, 'mainrow')]")
209+
210+
# Sub-select non-selected
211+
for row in status_table_rows:
212+
self.assertTrue('row_selected' in row.get_attribute('class'),
213+
"Non selected row found after selecting all: " + \
214+
row.text)
215+
216+
def test_status_overview_top_alert_producers(self):
217+
"""Check the top alert producers part of status overview"""
218+
219+
self.driver.get(self.live_server_url + "/status")
220+
221+
top_alert_table_rows = self.driver.find_elements(By.XPATH,
222+
"//table[@id='top_alert_producers']/tbody/tr"
223+
)
224+
225+
count = 0
226+
for row in top_alert_table_rows:
227+
if 'display' not in row.get_attribute('style'):
228+
count += 1
229+
230+
self.assertTrue(count <= 3, "Top alert producers returns too many rows")

adagios/utils.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@
2626
import adagios.settings
2727
import os
2828
import pynag.Utils.misc
29+
from django.test import LiveServerTestCase
30+
from django.utils import unittest
31+
import atexit
2932

3033
from django.utils.translation import ugettext as _
3134

35+
SELENIUM_DRIVER = None
3236

3337
def wait(object_type, WaitObject, WaitCondition, WaitTrigger, **kwargs):
3438
livestatus = adagios.status.utils.livestatus(None)
@@ -170,3 +174,54 @@ def terminate(self):
170174
self.restore_adagios_global_variables()
171175
super(FakeAdagiosEnvironment, self).terminate()
172176

177+
178+
class SeleniumTestCase(LiveServerTestCase):
179+
driver = None
180+
environment = None
181+
182+
@classmethod
183+
def setUpClass(cls):
184+
global SELENIUM_DRIVER
185+
try:
186+
from selenium import webdriver
187+
except ImportError:
188+
raise unittest.SkipTest("No selenium installed")
189+
190+
super(SeleniumTestCase, cls).setUpClass()
191+
192+
cls.nagios_config = adagios.settings.nagios_config
193+
cls.environment = adagios.utils.FakeAdagiosEnvironment()
194+
cls.environment.create_minimal_environment()
195+
cls.environment.configure_livestatus()
196+
cls.environment.update_adagios_global_variables()
197+
cls.environment.start()
198+
cls.livestatus = cls.environment.get_livestatus()
199+
200+
if not SELENIUM_DRIVER:
201+
if 'TRAVIS' in os.environ:
202+
capabilities = webdriver.DesiredCapabilities.CHROME
203+
capabilities["build"] = os.environ["TRAVIS_BUILD_NUMBER"]
204+
capabilities["tags"] = [os.environ["TRAVIS_PYTHON_VERSION"], "CI"]
205+
capabilities["tunnel-identifier"] = os.environ["TRAVIS_JOB_NUMBER"]
206+
capabilities['platform'] = "Windows 8.1"
207+
capabilities['version'] = "31"
208+
209+
username = os.environ["SAUCE_USERNAME"]
210+
access_key = os.environ["SAUCE_ACCESS_KEY"]
211+
212+
hub_url = "%s:%[email protected]/wd/hub" % (username, access_key)
213+
SELENIUM_DRIVER = webdriver.Remote(desired_capabilities=capabilities, command_executor="http://%s" % hub_url)
214+
else:
215+
SELENIUM_DRIVER = webdriver.Firefox()
216+
# Exit browser when all tests are done
217+
atexit.register(SELENIUM_DRIVER.quit)
218+
219+
220+
221+
cls.driver = SELENIUM_DRIVER
222+
223+
@classmethod
224+
def tearDownClass(cls):
225+
cls.environment.terminate()
226+
super(SeleniumTestCase, cls).tearDownClass()
227+

0 commit comments

Comments
 (0)