# Path: k8s/client.py
# class NotFound(K8sClientException):
#     """The resource was not found, and the operation could not be completed"""
# 
# Path: k8s/models/common.py
# class ObjectMeta(Model):
#     annotations = Field(dict)
#     creationTimestamp = ReadOnlyField(datetime.datetime)
#     deletionGracePeriodSeconds = ReadOnlyField(int)
#     deletionTimestamp = ReadOnlyField(datetime.datetime)
#     finalizers = ListField(six.text_type)
#     generateName = Field(six.text_type)
#     generation = ReadOnlyField(int)
#     labels = Field(dict)
#     name = Field(six.text_type)
#     namespace = Field(six.text_type, "default")
#     ownerReferences = ListField(OwnerReference)
#     resourceVersion = ReadOnlyField(six.text_type)
#     selfLink = ReadOnlyField(six.text_type)
#     uid = ReadOnlyField(six.text_type)
# 
# Path: k8s/models/networking_v1_ingress.py
# class Ingress(Model):
#     class Meta:
#         list_url = "/apis/networking.k8s.io/v1/ingresses"
#         url_template = "/apis/networking.k8s.io/v1/namespaces/{namespace}/ingresses/{name}"
#         watch_list_url = "/apis/networking.k8s.io/v1/ingresses?watch=true"
# 
#     metadata = Field(ObjectMeta)
#     spec = Field(IngressSpec)
#     status = Field(IngressStatus)
# 
# class IngressSpec(Model):
#     defaultBackend = Field(IngressBackend)
#     ingressClassName = Field(six.text_type)
#     rules = ListField(IngressRule)
#     tls = ListField(IngressTLS)
# 
# class IngressRule(Model):
#     host = Field(six.text_type)
#     http = Field(HTTPIngressRuleValue)
# 
# class IngressBackend(Model):
#     resource = Field(TypedLocalObjectReference)
#     service = Field(IngressServiceBackend)
# 
# class HTTPIngressPath(Model):
#     backend = Field(IngressBackend)
#     path = Field(six.text_type)
#     pathType = Field(six.text_type)
# 
# class HTTPIngressRuleValue(Model):
#     paths = ListField(HTTPIngressPath)
# 
# class IngressServiceBackend(Model):
#     name = Field(six.text_type)
#     port = Field(ServiceBackendPort)
# 
# class ServiceBackendPort(Model):
#     name = Field(six.text_type)
#     number = Field(int)


import mock
import pytest
from k8s.client import NotFound
from k8s.models.common import ObjectMeta
from k8s.models.networking_v1_ingress import Ingress, IngressSpec, IngressRule, IngressBackend, HTTPIngressPath, \
    HTTPIngressRuleValue, IngressServiceBackend, ServiceBackendPort

    def test_updated_if_exists(self, get, put):
        mock_response = _create_mock_response()
        get.return_value = mock_response
        ingress = _create_default_ingress()

        from_api = Ingress.get_or_create(metadata=ingress.metadata, spec=ingress.spec)
        assert not from_api._new
        assert from_api.spec.rules[0].host == "dummy.example.com"
        call_params = from_api.as_dict()
        put.return_value.json.return_value = call_params

        from_api.save()
        pytest.helpers.assert_any_call(put, _uri(NAMESPACE, NAME), call_params)


def _create_mock_response():
    mock_response = mock.Mock()
    mock_response.json.return_value = {
        "apiVersion": "networking.k8s.io/v1",
        "kind": "Ingress",
        "metadata": {
            "creationTimestamp": "2016-11-23T13:43:42Z",
            "generation": 7,
            "labels": {
                "test": "true"
            },
            "name": NAME,
            "namespace": NAMESPACE,
            "resourceVersion": "96758807",

# Path: k8s/client.py
# class NotFound(K8sClientException):
#     """The resource was not found, and the operation could not be completed"""
# 
# Path: k8s/models/common.py
# class ObjectMeta(Model):
#     annotations = Field(dict)
#     creationTimestamp = ReadOnlyField(datetime.datetime)
#     deletionGracePeriodSeconds = ReadOnlyField(int)
#     deletionTimestamp = ReadOnlyField(datetime.datetime)
#     finalizers = ListField(six.text_type)
#     generateName = Field(six.text_type)
#     generation = ReadOnlyField(int)
#     labels = Field(dict)
#     name = Field(six.text_type)
#     namespace = Field(six.text_type, "default")
#     ownerReferences = ListField(OwnerReference)
#     resourceVersion = ReadOnlyField(six.text_type)
#     selfLink = ReadOnlyField(six.text_type)
#     uid = ReadOnlyField(six.text_type)
# 
# Path: k8s/models/networking_v1_ingress.py
# class Ingress(Model):
#     class Meta:
#         list_url = "/apis/networking.k8s.io/v1/ingresses"
#         url_template = "/apis/networking.k8s.io/v1/namespaces/{namespace}/ingresses/{name}"
#         watch_list_url = "/apis/networking.k8s.io/v1/ingresses?watch=true"
# 
#     metadata = Field(ObjectMeta)
#     spec = Field(IngressSpec)
#     status = Field(IngressStatus)
# 
# class IngressSpec(Model):
#     defaultBackend = Field(IngressBackend)
#     ingressClassName = Field(six.text_type)
#     rules = ListField(IngressRule)
#     tls = ListField(IngressTLS)
# 
# class IngressRule(Model):
#     host = Field(six.text_type)
#     http = Field(HTTPIngressRuleValue)
# 
# class IngressBackend(Model):
#     resource = Field(TypedLocalObjectReference)
#     service = Field(IngressServiceBackend)
# 
# class HTTPIngressPath(Model):
#     backend = Field(IngressBackend)
#     path = Field(six.text_type)
#     pathType = Field(six.text_type)
# 
# class HTTPIngressRuleValue(Model):
#     paths = ListField(HTTPIngressPath)
# 
# class IngressServiceBackend(Model):
#     name = Field(six.text_type)
#     port = Field(ServiceBackendPort)
# 
# class ServiceBackendPort(Model):
#     name = Field(six.text_type)
#     number = Field(int)


# Path: tests/k8s/test_networking_v1_ingress.py
import mock
import pytest
from k8s.client import NotFound
from k8s.models.common import ObjectMeta
from k8s.models.networking_v1_ingress import Ingress, IngressSpec, IngressRule, IngressBackend, HTTPIngressPath, \
    HTTPIngressRuleValue, IngressServiceBackend, ServiceBackendPort


    def test_updated_if_exists(self, get, put):
        mock_response = _create_mock_response()
        get.return_value = mock_response
        ingress = _create_default_ingress()

        from_api = Ingress.get_or_create(metadata=ingress.metadata, spec=ingress.spec)
        assert not from_api._new
        assert from_api.spec.rules[0].host == "dummy.example.com"
        call_params = from_api.as_dict()
        put.return_value.json.return_value = call_params

        from_api.save()
        pytest.helpers.assert_any_call(put, _uri(NAMESPACE, NAME), call_params)


def _create_mock_response():
    mock_response = mock.Mock()
    mock_response.json.return_value = {
        "apiVersion": "networking.k8s.io/v1",
        "kind": "Ingress",
        "metadata": {
            "creationTimestamp": "2016-11-23T13:43:42Z",
            "generation": 7,
            "labels": {
                "test": "true"
            },
            "name": NAME,
            "namespace": NAMESPACE,
            "resourceVersion": "96758807",

            "selfLink": _uri(NAMESPACE, NAME),