def test_adding_a_secret(self, mock_cmd, mock_write):
        ''' Testing adding a secret '''

        # Arrange

        # run_ansible input parameters
        params = {
            'state':
            'present',
            'namespace':
            'default',
            'name':
            'testsecretname',
            'contents': [{
                'path': "/tmp/somesecret.json",
                'data': "{'one': 1, 'two': 2, 'three': 3}",
            }],
            'decode':
            False,
            'kubeconfig':
            '/etc/origin/master/admin.kubeconfig',
            'debug':
            False,
            'files':
            None,
            'delete_after':
            True,
        }

        # Return values of our mocked function call. These get returned once per call.
        mock_cmd.side_effect = [
            (1, '', 'Error from server: secrets "testsecretname" not found'),
            (0, 'secret/testsecretname', ''),
        ]

        # Act
        results = OCSecret.run_ansible(params, False)

        # Assert
        self.assertTrue(results['changed'])
        self.assertEqual(results['results']['returncode'], 0)
        self.assertEqual(results['state'], 'present')

        # Making sure our mock was called as we expected
        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 test_adding_a_secret(self, mock_cmd, mock_write, mock_tmpfile_copy, mock_oc_binary):
        ''' Testing adding a secret '''

        # Arrange

        # run_ansible input parameters
        params = {
            'state': 'present',
            'namespace': 'default',
            'name': 'testsecretname',
            'type': 'Opaque',
            'contents': [{
                'path': "/tmp/somesecret.json",
                'data': "{'one': 1, 'two': 2, 'three': 3}",
            }],
            'decode': False,
            'kubeconfig': '/etc/origin/master/admin.kubeconfig',
            'debug': False,
            'files': None,
            'delete_after': True,
        }

        # Return values of our mocked function call. These get returned once per call.
        mock_cmd.side_effect = [
            (1, '', 'Error from server: secrets "testsecretname" not found'),
            (0, 'secret/testsecretname', ''),
        ]

        mock_oc_binary.side_effect = [
            'oc'
        ]

        mock_tmpfile_copy.side_effect = [
            '/tmp/mocked_kubeconfig',
        ]

        # Act
        results = OCSecret.run_ansible(params, False)

        # Assert
        self.assertTrue(results['changed'])
        self.assertEqual(results['results']['returncode'], 0)
        self.assertEqual(results['state'], 'present')

        # Making sure our mock was called as we expected
        mock_cmd.assert_has_calls([
            mock.call(['oc', 'get', 'secrets', 'testsecretname', '-o', 'json', '-n', 'default'], None),
            mock.call(['oc', 'secrets', 'new', 'testsecretname', '--type=Opaque', mock.ANY, '-n', 'default'], None),
        ])

        mock_write.assert_has_calls([
            mock.call(mock.ANY, "{'one': 1, 'two': 2, 'three': 3}"),
        ])
Exemple #3
0
    def test_adding_a_secret(self, mock_openshift_cmd):
        ''' Testing adding a secret '''

        # Arrange

        # run_ansible input parameters
        params = {
            'state':
            'present',
            'namespace':
            'default',
            'name':
            'secretname',
            'contents': [{
                'path': "/tmp/somesecret.json",
                'data': "{'one': 1, 'two': 2, 'three', 3}",
            }],
            'decode':
            False,
            'kubeconfig':
            '/etc/origin/master/admin.kubeconfig',
            'debug':
            False,
            'files':
            None,
            'delete_after':
            True,
        }

        # 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)
        ]

        # Act
        results = OCSecret.run_ansible(params, False)

        # Assert
        self.assertTrue(results['changed'])
        self.assertEqual(results['results']['returncode'], 0)
        self.assertEqual(results['state'], 'present')

        # Making sure our mock was called as we expected
        mock_openshift_cmd.assert_has_calls([
            mock.call(['get', 'secrets', '-o', 'json', 'secretname'],
                      output=True),
            mock.call([
                'secrets', 'new', 'secretname',
                'somesecret.json=/tmp/somesecret.json'
            ]),
        ])