<commit_before>import pytest

import salt.cloud.clouds.openstack as openstack

# from tests.support.helpers import TstSuiteLoggingHandler
from tests.support.mock import call, patch


@pytest.fixture
def configure_loader_modules():
    return {
        openstack: {
            "__opts__": {},
        }
    }


@pytest.fixture
def expected_config_stuff():
    vm = {"asdf": ...}

    with patch("copy.deepcopy", autospec=True, return_value=42), patch.dict(
        openstack.__opts__, {"foo": "bar"}
    ):
        yield vm


def test_when_not_HAS_NETADDR_then_ignore_cidr_should_be_False():

    with patch("salt.cloud.clouds.openstack.HAS_NETADDR", False):
        result = openstack.ignore_cidr("fnord", "fnord")

    assert result is False


def test_when_getting_cloud_config_values_expected_args_should_be_provided():
    expected_vm = "whatever"
    expected_calls = call(
        "ignore_cidr", expected_vm, openstack.__opts__, default="", search_global=False
    )


@pytest.mark.parametrize(
    "comment,example_ip,ignored_cidr,expected",
    [
        (
            "ip is in ignore_cidr",
            "203.0.113.1",
            "203.0.113.0/24",
            True,
        ),
        (
            "ip is not in ignore_cidr",
            "192.0.2.1",
            "203.0.113.0/24",
            False,
        ),
        (
            "ignore_cidr is empty",
            "192.0.2.1",
            "",
            False,
        ),
        (
            "ignore_cidr is False",
            "192.0.2.1",
            False,
            False,
        ),
        (
            "ignore_cidr is None",
            "192.0.2.1",
            None,
            False,
        ),
    ],
)
def test_when_ignore_cidr_is_configured_and_ip_is_provided_result_is_expected(
    comment, example_ip, ignored_cidr, expected
):
    with patch(
        "salt.config.get_cloud_config_value",
        autospec=True,
        return_value=ignored_cidr,
    ):
        result = openstack.ignore_cidr("fnord", example_ip)

    assert result is expected


@pytest.mark.parametrize(
    "comment,example_ips,ignored_cidr,expected",
    [
        (
            "ignore_cidr matches first 2 ips, expected value will be first ip that doesn't match cidr.",
            [
                "203.0.113.1",
                "203.0.113.2",
                "192.0.2.1",
                "192.0.2.2",
            ],
            "203.0.113.0/24",
            "192.0.2.1",
        ),
        (
            "ignore_cidr matches 2nd 2 IPs, expected value will be first ip in list. ",
            [
                "203.0.113.1",
                "203.0.113.2",
                "192.0.2.1",
                "192.0.2.2",
            ],
            "192.0.2.0/24",
            "203.0.113.1",
        ),
        (
            "ignore_cidr doesn't match any IPs, expected value will be first ip in list.",
            [
                "203.0.113.1",
                "203.0.113.2",
                "192.0.2.1",
                "192.0.2.2",
            ],
            "198.51.100.0/24",
            "203.0.113.1",
        ),
        (
            "ignore_cidr matches all IPs, expected value will be False.",
            [
                "203.0.113.1",
                "203.0.113.2",
                "203.0.113.3",
                "203.0.113.4",
            ],
            "203.0.113.0/24",
            False,
        ),
        (
            "When ignore_cidr is not set, return first ip",
            [
                "203.0.113.1",
                "203.0.113.2",
                "192.0.2.1",
                "192.0.2.2",
            ],
            None,
            "203.0.113.1",
        ),
    ],
)
def test_preferred_ip_function_returns_expected(
    comment, example_ips, ignored_cidr, expected
):
    with patch(
        "salt.config.get_cloud_config_value",
        autospec=True,
        return_value=ignored_cidr,
    ):
        result = openstack.preferred_ip("fnord", example_ips)

    assert result is expected


# fill out all of these tests, refactor some and then also add a changelog entry<commit_msg>Remove unneeded comments from test_openstack.py<commit_after>import pytest

