import pytest

from jsonschema import ValidationError

from enos_scalewayinstances import Configuration
from enos_scalewayinstances.configuration import PortToOpen


class TestSchema:
    def test_basic_schema(self):
        Configuration.from_settings(
            profile="steeltest", ports_to_open=[[5000, 5000, "server"]]
        ).add_machine(zone="fr-par-1", roles=["server"], number=1).finalize()

    def test_failed_schema(self):
        with pytest.raises(ValidationError):
            # Wrong ports configuration
            Configuration.from_settings(
                profile="steeltest", ports_to_open=[[5000, "server"]]
            ).add_machine(zone="fr-par-1", roles=["server"], number=1).finalize()

    def test_ports_to_open_schema(self):
        Configuration.from_settings(
            profile="steeltest", ports_to_open=[[5000, 5000, "server", "udp"]]
        ).add_machine(zone="fr-par-1", roles=["server"], number=1).finalize()

        with pytest.raises(ValidationError):
            # Not a valid protocol
            Configuration.from_settings(
                profile="steeltest", ports_to_open=[[5000, 5000, "server", "NOT_A_PROTOCOL"]]
            ).add_machine(zone="fr-par-1", roles=["server"], number=1).finalize()

    def test_not_enough_parameters_for_ports(self):
        with pytest.raises(ValidationError):
            # Not a valid protocol
            Configuration.from_settings(
                profile="steeltest", ports_to_open=[[5000, 5000]]
            ).add_machine(zone="fr-par-1", roles=["server"], number=1).finalize()

class TestConfiguration:
    def test_ports_to_open(self):
        conf = Configuration.from_settings(
            profile="steeltest", ports_to_open=[[5000, 5000, "server", "udp"]]
        ).add_machine(zone="fr-par-1", roles=["server"], number=1).finalize()

        pconf_list = conf.get_ports_configurations()

        assert type(pconf_list) is list
        assert len(pconf_list) == 1

        assert type(pconf_list[0]) is PortToOpen