summaryrefslogtreecommitdiffstats
path: root/src/dashboard/tests
diff options
context:
space:
mode:
authorTrevor Bramwell <tbramwell@linuxfoundation.org>2017-09-22 12:23:36 -0700
committerTrevor Bramwell <tbramwell@linuxfoundation.org>2017-09-22 12:23:36 -0700
commitb07bbdba03fe9b1c3da2f69b8bc28b8071d99ec1 (patch)
treeed2a67f71eb8f3d3e8e3c284e7d53f91ecd6cd96 /src/dashboard/tests
Rename pharos-dashboard and pharos-validator
As subdirectories of the pharos-tools repo, there is little need to keep the pharos prefix. Change-Id: Ica3d79411f409df638647300036c0664183c2725 Signed-off-by: Trevor Bramwell <tbramwell@linuxfoundation.org>
Diffstat (limited to 'src/dashboard/tests')
-rw-r--r--src/dashboard/tests/__init__.py10
-rw-r--r--src/dashboard/tests/test_models.py69
-rw-r--r--src/dashboard/tests/test_views.py75
3 files changed, 154 insertions, 0 deletions
diff --git a/src/dashboard/tests/__init__.py b/src/dashboard/tests/__init__.py
new file mode 100644
index 0000000..b5914ce
--- /dev/null
+++ b/src/dashboard/tests/__init__.py
@@ -0,0 +1,10 @@
+##############################################################################
+# Copyright (c) 2016 Max Breitenfeldt and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+
diff --git a/src/dashboard/tests/test_models.py b/src/dashboard/tests/test_models.py
new file mode 100644
index 0000000..3a3aeab
--- /dev/null
+++ b/src/dashboard/tests/test_models.py
@@ -0,0 +1,69 @@
+##############################################################################
+# Copyright (c) 2016 Max Breitenfeldt and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+
+from datetime import timedelta
+from math import ceil, floor
+
+from django.test import TestCase
+from django.utils import timezone
+
+from booking.models import *
+from dashboard.models import Resource
+from jenkins.models import JenkinsSlave
+
+
+class ResourceModelTestCase(TestCase):
+ def setUp(self):
+ self.slave = JenkinsSlave.objects.create(name='test', url='test')
+ self.owner = User.objects.create(username='owner')
+
+ self.res1 = Resource.objects.create(name='res1', slave=self.slave, description='x',
+ url='x', owner=self.owner)
+
+ def test_booking_utilization(self):
+ utilization = self.res1.get_booking_utilization(1)
+ self.assertTrue(utilization['booked_seconds'] == 0)
+ self.assertTrue(utilization['available_seconds'] == timedelta(weeks=1).total_seconds())
+
+ start = timezone.now() + timedelta(days=1)
+ end = start + timedelta(days=1)
+ booking = Booking.objects.create(start=start, end=end, purpose='test', resource=self.res1,
+ user=self.owner)
+
+ utilization = self.res1.get_booking_utilization(1)
+ booked_seconds = timedelta(days=1).total_seconds()
+ self.assertEqual(utilization['booked_seconds'], booked_seconds)
+
+ utilization = self.res1.get_booking_utilization(-1)
+ self.assertEqual(utilization['booked_seconds'], 0)
+
+ booking.delete()
+ start = timezone.now() - timedelta(days=1)
+ end = start + timedelta(days=2)
+ booking = Booking.objects.create(start=start, end=end, purpose='test', resource=self.res1,
+ user=self.owner)
+ booked_seconds = self.res1.get_booking_utilization(1)['booked_seconds']
+ # use ceil because a fraction of the booked time has already passed now
+ booked_seconds = ceil(booked_seconds)
+ self.assertEqual(booked_seconds, timedelta(days=1).total_seconds())
+
+ booking.delete()
+ start = timezone.now() + timedelta(days=6)
+ end = start + timedelta(days=2)
+ booking = Booking.objects.create(start=start, end=end, purpose='test', resource=self.res1,
+ user=self.owner)
+ booked_seconds = self.res1.get_booking_utilization(1)['booked_seconds']
+ booked_seconds = floor(booked_seconds)
+ self.assertEqual(booked_seconds, timedelta(days=1).total_seconds())
+
+
+
+
+
diff --git a/src/dashboard/tests/test_views.py b/src/dashboard/tests/test_views.py
new file mode 100644
index 0000000..f5e17c2
--- /dev/null
+++ b/src/dashboard/tests/test_views.py
@@ -0,0 +1,75 @@
+##############################################################################
+# Copyright (c) 2016 Max Breitenfeldt and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+
+from django.test import TestCase
+from django.urls import reverse
+
+from dashboard.models import Resource
+from jenkins.models import JenkinsSlave
+
+
+class DashboardViewTestCase(TestCase):
+ def setUp(self):
+ self.slave_active = JenkinsSlave.objects.create(name='slave_active', url='x', active=True)
+ self.slave_inactive = JenkinsSlave.objects.create(name='slave_inactive', url='x',
+ active=False)
+ self.res_active = Resource.objects.create(name='res_active', slave=self.slave_active,
+ description='x', url='x')
+ self.res_inactive = Resource.objects.create(name='res_inactive', slave=self.slave_inactive,
+ description='x', url='x')
+
+ def test_booking_utilization_json(self):
+ url = reverse('dashboard:booking_utilization', kwargs={'resource_id': 0, 'weeks': 0})
+ self.assertEqual(self.client.get(url).status_code, 404)
+
+ url = reverse('dashboard:booking_utilization', kwargs={'resource_id': self.res_active.id,
+ 'weeks': 0})
+ response = self.client.get(url)
+ self.assertEqual(response.status_code, 200)
+ self.assertContains(response, 'data')
+
+ def test_jenkins_utilization_json(self):
+ url = reverse('dashboard:jenkins_utilization', kwargs={'resource_id': 0, 'weeks': 0})
+ self.assertEqual(self.client.get(url).status_code, 404)
+
+ url = reverse('dashboard:jenkins_utilization', kwargs={'resource_id': self.res_active.id,
+ 'weeks': 0})
+ response = self.client.get(url)
+ self.assertEqual(response.status_code, 200)
+ self.assertContains(response, 'data')
+
+ def test_jenkins_slaves_view(self):
+ url = reverse('dashboard:jenkins_slaves')
+ response = self.client.get(url)
+ self.assertEqual(response.status_code, 200)
+ self.assertIn(self.slave_active, response.context['slaves'])
+ self.assertNotIn(self.slave_inactive, response.context['slaves'])
+
+ def test_ci_pods_view(self):
+ url = reverse('dashboard:ci_pods')
+ response = self.client.get(url)
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(len(response.context['ci_pods']), 0)
+
+ self.slave_active.ci_slave = True
+ self.slave_inactive.ci_slave = True
+ self.slave_active.save()
+ self.slave_inactive.save()
+
+ response = self.client.get(url)
+ self.assertIn(self.res_active, response.context['ci_pods'])
+ self.assertNotIn(self.res_inactive, response.context['ci_pods'])
+
+ def test_dev_pods_view(self):
+ url = reverse('dashboard:dev_pods')
+ response = self.client.get(url)
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(len(response.context['dev_pods']), 0)
+