test_oc_service.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. '''
  2. Unit tests for oc service
  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
  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_service import OCService, locate_oc_binary # noqa: E402
  18. # pylint: disable=too-many-public-methods
  19. class OCServiceTest(unittest.TestCase):
  20. '''
  21. Test class for OCService
  22. '''
  23. @mock.patch('oc_service.Utils.create_tmpfile_copy')
  24. @mock.patch('oc_service.OCService._run')
  25. def test_state_list(self, mock_cmd, mock_tmpfile_copy):
  26. ''' Testing a get '''
  27. params = {'name': 'router',
  28. 'namespace': 'default',
  29. 'ports': None,
  30. 'state': 'list',
  31. 'labels': None,
  32. 'clusterip': None,
  33. 'portalip': None,
  34. 'selector': None,
  35. 'session_affinity': None,
  36. 'service_type': None,
  37. 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
  38. 'debug': False}
  39. service = '''{
  40. "kind": "Service",
  41. "apiVersion": "v1",
  42. "metadata": {
  43. "name": "router",
  44. "namespace": "default",
  45. "selfLink": "/api/v1/namespaces/default/services/router",
  46. "uid": "fabd2440-e3d8-11e6-951c-0e3dd518cefa",
  47. "resourceVersion": "3206",
  48. "creationTimestamp": "2017-01-26T15:06:14Z",
  49. "labels": {
  50. "router": "router"
  51. }
  52. },
  53. "spec": {
  54. "ports": [
  55. {
  56. "name": "80-tcp",
  57. "protocol": "TCP",
  58. "port": 80,
  59. "targetPort": 80
  60. },
  61. {
  62. "name": "443-tcp",
  63. "protocol": "TCP",
  64. "port": 443,
  65. "targetPort": 443
  66. },
  67. {
  68. "name": "1936-tcp",
  69. "protocol": "TCP",
  70. "port": 1936,
  71. "targetPort": 1936
  72. },
  73. {
  74. "name": "5000-tcp",
  75. "protocol": "TCP",
  76. "port": 5000,
  77. "targetPort": 5000
  78. }
  79. ],
  80. "selector": {
  81. "router": "router"
  82. },
  83. "clusterIP": "172.30.129.161",
  84. "type": "ClusterIP",
  85. "sessionAffinity": "None"
  86. },
  87. "status": {
  88. "loadBalancer": {}
  89. }
  90. }'''
  91. mock_cmd.side_effect = [
  92. (0, service, '')
  93. ]
  94. mock_tmpfile_copy.side_effect = [
  95. '/tmp/mocked_kubeconfig',
  96. ]
  97. results = OCService.run_ansible(params, False)
  98. self.assertFalse(results['changed'])
  99. self.assertEqual(results['results']['results'][0]['metadata']['name'], 'router')
  100. @mock.patch('oc_service.Utils.create_tmpfile_copy')
  101. @mock.patch('oc_service.OCService._run')
  102. def test_create(self, mock_cmd, mock_tmpfile_copy):
  103. ''' Testing a create service '''
  104. params = {'name': 'router',
  105. 'namespace': 'default',
  106. 'ports': {'name': '9000-tcp',
  107. 'port': 9000,
  108. 'protocol': 'TCP',
  109. 'targetPOrt': 9000},
  110. 'state': 'present',
  111. 'labels': None,
  112. 'clusterip': None,
  113. 'portalip': None,
  114. 'selector': {'router': 'router'},
  115. 'session_affinity': 'ClientIP',
  116. 'service_type': 'ClusterIP',
  117. 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
  118. 'debug': False}
  119. service = '''{
  120. "kind": "Service",
  121. "apiVersion": "v1",
  122. "metadata": {
  123. "name": "router",
  124. "namespace": "default",
  125. "selfLink": "/api/v1/namespaces/default/services/router",
  126. "uid": "fabd2440-e3d8-11e6-951c-0e3dd518cefa",
  127. "resourceVersion": "3206",
  128. "creationTimestamp": "2017-01-26T15:06:14Z",
  129. "labels": {
  130. "router": "router"
  131. }
  132. },
  133. "spec": {
  134. "ports": [
  135. {
  136. "name": "80-tcp",
  137. "protocol": "TCP",
  138. "port": 80,
  139. "targetPort": 80
  140. },
  141. {
  142. "name": "443-tcp",
  143. "protocol": "TCP",
  144. "port": 443,
  145. "targetPort": 443
  146. },
  147. {
  148. "name": "1936-tcp",
  149. "protocol": "TCP",
  150. "port": 1936,
  151. "targetPort": 1936
  152. },
  153. {
  154. "name": "5000-tcp",
  155. "protocol": "TCP",
  156. "port": 5000,
  157. "targetPort": 5000
  158. }
  159. ],
  160. "selector": {
  161. "router": "router"
  162. },
  163. "clusterIP": "172.30.129.161",
  164. "type": "ClusterIP",
  165. "sessionAffinity": "None"
  166. },
  167. "status": {
  168. "loadBalancer": {}
  169. }
  170. }'''
  171. mock_cmd.side_effect = [
  172. (1, '', 'Error from server: services "router" not found'),
  173. (1, '', 'Error from server: services "router" not found'),
  174. (0, service, ''),
  175. (0, service, '')
  176. ]
  177. mock_tmpfile_copy.side_effect = [
  178. '/tmp/mocked_kubeconfig',
  179. ]
  180. results = OCService.run_ansible(params, False)
  181. self.assertTrue(results['changed'])
  182. self.assertTrue(results['results']['returncode'] == 0)
  183. self.assertEqual(results['results']['results'][0]['metadata']['name'], 'router')
  184. @unittest.skipIf(six.PY3, 'py2 test only')
  185. @mock.patch('os.path.exists')
  186. @mock.patch('os.environ.get')
  187. def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists):
  188. ''' Testing binary lookup fallback '''
  189. mock_env_get.side_effect = lambda _v, _d: ''
  190. mock_path_exists.side_effect = lambda _: False
  191. self.assertEqual(locate_oc_binary(), 'oc')
  192. @unittest.skipIf(six.PY3, 'py2 test only')
  193. @mock.patch('os.path.exists')
  194. @mock.patch('os.environ.get')
  195. def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists):
  196. ''' Testing binary lookup in path '''
  197. oc_bin = '/usr/bin/oc'
  198. mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
  199. mock_path_exists.side_effect = lambda f: f == oc_bin
  200. self.assertEqual(locate_oc_binary(), oc_bin)
  201. @unittest.skipIf(six.PY3, 'py2 test only')
  202. @mock.patch('os.path.exists')
  203. @mock.patch('os.environ.get')
  204. def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists):
  205. ''' Testing binary lookup in /usr/local/bin '''
  206. oc_bin = '/usr/local/bin/oc'
  207. mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
  208. mock_path_exists.side_effect = lambda f: f == oc_bin
  209. self.assertEqual(locate_oc_binary(), oc_bin)
  210. @unittest.skipIf(six.PY3, 'py2 test only')
  211. @mock.patch('os.path.exists')
  212. @mock.patch('os.environ.get')
  213. def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists):
  214. ''' Testing binary lookup in ~/bin '''
  215. oc_bin = os.path.expanduser('~/bin/oc')
  216. mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
  217. mock_path_exists.side_effect = lambda f: f == oc_bin
  218. self.assertEqual(locate_oc_binary(), oc_bin)
  219. @unittest.skipIf(six.PY2, 'py3 test only')
  220. @mock.patch('shutil.which')
  221. @mock.patch('os.environ.get')
  222. def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which):
  223. ''' Testing binary lookup fallback '''
  224. mock_env_get.side_effect = lambda _v, _d: ''
  225. mock_shutil_which.side_effect = lambda _f, path=None: None
  226. self.assertEqual(locate_oc_binary(), 'oc')
  227. @unittest.skipIf(six.PY2, 'py3 test only')
  228. @mock.patch('shutil.which')
  229. @mock.patch('os.environ.get')
  230. def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which):
  231. ''' Testing binary lookup in path '''
  232. oc_bin = '/usr/bin/oc'
  233. mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
  234. mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
  235. self.assertEqual(locate_oc_binary(), oc_bin)
  236. @unittest.skipIf(six.PY2, 'py3 test only')
  237. @mock.patch('shutil.which')
  238. @mock.patch('os.environ.get')
  239. def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which):
  240. ''' Testing binary lookup in /usr/local/bin '''
  241. oc_bin = '/usr/local/bin/oc'
  242. mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
  243. mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
  244. self.assertEqual(locate_oc_binary(), oc_bin)
  245. @unittest.skipIf(six.PY2, 'py3 test only')
  246. @mock.patch('shutil.which')
  247. @mock.patch('os.environ.get')
  248. def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which):
  249. ''' Testing binary lookup in ~/bin '''
  250. oc_bin = os.path.expanduser('~/bin/oc')
  251. mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
  252. mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
  253. self.assertEqual(locate_oc_binary(), oc_bin)