From aa97c28b6a9b468498fffe565d314b07141f163b Mon Sep 17 00:00:00 2001 From: Thomas Wiest Date: Sun, 29 Jan 2017 14:47:07 -0500 Subject: Changed lib_openshift to use real temporary files. --- roles/lib_openshift/src/test/unit/oc_secret.py | 33 ++++++++++------------ .../src/test/unit/oc_serviceaccount_secret.py | 10 +++---- 2 files changed, 20 insertions(+), 23 deletions(-) (limited to 'roles/lib_openshift/src/test') diff --git a/roles/lib_openshift/src/test/unit/oc_secret.py b/roles/lib_openshift/src/test/unit/oc_secret.py index 835918b95..c81f0514b 100755 --- a/roles/lib_openshift/src/test/unit/oc_secret.py +++ b/roles/lib_openshift/src/test/unit/oc_secret.py @@ -35,8 +35,9 @@ class OCSecretTest(unittest.TestCase): ''' setup method will create a file and set to known configuration ''' pass - @mock.patch('oc_secret.OCSecret.openshift_cmd') - def test_adding_a_secret(self, mock_openshift_cmd): + @mock.patch('oc_secret.Utils._write') + @mock.patch('oc_secret.OCSecret._run') + def test_adding_a_secret(self, mock_cmd, mock_write): ''' Testing adding a secret ''' # Arrange @@ -45,10 +46,10 @@ class OCSecretTest(unittest.TestCase): params = { 'state': 'present', 'namespace': 'default', - 'name': 'secretname', + 'name': 'testsecretname', 'contents': [{ 'path': "/tmp/somesecret.json", - 'data': "{'one': 1, 'two': 2, 'three', 3}", + 'data': "{'one': 1, 'two': 2, 'three': 3}", }], 'decode': False, 'kubeconfig': '/etc/origin/master/admin.kubeconfig', @@ -58,17 +59,9 @@ class OCSecretTest(unittest.TestCase): } # Return values of our mocked function call. These get returned once per call. - mock_openshift_cmd.side_effect = [ - { - "cmd": "/usr/bin/oc get secrets -o json secretname", - "results": "", - "returncode": 0, - }, # oc output for first call to openshift_cmd (oc secrets get) - { - "cmd": "/usr/bin/oc secrets new secretname somesecret.json=/tmp/somesecret.json", - "results": "", - "returncode": 0, - }, # oc output for second call to openshift_cmd (oc secrets new) + mock_cmd.side_effect = [ + (1, '', 'Error from server: secrets "testsecretname" not found'), + (0, 'secret/testsecretname', ''), ] # Act @@ -80,9 +73,13 @@ class OCSecretTest(unittest.TestCase): self.assertEqual(results['state'], 'present') # Making sure our mock was called as we expected - mock_openshift_cmd.assert_has_calls([ - mock.call(['get', 'secrets', 'secretname', '-o', 'json'], output=True), - mock.call(['secrets', 'new', 'secretname', 'somesecret.json=/tmp/somesecret.json']), + mock_cmd.assert_has_calls([ + mock.call(['oc', '-n', 'default', 'get', 'secrets', 'testsecretname', '-o', 'json'], None), + mock.call(['oc', '-n', 'default', 'secrets', 'new', 'testsecretname', mock.ANY], None), + ]) + + mock_write.assert_has_calls([ + mock.call(mock.ANY, "{'one': 1, 'two': 2, 'three': 3}"), ]) def tearDown(self): diff --git a/roles/lib_openshift/src/test/unit/oc_serviceaccount_secret.py b/roles/lib_openshift/src/test/unit/oc_serviceaccount_secret.py index 342da961b..08fc9f6df 100755 --- a/roles/lib_openshift/src/test/unit/oc_serviceaccount_secret.py +++ b/roles/lib_openshift/src/test/unit/oc_serviceaccount_secret.py @@ -149,18 +149,18 @@ metadata: mock_cmd.assert_has_calls([ mock.call(['oc', '-n', 'default', 'get', 'sa', 'builder', '-o', 'json'], None), mock.call(['oc', '-n', 'default', 'get', 'sa', 'builder', '-o', 'json'], None), - mock.call(['oc', '-n', 'default', 'replace', '-f', '/tmp/builder'], None), + mock.call(['oc', '-n', 'default', 'replace', '-f', mock.ANY], None), mock.call(['oc', '-n', 'default', 'get', 'sa', 'builder', '-o', 'json'], None) ]) mock_write.assert_has_calls([ - mock.call('/tmp/builder', builder_yaml_file) + mock.call(mock.ANY, builder_yaml_file) ]) @mock.patch('oc_serviceaccount_secret.Yedit._write') @mock.patch('oc_serviceaccount_secret.OCServiceAccountSecret._run') def test_removing_a_secret_to_a_serviceaccount(self, mock_cmd, mock_write): - ''' Testing adding a secret to a service account ''' + ''' Testing removing a secret to a service account ''' # Arrange @@ -241,11 +241,11 @@ metadata: mock_cmd.assert_has_calls([ mock.call(['oc', '-n', 'default', 'get', 'sa', 'builder', '-o', 'json'], None), mock.call(['oc', '-n', 'default', 'get', 'sa', 'builder', '-o', 'json'], None), - mock.call(['oc', '-n', 'default', 'replace', '-f', '/tmp/builder'], None), + mock.call(['oc', '-n', 'default', 'replace', '-f', mock.ANY], None), ]) mock_write.assert_has_calls([ - mock.call('/tmp/builder', builder_yaml_file) + mock.call(mock.ANY, builder_yaml_file) ]) def tearDown(self): -- cgit v1.2.3