[tracker/wip/carlosg/http-endpoint: 7/10] tests: Add basic functional test for the HTTP endpoint




commit e0c820c46a5a4cd79843ba4a9040fa9e43e056d0
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sat Dec 12 18:27:00 2020 +0100

    tests: Add basic functional test for the HTTP endpoint
    
    Test it via the CLI atm, in order to exercise as much as possible.

 tests/functional-tests/cli.py      | 21 +++++++++++++++++++++
 tests/functional-tests/fixtures.py | 29 +++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)
---
diff --git a/tests/functional-tests/cli.py b/tests/functional-tests/cli.py
index 76ee55c16..1318e754e 100644
--- a/tests/functional-tests/cli.py
+++ b/tests/functional-tests/cli.py
@@ -24,6 +24,7 @@ import unittest
 
 import configuration
 import fixtures
+import random
 
 
 class TestCli(fixtures.TrackerCommandLineTestCase):
@@ -82,6 +83,26 @@ class TestCli(fixtures.TrackerCommandLineTestCase):
             self.run_cli(
                 ['tracker3', 'import', '--database', tmpdir, testdata]);
 
+    def test_http_endpoint(self):
+        """Create a HTTP endpoint for local testing"""
+
+        with self.tmpdir() as tmpdir:
+            ontology_path = configuration.ontologies_dir()
+            port = random.randint(32000, 65000)
+            address = 'http://127.0.0.1:%d/sparql' % port
+
+            # Create the database
+            self.run_background(
+                ['tracker3', 'endpoint', '--database', tmpdir,
+                 '--ontology-path', ontology_path, '--http-port', port],
+               'Listening')
+
+            # Sanity check that it works.
+            self.run_cli(
+                ['tracker3', 'sparql',
+                 '--remote-service', address,
+                 '--query', 'ASK { ?u a rdfs:Resource }'])
+
 
 if __name__ == '__main__':
     fixtures.tracker_test_main()
diff --git a/tests/functional-tests/fixtures.py b/tests/functional-tests/fixtures.py
index 8c16b40b4..e66cc1fa1 100644
--- a/tests/functional-tests/fixtures.py
+++ b/tests/functional-tests/fixtures.py
@@ -275,6 +275,12 @@ class TrackerCommandLineTestCase(ut.TestCase):
         path = self.env.get('PATH', []).split(':')
         self.env['PATH'] = ':'.join([cfg.cli_dir()] + path)
         self.env['TRACKER_CLI_SUBCOMMANDS_DIR'] = os.path.join(cfg.cli_dir(), 'subcommands')
+        self.bg_processes = []
+
+    def tearDown(self):
+        for bg_process in self.bg_processes:
+            bg_process.terminate()
+            bg_process.wait()
 
     @contextlib.contextmanager
     def tmpdir(self):
@@ -306,3 +312,26 @@ class TrackerCommandLineTestCase(ut.TestCase):
                 "Error: %s" % result.stderr.decode('utf-8')]))
 
         return result.stdout.decode('utf-8')
+
+    def run_background(self, command, init_string=None):
+        command = [str(c) for c in command]
+        log.info("Running in background: %s", ' '.join(command))
+        result = subprocess.Popen(
+            command, stdout=subprocess.PIPE,
+            stderr=subprocess.DEVNULL, env=self.env,
+            encoding='UTF-8')
+        initialized = False
+
+        if result.returncode != None:
+            raise CliError('\n'.join([
+                "CLI command failed.",
+                "Command: %s" % ' '.join(command),
+                "Error: %s" % result.stderr.decode('utf-8')]))
+
+        # Wait for the specified output
+        while init_string and not initialized:
+            txt = result.stdout.readline();
+            initialized = txt.find(init_string) >= 0
+
+        result.stdout.close()
+        self.bg_processes.append(result);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]