Exemplo n.º 1
0
    def test(self):
        import toml

        c = KiwiConfig()
        version = toml.load("./pyproject.toml")["tool"]["poetry"]["version"]

        assert c == KiwiConfig.from_default()

        assert c.version == version
        assert len(c.shells) == 1
        assert c.shells[0] == Path("/bin/bash")
        assert c.projects == []
        assert c.environment == {}
        assert c.storage.directory == Path("/var/local/kiwi")
        assert c.network.name == "kiwi_hub"
        assert c.network.cidr == IPv4Network("10.22.46.0/24")

        kiwi_dict = {
            "version": version,
            "shells": ["/bin/bash"],
            "storage": {
                "directory": "/var/local/kiwi"
            },
            "network": {
                "name": "kiwi_hub",
                "cidr": "10.22.46.0/24",
            },
        }
        assert c.kiwi_dict == kiwi_dict

        assert c.kiwi_yml == YAML().dump_kiwi_yml(kiwi_dict)
Exemplo n.º 2
0
    def test_empty(self):
        c = KiwiConfig(projects=None)

        assert c == KiwiConfig(projects=[])
        assert c.projects == []

        assert c.get_project_config("invalid") is None
Exemplo n.º 3
0
    def test_list(self):
        c = KiwiConfig(environment=[])

        assert c.environment == {}

        c = KiwiConfig(environment=[
            "variable=value",
        ])

        assert len(c.environment) == 1
        assert "variable" in c.environment
        assert c.environment["variable"] == "value"

        c = KiwiConfig(environment=[
            "variable",
        ])

        assert len(c.environment) == 1
        assert "variable" in c.environment
        assert c.environment["variable"] is None

        c = KiwiConfig(environment=[
            123,
        ])

        assert len(c.environment) == 1
        assert "123" in c.environment
        assert c.environment["123"] is None
Exemplo n.º 4
0
    def test_coercible(self):
        c = KiwiConfig(projects="project")

        assert c == KiwiConfig(projects=["project"])

        assert len(c.projects) == 1
        p = c.projects[0]
        assert p.name == "project"
        assert p.enabled
        assert p.override_storage is None
Exemplo n.º 5
0
    def test_dict(self):
        c = KiwiConfig(projects={"name": "project"})

        assert c == KiwiConfig(projects=[{"name": "project"}])

        assert len(c.projects) == 1
        p = c.projects[0]
        assert p.name == "project"
        assert p.enabled
        assert p.override_storage is None
Exemplo n.º 6
0
    def test_list(self):
        c = KiwiConfig(shells=["/bin/sh", "sh"])

        assert len(c.shells) == 2
        assert c.shells[0] == Path("/bin/sh")
        assert c.shells[1] == Path("sh")

        c = KiwiConfig(shells=["/bin/bash"])

        assert len(c.shells) == 1
        assert c.shells[0] == Path("/bin/bash")
Exemplo n.º 7
0
    def test_coercible(self):
        c = KiwiConfig(shells="/bin/bash")

        assert c == KiwiConfig(shells=Path("/bin/bash"))

        assert len(c.shells) == 1
        assert c.shells[0] == Path("/bin/bash")

        c = KiwiConfig(shells=123)

        assert len(c.shells) == 1
        assert c.shells[0] == Path("123")
Exemplo n.º 8
0
    def test_dict(self):
        c = KiwiConfig(environment={})

        assert c.environment == {}

        kiwi_dict = {"variable": "value"}
        c = KiwiConfig(environment=kiwi_dict)

        assert len(c.environment) == 1
        assert "variable" in c.environment
        assert c.environment["variable"] == "value"

        assert c.kiwi_dict["environment"] == kiwi_dict
Exemplo n.º 9
0
    def test_dict(self):
        kiwi_dict = {
            "name": "test_hub",
            "cidr": "1.2.3.4/32",
        }
        c = KiwiConfig(network=kiwi_dict)

        assert c == KiwiConfig(network={
            "name": "TEST_HUB",
            "cidr": "1.2.3.4/32",
        })

        assert c.network.name == "test_hub"
        assert c.network.cidr == IPv4Network("1.2.3.4/32")
        assert c.network.kiwi_dict == kiwi_dict
Exemplo n.º 10
0
    def test_valid(self):
        c = KiwiConfig(version="0.0.0")
        assert c.version == "0.0.0"

        c = KiwiConfig(version="0.0")
        assert c.version == "0.0"

        c = KiwiConfig(version="0")
        assert c.version == "0"

        c = KiwiConfig(version=1.0)
        assert c.version == "1.0"

        c = KiwiConfig(version=1)
        assert c.version == "1"
