From 734517719513a9b24d5429f96332c6a8f7e01dcd Mon Sep 17 00:00:00 2001
From: ldericher <40151420+ldericher@users.noreply.github.com>
Date: Thu, 14 Oct 2021 04:34:09 +0200
Subject: [PATCH] Some pytests for config.py
---
.idea/runConfigurations/Tests.xml | 18 ++++
.idea/runConfigurations/kiwi_next.xml | 6 +-
kiwi_scp/config.py | 16 ++--
pyproject.toml | 3 +
tests/__init__.py | 0
tests/test_config.py | 118 ++++++++++++++++++++++++++
6 files changed, 151 insertions(+), 10 deletions(-)
create mode 100644 .idea/runConfigurations/Tests.xml
create mode 100644 tests/__init__.py
create mode 100644 tests/test_config.py
diff --git a/.idea/runConfigurations/Tests.xml b/.idea/runConfigurations/Tests.xml
new file mode 100644
index 0000000..17c8681
--- /dev/null
+++ b/.idea/runConfigurations/Tests.xml
@@ -0,0 +1,18 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/runConfigurations/kiwi_next.xml b/.idea/runConfigurations/kiwi_next.xml
index 9b3bd56..b4babb9 100644
--- a/.idea/runConfigurations/kiwi_next.xml
+++ b/.idea/runConfigurations/kiwi_next.xml
@@ -1,5 +1,5 @@
-
+
@@ -11,11 +11,11 @@
-
+
-
+
diff --git a/kiwi_scp/config.py b/kiwi_scp/config.py
index 0cbf6d1..a88eb4b 100644
--- a/kiwi_scp/config.py
+++ b/kiwi_scp/config.py
@@ -181,7 +181,7 @@ class Config(BaseModel):
result: Dict[str, Optional[str]] = {}
for item in value:
- key, value = parse_str(item)
+ key, value = parse_str(str(item))
result[key] = value
return result
@@ -193,10 +193,12 @@ class Config(BaseModel):
key, value = parse_str(value)
return {key: value}
- elif isinstance(value, int):
- # integer format (just define single oddly named variable)
- return {str(value): None}
-
else:
- # undefined format
- raise ValueError
+ # any other format (try to coerce to str first)
+ try:
+ key, value = parse_str(str(value))
+ return {key: value}
+
+ except Exception as e:
+ # undefined format
+ raise ValueError("Invalid Environment Format")
diff --git a/pyproject.toml b/pyproject.toml
index e1ab88b..85844ed 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -11,9 +11,12 @@ pydantic = "^1.8.2"
[tool.poetry.dev-dependencies]
virtualenv = "^20.8.1"
+pytest = "^6.2.5"
+toml = "^0.10.2"
[tool.poetry.scripts]
kiwi = "kiwi_scp.scripts.kiwi:main"
+kiwi_next = "kiwi_scp.scripts.kiwi_next:main"
[build-system]
requires = ["poetry-core>=1.0.0"]
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/test_config.py b/tests/test_config.py
new file mode 100644
index 0000000..fa08258
--- /dev/null
+++ b/tests/test_config.py
@@ -0,0 +1,118 @@
+from ipaddress import IPv4Network
+from pathlib import Path
+
+import pytest
+import toml
+from pydantic import ValidationError
+
+from kiwi_scp.config import Config
+
+
+def test_default():
+ c = Config()
+ version = toml.load("./pyproject.toml")["tool"]["poetry"]["version"]
+
+ assert c.version == version
+ assert len(c.shells) == 1
+ assert c.shells[0] == Path("/bin/bash")
+ assert c.projects is None
+ 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")
+
+
+#############
+# ENVIRONMENT
+#############
+
+def test_env_empty():
+ c = Config(environment=None)
+
+ assert c.environment == {}
+
+
+def test_env_dict():
+ c = Config(environment={})
+
+ assert c.environment == {}
+
+ c = Config(environment={
+ "variable": "value"
+ })
+
+ assert len(c.environment) == 1
+ assert "variable" in c.environment
+ assert c.environment["variable"] == "value"
+
+
+def test_env_list():
+ c = Config(environment=[])
+
+ assert c.environment == {}
+
+ c = Config(environment=[
+ "variable=value",
+ ])
+
+ assert len(c.environment) == 1
+ assert "variable" in c.environment
+ assert c.environment["variable"] == "value"
+
+ c = Config(environment=[
+ "variable",
+ ])
+
+ assert len(c.environment) == 1
+ assert "variable" in c.environment
+ assert c.environment["variable"] is None
+
+ c = Config(environment=[
+ 123,
+ ])
+
+ assert len(c.environment) == 1
+ assert "123" in c.environment
+ assert c.environment["123"] is None
+
+
+def test_env_str():
+ c = Config(environment="variable=value")
+
+ assert len(c.environment) == 1
+ assert "variable" in c.environment
+ assert c.environment["variable"] == "value"
+
+ c = Config(environment="variable")
+
+ assert len(c.environment) == 1
+ assert "variable" in c.environment
+ assert c.environment["variable"] is None
+
+
+def test_env_coercible():
+ c = Config(environment=123)
+
+ assert len(c.environment) == 1
+ assert "123" in c.environment
+ assert c.environment["123"] is None
+
+ c = Config(environment=123.4)
+
+ assert len(c.environment) == 1
+ assert "123.4" in c.environment
+ assert c.environment["123.4"] is None
+
+
+def test_env_undef():
+ class UnCoercible:
+ def __str__(self):
+ raise ValueError
+
+ with pytest.raises(ValidationError) as exc_info:
+ Config(environment=UnCoercible())
+
+ assert len(exc_info.value.errors()) == 1
+ error = exc_info.value.errors()[0]
+ assert error["msg"] == "Invalid Environment Format"
+ assert error["type"] == "value_error"