|
1 | 1 | """ |
2 | | -Unit tests for the Drycc scheduler module. |
| 2 | +Unit tests for the Deis scheduler module. |
3 | 3 |
|
4 | 4 | Run the tests with "./manage.py test scheduler" |
5 | 5 | """ |
6 | | -import json |
7 | 6 | import requests |
8 | 7 | import requests_mock |
9 | 8 | from unittest import mock |
| 9 | +from packaging.version import parse |
10 | 10 |
|
11 | | -from django.conf import settings |
12 | 11 | from django.test import TestCase |
13 | 12 |
|
14 | 13 | import scheduler |
15 | | -from scheduler import exceptions |
16 | 14 |
|
17 | 15 |
|
18 | | -def mock_session(): |
| 16 | +def mock_session_for_version(blah=None): |
19 | 17 | return requests.Session() |
20 | 18 |
|
21 | 19 |
|
22 | 20 | def connection_refused_matcher(request): |
23 | 21 | raise requests.ConnectionError("connection refused") |
24 | 22 |
|
25 | 23 |
|
26 | | -@mock.patch('scheduler.get_session', mock_session) |
| 24 | +@mock.patch('scheduler.get_session', mock_session_for_version) |
27 | 25 | class KubeHTTPClientTest(TestCase): |
28 | | - """Tests kubernetes HTTP client calls""" |
| 26 | + """Tests kubernetes HTTP client version calls""" |
29 | 27 |
|
30 | 28 | def setUp(self): |
31 | 29 | self.adapter = requests_mock.Adapter() |
32 | | - self.path = '/foo' |
33 | | - self.url = settings.SCHEDULER_URL + self.path |
| 30 | + self.url = 'http://versiontest.example.com' |
| 31 | + self.path = '/version' |
| 32 | + |
34 | 33 | # use the real scheduler client. |
35 | | - self.scheduler = scheduler.KubeHTTPClient(settings.SCHEDULER_URL) |
| 34 | + self.scheduler = scheduler.KubeHTTPClient(self.url) |
36 | 35 | self.scheduler.session.mount(self.url, self.adapter) |
37 | 36 |
|
38 | | - def test_head(self): |
39 | | - """ |
40 | | - Test that calling .http_head() uses the client session to make a HEAD request. |
41 | | - """ |
42 | | - self.adapter.register_uri('HEAD', self.url) |
43 | | - response = self.scheduler.http_head(self.path) |
44 | | - assert response is not None |
45 | | - self.assertTrue(self.adapter.called) |
46 | | - self.assertEqual(self.adapter.call_count, 1) |
47 | | - # ensure that connection errors get raised as a KubeException |
48 | | - self.adapter.add_matcher(connection_refused_matcher) |
49 | | - with self.assertRaises(exceptions.KubeException): |
50 | | - self.scheduler.http_head(self.path) |
51 | | - |
52 | | - def test_get(self): |
53 | | - """ |
54 | | - Test that calling .http_get() uses the client session to make a GET request. |
55 | | - """ |
56 | | - self.adapter.register_uri('GET', self.url) |
57 | | - response = self.scheduler.http_get(self.path) |
58 | | - assert response is not None |
59 | | - self.assertTrue(self.adapter.called) |
60 | | - self.assertEqual(self.adapter.call_count, 1) |
61 | | - # ensure that connection errors get raised as a KubeException |
62 | | - self.adapter.add_matcher(connection_refused_matcher) |
63 | | - with self.assertRaises(exceptions.KubeException): |
64 | | - self.scheduler.http_get(self.path) |
65 | | - |
66 | | - def test_post(self): |
| 37 | + def test_version_for_gke(self): |
67 | 38 | """ |
68 | | - Test that calling .http_post() uses the client session to make a POST request. |
| 39 | + Ensure that version() sanitizes info from GKE clusters |
69 | 40 | """ |
70 | | - self.adapter.register_uri('POST', self.url) |
71 | | - response = self.scheduler.http_post(self.path, data=json.dumps({'hello': 'world'})) |
72 | | - assert response is not None |
73 | | - self.assertTrue(self.adapter.called) |
74 | | - self.assertEqual(self.adapter.call_count, 1) |
75 | | - # ensure that connection errors get raised as a KubeException |
76 | | - self.adapter.add_matcher(connection_refused_matcher) |
77 | | - with self.assertRaises(exceptions.KubeException): |
78 | | - self.scheduler.http_post(self.path) |
79 | | - |
80 | | - def test_put(self): |
| 41 | + |
| 42 | + cases = { |
| 43 | + "1.12": {"major": "1", "minor": "12-gke"}, |
| 44 | + "1.10": {"major": "1", "minor": "10-gke"}, |
| 45 | + "1.9": {"major": "1", "minor": "9-gke"}, |
| 46 | + "1.8": {"major": "1", "minor": "8-gke"}, |
| 47 | + } |
| 48 | + |
| 49 | + for canonical in cases: |
| 50 | + resp = cases[canonical] |
| 51 | + self.adapter.register_uri('GET', self.url + self.path, json=resp) |
| 52 | + |
| 53 | + expected = parse(canonical) |
| 54 | + actual = self.scheduler.version() |
| 55 | + |
| 56 | + self.assertEqual(expected, actual, "{} breaks".format(resp)) |
| 57 | + |
| 58 | + def test_version_for_eks(self): |
81 | 59 | """ |
82 | | - Test that calling .http_put() uses the client session to make a PUT request. |
| 60 | + Ensure that version() sanitizes info from EKS clusters |
83 | 61 | """ |
84 | | - self.adapter.register_uri('PUT', self.url) |
85 | | - response = self.scheduler.http_put(self.path, data=json.dumps({'hello': 'world'})) |
86 | | - assert response is not None |
87 | | - self.assertTrue(self.adapter.called) |
88 | | - self.assertEqual(self.adapter.call_count, 1) |
89 | | - # ensure that connection errors get raised as a KubeException |
90 | | - self.adapter.add_matcher(connection_refused_matcher) |
91 | | - with self.assertRaises(exceptions.KubeException): |
92 | | - self.scheduler.http_put(self.path) |
93 | | - |
94 | | - def test_delete(self): |
| 62 | + |
| 63 | + cases = { |
| 64 | + "1.12": {"major": "1", "minor": "12+"}, |
| 65 | + "1.10": {"major": "1", "minor": "10+"}, |
| 66 | + "1.9": {"major": "1", "minor": "9+"}, |
| 67 | + "1.8": {"major": "1", "minor": "8+"}, |
| 68 | + } |
| 69 | + |
| 70 | + for canonical in cases: |
| 71 | + resp = cases[canonical] |
| 72 | + self.adapter.register_uri('GET', self.url + self.path, json=resp) |
| 73 | + |
| 74 | + expected = parse(canonical) |
| 75 | + actual = self.scheduler.version() |
| 76 | + |
| 77 | + self.assertEqual(expected, actual, "{} breaks".format(resp)) |
| 78 | + |
| 79 | + def test_version_vanilla(self): |
95 | 80 | """ |
96 | | - Test that calling .http_delete() uses the client session to make a DELETE request. |
| 81 | + Ensure that version() sanitizes info from vanilla k8s clusters |
97 | 82 | """ |
98 | | - self.adapter.register_uri('DELETE', self.url) |
99 | | - response = self.scheduler.http_delete(self.path) |
100 | | - assert response is not None |
101 | | - self.assertTrue(self.adapter.called) |
102 | | - self.assertEqual(self.adapter.call_count, 1) |
103 | | - # ensure that connection errors get raised as a KubeException |
104 | | - self.adapter.add_matcher(connection_refused_matcher) |
105 | | - with self.assertRaises(exceptions.KubeException): |
106 | | - self.scheduler.http_delete(self.path) |
| 83 | + |
| 84 | + cases = { |
| 85 | + "1.12": {"major": "1", "minor": "12"}, |
| 86 | + "1.10": {"major": "1", "minor": "10"}, |
| 87 | + "1.9": {"major": "1", "minor": "9"}, |
| 88 | + "1.8": {"major": "1", "minor": "8"}, |
| 89 | + } |
| 90 | + |
| 91 | + for canonical in cases: |
| 92 | + resp = cases[canonical] |
| 93 | + self.adapter.register_uri('GET', self.url + self.path, json=resp) |
| 94 | + |
| 95 | + expected = parse(canonical) |
| 96 | + actual = self.scheduler.version() |
| 97 | + |
| 98 | + self.assertEqual(expected, actual, "{} breaks".format(resp)) |
0 commit comments