'Testing getting a route'

@mock.patch('oc_route.locate_oc_binary')
@mock.patch('oc_route.Utils.create_tmpfile_copy')
@mock.patch('oc_route.Yedit._write')
@mock.patch('oc_route.OCRoute._run')
def test_create_route(self, mock_cmd, mock_write, mock_tmpfile_copy, mock_oc_binary):

    params = {'kubeconfig': '/etc/origin/master/admin.kubeconfig', 'state': 'present', 'debug': False, 'name': 'test', 'namespace': 'default', 'tls_termination': 'edge', 'dest_cacert_path': None, 'cacert_path': None, 'cert_path': None, 'key_path': None, 'dest_cacert_content': None, 'cacert_content': 'testing', 'cert_content': 'testing', 'key_content': 'testing', 'service_name': 'testservice', 'host': 'test.openshift.com', 'wildcard_policy': None, 'weight': None, 'port': None}
    route_result = '{\n                                                                "apiVersion":    "v1",\n                                                                "kind":    "Route",\n                                                                "metadata":    {\n                                                                                "creationTimestamp":    "2017-02-07T20:55:10Z",\n                                                                                "name":    "test",\n                                                                                "namespace":    "default",\n                                                                                "resourceVersion":    "517745",\n                                                                                "selfLink":    "/oapi/v1/namespaces/default/routes/test",\n                                                                                "uid":    "b6f25898-ed77-11e6-9755-0e737db1e63a"\n                                                                },\n                                                                "spec":    {\n                                                                                "host":    "test.openshift.com",\n                                                                                "tls":    {\n                                                                                                "caCertificate":    "testing",\n                                                                                                "certificate":    "testing",\n                                                                                                "key":    "testing",\n                                                                                                "termination":    "edge"\n                                                                                },\n                                                                                "to":    {\n                                                                                                "kind":    "Service",\n                                                                                                "name":    "testservice",\n                                                                                                "weight":    100\n                                                                                },\n                                                                                "wildcardPolicy":    "None"\n                                                                },\n                                                                "status":    {\n                                                                                "ingress":    [\n                                                                                                {\n                                                                                                                "conditions":    [\n                                                                                                                                {\n                                                                                                                                                "lastTransitionTime":    "2017-02-07T20:55:10Z",\n                                                                                                                                                "status":    "True",\n                                                                                                                                                "type":    "Admitted"\n                                                                                                                                }\n                                                                                                                ],\n                                                                                                                "host":    "test.openshift.com",\n                                                                                                                "routerName":    "router",\n                                                                                                                "wildcardPolicy":    "None"\n                                                                                                }\n                                                                                ]\n                                                                }\n                                                }'
    test_route = 'kind:    Route\nspec:\n        tls:\n                caCertificate:    testing\n                termination:    edge\n                certificate:    testing\n                key:    testing\n        to:\n                kind:    Service\n                name:    testservice\n                weight:    100\n        host:    test.openshift.com\n        wildcardPolicy:    None\napiVersion:    v1\nmetadata:\n        namespace:    default\n        name:    test\n'
    mock_cmd.side_effect = [(1, '', 'Error    from    server:    routes    "test"    not    found'), (1, '', 'Error    from    server:    routes    "test"    not    found'), (0, 'route    "test"    created', ''), (0, route_result, '')]
    mock_oc_binary.side_effect = ['oc']
    mock_tmpfile_copy.side_effect = ['/tmp/mock.kubeconfig']
    mock_write.assert_has_calls = [mock.call('/tmp/test', test_route)]
    results = OCRoute.run_ansible(params, False)
    self.assertTrue(results['changed'])
    self.assertEqual(results['state'], 'present')
    self.assertEqual(results['results']['results'][0]['metadata']['name'], 'test')
    mock_cmd.assert_has_calls([mock.call(['oc', 'get', 'route', 'test', '-o', 'json', '-n', 'default'], None), mock.call(['oc', 'create', '-f', mock.ANY, '-n', 'default'], None), mock.call(['oc', 'get', 'route', 'test', '-o', 'json', '-n', 'default'], None)])
