summaryrefslogtreecommitdiffstats
path: root/pharos-dashboard/src/jenkins
diff options
context:
space:
mode:
authormaxbr <maxbr@mi.fu-berlin.de>2016-10-17 14:56:11 +0200
committermaxbr <maxbr@mi.fu-berlin.de>2016-10-17 14:56:11 +0200
commit61f00f4110b1d33827e3bd19ffef34f10be8273a (patch)
tree2c24ca2a8414eb1f40d5363e03d3c2b21e1fec32 /pharos-dashboard/src/jenkins
parentadafb09cb1f221259970d16886ea0e03169899a5 (diff)
Add unit tests
JIRA: RELENG-12 This commit increases test statement coverage to 84%. It also fixes the bugs that emerged while testing. Change-Id: I696091f1a07f8b7647872c7cb15f4368a4690619 Signed-off-by: maxbr <maxbr@mi.fu-berlin.de>
Diffstat (limited to 'pharos-dashboard/src/jenkins')
-rw-r--r--pharos-dashboard/src/jenkins/adapter.py2
-rw-r--r--pharos-dashboard/src/jenkins/models.py2
-rw-r--r--pharos-dashboard/src/jenkins/tasks.py15
-rw-r--r--pharos-dashboard/src/jenkins/tests.py77
4 files changed, 95 insertions, 1 deletions
diff --git a/pharos-dashboard/src/jenkins/adapter.py b/pharos-dashboard/src/jenkins/adapter.py
index ff0508d..edf502f 100644
--- a/pharos-dashboard/src/jenkins/adapter.py
+++ b/pharos-dashboard/src/jenkins/adapter.py
@@ -9,8 +9,8 @@
import logging
-
import re
+
import requests
from django.core.cache import cache
diff --git a/pharos-dashboard/src/jenkins/models.py b/pharos-dashboard/src/jenkins/models.py
index 0875bba..8254ff3 100644
--- a/pharos-dashboard/src/jenkins/models.py
+++ b/pharos-dashboard/src/jenkins/models.py
@@ -29,6 +29,8 @@ class JenkinsSlave(models.Model):
last_job_installer = models.CharField(max_length=50, default='')
last_job_result = models.CharField(max_length=30, default='')
+ active = models.BooleanField(default=False)
+
def get_utilization(self, timedelta):
"""
Return a dictionary containing the count of idle, online and offline measurements in the time from
diff --git a/pharos-dashboard/src/jenkins/tasks.py b/pharos-dashboard/src/jenkins/tasks.py
index 7c03782..ea986c1 100644
--- a/pharos-dashboard/src/jenkins/tasks.py
+++ b/pharos-dashboard/src/jenkins/tasks.py
@@ -10,6 +10,7 @@
from celery import shared_task
+from dashboard.models import Resource
from jenkins.models import JenkinsSlave, JenkinsStatistic
from .adapter import *
@@ -20,14 +21,28 @@ def sync_jenkins():
def update_jenkins_slaves():
+ JenkinsSlave.objects.all().update(active=False)
+
jenkins_slaves = get_all_slaves()
for slave in jenkins_slaves:
jenkins_slave, created = JenkinsSlave.objects.get_or_create(name=slave['displayName'],
url=get_slave_url(slave))
+ jenkins_slave.active = True
jenkins_slave.ci_slave = is_ci_slave(slave['displayName'])
jenkins_slave.dev_pod = is_dev_pod(slave['displayName'])
jenkins_slave.status = get_slave_status(slave)
+ # if this is a new slave and a pod, check if there is a resource for it, create one if not
+ if created and 'pod' in slave['displayName']:
+ # parse resource name from slave name
+ # naming example: orange-pod1, resource name: Orange POD 1
+ tokens = slave['displayName'].split('-')
+ name = tokens[0].capitalize() + ' POD '# company name
+ name += tokens[1][3:] # remove 'pod'
+ resource, created = Resource.objects.get_or_create(name=name)
+ resource.slave = jenkins_slave
+ resource.save()
+
last_job = get_jenkins_job(jenkins_slave.name)
if last_job is not None:
last_job = parse_job(last_job)
diff --git a/pharos-dashboard/src/jenkins/tests.py b/pharos-dashboard/src/jenkins/tests.py
index 4f350d2..3723cd3 100644
--- a/pharos-dashboard/src/jenkins/tests.py
+++ b/pharos-dashboard/src/jenkins/tests.py
@@ -8,9 +8,11 @@
##############################################################################
+from datetime import timedelta
from unittest import TestCase
import jenkins.adapter as jenkins
+from jenkins.models import *
# Tests that the data we get with the jenkinsadapter contains all the
@@ -28,12 +30,27 @@ class JenkinsAdapterTestCase(TestCase):
self.assertTrue('idle' in slave)
self.assertTrue('offline' in slave)
+ def test_get_slave(self):
+ slaves = jenkins.get_all_slaves()
+ self.assertEqual(slaves[0], jenkins.get_slave(slaves[0]['displayName']))
+ self.assertEqual({}, jenkins.get_slave('098f6bcd4621d373cade4e832627b4f6'))
+
def test_get_ci_slaves(self):
slaves = jenkins.get_ci_slaves()
self.assertTrue(len(slaves) > 0)
for slave in slaves:
self.assertTrue('nodeName' in slave)
+ def test_get_jenkins_job(self):
+ slaves = jenkins.get_ci_slaves()
+ job = None
+ for slave in slaves:
+ job = jenkins.get_jenkins_job(slave['nodeName'])
+ if job is not None:
+ break
+ # We need to test at least one job
+ self.assertNotEqual(job, None)
+
def test_get_all_jobs(self):
jobs = jenkins.get_all_jobs()
lastBuild = False
@@ -50,3 +67,63 @@ class JenkinsAdapterTestCase(TestCase):
self.assertTrue('timestamp' in job['lastBuild'])
self.assertTrue('builtOn' in job['lastBuild'])
self.assertTrue(lastBuild)
+
+ def test_parse_job(self):
+ job = {
+ "displayName": "apex-deploy-baremetal-os-nosdn-fdio-noha-colorado",
+ "url": "https://build.opnfv.org/ci/job/apex-deploy-baremetal-os-nosdn-fdio-noha-colorado/",
+ "lastBuild": {
+ "building": False,
+ "fullDisplayName": "apex-deploy-baremetal-os-nosdn-fdio-noha-colorado #37",
+ "result": "SUCCESS",
+ "timestamp": 1476283629917,
+ "builtOn": "lf-pod1"
+ }
+ }
+
+ job = jenkins.parse_job(job)
+ self.assertEqual(job['scenario'], 'os-nosdn-fdio-noha')
+ self.assertEqual(job['installer'], 'apex')
+ self.assertEqual(job['branch'], 'colorado')
+ self.assertEqual(job['result'], 'SUCCESS')
+ self.assertEqual(job['building'], False)
+ self.assertEqual(job['url'],
+ "https://build.opnfv.org/ci/job/apex-deploy-baremetal-os-nosdn-fdio-noha-colorado/")
+ self.assertEqual(job['name'],
+ 'apex-deploy-baremetal-os-nosdn-fdio-noha-colorado')
+
+ def test_get_slave_status(self):
+ slave = {
+ 'offline': True,
+ 'idle': False
+ }
+ self.assertEqual(jenkins.get_slave_status(slave), 'offline')
+ slave = {
+ 'offline': False,
+ 'idle': False
+ }
+ self.assertEqual(jenkins.get_slave_status(slave), 'online')
+ slave = {
+ 'offline': False,
+ 'idle': True
+ }
+ self.assertEqual(jenkins.get_slave_status(slave), 'online / idle')
+
+
+class JenkinsModelTestCase(TestCase):
+ def test_get_utilization(self):
+ jenkins_slave = JenkinsSlave.objects.create(name='test', status='offline', url='')
+ utilization = jenkins_slave.get_utilization(timedelta(weeks=1))
+ self.assertEqual(utilization['idle'], 0)
+ self.assertEqual(utilization['offline'], 0)
+ self.assertEqual(utilization['online'], 0)
+
+ for i in range(10):
+ JenkinsStatistic.objects.create(slave=jenkins_slave,
+ offline=True, idle=True,
+ online=True)
+
+ utilization = jenkins_slave.get_utilization(timedelta(weeks=1))
+ self.assertEqual(utilization['idle'], 10)
+ self.assertEqual(utilization['offline'], 10)
+ self.assertEqual(utilization['online'], 10)