Exemplo n.º 11
0
class TestDefault:
    cfg = KiwiConfig()

    def test_example(self):
        p = Project(
            directory=Path("example/hello_world"),
            parent_instance=None,
        )

        ss = p.services

        assert len(ss.content) == 5

        s = ss.content[0]

        assert s.name == "greeter"

        ss2 = p.services.filter_existing(["nonexistent"])

        assert len(ss2.content) == 0

    def test_empty(self):
        p = Project(
            directory=Path("nonexistent"),
            parent_instance=None,
        )

        with pytest.raises(FileNotFoundError) as exc_info:
            _ = p.services

        assert exc_info.value.filename == f"nonexistent/{COMPOSE_FILE_NAME}"
Exemplo n.º 12
0
    def test_uncoercible(self):
        with pytest.raises(ValidationError) as exc_info:
            KiwiConfig(shells=UnCoercible())

        assert len(exc_info.value.errors()) == 1
        error = exc_info.value.errors()[0]
        assert error[
            "msg"] == "Invalid 'KiwiConfig'.'shells' Format: UnCoercible()"
        assert error["type"] == "value_error.invalidformat"

        with pytest.raises(ValidationError) as exc_info:
            KiwiConfig(shells=["/bin/bash", UnCoercible()])

        assert len(exc_info.value.errors()) == 1
        error = exc_info.value.errors()[0]
        assert error["msg"] == "value is not a valid path"
        assert error["type"] == "type_error.path"
Exemplo n.º 13
0
    def test_reserved_name(self):
        with pytest.raises(ValidationError) as exc_info:
            KiwiConfig(projects={"name": "config"})

        assert len(exc_info.value.errors()) == 1
        error = exc_info.value.errors()[0]
        assert error["msg"] == "Project name 'config' is reserved!"
        assert error["type"] == "value_error.projectnamereserved"
Exemplo n.º 14
0
    def test_invalid(self):
        with pytest.raises(ValidationError) as exc_info:
            KiwiConfig(network=True)

        assert len(exc_info.value.errors()) == 1
        error = exc_info.value.errors()[0]
        assert error["msg"] == "Invalid 'KiwiConfig'.'network' Format: True"
        assert error["type"] == "value_error.invalidformat"
Exemplo n.º 15
0
    def test_invalid(self):
        with pytest.raises(ValidationError) as exc_info:
            KiwiConfig(storage=True)

        assert len(exc_info.value.errors()) == 1
        error = exc_info.value.errors()[0]
        assert error["msg"] == "Invalid 'StorageConfig' Format: '{}'"
        assert error["type"] == "value_error.invalidformat"
Exemplo n.º 16
0
    def test_empty(self):
        with pytest.raises(ValidationError) as exc_info:
            KiwiConfig(network=None)

        assert len(exc_info.value.errors()) == 1
        error = exc_info.value.errors()[0]
        assert error["msg"] == "Member 'KiwiConfig'.'network' is required!"
        assert error["type"] == "value_error.missingmember"
Exemplo n.º 17
0
    def test_invalid_dict(self):
        with pytest.raises(ValidationError) as exc_info:
            KiwiConfig(storage={"random key": "random value"})

        assert len(exc_info.value.errors()) == 1
        error = exc_info.value.errors()[0]
        assert error[
            "msg"] == "Invalid 'StorageConfig' Format: \"{'random key': 'random value'}\""
        assert error["type"] == "value_error.invalidformat"
Exemplo n.º 18
0
    def test_long(self):
        kiwi_dict = {
            "name": "project",
            "enabled": False,
            "override_storage": {
                "directory": "/test/directory"
            },
        }
        c = KiwiConfig(projects=[kiwi_dict])

        assert len(c.projects) == 1
        p = c.projects[0]
        assert p.name == "project"
        assert p == c.get_project_config("project")
        assert not p.enabled
        assert p.override_storage is not None

        assert c.kiwi_dict["projects"][0] == kiwi_dict
Exemplo n.º 19
0
    def test_uncoercible(self):
        with pytest.raises(ValidationError) as exc_info:
            KiwiConfig(environment=UnCoercible())

        assert len(exc_info.value.errors()) == 1
        error = exc_info.value.errors()[0]
        assert error[
            "msg"] == "Invalid 'KiwiConfig'.'environment' Format: UnCoercible()"
        assert error["type"] == "value_error.invalidformat"

        with pytest.raises(ValidationError) as exc_info:
            KiwiConfig(environment=["valid", UnCoercible()])

        assert len(exc_info.value.errors()) == 1
        error = exc_info.value.errors()[0]
        assert error[
            "msg"] == "Invalid 'KiwiConfig'.'environment' Format: None"
        assert error["type"] == "value_error.invalidformat"
Exemplo n.º 20
0
    def test_uncoercible(self):
        with pytest.raises(ValidationError) as exc_info:
            KiwiConfig(projects=["valid", UnCoercible()])

        assert len(exc_info.value.errors()) == 1
        error = exc_info.value.errors()[0]
        assert error[
            "msg"] == "Invalid 'KiwiConfig'.'projects' Format: ['valid', UnCoercible()]"
        assert error["type"] == "value_error.invalidformat"

        with pytest.raises(ValidationError) as exc_info:
            KiwiConfig(projects=UnCoercible())

        assert len(exc_info.value.errors()) == 1
        error = exc_info.value.errors()[0]
        assert error[
            "msg"] == "Invalid 'KiwiConfig'.'projects' Format: UnCoercible()"
        assert error["type"] == "value_error.invalidformat"
