From 0e3cd6f34c09a6f41bf3514db9ac9c4d554b389f Mon Sep 17 00:00:00 2001 From: Carlos Garnacho Date: Sat, 12 Dec 2020 18:27:00 +0100 Subject: 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); -- cgit v1.2.1