test_oc_secret.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #!/usr/bin/env python2
  2. '''
  3. Unit tests for oc secret
  4. '''
  5. # To run:
  6. # ./oc_secret.py
  7. #
  8. # .
  9. # Ran 1 test in 0.002s
  10. #
  11. # OK
  12. import os
  13. import six
  14. import sys
  15. import unittest
  16. import mock
  17. # Removing invalid variable names for tests so that I can
  18. # keep them brief
  19. # pylint: disable=invalid-name,no-name-in-module
  20. # Disable import-error b/c our libraries aren't loaded in jenkins
  21. # pylint: disable=import-error,wrong-import-position
  22. # place class in our python path
  23. module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
  24. sys.path.insert(0, module_path)
  25. from oc_secret import OCSecret, locate_oc_binary # noqa: E402
  26. class OCSecretTest(unittest.TestCase):
  27. '''
  28. Test class for OCSecret
  29. '''
  30. def setUp(self):
  31. ''' setup method will create a file and set to known configuration '''
  32. pass
  33. @mock.patch('oc_secret.locate_oc_binary')
  34. @mock.patch('oc_secret.Utils.create_tmpfile_copy')
  35. @mock.patch('oc_secret.Utils._write')
  36. @mock.patch('oc_secret.OCSecret._run')
  37. def test_adding_a_secret(self, mock_cmd, mock_write, mock_tmpfile_copy, mock_oc_binary):
  38. ''' Testing adding a secret '''
  39. # Arrange
  40. # run_ansible input parameters
  41. params = {
  42. 'state': 'present',
  43. 'namespace': 'default',
  44. 'name': 'testsecretname',
  45. 'contents': [{
  46. 'path': "/tmp/somesecret.json",
  47. 'data': "{'one': 1, 'two': 2, 'three': 3}",
  48. }],
  49. 'decode': False,
  50. 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
  51. 'debug': False,
  52. 'files': None,
  53. 'delete_after': True,
  54. }
  55. # Return values of our mocked function call. These get returned once per call.
  56. mock_cmd.side_effect = [
  57. (1, '', 'Error from server: secrets "testsecretname" not found'),
  58. (0, 'secret/testsecretname', ''),
  59. ]
  60. mock_oc_binary.side_effect = [
  61. 'oc'
  62. ]
  63. mock_tmpfile_copy.side_effect = [
  64. '/tmp/mocked_kubeconfig',
  65. ]
  66. # Act
  67. results = OCSecret.run_ansible(params, False)
  68. # Assert
  69. self.assertTrue(results['changed'])
  70. self.assertEqual(results['results']['returncode'], 0)
  71. self.assertEqual(results['state'], 'present')
  72. # Making sure our mock was called as we expected
  73. mock_cmd.assert_has_calls([
  74. mock.call(['oc', '-n', 'default', 'get', 'secrets', 'testsecretname', '-o', 'json'], None),
  75. mock.call(['oc', '-n', 'default', 'secrets', 'new', 'testsecretname', mock.ANY], None),
  76. ])
  77. mock_write.assert_has_calls([
  78. mock.call(mock.ANY, "{'one': 1, 'two': 2, 'three': 3}"),
  79. ])
  80. @unittest.skipIf(six.PY3, 'py2 test only')
  81. @mock.patch('os.path.exists')
  82. @mock.patch('os.environ.get')
  83. def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists):
  84. ''' Testing binary lookup fallback '''
  85. mock_env_get.side_effect = lambda _v, _d: ''
  86. mock_path_exists.side_effect = lambda _: False
  87. self.assertEqual(locate_oc_binary(), 'oc')
  88. @unittest.skipIf(six.PY3, 'py2 test only')
  89. @mock.patch('os.path.exists')
  90. @mock.patch('os.environ.get')
  91. def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists):
  92. ''' Testing binary lookup in path '''
  93. oc_bin = '/usr/bin/oc'
  94. mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
  95. mock_path_exists.side_effect = lambda f: f == oc_bin
  96. self.assertEqual(locate_oc_binary(), oc_bin)
  97. @unittest.skipIf(six.PY3, 'py2 test only')
  98. @mock.patch('os.path.exists')
  99. @mock.patch('os.environ.get')
  100. def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists):
  101. ''' Testing binary lookup in /usr/local/bin '''
  102. oc_bin = '/usr/local/bin/oc'
  103. mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
  104. mock_path_exists.side_effect = lambda f: f == oc_bin
  105. self.assertEqual(locate_oc_binary(), oc_bin)
  106. @unittest.skipIf(six.PY3, 'py2 test only')
  107. @mock.patch('os.path.exists')
  108. @mock.patch('os.environ.get')
  109. def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists):
  110. ''' Testing binary lookup in ~/bin '''
  111. oc_bin = os.path.expanduser('~/bin/oc')
  112. mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
  113. mock_path_exists.side_effect = lambda f: f == oc_bin
  114. self.assertEqual(locate_oc_binary(), oc_bin)
  115. @unittest.skipIf(six.PY2, 'py3 test only')
  116. @mock.patch('shutil.which')
  117. @mock.patch('os.environ.get')
  118. def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which):
  119. ''' Testing binary lookup fallback '''
  120. mock_env_get.side_effect = lambda _v, _d: ''
  121. mock_shutil_which.side_effect = lambda _f, path=None: None
  122. self.assertEqual(locate_oc_binary(), 'oc')
  123. @unittest.skipIf(six.PY2, 'py3 test only')
  124. @mock.patch('shutil.which')
  125. @mock.patch('os.environ.get')
  126. def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which):
  127. ''' Testing binary lookup in path '''
  128. oc_bin = '/usr/bin/oc'
  129. mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
  130. mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
  131. self.assertEqual(locate_oc_binary(), oc_bin)
  132. @unittest.skipIf(six.PY2, 'py3 test only')
  133. @mock.patch('shutil.which')
  134. @mock.patch('os.environ.get')
  135. def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which):
  136. ''' Testing binary lookup in /usr/local/bin '''
  137. oc_bin = '/usr/local/bin/oc'
  138. mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
  139. mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
  140. self.assertEqual(locate_oc_binary(), oc_bin)
  141. @unittest.skipIf(six.PY2, 'py3 test only')
  142. @mock.patch('shutil.which')
  143. @mock.patch('os.environ.get')
  144. def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which):
  145. ''' Testing binary lookup in ~/bin '''
  146. oc_bin = os.path.expanduser('~/bin/oc')
  147. mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
  148. mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
  149. self.assertEqual(locate_oc_binary(), oc_bin)
  150. def tearDown(self):
  151. '''TearDown method'''
  152. pass
  153. if __name__ == "__main__":
  154. unittest.main()