<commit_before>import json
import pytest
from rancher import ApiError
from .common import random_str
from .conftest import wait_for


@pytest.mark.skip(reason="cluster-defaults disabled")
def test_generic_initial_defaults(admin_mc):
    cclient = admin_mc.client
    schema_defaults = {}
    setting_defaults = {}

    data = cclient.schema.types['cluster'].resourceFields
    default = data["enableNetworkPolicy"]["default"]

    for name in cclient.schema.types['cluster'].resourceFields.keys():
        if name == "enableNetworkPolicy":
            schema_defaults["enableNetworkPolicy"] = default

    for name in cclient.schema.types['rancherKubernetesEngineConfig'] \
            .resourceFields.keys():
        if name == "ignoreDockerVersion":
            schema_defaults["ignoreDockerVersion"] = cclient.schema. \
                types["rancherKubernetesEngineConfig"]. \
                resourceFields["ignoreDockerVersion"]. \
                data_dict()["default"]

    setting = cclient.list_setting(name="cluster-defaults")
    data = json.loads(setting['data'][0]['default'])

    setting_defaults["enableNetworkPolicy"] = data["enableNetworkPolicy"]
    setting_defaults["ignoreDockerVersion"] = \
        data["rancherKubernetesEngineConfig"]["ignoreDockerVersion"]

    assert schema_defaults == setting_defaults


def test_generic_initial_conditions(admin_mc, remove_resource):
    cluster = admin_mc.client.create_cluster(
        name=random_str(), amazonElasticContainerServiceConfig={
            "accessKey": "asdfsd"})
    remove_resource(cluster)

    assert len(cluster.conditions) == 3
    assert cluster.conditions[0].type == 'Pending'
    assert cluster.conditions[0].status == 'True'

    assert cluster.conditions[1].type == 'Provisioned'
    assert cluster.conditions[1].status == 'Unknown'

    assert cluster.conditions[2].type == 'Waiting'
    assert cluster.conditions[2].status == 'Unknown'

    assert 'exportYaml' not in cluster.actions


def test_eks_cluster_immutable_subnets(admin_mc, remove_resource):
    cluster = admin_mc.client.create_cluster(
        name=random_str(), amazonElasticContainerServiceConfig={
            "accessKey": "asdfsd",
            "secretKey": "verySecretKey",
            "subnets": [
                "subnet-045bfaeca7d3f1cb3",
                "subnet-02388a166136f98c4"
            ]})
    remove_resource(cluster)

    # try to edit cluster subnets
    with pytest.raises(ApiError) as e:
        admin_mc.client.update_by_id_cluster(
            id=cluster.id,
            amazonElasticContainerServiceConfig={
                 "accessKey": "asdfsd",
                 "secretKey": "verySecretKey",
                 "subnets": [
                     "subnet-045bfaeca7d3f1cb3"
                 ]})

    assert e.value.error.status == 422
    assert e.value.error.message == 'cannot modify EKS subnets after creation'

    # tests updates still work
    new = admin_mc.client.update_by_id_cluster(
       id=cluster.id,
       name=cluster.name,
       description="update",
       amazonElasticContainerServiceConfig={
           # required field when updating KE clusters
           "driverName": "amazonelasticcontainerservice",<commit_msg>EKS subnet test failing because cluster slow to create<commit_after>import json
import pytest
from rancher import ApiError
from .common import random_str
from .conftest import wait_for, wait_for_condition


@pytest.mark.skip(reason="cluster-defaults disabled")
def test_generic_initial_defaults(admin_mc):
    cclient = admin_mc.client
    schema_defaults = {}
    setting_defaults = {}

    data = cclient.schema.types['cluster'].resourceFields
    default = data["enableNetworkPolicy"]["default"]

    for name in cclient.schema.types['cluster'].resourceFields.keys():
        if name == "enableNetworkPolicy":
            schema_defaults["enableNetworkPolicy"] = default

    for name in cclient.schema.types['rancherKubernetesEngineConfig'] \
            .resourceFields.keys():
        if name == "ignoreDockerVersion":
            schema_defaults["ignoreDockerVersion"] = cclient.schema. \
                types["rancherKubernetesEngineConfig"]. \
                resourceFields["ignoreDockerVersion"]. \
                data_dict()["default"]

    setting = cclient.list_setting(name="cluster-defaults")
    data = json.loads(setting['data'][0]['default'])

    setting_defaults["enableNetworkPolicy"] = data["enableNetworkPolicy"]
    setting_defaults["ignoreDockerVersion"] = \
        data["rancherKubernetesEngineConfig"]["ignoreDockerVersion"]

    assert schema_defaults == setting_defaults


def test_generic_initial_conditions(admin_mc, remove_resource):
    cluster = admin_mc.client.create_cluster(
        name=random_str(), amazonElasticContainerServiceConfig={
            "accessKey": "asdfsd"})
    remove_resource(cluster)

    assert len(cluster.conditions) == 3
    assert cluster.conditions[0].type == 'Pending'
    assert cluster.conditions[0].status == 'True'

    assert cluster.conditions[1].type == 'Provisioned'
    assert cluster.conditions[1].status == 'Unknown'

    assert cluster.conditions[2].type == 'Waiting'
    assert cluster.conditions[2].status == 'Unknown'

    assert 'exportYaml' not in cluster.actions


def test_eks_cluster_immutable_subnets(admin_mc, remove_resource):
    cluster = admin_mc.client.create_cluster(
        name=random_str(), amazonElasticContainerServiceConfig={
            "accessKey": "asdfsd",
            "secretKey": "verySecretKey",
            "subnets": [
                "subnet-045bfaeca7d3f1cb3",
                "subnet-02388a166136f98c4"
            ]})
    remove_resource(cluster)
    wait_for_condition(
        'DefaultProjectCreated', 'True', admin_mc.client, cluster)

    # try to edit cluster subnets
    with pytest.raises(ApiError) as e:
        admin_mc.client.update_by_id_cluster(
            id=cluster.id,
            amazonElasticContainerServiceConfig={
                 "accessKey": "asdfsd",
                 "secretKey": "verySecretKey",
                 "subnets": [
                     "subnet-045bfaeca7d3f1cb3"
                 ]})

    assert e.value.error.status == 422
    assert e.value.error.message == 'cannot modify EKS subnets after creation'

    # tests updates still work
    new = admin_mc.client.update_by_id_cluster(
       id=cluster.id,
       name=cluster.name,
       description="update",
       amazonElasticContainerServiceConfig={
           # required field when updating KE clusters
           "driverName": "amazonelasticcontainerservice",
{'fraction_non_alphanumeric': 0.10640558591378263, 'fraction_numerical': 0.011839708561020037, 'mean_word_length': 3.10785536159601, 'pattern_counts': {'":': 20, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1783251', 'n_tokens_mistral': 1885, 'n_tokens_neox': 1812, 'n_words': 333}