Exemplo n.º 21
0
    def test_invalid_dict(self):
        with pytest.raises(ValidationError) as exc_info:
            KiwiConfig(network={"name": "test_hub"})

        assert len(exc_info.value.errors()) == 1
        error = exc_info.value.errors()[0]
        assert error["msg"] == "field required"
        assert error["type"] == "value_error.missing"

        with pytest.raises(ValidationError) as exc_info:
            KiwiConfig(network={
                "name": "test_hub",
                "cidr": "1.2.3.4/123",
            })

        assert len(exc_info.value.errors()) == 1
        error = exc_info.value.errors()[0]
        assert error["msg"] == "value is not a valid IPv4 network"
        assert error["type"] == "value_error.ipv4network"
Exemplo n.º 22
0
    def test_invalid(self):
        # definitely not a version
        with pytest.raises(ValidationError) as exc_info:
            KiwiConfig(version="dnaf")

        assert len(exc_info.value.errors()) == 1
        error = exc_info.value.errors()[0]
        assert error["msg"].find("string does not match regex") != -1
        assert error["type"] == "value_error.str.regex"

        # almost a version
        with pytest.raises(ValidationError) as exc_info:
            c = KiwiConfig(version="0.0.0alpha")
            print(c.version)

        assert len(exc_info.value.errors()) == 1
        error = exc_info.value.errors()[0]
        assert error["msg"].find("string does not match regex") != -1
        assert error["type"] == "value_error.str.regex"
Exemplo n.º 23
0
    def test_invalid_dict(self):
        with pytest.raises(ValidationError) as exc_info:
            KiwiConfig(
                projects={
                    "random key 1": "random value 1",
                    "random key 2": "random value 2",
                })

        assert len(exc_info.value.errors()) == 1
        error = exc_info.value.errors()[0]
        assert error["msg"] == "Invalid 'ProjectConfig' Format: " \
                               "{'random key 1': 'random value 1', 'random key 2': 'random value 2'}"
        assert error["type"] == "value_error.invalidformat"
Exemplo n.º 24
0
    def test_storage_list(self):
        kiwi_dict = {
            "name": "project",
            "enabled": False,
            "override_storage": ["/test/directory"],
        }
        c = KiwiConfig(projects=[kiwi_dict])

        assert len(c.projects) == 1
        p = c.projects[0]
        assert p.name == "project"
        assert not p.enabled
        assert p.override_storage is not None
Exemplo n.º 25
0
    def test_storage_invalid(self):
        kiwi_dict = {
            "name": "project",
            "enabled": False,
            "override_storage": True,
        }
        with pytest.raises(ValidationError) as exc_info:
            KiwiConfig(projects=[kiwi_dict])

        assert len(exc_info.value.errors()) == 1
        error = exc_info.value.errors()[0]
        assert error["msg"] == "Invalid 'StorageConfig' Format: '{}'"
        assert error["type"] == "value_error.invalidformat"
Exemplo n.º 26
0
    def test_coercible(self):
        c = KiwiConfig(environment="variable=value")

        assert len(c.environment) == 1
        assert "variable" in c.environment
        assert c.environment["variable"] == "value"

        c = KiwiConfig(environment="variable")

        assert len(c.environment) == 1
        assert "variable" in c.environment
        assert c.environment["variable"] is None

        c = KiwiConfig(environment=123)

        assert len(c.environment) == 1
        assert "123" in c.environment
        assert c.environment["123"] is None

        c = KiwiConfig(environment=123.4)

        assert len(c.environment) == 1
        assert "123.4" in c.environment
        assert c.environment["123.4"] is None
Exemplo n.º 27
0
    def test_short(self):
        kiwi_dict = {
            "project": False,
        }
        c = KiwiConfig(projects=[kiwi_dict])

        assert len(c.projects) == 1
        p = c.projects[0]
        assert p.name == "project"
        assert not p.enabled
        assert p.override_storage is None

        resulting_kiwi_dict = {
            "name": "project",
            "enabled": False,
        }
        assert p.kiwi_dict == resulting_kiwi_dict
Exemplo n.º 28
0
    def test_list(self):
        c = KiwiConfig(storage=["/test/directory"])

        assert c.storage.directory == Path("/test/directory")
Exemplo n.º 29
0
    def test_empty(self):
        c = KiwiConfig(shells=None)

        assert c == KiwiConfig(shells=[])

        assert c.shells == []
Exemplo n.º 30
0
    def test_str(self):
        c = KiwiConfig(storage="/test/directory")

        assert c.storage.directory == Path("/test/directory")