'Testing adding a secret to a service account'

@mock.patch('oc_serviceaccount_secret.locate_oc_binary')
@mock.patch('oc_serviceaccount_secret.Utils.create_tmpfile_copy')
@mock.patch('oc_serviceaccount_secret.Yedit._write')
@mock.patch('oc_serviceaccount_secret.OCServiceAccountSecret._run')
def test_adding_a_secret_to_a_serviceaccount(self, mock_cmd, mock_write, mock_tmpfile_copy, mock_oc_binary):

    params = {'state': 'present', 'namespace': 'default', 'secret': 'newsecret', 'service_account': 'builder', 'kubeconfig': '/etc/origin/master/admin.kubeconfig', 'debug': False}
    oc_get_sa_before = '{\n                                                "apiVersion":    "v1",\n                                                "imagePullSecrets":    [\n                                                                {\n                                                                                "name":    "builder-dockercfg-rsrua"\n                                                                }\n                                                ],\n                                                "kind":    "ServiceAccount",\n                                                "metadata":    {\n                                                                "name":    "builder",\n                                                                "namespace":    "default",\n                                                                "selfLink":    "/api/v1/namespaces/default/serviceaccounts/builder",\n                                                                "uid":    "cf47bca7-ebc4-11e6-b041-0ed9df7abc38",\n                                                                "resourceVersion":    "302879",\n                                                                "creationTimestamp":    "2017-02-05T17:02:00Z"\n                                                },\n                                                "secrets":    [\n                                                                {\n                                                                                "name":    "builder-dockercfg-rsrua"\n                                                                },\n                                                                {\n                                                                                "name":    "builder-token-akqxi"\n                                                                }\n\n                                                ]\n                                }\n                                '
    oc_get_sa_after = '{\n                                                "apiVersion":    "v1",\n                                                "imagePullSecrets":    [\n                                                                {\n                                                                                "name":    "builder-dockercfg-rsrua"\n                                                                }\n                                                ],\n                                                "kind":    "ServiceAccount",\n                                                "metadata":    {\n                                                                "name":    "builder",\n                                                                "namespace":    "default",\n                                                                "selfLink":    "/api/v1/namespaces/default/serviceaccounts/builder",\n                                                                "uid":    "cf47bca7-ebc4-11e6-b041-0ed9df7abc38",\n                                                                "resourceVersion":    "302879",\n                                                                "creationTimestamp":    "2017-02-05T17:02:00Z"\n                                                },\n                                                "secrets":    [\n                                                                {\n                                                                                "name":    "builder-dockercfg-rsrua"\n                                                                },\n                                                                {\n                                                                                "name":    "builder-token-akqxi"\n                                                                },\n                                                                {\n                                                                                "name":    "newsecret"\n                                                                }\n\n                                                ]\n                                }\n                                '
    builder_ryaml_file = "secrets:\n-    name:    builder-dockercfg-rsrua\n-    name:    builder-token-akqxi\n-    name:    newsecret\nkind:    ServiceAccount\nimagePullSecrets:\n-    name:    builder-dockercfg-rsrua\napiVersion:    v1\nmetadata:\n        name:    builder\n        namespace:    default\n        resourceVersion:    '302879'\n        creationTimestamp:    '2017-02-05T17:02:00Z'\n        selfLink:    /api/v1/namespaces/default/serviceaccounts/builder\n        uid:    cf47bca7-ebc4-11e6-b041-0ed9df7abc38\n"
    builder_pyyaml_file = "apiVersion:    v1\nimagePullSecrets:\n-    name:    builder-dockercfg-rsrua\nkind:    ServiceAccount\nmetadata:\n        creationTimestamp:    '2017-02-05T17:02:00Z'\n        name:    builder\n        namespace:    default\n        resourceVersion:    '302879'\n        selfLink:    /api/v1/namespaces/default/serviceaccounts/builder\n        uid:    cf47bca7-ebc4-11e6-b041-0ed9df7abc38\nsecrets:\n-    name:    builder-dockercfg-rsrua\n-    name:    builder-token-akqxi\n-    name:    newsecret\n"
    mock_cmd.side_effect = [(0, oc_get_sa_before, ''), (0, oc_get_sa_before, ''), (0, 'serviceaccount    "builder"    replaced', ''), (0, oc_get_sa_after, '')]
    mock_oc_binary.side_effect = ['oc']
    mock_tmpfile_copy.side_effect = ['/tmp/mocked_kubeconfig']
    results = OCServiceAccountSecret.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', 'builder', '-o', 'json', '-n', 'default'], None), mock.call(['oc', 'get', 'sa', 'builder', '-o', 'json', '-n', 'default'], None), mock.call(['oc', 'replace', '-f', mock.ANY, '-n', 'default'], None), mock.call(['oc', 'get', 'sa', 'builder', '-o', 'json', '-n', 'default'], None)])
    yaml_file = builder_pyyaml_file
    if (YAML_TYPE == 'ruamel'):
        yaml_file = builder_ryaml_file
    mock_write.assert_has_calls([mock.call(mock.ANY, yaml_file)])