import salt.cloud.clouds.openstack as openstack
from tests.support.mock import call, patch


@pytest.fixture
def configure_loader_modules():
    return {
        openstack: {
            "__opts__": {},
        }
    }


@pytest.fixture
def expected_config_stuff():
    vm = {"asdf": ...}

    with patch("copy.deepcopy", autospec=True, return_value=42), patch.dict(
        openstack.__opts__, {"foo": "bar"}
    ):
        yield vm


def test_when_not_HAS_NETADDR_then_ignore_cidr_should_be_False():

    with patch("salt.cloud.clouds.openstack.HAS_NETADDR", False):
        result = openstack.ignore_cidr("fnord", "fnord")

    assert result is False


def test_when_getting_cloud_config_values_expected_args_should_be_provided():
    expected_vm = "whatever"
    expected_calls = call(
        "ignore_cidr", expected_vm, openstack.__opts__, default="", search_global=False
    )


@pytest.mark.parametrize(
    "comment,example_ip,ignored_cidr,expected",
    [
        (
            "ip is in ignore_cidr",
            "203.0.113.1",
            "203.0.113.0/24",
            True,
        ),
        (
            "ip is not in ignore_cidr",
            "192.0.2.1",
            "203.0.113.0/24",
            False,
        ),
        (
            "ignore_cidr is empty",
            "192.0.2.1",
            "",
            False,
        ),
        (
            "ignore_cidr is False",
            "192.0.2.1",
            False,
            False,
        ),
        (
            "ignore_cidr is None",
            "192.0.2.1",
            None,
            False,
        ),
    ],
)
def test_when_ignore_cidr_is_configured_and_ip_is_provided_result_is_expected(
    comment, example_ip, ignored_cidr, expected
):
    with patch(
        "salt.config.get_cloud_config_value",
        autospec=True,
        return_value=ignored_cidr,
    ):
        result = openstack.ignore_cidr("fnord", example_ip)

    assert result is expected


@pytest.mark.parametrize(
    "comment,example_ips,ignored_cidr,expected",
    [
        (
            "ignore_cidr matches first 2 ips, expected value will be first ip that doesn't match cidr.",
            [
                "203.0.113.1",
                "203.0.113.2",
                "192.0.2.1",
                "192.0.2.2",
            ],
            "203.0.113.0/24",
            "192.0.2.1",
        ),
        (
            "ignore_cidr matches 2nd 2 IPs, expected value will be first ip in list. ",
            [
                "203.0.113.1",
                "203.0.113.2",
                "192.0.2.1",
                "192.0.2.2",
            ],
            "192.0.2.0/24",
            "203.0.113.1",
        ),
        (
            "ignore_cidr doesn't match any IPs, expected value will be first ip in list.",
            [
                "203.0.113.1",
                "203.0.113.2",
                "192.0.2.1",
                "192.0.2.2",
            ],
            "198.51.100.0/24",
            "203.0.113.1",
        ),
        (
            "ignore_cidr matches all IPs, expected value will be False.",
            [
                "203.0.113.1",
                "203.0.113.2",
                "203.0.113.3",
                "203.0.113.4",
            ],
            "203.0.113.0/24",
            False,
        ),
        (
            "When ignore_cidr is not set, return first ip",
            [
                "203.0.113.1",
                "203.0.113.2",
                "192.0.2.1",
                "192.0.2.2",
            ],
            None,
            "203.0.113.1",
        ),
    ],
)
def test_preferred_ip_function_returns_expected(
    comment, example_ips, ignored_cidr, expected
):
    with patch(
        "salt.config.get_cloud_config_value",
        autospec=True,
        return_value=ignored_cidr,
    ):
        result = openstack.preferred_ip("fnord", example_ips)

    assert result is expected
{'fraction_non_alphanumeric': 0.12408305358697004, 'fraction_numerical': 0.06713912719134651, 'mean_word_length': 1.382701421800948, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 70, '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': '5775532', 'n_tokens_mistral': 2915, 'n_tokens_neox': 2641, 'n_words': 452}