oc_label.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #!/usr/bin/env python2
  2. '''
  3. Unit tests for oc label
  4. '''
  5. # To run
  6. # python -m unittest version
  7. #
  8. # .
  9. # Ran 1 test in 0.597s
  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
  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_label import OCLabel # noqa: E402
  25. class OCLabelTest(unittest.TestCase):
  26. '''
  27. Test class for OCLabel
  28. '''
  29. def setUp(self):
  30. ''' setup method will create a file and set to known configuration '''
  31. pass
  32. @mock.patch('oc_label.Utils.create_tmpfile_copy')
  33. @mock.patch('oc_label.OCLabel._run')
  34. def test_state_list(self, mock_cmd, mock_tmpfile_copy):
  35. ''' Testing a label list '''
  36. params = {'name': 'default',
  37. 'namespace': 'default',
  38. 'labels': None,
  39. 'state': 'list',
  40. 'kind': 'namespace',
  41. 'selector': None,
  42. 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
  43. 'debug': False}
  44. ns = '''{
  45. "kind": "Namespace",
  46. "apiVersion": "v1",
  47. "metadata": {
  48. "name": "default",
  49. "selfLink": "/api/v1/namespaces/default",
  50. "uid": "c45b9547-e3d3-11e6-ba9c-0eece8f2ce22",
  51. "resourceVersion": "403024",
  52. "creationTimestamp": "2017-01-26T14:28:55Z",
  53. "labels": {
  54. "storage_pv_quota": "False"
  55. },
  56. "annotations": {
  57. "openshift.io/node-selector": "",
  58. "openshift.io/sa.initialized-roles": "true",
  59. "openshift.io/sa.scc.mcs": "s0:c1,c0",
  60. "openshift.io/sa.scc.supplemental-groups": "1000000000/10000",
  61. "openshift.io/sa.scc.uid-range": "1000000000/10000"
  62. }
  63. },
  64. "spec": {
  65. "finalizers": [
  66. "kubernetes",
  67. "openshift.io/origin"
  68. ]
  69. },
  70. "status": {
  71. "phase": "Active"
  72. }
  73. }'''
  74. mock_cmd.side_effect = [
  75. (0, ns, ''),
  76. ]
  77. mock_tmpfile_copy.side_effect = [
  78. '/tmp/mocked_kubeconfig',
  79. ]
  80. results = OCLabel.run_ansible(params, False)
  81. self.assertFalse(results['changed'])
  82. self.assertTrue(results['results']['labels'] == [{'storage_pv_quota': 'False'}])
  83. @mock.patch('oc_label.Utils.create_tmpfile_copy')
  84. @mock.patch('oc_label.OCLabel._run')
  85. def test_state_present(self, mock_cmd, mock_tmpfile_copy):
  86. ''' Testing a label list '''
  87. params = {'name': 'default',
  88. 'namespace': 'default',
  89. 'labels': [
  90. {'key': 'awesomens', 'value': 'testinglabel'},
  91. {'key': 'storage_pv_quota', 'value': 'False'}
  92. ],
  93. 'state': 'present',
  94. 'kind': 'namespace',
  95. 'selector': None,
  96. 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
  97. 'debug': False}
  98. ns = '''{
  99. "kind": "Namespace",
  100. "apiVersion": "v1",
  101. "metadata": {
  102. "name": "default",
  103. "selfLink": "/api/v1/namespaces/default",
  104. "uid": "c45b9547-e3d3-11e6-ba9c-0eece8f2ce22",
  105. "resourceVersion": "403024",
  106. "creationTimestamp": "2017-01-26T14:28:55Z",
  107. "labels": {
  108. "storage_pv_quota": "False"
  109. },
  110. "annotations": {
  111. "openshift.io/node-selector": "",
  112. "openshift.io/sa.initialized-roles": "true",
  113. "openshift.io/sa.scc.mcs": "s0:c1,c0",
  114. "openshift.io/sa.scc.supplemental-groups": "1000000000/10000",
  115. "openshift.io/sa.scc.uid-range": "1000000000/10000"
  116. }
  117. },
  118. "spec": {
  119. "finalizers": [
  120. "kubernetes",
  121. "openshift.io/origin"
  122. ]
  123. },
  124. "status": {
  125. "phase": "Active"
  126. }
  127. }'''
  128. ns1 = '''{
  129. "kind": "Namespace",
  130. "apiVersion": "v1",
  131. "metadata": {
  132. "name": "default",
  133. "selfLink": "/api/v1/namespaces/default",
  134. "uid": "c45b9547-e3d3-11e6-ba9c-0eece8f2ce22",
  135. "resourceVersion": "403024",
  136. "creationTimestamp": "2017-01-26T14:28:55Z",
  137. "labels": {
  138. "storage_pv_quota": "False",
  139. "awesomens": "testinglabel"
  140. },
  141. "annotations": {
  142. "openshift.io/node-selector": "",
  143. "openshift.io/sa.initialized-roles": "true",
  144. "openshift.io/sa.scc.mcs": "s0:c1,c0",
  145. "openshift.io/sa.scc.supplemental-groups": "1000000000/10000",
  146. "openshift.io/sa.scc.uid-range": "1000000000/10000"
  147. }
  148. },
  149. "spec": {
  150. "finalizers": [
  151. "kubernetes",
  152. "openshift.io/origin"
  153. ]
  154. },
  155. "status": {
  156. "phase": "Active"
  157. }
  158. }'''
  159. mock_cmd.side_effect = [
  160. (0, ns, ''),
  161. (0, '', ''),
  162. (0, ns1, ''),
  163. ]
  164. mock_tmpfile_copy.side_effect = [
  165. '/tmp/mocked_kubeconfig',
  166. ]
  167. results = OCLabel.run_ansible(params, False)
  168. self.assertTrue(results['changed'])
  169. self.assertTrue(results['results']['results']['labels'][0] ==
  170. {'storage_pv_quota': 'False', 'awesomens': 'testinglabel'})
  171. def tearDown(self):
  172. '''TearDown method'''
  173. pass
  174. if __name__ == "__main__":
  175. unittest.main()