|
3 | 3 |
|
4 | 4 | Run the tests with './manage.py test scheduler' |
5 | 5 | """ |
| 6 | +from unittest import mock |
| 7 | +from datetime import datetime, timedelta |
6 | 8 | from scheduler import KubeHTTPException |
7 | 9 | from scheduler.tests import TestCase |
8 | 10 | from scheduler.utils import generate_random_name |
@@ -91,3 +93,109 @@ def test_get_pod(self): |
91 | 93 | }, |
92 | 94 | data['metadata']['labels'] |
93 | 95 | ) |
| 96 | + |
| 97 | + def test_liveness_status(self): |
| 98 | + # Missing Ready type means pod has passed liveness check |
| 99 | + pod = {'status': {'conditions': []}} |
| 100 | + self.assertTrue(self.scheduler.pod.liveness_status(pod)) |
| 101 | + |
| 102 | + # fake out a minimal pod structure for success |
| 103 | + pod['status']['conditions'].append({ |
| 104 | + 'type': 'Ready', |
| 105 | + 'status': 'True' |
| 106 | + }) |
| 107 | + self.assertTrue(self.scheduler.pod.liveness_status(pod)) |
| 108 | + |
| 109 | + # fake out a minimal pod structure for failure |
| 110 | + pod['status']['conditions'][0]['status'] = 'False' |
| 111 | + self.assertFalse(self.scheduler.pod.liveness_status(pod)) |
| 112 | + |
| 113 | + def test_readiness_status(self): |
| 114 | + # create a pod to then manipulate |
| 115 | + name = self.create() |
| 116 | + pod = self.scheduler.pod.get(self.namespace, name).json() |
| 117 | + |
| 118 | + # on a newly created pod has an overall "Running" status with ready state on a container |
| 119 | + self.assertEqual(self.scheduler.pod.readiness_status(pod), 'Running') |
| 120 | + |
| 121 | + # on a newly created pod has been deleted with ready state on a container |
| 122 | + pod['metadata']['deletionTimestamp'] = 'fake' |
| 123 | + self.assertEqual(self.scheduler.pod.readiness_status(pod), 'Terminating') |
| 124 | + del pod['metadata']['deletionTimestamp'] |
| 125 | + |
| 126 | + # now say the app container is not Ready |
| 127 | + container = pod['status']['containerStatuses'][0] |
| 128 | + container['ready'] = False |
| 129 | + |
| 130 | + # state of the container is Starting when container is not ready but says Running |
| 131 | + self.assertTrue('running' in container['state'].keys()) |
| 132 | + self.assertEqual(self.scheduler.pod.readiness_status(pod), 'Starting') |
| 133 | + |
| 134 | + # verify Terminating status |
| 135 | + del container['state']['running'] |
| 136 | + container['state']['terminated'] = 'fake' |
| 137 | + self.assertEqual(self.scheduler.pod.readiness_status(pod), 'Terminating') |
| 138 | + |
| 139 | + # test metdata terminating |
| 140 | + del container['state']['terminated'] |
| 141 | + pod['metadata']['deletionTimestamp'] = 'fake' |
| 142 | + self.assertEqual(self.scheduler.pod.readiness_status(pod), 'Terminating') |
| 143 | + del pod['metadata']['deletionTimestamp'] |
| 144 | + |
| 145 | + # inject fake state |
| 146 | + container['state']['random'] = 'fake' |
| 147 | + self.assertEqual(self.scheduler.pod.readiness_status(pod), 'Unknown') |
| 148 | + |
| 149 | + # no containers around means Unknown |
| 150 | + pod['status']['containerStatuses'] = [] |
| 151 | + self.assertEqual(self.scheduler.pod.readiness_status(pod), 'Unknown') |
| 152 | + |
| 153 | + def test_ready(self): |
| 154 | + # create a pod to then manipulate |
| 155 | + name = self.create() |
| 156 | + pod = self.scheduler.pod.get(self.namespace, name).json() |
| 157 | + |
| 158 | + # pod itself shouldn't be ready yet |
| 159 | + self.assertFalse(self.scheduler.pod.ready(pod)) |
| 160 | + |
| 161 | + # only pod but no other probe |
| 162 | + pod['status']['phase'] = 'Running' |
| 163 | + # fake out functions for failure |
| 164 | + with mock.patch('scheduler.resources.pod.Pod.readiness_status') as ready: |
| 165 | + ready.return_value = 'Starting' |
| 166 | + |
| 167 | + # only readiness is ready so overall ready status is False |
| 168 | + self.assertFalse(self.scheduler.pod.ready(pod)) |
| 169 | + |
| 170 | + with mock.patch('scheduler.resources.pod.Pod.liveness_status') as liveness: |
| 171 | + liveness.return_value = False |
| 172 | + |
| 173 | + # all things have lined up, go time |
| 174 | + self.assertFalse(self.scheduler.pod.ready(pod)) |
| 175 | + |
| 176 | + # fake out other functions since they are tested by themselves |
| 177 | + with mock.patch('scheduler.resources.pod.Pod.readiness_status') as ready: |
| 178 | + ready.return_value = 'Running' |
| 179 | + with mock.patch('scheduler.resources.pod.Pod.liveness_status') as liveness: |
| 180 | + # keep liveness as failing for now |
| 181 | + liveness.return_value = False |
| 182 | + |
| 183 | + # only readiness is ready so overall ready status is False |
| 184 | + self.assertFalse(self.scheduler.pod.ready(pod)) |
| 185 | + |
| 186 | + # all things have lined up, go time |
| 187 | + liveness.return_value = True |
| 188 | + self.assertTrue(self.scheduler.pod.ready(pod)) |
| 189 | + |
| 190 | + def test_deleted(self): |
| 191 | + # create a pod to then manipulate |
| 192 | + name = self.create() |
| 193 | + pod = self.scheduler.pod.get(self.namespace, name).json() |
| 194 | + |
| 195 | + # pod should no be deleted yet |
| 196 | + self.assertFalse(self.scheduler.pod.deleted(pod)) |
| 197 | + |
| 198 | + # set deleted 10 minutes in the past |
| 199 | + ts_deleted = datetime.utcnow() - timedelta(minutes=10) |
| 200 | + pod['metadata']['deletionTimestamp'] = ts_deleted.strftime(self.scheduler.DATETIME_FORMAT) |
| 201 | + self.assertTrue(self.scheduler.pod.deleted(pod)) |
0 commit comments