]> git.ipfire.org Git - pbs.git/commitdiff
test: Add scaffolding for a testsuite for the backend
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 12 Oct 2022 13:46:23 +0000 (13:46 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 12 Oct 2022 13:46:23 +0000 (13:46 +0000)
I suppose it is very hard to test the frontend part, but that is not the
highest priority.

It would already be very helpful to test the backend parts.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
.gitignore
Makefile.am
tests/setup.py [new file with mode: 0755]
tests/test.py [new file with mode: 0644]

index 99b2e2c7e0b2cd31d1fb9b8e05cca77d292d7e06..12784524a8fdd125fb3d7fa83da0edbc3dc8199b 100644 (file)
@@ -15,6 +15,7 @@
 *.cache
 *.gmo
 *.mo
+*.trs
 *~
 Makefile.in
 aclocal.m4
index 9650b417675e3b1e57b503a5f431113ca4574bdf..a8daded5ecf3c6cd828366e645dbe78abbb9b956 100644 (file)
@@ -468,3 +468,18 @@ src/static/css/highlight.css: Makefile
 
 src/static/js/foundation.min.js: src/third-party/foundation-sites/dist/js/foundation.js
        $(UGLIFYJS_PROCESS)
+
+# ------------------------------------------------------------------------------
+
+TESTS = \
+       $(dist_check_SCRIPTS)
+
+TESTS_ENVIRONMENT = \
+       PYTHONPATH="$(abs_top_srcdir)/src" \
+       abs_top_srcdir="$(abs_top_srcdir)"
+
+dist_check_SCRIPTS = \
+       tests/setup.py
+
+EXTRA_DIST += \
+       tests/test.py
diff --git a/tests/setup.py b/tests/setup.py
new file mode 100755 (executable)
index 0000000..015e1c4
--- /dev/null
@@ -0,0 +1,17 @@
+#!/usr/bin/python3
+
+import unittest
+
+import test
+
+class SetupTestCase(test.TestCase):
+       def test_pakfire(self):
+               """
+                       This test checks whether we can launch into a local Pakfire instance
+               """
+               with self.backend.pakfire() as p:
+                       pass
+
+
+if __name__ == "__main__":
+       unittest.main()
diff --git a/tests/test.py b/tests/test.py
new file mode 100644 (file)
index 0000000..648f457
--- /dev/null
@@ -0,0 +1,75 @@
+#!/usr/bin/python3
+
+import configparser
+import os
+import socket
+import tempfile
+import unittest
+
+from buildservice import Backend
+from buildservice import database
+from buildservice import misc
+
+class TestCase(unittest.IsolatedAsyncioTestCase):
+       """
+               This is the base class for all tests
+       """
+       def source_path(self, path):
+               try:
+                       source_path = os.environ["abs_top_srcdir"]
+               except KeyError as e:
+                       raise ValueError("source path is not set") from e
+
+               return os.path.join(source_path, path)
+
+       def _setup_database(self):
+               """
+                       Creates a new database and imports the default schema
+               """
+               # Path to the schema
+               schema = self.source_path("src/database.sql")
+
+               # Load the schema
+               with open(schema) as f:
+                       schema = f.read()
+
+               # Database Server & Name
+               host = os.environ.get("DB_HOST", socket.gethostname())
+               name = os.environ.get("DB_NAME", "pakfiretest")
+
+               # Credentials
+               username = os.environ.get("DB_USER", "pakfiretest")
+               password = os.environ.get("DB_PASS", "pakfiretest")
+
+               # Connect to the database
+               db = database.Connection(host, name, username, password)
+
+               with db.transaction():
+                       # Drop anything existing
+                       db.execute("DROP SCHEMA public CASCADE")
+
+                       # Re-create the schema
+                       db.execute("CREATE SCHEMA public")
+                       db.execute(schema)
+
+               # Return the credentials
+               return {
+                       "name"     : name,
+                       "hostname" : host,
+                       "user"     : username,
+                       "password" : password,
+               }
+
+       def setUp(self):
+               # Create a configuration file
+               conf = configparser.ConfigParser()
+
+               # Setup the database
+               conf["database"] = self._setup_database()
+
+               with tempfile.NamedTemporaryFile("w") as f:
+                       # Write configuration
+                       conf.write(f)
+
+                       # Initialize the backend
+                       self.backend = Backend(f.name)