test_oc_route.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. '''
  2. Unit tests for oc route
  3. '''
  4. import os
  5. import six
  6. import sys
  7. import unittest
  8. import mock
  9. # Removing invalid variable names for tests so that I can
  10. # keep them brief
  11. # pylint: disable=invalid-name,no-name-in-module
  12. # Disable import-error b/c our libraries aren't loaded in jenkins
  13. # pylint: disable=import-error,wrong-import-position
  14. # place class in our python path
  15. module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
  16. sys.path.insert(0, module_path)
  17. from oc_route import OCRoute, locate_oc_binary # noqa: E402
  18. class OCRouteTest(unittest.TestCase):
  19. '''
  20. Test class for OCRoute
  21. '''
  22. @mock.patch('oc_route.locate_oc_binary')
  23. @mock.patch('oc_route.Utils.create_tmpfile_copy')
  24. @mock.patch('oc_route.OCRoute._run')
  25. def test_list_route(self, mock_cmd, mock_tmpfile_copy, mock_oc_binary):
  26. ''' Testing getting a route '''
  27. # Arrange
  28. # run_ansible input parameters
  29. params = {
  30. 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
  31. 'state': 'list',
  32. 'debug': False,
  33. 'name': 'test',
  34. 'namespace': 'default',
  35. 'tls_termination': 'passthrough',
  36. 'dest_cacert_path': None,
  37. 'cacert_path': None,
  38. 'cert_path': None,
  39. 'key_path': None,
  40. 'dest_cacert_content': None,
  41. 'cacert_content': None,
  42. 'cert_content': None,
  43. 'key_content': None,
  44. 'service_name': 'testservice',
  45. 'host': 'test.openshift.com',
  46. 'wildcard_policy': None,
  47. 'weight': None,
  48. 'port': None
  49. }
  50. route_result = '''{
  51. "kind": "Route",
  52. "apiVersion": "v1",
  53. "metadata": {
  54. "name": "test",
  55. "namespace": "default",
  56. "selfLink": "/oapi/v1/namespaces/default/routes/test",
  57. "uid": "1b127c67-ecd9-11e6-96eb-0e0d9bdacd26",
  58. "resourceVersion": "439182",
  59. "creationTimestamp": "2017-02-07T01:59:48Z"
  60. },
  61. "spec": {
  62. "host": "test.example",
  63. "to": {
  64. "kind": "Service",
  65. "name": "test",
  66. "weight": 100
  67. },
  68. "port": {
  69. "targetPort": 8443
  70. },
  71. "tls": {
  72. "termination": "passthrough"
  73. },
  74. "wildcardPolicy": "None"
  75. },
  76. "status": {
  77. "ingress": [
  78. {
  79. "host": "test.example",
  80. "routerName": "router",
  81. "conditions": [
  82. {
  83. "type": "Admitted",
  84. "status": "True",
  85. "lastTransitionTime": "2017-02-07T01:59:48Z"
  86. }
  87. ],
  88. "wildcardPolicy": "None"
  89. }
  90. ]
  91. }
  92. }'''
  93. # Return values of our mocked function call. These get returned once per call.
  94. mock_cmd.side_effect = [
  95. # First call to mock
  96. (0, route_result, ''),
  97. ]
  98. mock_oc_binary.side_effect = [
  99. 'oc'
  100. ]
  101. mock_tmpfile_copy.side_effect = [
  102. '/tmp/mock.kubeconfig',
  103. ]
  104. # Act
  105. results = OCRoute.run_ansible(params, False)
  106. # Assert
  107. self.assertFalse(results['changed'])
  108. self.assertEqual(results['state'], 'list')
  109. self.assertEqual(results['results'][0]['metadata']['name'], 'test')
  110. # Making sure our mock was called as we expected
  111. mock_cmd.assert_has_calls([
  112. mock.call(['oc', 'get', 'route', 'test', '-o', 'json', '-n', 'default'], None),
  113. ])
  114. @mock.patch('oc_route.locate_oc_binary')
  115. @mock.patch('oc_route.Utils.create_tmpfile_copy')
  116. @mock.patch('oc_route.Yedit._write')
  117. @mock.patch('oc_route.OCRoute._run')
  118. def test_create_route(self, mock_cmd, mock_write, mock_tmpfile_copy, mock_oc_binary):
  119. ''' Testing getting a route '''
  120. # Arrange
  121. # run_ansible input parameters
  122. params = {
  123. 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
  124. 'state': 'present',
  125. 'debug': False,
  126. 'name': 'test',
  127. 'namespace': 'default',
  128. 'tls_termination': 'edge',
  129. 'dest_cacert_path': None,
  130. 'cacert_path': None,
  131. 'cert_path': None,
  132. 'key_path': None,
  133. 'dest_cacert_content': None,
  134. 'cacert_content': 'testing',
  135. 'cert_content': 'testing',
  136. 'key_content': 'testing',
  137. 'service_name': 'testservice',
  138. 'host': 'test.openshift.com',
  139. 'wildcard_policy': None,
  140. 'weight': None,
  141. 'port': None
  142. }
  143. route_result = '''{
  144. "apiVersion": "v1",
  145. "kind": "Route",
  146. "metadata": {
  147. "creationTimestamp": "2017-02-07T20:55:10Z",
  148. "name": "test",
  149. "namespace": "default",
  150. "resourceVersion": "517745",
  151. "selfLink": "/oapi/v1/namespaces/default/routes/test",
  152. "uid": "b6f25898-ed77-11e6-9755-0e737db1e63a"
  153. },
  154. "spec": {
  155. "host": "test.openshift.com",
  156. "tls": {
  157. "caCertificate": "testing",
  158. "certificate": "testing",
  159. "key": "testing",
  160. "termination": "edge"
  161. },
  162. "to": {
  163. "kind": "Service",
  164. "name": "testservice",
  165. "weight": 100
  166. },
  167. "wildcardPolicy": "None"
  168. },
  169. "status": {
  170. "ingress": [
  171. {
  172. "conditions": [
  173. {
  174. "lastTransitionTime": "2017-02-07T20:55:10Z",
  175. "status": "True",
  176. "type": "Admitted"
  177. }
  178. ],
  179. "host": "test.openshift.com",
  180. "routerName": "router",
  181. "wildcardPolicy": "None"
  182. }
  183. ]
  184. }
  185. }'''
  186. test_route = '''\
  187. kind: Route
  188. spec:
  189. tls:
  190. caCertificate: testing
  191. termination: edge
  192. certificate: testing
  193. key: testing
  194. to:
  195. kind: Service
  196. name: testservice
  197. weight: 100
  198. host: test.openshift.com
  199. wildcardPolicy: None
  200. apiVersion: v1
  201. metadata:
  202. namespace: default
  203. name: test
  204. '''
  205. # Return values of our mocked function call. These get returned once per call.
  206. mock_cmd.side_effect = [
  207. # First call to mock
  208. (1, '', 'Error from server: routes "test" not found'),
  209. (1, '', 'Error from server: routes "test" not found'),
  210. (0, 'route "test" created', ''),
  211. (0, route_result, ''),
  212. ]
  213. mock_oc_binary.side_effect = [
  214. 'oc'
  215. ]
  216. mock_tmpfile_copy.side_effect = [
  217. '/tmp/mock.kubeconfig',
  218. ]
  219. mock_write.assert_has_calls = [
  220. # First call to mock
  221. mock.call('/tmp/test', test_route)
  222. ]
  223. # Act
  224. results = OCRoute.run_ansible(params, False)
  225. # Assert
  226. self.assertTrue(results['changed'])
  227. self.assertEqual(results['state'], 'present')
  228. self.assertEqual(results['results']['results'][0]['metadata']['name'], 'test')
  229. # Making sure our mock was called as we expected
  230. mock_cmd.assert_has_calls([
  231. mock.call(['oc', 'get', 'route', 'test', '-o', 'json', '-n', 'default'], None),
  232. mock.call(['oc', 'create', '-f', mock.ANY, '-n', 'default'], None),
  233. mock.call(['oc', 'get', 'route', 'test', '-o', 'json', '-n', 'default'], None),
  234. ])
  235. @unittest.skipIf(six.PY3, 'py2 test only')
  236. @mock.patch('os.path.exists')
  237. @mock.patch('os.environ.get')
  238. def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists):
  239. ''' Testing binary lookup fallback '''
  240. mock_env_get.side_effect = lambda _v, _d: ''
  241. mock_path_exists.side_effect = lambda _: False
  242. self.assertEqual(locate_oc_binary(), 'oc')
  243. @unittest.skipIf(six.PY3, 'py2 test only')
  244. @mock.patch('os.path.exists')
  245. @mock.patch('os.environ.get')
  246. def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists):
  247. ''' Testing binary lookup in path '''
  248. oc_bin = '/usr/bin/oc'
  249. mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
  250. mock_path_exists.side_effect = lambda f: f == oc_bin
  251. self.assertEqual(locate_oc_binary(), oc_bin)
  252. @unittest.skipIf(six.PY3, 'py2 test only')
  253. @mock.patch('os.path.exists')
  254. @mock.patch('os.environ.get')
  255. def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists):
  256. ''' Testing binary lookup in /usr/local/bin '''
  257. oc_bin = '/usr/local/bin/oc'
  258. mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
  259. mock_path_exists.side_effect = lambda f: f == oc_bin
  260. self.assertEqual(locate_oc_binary(), oc_bin)
  261. @unittest.skipIf(six.PY3, 'py2 test only')
  262. @mock.patch('os.path.exists')
  263. @mock.patch('os.environ.get')
  264. def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists):
  265. ''' Testing binary lookup in ~/bin '''
  266. oc_bin = os.path.expanduser('~/bin/oc')
  267. mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
  268. mock_path_exists.side_effect = lambda f: f == oc_bin
  269. self.assertEqual(locate_oc_binary(), oc_bin)
  270. @unittest.skipIf(six.PY2, 'py3 test only')
  271. @mock.patch('shutil.which')
  272. @mock.patch('os.environ.get')
  273. def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which):
  274. ''' Testing binary lookup fallback '''
  275. mock_env_get.side_effect = lambda _v, _d: ''
  276. mock_shutil_which.side_effect = lambda _f, path=None: None
  277. self.assertEqual(locate_oc_binary(), 'oc')
  278. @unittest.skipIf(six.PY2, 'py3 test only')
  279. @mock.patch('shutil.which')
  280. @mock.patch('os.environ.get')
  281. def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which):
  282. ''' Testing binary lookup in path '''
  283. oc_bin = '/usr/bin/oc'
  284. mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
  285. mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
  286. self.assertEqual(locate_oc_binary(), oc_bin)
  287. @unittest.skipIf(six.PY2, 'py3 test only')
  288. @mock.patch('shutil.which')
  289. @mock.patch('os.environ.get')
  290. def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which):
  291. ''' Testing binary lookup in /usr/local/bin '''
  292. oc_bin = '/usr/local/bin/oc'
  293. mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
  294. mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
  295. self.assertEqual(locate_oc_binary(), oc_bin)
  296. @unittest.skipIf(six.PY2, 'py3 test only')
  297. @mock.patch('shutil.which')
  298. @mock.patch('os.environ.get')
  299. def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which):
  300. ''' Testing binary lookup in ~/bin '''
  301. oc_bin = os.path.expanduser('~/bin/oc')
  302. mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
  303. mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
  304. self.assertEqual(locate_oc_binary(), oc_bin)