Repository: exasol/integration-test-docker-environment (language: python)

Issue:
Fix comparison of db versions
## Background 

#140 we verify if a certain db version is supported. 
The comparison of the db version is done using a string compare.
However, this comparison does not work for:
7.0.9 versus 7.0.14.

## Acceptance Criteria

Fix db version comparison


Discussion / hints:

Fix patch:
diff --git a/doc/changes/changes_0.8.0.md b/doc/changes/changes_0.8.0.md
index 8aa018859..b3b2bb13a 100644
--- a/doc/changes/changes_0.8.0.md
+++ b/doc/changes/changes_0.8.0.md
@@ -23,6 +23,7 @@ If you need further versions, please open an issue.
 
  - #148: Fix broken Github actions
  - #152: Fix bug where Bucket- and Database-forward might be the same 
+ - #154: Fix comparison of db versions
 
 ## Features / Enhancements:
 


Test patch:
diff --git a/exasol_integration_test_docker_environment/lib/test_environment/db_version.py b/exasol_integration_test_docker_environment/lib/test_environment/db_version.py
new file mode 100644
index 000000000..8a1c48d8e
--- /dev/null
+++ b/exasol_integration_test_docker_environment/lib/test_environment/db_version.py
@@ -0,0 +1,49 @@
+from typing import Optional
+
+from exasol_integration_test_docker_environment.cli.options.test_environment_options import LATEST_DB_VERSION
+
+DEFAULT_VERSION = "default"
+
+
+class DbVersion:
+    def __init__(self, major, minor, stable):
+        self.major = major
+        self.minor = minor
+        self.stable = stable
+
+    @classmethod
+    def from_db_version_str(cls, db_version_str: Optional[str]):
+        db_version = db_version_str
+        if db_version_str in (None, DEFAULT_VERSION):
+            db_version = LATEST_DB_VERSION
+        if db_version.endswith("-d1"):
+            db_version = "-".join(db_version.split("-")[0:-1])
+        version = tuple([int(v) for v in db_version.split(".")])
+        if len(version) != 3:
+            raise ValueError(f"Invalid db version given: {db_version}")
+        return DbVersion(version[0], version[1], version[2])
+
+    def __str__(self):
+        return f"{self.major}.{self.minor}.{self.stable}"
+
+    def to_tuple(self):
+        return self.major, self.minor, self.stable
+
+    def __eq__(self, other) -> bool:
+        return self.to_tuple() == other.to_tuple()
+
+    def __lt__(self, other) -> bool:
+        return self.to_tuple() < other.to_tuple()
+
+    def __le__(self, other) -> bool:
+        return self.to_tuple() <= other.to_tuple()
+
+    def __ge__(self, other) -> bool:
+        return self.to_tuple() >= other.to_tuple()
+
+    def __gt__(self, other) -> bool:
+        return self.to_tuple() > other.to_tuple()
+
+
+def db_version_supports_custom_certificates(db_version_str: Optional[str]) -> bool:
+    return DbVersion.from_db_version_str(db_version_str) > DbVersion(7, 0, 5)
diff --git a/exasol_integration_test_docker_environment/lib/test_environment/spawn_test_database.py b/exasol_integration_test_docker_environment/lib/test_environment/spawn_test_database.py
index 5430cd7cb..660a790c0 100644
--- a/exasol_integration_test_docker_environment/lib/test_environment/spawn_test_database.py
+++ b/exasol_integration_test_docker_environment/lib/test_environment/spawn_test_database.py
@@ -18,6 +18,7 @@
 from exasol_integration_test_docker_environment.lib.data.docker_network_info import DockerNetworkInfo
 from exasol_integration_test_docker_environment.lib.docker.images.create.utils.pull_log_handler import PullLogHandler
 from exasol_integration_test_docker_environment.lib.docker.images.image_info import ImageInfo
+from exasol_integration_test_docker_environment.lib.test_environment.db_version import DbVersion
 from exasol_integration_test_docker_environment.lib.test_environment.docker_container_copy import DockerContainerCopy
 from exasol_integration_test_docker_environment.lib.test_environment.parameter.docker_db_test_environment_parameter import \
     DockerDBTestEnvironmentParameter
@@ -43,10 +44,8 @@ def __init__(self, *args, **kwargs):
             raise Exception(
                 "ip_address_index_in_subnet needs to be greater than 0 got %s"
                 % self.ip_address_index_in_subnet)
-        if self.docker_db_image_version.endswith("-d1"):
-            self.db_version = "-".join(self.docker_db_image_version.split("-")[0:-1])
-        else:
-            self.db_version = self.docker_db_image_version
+
+        self.db_version = DbVersion.from_db_version_str(self.docker_db_image_version)
         self.docker_db_config_resource_name = f"docker_db_config/{self.db_version}"
 
     def run_task(self):
