Development Override Compose File
[laas.git] / src / jenkins / tests.py
1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
3 #
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 ##############################################################################
9
10
11 from datetime import timedelta
12 from unittest import TestCase
13
14 import jenkins.adapter as jenkins
15 from jenkins.models import *
16
17
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)
28         for slave in slaves:
29             self.assertTrue('displayName' in slave)
30             self.assertTrue('idle' in slave)
31             self.assertTrue('offline' in slave)
32
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'))
37
38     def test_get_ci_slaves(self):
39         slaves = jenkins.get_ci_slaves()
40         self.assertTrue(len(slaves) > 0)
41         for slave in slaves:
42             self.assertTrue('nodeName' in slave)
43
44     def test_get_jenkins_job(self):
45         slaves = jenkins.get_ci_slaves()
46         job = None
47         for slave in slaves:
48             job = jenkins.get_jenkins_job(slave['nodeName'])
49             if job is not None:
50                 break
51         # We need to test at least one job
52         self.assertNotEqual(job, None)
53
54     def test_get_all_jobs(self):
55         jobs = jenkins.get_all_jobs()
56         lastBuild = False
57         self.assertTrue(len(jobs) > 0)
58         for job in jobs:
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:
63                 lastBuild = True
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)
70
71     def test_parse_job(self):
72         job = {
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/",
75             "lastBuild": {
76                 "building": False,
77                 "fullDisplayName": "apex-deploy-baremetal-os-nosdn-fdio-noha-colorado #37",
78                 "result": "SUCCESS",
79                 "timestamp": 1476283629917,
80                 "builtOn": "lf-pod1"
81             }
82         }
83
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')
94
95     def test_get_slave_status(self):
96         slave = {
97             'offline': True,
98             'idle': False
99         }
100         self.assertEqual(jenkins.get_slave_status(slave), 'offline')
101         slave = {
102             'offline': False,
103             'idle': False
104         }
105         self.assertEqual(jenkins.get_slave_status(slave), 'online')
106         slave = {
107             'offline': False,
108             'idle': True
109         }
110         self.assertEqual(jenkins.get_slave_status(slave), 'online / idle')
111
112
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)
120
121         for i in range(10):
122             JenkinsStatistic.objects.create(slave=jenkins_slave,
123                                             offline=True, idle=True,
124                                             online=True)
125
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)