1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
11 from datetime import timedelta
12 from unittest import TestCase
14 import jenkins.adapter as jenkins
15 from jenkins.models import *
18 # Tests that the data we get with the jenkinsadapter contains all the
19 # data we need. These test will fail if;
20 # - there is no internet connection
21 # - the opnfv jenkins url has changed
22 # - the jenkins api has changed
23 # - jenkins is not set up / there is no data
24 class JenkinsAdapterTestCase(TestCase):
25 def test_get_all_slaves(self):
26 slaves = jenkins.get_all_slaves()
27 self.assertTrue(len(slaves) > 0)
29 self.assertTrue('displayName' in slave)
30 self.assertTrue('idle' in slave)
31 self.assertTrue('offline' in slave)
33 def test_get_slave(self):
34 slaves = jenkins.get_all_slaves()
35 self.assertEqual(slaves[0], jenkins.get_slave(slaves[0]['displayName']))
36 self.assertEqual({}, jenkins.get_slave('098f6bcd4621d373cade4e832627b4f6'))
38 def test_get_ci_slaves(self):
39 slaves = jenkins.get_ci_slaves()
40 self.assertTrue(len(slaves) > 0)
42 self.assertTrue('nodeName' in slave)
44 def test_get_jenkins_job(self):
45 slaves = jenkins.get_ci_slaves()
48 job = jenkins.get_jenkins_job(slave['nodeName'])
51 # We need to test at least one job
52 self.assertNotEqual(job, None)
54 def test_get_all_jobs(self):
55 jobs = jenkins.get_all_jobs()
57 self.assertTrue(len(jobs) > 0)
59 self.assertTrue('displayName' in job)
60 self.assertTrue('url' in job)
61 self.assertTrue('lastBuild' in job)
62 if job['lastBuild'] is not None:
64 self.assertTrue('building' in job['lastBuild'])
65 self.assertTrue('fullDisplayName' in job['lastBuild'])
66 self.assertTrue('result' in job['lastBuild'])
67 self.assertTrue('timestamp' in job['lastBuild'])
68 self.assertTrue('builtOn' in job['lastBuild'])
69 self.assertTrue(lastBuild)
71 def test_parse_job(self):
73 "displayName": "apex-deploy-baremetal-os-nosdn-fdio-noha-colorado",
74 "url": "https://build.opnfv.org/ci/job/apex-deploy-baremetal-os-nosdn-fdio-noha-colorado/",
77 "fullDisplayName": "apex-deploy-baremetal-os-nosdn-fdio-noha-colorado #37",
79 "timestamp": 1476283629917,
84 job = jenkins.parse_job(job)
85 self.assertEqual(job['scenario'], 'os-nosdn-fdio-noha')
86 self.assertEqual(job['installer'], 'apex')
87 self.assertEqual(job['branch'], 'colorado')
88 self.assertEqual(job['result'], 'SUCCESS')
89 self.assertEqual(job['building'], False)
90 self.assertEqual(job['url'],
91 "https://build.opnfv.org/ci/job/apex-deploy-baremetal-os-nosdn-fdio-noha-colorado/")
92 self.assertEqual(job['name'],
93 'apex-deploy-baremetal-os-nosdn-fdio-noha-colorado')
95 def test_get_slave_status(self):
100 self.assertEqual(jenkins.get_slave_status(slave), 'offline')
105 self.assertEqual(jenkins.get_slave_status(slave), 'online')
110 self.assertEqual(jenkins.get_slave_status(slave), 'online / idle')
113 class JenkinsModelTestCase(TestCase):
114 def test_get_utilization(self):
115 jenkins_slave = JenkinsSlave.objects.create(name='test', status='offline', url='')
116 utilization = jenkins_slave.get_utilization(timedelta(weeks=1))
117 self.assertEqual(utilization['idle'], 0)
118 self.assertEqual(utilization['offline'], 0)
119 self.assertEqual(utilization['online'], 0)
122 JenkinsStatistic.objects.create(slave=jenkins_slave,
123 offline=True, idle=True,
126 utilization = jenkins_slave.get_utilization(timedelta(weeks=1))
127 self.assertEqual(utilization['idle'], 10)
128 self.assertEqual(utilization['offline'], 10)
129 self.assertEqual(utilization['online'], 10)