@@ -237,7 +236,7 @@ def _add_exa_conf(self, copy: DockerContainerCopy,
             f"{self.docker_db_config_resource_name}/EXAConf") # type: bytes
         template = Template(template_str.decode("utf-8"))
         rendered_template = template.render(private_network=db_private_network,
-                                            db_version=self.db_version,
+                                            db_version=str(self.db_version),
                                             image_version=self.docker_db_image_version,
                                             mem_size=self.mem_size,
                                             disk_size=self.disk_size,
diff --git a/exasol_integration_test_docker_environment/lib/test_environment/spawn_test_environment_with_docker_db.py b/exasol_integration_test_docker_environment/lib/test_environment/spawn_test_environment_with_docker_db.py
index b04ce1fab..413c0f33c 100644
--- a/exasol_integration_test_docker_environment/lib/test_environment/spawn_test_environment_with_docker_db.py
+++ b/exasol_integration_test_docker_environment/lib/test_environment/spawn_test_environment_with_docker_db.py
@@ -11,12 +11,13 @@
     CreateSSLCertificatesTask
 from exasol_integration_test_docker_environment.lib.test_environment.database_waiters.wait_for_test_docker_database import \
     WaitForTestDockerDatabase
+from exasol_integration_test_docker_environment.lib.test_environment.db_version import \
+    db_version_supports_custom_certificates
 from exasol_integration_test_docker_environment.lib.test_environment.parameter.docker_db_test_environment_parameter import \
     DockerDBTestEnvironmentParameter
 from exasol_integration_test_docker_environment.lib.test_environment.prepare_network_for_test_environment import \
     PrepareDockerNetworkForTestEnvironment
 from exasol_integration_test_docker_environment.lib.test_environment.spawn_test_database import SpawnTestDockerDatabase
-from exasol_integration_test_docker_environment.testing.utils import db_version_supports_custom_certificates
 
 
 class SpawnTestEnvironmentWithDockerDB(
diff --git a/exasol_integration_test_docker_environment/test/test_db_version_supports_custom_certificates.py b/exasol_integration_test_docker_environment/test/test_db_version_supports_custom_certificates.py
new file mode 100644
index 000000000..cf241ca76
--- /dev/null
+++ b/exasol_integration_test_docker_environment/test/test_db_version_supports_custom_certificates.py
@@ -0,0 +1,38 @@
+import unittest
+
+from exasol_integration_test_docker_environment.lib.test_environment.db_version import \
+    db_version_supports_custom_certificates
+
+
+class TestDbVersionSupportCustomCertificates(unittest.TestCase):
+
+    def test_default(self):
+        self.assertTrue(db_version_supports_custom_certificates("default"))
+
+    def test_none(self):
+        self.assertTrue(db_version_supports_custom_certificates(None))
+
+    def test_7_0_5(self):
+        self.assertFalse(db_version_supports_custom_certificates("7.0.5"))
+
+    def test_7_0_14(self):
+        self.assertTrue(db_version_supports_custom_certificates("7.0.14"))
+
+    def test_6_0_0(self):
+        self.assertFalse(db_version_supports_custom_certificates("6.0.0"))
+
+    def test_6_0_0_d_1(self):
+        self.assertFalse(db_version_supports_custom_certificates("6.0.0-d1"))
+
+    def test_7_1_3(self):
+        self.assertTrue(db_version_supports_custom_certificates("7.1.3"))
+
+    def test_7_1_3_d_1(self):
+        self.assertTrue(db_version_supports_custom_certificates("7.1.3-d1"))
+
+    def test_throw_error(self):
+        self.assertRaises(ValueError, db_version_supports_custom_certificates, "7abc")
+
+
+if __name__ == '__main__':
+    unittest.main()
diff --git a/exasol_integration_test_docker_environment/test/test_test_environment_certificate.py b/exasol_integration_test_docker_environment/test/test_test_environment_certificate.py
index 6531cf168..436b968bf 100644
--- a/exasol_integration_test_docker_environment/test/test_test_environment_certificate.py
+++ b/exasol_integration_test_docker_environment/test/test_test_environment_certificate.py
@@ -2,10 +2,11 @@
 
 
 from exasol_integration_test_docker_environment.lib.docker import ContextDockerClient
+from exasol_integration_test_docker_environment.lib.test_environment.db_version import \
+    db_version_supports_custom_certificates
 from exasol_integration_test_docker_environment.testing import utils
 from exasol_integration_test_docker_environment.testing.exaslct_test_environment import ExaslctTestEnvironment
-from exasol_integration_test_docker_environment.testing.utils import check_db_version_from_env, \
-    db_version_supports_custom_certificates
+from exasol_integration_test_docker_environment.testing.utils import check_db_version_from_env
 
 
 class CertificateTest(unittest.TestCase):
diff --git a/exasol_integration_test_docker_environment/testing/utils.py b/exasol_integration_test_docker_environment/testing/utils.py
index c1fe7f617..05c451eb5 100644
--- a/exasol_integration_test_docker_environment/testing/utils.py
+++ b/exasol_integration_test_docker_environment/testing/utils.py
@@ -76,10 +76,3 @@ def check_db_version_from_env() -> Optional[str]:
     if "EXASOL_VERSION" in os.environ and os.environ["EXASOL_VERSION"] != "default":
         retval = os.environ["EXASOL_VERSION"]
     return retval
-
-
-def db_version_supports_custom_certificates(db_version: Optional[str]) -> bool:
-    # 1. If db_version is None => Latest DB version is used (which already supported custom certificates in EXAConf
-    # 2. If db_version is "default" => Return True, as "default" is lexically greater than "7.0.5"
-    # 3. Return result of db version comparison
-    return True if db_version is None or db_version > "7.0.5" else False