'Testing adding a serviceaccount'

@mock.patch('oc_serviceaccount.locate_oc_binary')
@mock.patch('oc_serviceaccount.Utils.create_tmpfile_copy')
@mock.patch('oc_serviceaccount.OCServiceAccount._run')
def test_adding_a_serviceaccount(self, mock_cmd, mock_tmpfile_copy, mock_oc_binary):

    params = {'kubeconfig': '/etc/origin/master/admin.kubeconfig', 'state': 'present', 'debug': False, 'name': 'testserviceaccountname', 'namespace': 'default', 'secrets': None, 'image_pull_secrets': None}
    valid_result_json = '{\n                                                "kind":    "ServiceAccount",\n                                                "apiVersion":    "v1",\n                                                "metadata":    {\n                                                                "name":    "testserviceaccountname",\n                                                                "namespace":    "default",\n                                                                "selfLink":    "/api/v1/namespaces/default/serviceaccounts/testserviceaccountname",\n                                                                "uid":    "4d8320c9-e66f-11e6-8edc-0eece8f2ce22",\n                                                                "resourceVersion":    "328450",\n                                                                "creationTimestamp":    "2017-01-29T22:07:19Z"\n                                                },\n                                                "secrets":    [\n                                                                {\n                                                                                "name":    "testserviceaccountname-dockercfg-4lqd0"\n                                                                },\n                                                                {\n                                                                                "name":    "testserviceaccountname-token-9h0ej"\n                                                                }\n                                                ],\n                                                "imagePullSecrets":    [\n                                                                {\n                                                                                "name":    "testserviceaccountname-dockercfg-4lqd0"\n                                                                }\n                                                ]\n                                }'
    mock_cmd.side_effect = [(1, '', 'Error    from    server:    serviceaccounts    "testserviceaccountname"    not    found'), (0, 'serviceaccount    "testserviceaccountname"    created', ''), (0, valid_result_json, '')]
    mock_oc_binary.side_effect = ['oc']
    mock_tmpfile_copy.side_effect = ['/tmp/mocked_kubeconfig']
    results = OCServiceAccount.run_ansible(params, False)
    self.assertTrue(results['changed'])
    self.assertEqual(results['results']['returncode'], 0)
    self.assertEqual(results['state'], 'present')
    mock_cmd.assert_has_calls([mock.call(['oc', 'get', 'sa', 'testserviceaccountname', '-o', 'json', '-n', 'default'], None), mock.call(['oc', 'create', '-f', mock.ANY, '-n', 'default'], None), mock.call(['oc', 'get', 'sa', 'testserviceaccountname', '-o', 'json', '-n', 'default'], None)])
