oc_secret.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 sys
  14. import unittest
  15. import mock
  16. # Removing invalid variable names for tests so that I can
  17. # keep them brief
  18. # pylint: disable=invalid-name,no-name-in-module
  19. # Disable import-error b/c our libraries aren't loaded in jenkins
  20. # pylint: disable=import-error,wrong-import-position
  21. # place class in our python path
  22. module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
  23. sys.path.insert(0, module_path)
  24. from oc_secret import OCSecret # noqa: E402
  25. class OCSecretTest(unittest.TestCase):
  26. '''
  27. Test class for OCSecret
  28. '''
  29. def setUp(self):
  30. ''' setup method will create a file and set to known configuration '''
  31. pass
  32. @mock.patch('oc_secret.OCSecret.openshift_cmd')
  33. def test_adding_a_secret(self, mock_openshift_cmd):
  34. ''' Testing adding a secret '''
  35. # Arrange
  36. # run_ansible input parameters
  37. params = {
  38. 'state': 'present',
  39. 'namespace': 'default',
  40. 'name': 'secretname',
  41. 'contents': [{
  42. 'path': "/tmp/somesecret.json",
  43. 'data': "{'one': 1, 'two': 2, 'three', 3}",
  44. }],
  45. 'decode': False,
  46. 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
  47. 'debug': False,
  48. 'files': None,
  49. 'delete_after': True,
  50. }
  51. # Return values of our mocked function call. These get returned once per call.
  52. mock_openshift_cmd.side_effect = [
  53. {
  54. "cmd": "/usr/bin/oc get secrets -o json secretname",
  55. "results": "",
  56. "returncode": 0,
  57. }, # oc output for first call to openshift_cmd (oc secrets get)
  58. {
  59. "cmd": "/usr/bin/oc secrets new secretname somesecret.json=/tmp/somesecret.json",
  60. "results": "",
  61. "returncode": 0,
  62. }, # oc output for second call to openshift_cmd (oc secrets new)
  63. ]
  64. # Act
  65. results = OCSecret.run_ansible(params, False)
  66. # Assert
  67. self.assertTrue(results['changed'])
  68. self.assertEqual(results['results']['returncode'], 0)
  69. self.assertEqual(results['state'], 'present')
  70. # Making sure our mock was called as we expected
  71. mock_openshift_cmd.assert_has_calls([
  72. mock.call(['get', 'secrets', '-o', 'json', 'secretname'], output=True),
  73. mock.call(['secrets', 'new', 'secretname', 'somesecret.json=/tmp/somesecret.json']),
  74. ])
  75. def tearDown(self):
  76. '''TearDown method'''
  77. pass
  78. if __name__ == "__main__":
  79. unittest.main()