<commit_before>from .common import random_str, auth_check
import pytest


def test_ingress_fields(admin_pc_client):
    auth_check(admin_pc_client.schema, 'ingress', 'crud', {
        'namespaceId': 'cr',
        'projectId': 'cr',
        'rules': 'cru',
        'tls': 'cru',
        'ingressClassName': 'cru',
        'backend': 'cru',
        'defaultBackend': 'cru',
        'publicEndpoints': 'r',
        'status': 'r',
    })

    auth_check(admin_pc_client.schema, 'ingressBackend', '', {
        'serviceId': 'cru',
        'service': 'cru',
        'targetPort': 'cru',
        'resource': 'cru',
        'workloadIds': 'cru',
    })

    auth_check(admin_pc_client.schema, 'ingressRule', '', {
        'host': 'cru',
        'paths': 'cru',
    })

    auth_check(admin_pc_client.schema, 'httpIngressPath', '', {
        'resource': 'cru',
        'pathType': 'cru',
        'path': 'cru',
        'serviceId': 'cru',
        'service': 'cru',
        'targetPort': 'cru',
        'workloadIds': 'cru',
    })


@pytest.mark.skip(reason='skipping for now, enable with ingress 1.22 fix')
def test_ingress(admin_pc, admin_cc_client):
    client = admin_pc.client

    ns = admin_cc_client.create_namespace(name=random_str(),
                                          projectId=admin_pc.project.id)

    name = random_str()
    workload = client.create_workload(
        name=name,
        namespaceId=ns.id,
        scale=1,
        containers=[{
            'name': 'one',
            'image': 'nginx',
        }])

    name = random_str() + "." + random_str()
    ingress = client.create_ingress(name=name,
                                    namespaceId=ns.id,
                                    rules=[{
                                        'host': "foo.com",
                                        'paths': [
                                            {
                                                'path': '/',
                                                'targetPort': 80,
                                                'workloadIds':
                                                [workload.id],
                                            },
                                        ]},
                                    ])

    assert len(ingress.rules) == 1
    assert ingress.rules[0].host == "foo.com"
    path = ingress.rules[0].paths[0]
    assert path.path == '/'
    assert path.targetPort == 80
    assert path.workloadIds == [workload.id]
    assert path.serviceId is None

    client.delete(ns)


@pytest.mark.skip(reason='skipping for now, enable with ingress 1.22 fix')
def test_ingress_rules_same_hostPortPath(admin_pc, admin_cc_client):
    client = admin_pc.client

    ns = admin_cc_client.create_namespace(name=random_str(),
                                          projectId=admin_pc.project.id)

    name = random_str()
    workload1 = client.create_workload(
        name=name,
        namespaceId=ns.id,
        scale=1,
        containers=[{
            'name': 'one',
            'image': 'nginx',
        }])

    name = random_str()
    workload2 = client.create_workload(
        name=name,
        namespaceId=ns.id,
        scale=1,<commit_msg>Unskip ingress integration tests<commit_after>from .common import random_str, auth_check


def test_ingress_fields(admin_pc_client):
    auth_check(admin_pc_client.schema, 'ingress', 'crud', {
        'namespaceId': 'cr',
        'projectId': 'cr',
        'rules': 'cru',
        'tls': 'cru',
        'ingressClassName': 'cru',
        'backend': 'cru',
        'defaultBackend': 'cru',
        'publicEndpoints': 'r',
        'status': 'r',
    })

    auth_check(admin_pc_client.schema, 'ingressBackend', '', {
        'serviceId': 'cru',
        'service': 'cru',
        'targetPort': 'cru',
        'resource': 'cru',
        'workloadIds': 'cru',
    })

    auth_check(admin_pc_client.schema, 'ingressRule', '', {
        'host': 'cru',
        'paths': 'cru',
    })

    auth_check(admin_pc_client.schema, 'httpIngressPath', '', {
        'resource': 'cru',
        'pathType': 'cru',
        'path': 'cru',
        'serviceId': 'cru',
        'service': 'cru',
        'targetPort': 'cru',
        'workloadIds': 'cru',
    })


def test_ingress(admin_pc, admin_cc_client):
    client = admin_pc.client

    ns = admin_cc_client.create_namespace(name=random_str(),
                                          projectId=admin_pc.project.id)

    name = random_str()
    workload = client.create_workload(
        name=name,
        namespaceId=ns.id,
        scale=1,
        containers=[{
            'name': 'one',
            'image': 'nginx',
        }])

    name = random_str() + "." + random_str()
    ingress = client.create_ingress(name=name,
                                    namespaceId=ns.id,
                                    rules=[{
                                        'host': "foo.com",
                                        'paths': [
                                            {
                                                'path': '/',
                                                'targetPort': 80,
                                                'workloadIds':
                                                [workload.id],
                                            },
                                        ]},
                                    ])

    assert len(ingress.rules) == 1
    assert ingress.rules[0].host == "foo.com"
    path = ingress.rules[0].paths[0]
    assert path.path == '/'
    assert path.targetPort == 80
    assert path.workloadIds == [workload.id]
    assert path.serviceId is None

    client.delete(ns)


def test_ingress_rules_same_hostPortPath(admin_pc, admin_cc_client):
    client = admin_pc.client

    ns = admin_cc_client.create_namespace(name=random_str(),
                                          projectId=admin_pc.project.id)

    name = random_str()
    workload1 = client.create_workload(
        name=name,
        namespaceId=ns.id,
        scale=1,
        containers=[{
            'name': 'one',
            'image': 'nginx',
        }])

    name = random_str()
    workload2 = client.create_workload(
        name=name,
        namespaceId=ns.id,
        scale=1,
{'fraction_non_alphanumeric': 0.13107957263594322, 'fraction_numerical': 0.005102854409185138, 'mean_word_length': 1.4234930448222565, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 44, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1872425', 'n_tokens_mistral': 1806, 'n_tokens_neox': 1732, 'n_words': 310}