From a9b1920c2f767ebe9f3518b34b9d5afdac028fb4 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Fri, 3 Jun 2022 11:52:18 -0400 Subject: [PATCH] Added integration testing. --- Jenkinsfile | 12 +- Pipfile | 12 ++ Pipfile.lock | 175 +++++++++++++++++++++++++++ src/main.rs | 2 +- tests/__init__.py | 0 tests/features/server_start.feature | 9 ++ tests/step_defs/__init__.py | 0 tests/step_defs/conftest.py | 17 +++ tests/step_defs/page.py | 22 ++++ tests/step_defs/server.py | 40 ++++++ tests/step_defs/test_server_start.py | 25 ++++ 11 files changed, 311 insertions(+), 3 deletions(-) create mode 100644 Pipfile create mode 100644 Pipfile.lock create mode 100644 tests/__init__.py create mode 100644 tests/features/server_start.feature create mode 100644 tests/step_defs/__init__.py create mode 100644 tests/step_defs/conftest.py create mode 100644 tests/step_defs/page.py create mode 100644 tests/step_defs/server.py create mode 100644 tests/step_defs/test_server_start.py diff --git a/Jenkinsfile b/Jenkinsfile index 6d28424..0ed26fa 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -2,7 +2,7 @@ pipeline { agent any stages { - stage('Linux Testing') { + stage('Rust Testing') { steps { sh "cargo test" } @@ -10,8 +10,16 @@ pipeline { stage('Linux Build') { steps { sh "cargo build --release" - archiveArtifacts artifacts: 'target/release/morethantext_web', fingerprint: true } } + stage('Integration Testing') { + steps { + sh "pipenv install" + sh "pipenv run pytest" + } + } + stage('Archiving') { + archiveArtifacts artifacts: 'target/release/morethantext_web', fingerprint: true + } } } diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..49fa1e8 --- /dev/null +++ b/Pipfile @@ -0,0 +1,12 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] +pytest-bdd = "*" + +[dev-packages] + +[requires] +python_version = "3.9" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..5823c67 --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,175 @@ +{ + "_meta": { + "hash": { + "sha256": "41467a4a7785d185910021a595ee9badef833f032850b288bb87be5ff76f2826" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.9" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "attrs": { + "hashes": [ + "sha256:2d27e3784d7a565d36ab851fe94887c5eccd6a463168875832a1be79c82828b4", + "sha256:626ba8234211db98e869df76230a137c4c40a12d72445c45d5f5b716f076e2fd" + ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'", + "version": "==21.4.0" + }, + "glob2": { + "hashes": [ + "sha256:85c3dbd07c8aa26d63d7aacee34fa86e9a91a3873bc30bf62ec46e531f92ab8c" + ], + "version": "==0.7" + }, + "iniconfig": { + "hashes": [ + "sha256:011e24c64b7f47f6ebd835bb12a743f2fbe9a26d4cecaa7f53bc4f35ee9da8b3", + "sha256:bc3af051d7d14b2ee5ef9969666def0cd1a000e121eaea580d4a313df4b37f32" + ], + "version": "==1.1.1" + }, + "mako": { + "hashes": [ + "sha256:23aab11fdbbb0f1051b93793a58323ff937e98e34aece1c4219675122e57e4ba", + "sha256:9a7c7e922b87db3686210cf49d5d767033a41d4010b284e747682c92bddd8b39" + ], + "markers": "python_version >= '3.7'", + "version": "==1.2.0" + }, + "markupsafe": { + "hashes": [ + "sha256:0212a68688482dc52b2d45013df70d169f542b7394fc744c02a57374a4207003", + "sha256:089cf3dbf0cd6c100f02945abeb18484bd1ee57a079aefd52cffd17fba910b88", + "sha256:10c1bfff05d95783da83491be968e8fe789263689c02724e0c691933c52994f5", + "sha256:33b74d289bd2f5e527beadcaa3f401e0df0a89927c1559c8566c066fa4248ab7", + "sha256:3799351e2336dc91ea70b034983ee71cf2f9533cdff7c14c90ea126bfd95d65a", + "sha256:3ce11ee3f23f79dbd06fb3d63e2f6af7b12db1d46932fe7bd8afa259a5996603", + "sha256:421be9fbf0ffe9ffd7a378aafebbf6f4602d564d34be190fc19a193232fd12b1", + "sha256:43093fb83d8343aac0b1baa75516da6092f58f41200907ef92448ecab8825135", + "sha256:46d00d6cfecdde84d40e572d63735ef81423ad31184100411e6e3388d405e247", + "sha256:4a33dea2b688b3190ee12bd7cfa29d39c9ed176bda40bfa11099a3ce5d3a7ac6", + "sha256:4b9fe39a2ccc108a4accc2676e77da025ce383c108593d65cc909add5c3bd601", + "sha256:56442863ed2b06d19c37f94d999035e15ee982988920e12a5b4ba29b62ad1f77", + "sha256:671cd1187ed5e62818414afe79ed29da836dde67166a9fac6d435873c44fdd02", + "sha256:694deca8d702d5db21ec83983ce0bb4b26a578e71fbdbd4fdcd387daa90e4d5e", + "sha256:6a074d34ee7a5ce3effbc526b7083ec9731bb3cbf921bbe1d3005d4d2bdb3a63", + "sha256:6d0072fea50feec76a4c418096652f2c3238eaa014b2f94aeb1d56a66b41403f", + "sha256:6fbf47b5d3728c6aea2abb0589b5d30459e369baa772e0f37a0320185e87c980", + "sha256:7f91197cc9e48f989d12e4e6fbc46495c446636dfc81b9ccf50bb0ec74b91d4b", + "sha256:86b1f75c4e7c2ac2ccdaec2b9022845dbb81880ca318bb7a0a01fbf7813e3812", + "sha256:8dc1c72a69aa7e082593c4a203dcf94ddb74bb5c8a731e4e1eb68d031e8498ff", + "sha256:8e3dcf21f367459434c18e71b2a9532d96547aef8a871872a5bd69a715c15f96", + "sha256:8e576a51ad59e4bfaac456023a78f6b5e6e7651dcd383bcc3e18d06f9b55d6d1", + "sha256:96e37a3dc86e80bf81758c152fe66dbf60ed5eca3d26305edf01892257049925", + "sha256:97a68e6ada378df82bc9f16b800ab77cbf4b2fada0081794318520138c088e4a", + "sha256:99a2a507ed3ac881b975a2976d59f38c19386d128e7a9a18b7df6fff1fd4c1d6", + "sha256:a49907dd8420c5685cfa064a1335b6754b74541bbb3706c259c02ed65b644b3e", + "sha256:b09bf97215625a311f669476f44b8b318b075847b49316d3e28c08e41a7a573f", + "sha256:b7bd98b796e2b6553da7225aeb61f447f80a1ca64f41d83612e6139ca5213aa4", + "sha256:b87db4360013327109564f0e591bd2a3b318547bcef31b468a92ee504d07ae4f", + "sha256:bcb3ed405ed3222f9904899563d6fc492ff75cce56cba05e32eff40e6acbeaa3", + "sha256:d4306c36ca495956b6d568d276ac11fdd9c30a36f1b6eb928070dc5360b22e1c", + "sha256:d5ee4f386140395a2c818d149221149c54849dfcfcb9f1debfe07a8b8bd63f9a", + "sha256:dda30ba7e87fbbb7eab1ec9f58678558fd9a6b8b853530e176eabd064da81417", + "sha256:e04e26803c9c3851c931eac40c695602c6295b8d432cbe78609649ad9bd2da8a", + "sha256:e1c0b87e09fa55a220f058d1d49d3fb8df88fbfab58558f1198e08c1e1de842a", + "sha256:e72591e9ecd94d7feb70c1cbd7be7b3ebea3f548870aa91e2732960fa4d57a37", + "sha256:e8c843bbcda3a2f1e3c2ab25913c80a3c5376cd00c6e8c4a86a89a28c8dc5452", + "sha256:efc1913fd2ca4f334418481c7e595c00aad186563bbc1ec76067848c7ca0a933", + "sha256:f121a1420d4e173a5d96e47e9a0c0dcff965afdf1626d28de1460815f7c4ee7a", + "sha256:fc7b548b17d238737688817ab67deebb30e8073c95749d55538ed473130ec0c7" + ], + "markers": "python_version >= '3.7'", + "version": "==2.1.1" + }, + "packaging": { + "hashes": [ + "sha256:dd47c42927d89ab911e606518907cc2d3a1f38bbd026385970643f9c5b8ecfeb", + "sha256:ef103e05f519cdc783ae24ea4e2e0f508a9c99b2d4969652eed6a2e1ea5bd522" + ], + "markers": "python_version >= '3.6'", + "version": "==21.3" + }, + "parse": { + "hashes": [ + "sha256:9ff82852bcb65d139813e2a5197627a94966245c897796760a3a2a8eb66f020b" + ], + "markers": "python_version >= '3.0'", + "version": "==1.19.0" + }, + "parse-type": { + "hashes": [ + "sha256:20b43c660e48ed47f433bce5873a2a3d4b9b6a7ba47bd7f7d2a7cec4bec5551f", + "sha256:c148e88436bd54dab16484108e882be3367f44952c649c9cd6b82a7370b650cb" + ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", + "version": "==0.6.0" + }, + "pluggy": { + "hashes": [ + "sha256:4224373bacce55f955a878bf9cfa763c1e360858e330072059e10bad68531159", + "sha256:74134bbf457f031a36d68416e1509f34bd5ccc019f0bcc952c7b909d06b37bd3" + ], + "markers": "python_version >= '3.6'", + "version": "==1.0.0" + }, + "py": { + "hashes": [ + "sha256:51c75c4126074b472f746a24399ad32f6053d1b34b68d2fa41e558e6f4a98719", + "sha256:607c53218732647dff4acdfcd50cb62615cedf612e72d1724fb1a0cc6405b378" + ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'", + "version": "==1.11.0" + }, + "pyparsing": { + "hashes": [ + "sha256:2b020ecf7d21b687f219b71ecad3631f644a47f01403fa1d1036b0c6416d70fb", + "sha256:5026bae9a10eeaefb61dab2f09052b9f4307d44aee4eda64b309723d8d206bbc" + ], + "markers": "python_full_version >= '3.6.8'", + "version": "==3.0.9" + }, + "pytest": { + "hashes": [ + "sha256:13d0e3ccfc2b6e26be000cb6568c832ba67ba32e719443bfe725814d3c42433c", + "sha256:a06a0425453864a270bc45e71f783330a7428defb4230fb5e6a731fde06ecd45" + ], + "markers": "python_version >= '3.7'", + "version": "==7.1.2" + }, + "pytest-bdd": { + "hashes": [ + "sha256:c7cf12209606421f61f36b5dc63beccd0c82d29446c0592cf68af2dad0a9761d", + "sha256:fab7093ed3d5e51ee0c68de093c90e4f40de345bd9a54a188b2991ce2a2a39cf" + ], + "index": "pypi", + "version": "==5.0.0" + }, + "six": { + "hashes": [ + "sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926", + "sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254" + ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", + "version": "==1.16.0" + }, + "tomli": { + "hashes": [ + "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc", + "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f" + ], + "markers": "python_version >= '3.7'", + "version": "==2.0.1" + } + }, + "develop": {} +} diff --git a/src/main.rs b/src/main.rs index 1a49057..ca32f11 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use tide::{http::StatusCode, Request, Response}; #[async_std::main] async fn main() -> tide::Result<()> { let app = app_setup().await; - app.listen("127.0.0.1:8080").await?; + app.listen("127.0.0.1:9090").await?; Ok(()) } diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/features/server_start.feature b/tests/features/server_start.feature new file mode 100644 index 0000000..11fe3fa --- /dev/null +++ b/tests/features/server_start.feature @@ -0,0 +1,9 @@ +Feature: Server Start + + Acceptance testing related to server start up. + + Scenario: Default start up + Given a server + And it is running + When the home page is accessed + Then the status should be OK diff --git a/tests/step_defs/__init__.py b/tests/step_defs/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/step_defs/conftest.py b/tests/step_defs/conftest.py new file mode 100644 index 0000000..c7b8879 --- /dev/null +++ b/tests/step_defs/conftest.py @@ -0,0 +1,17 @@ +"""PyTest configuration.""" + +from pytest import fixture +from .server import Server +from .page import Page + +@fixture +def server(): + """Create a server instance.""" + serv = Server() + yield serv + serv.destroy() + +@fixture +def page(): + pg = Page() + return pg diff --git a/tests/step_defs/page.py b/tests/step_defs/page.py new file mode 100644 index 0000000..017ba77 --- /dev/null +++ b/tests/step_defs/page.py @@ -0,0 +1,22 @@ +"""Fisture class for page functions.""" + +from urllib import request +from urllib.error import HTTPError + +class Page: + """Class for getting an individual page.""" + + def __init__(self): + """Init function.""" + self.res = None + + def request_url(self, url): + """Make a page request.""" + try: + self.res = request.urlopen(url) + except HTTPError as err: + self.res = err + + def get_status_code(self): + """Return the status code.""" + return self.res.status diff --git a/tests/step_defs/server.py b/tests/step_defs/server.py new file mode 100644 index 0000000..4057327 --- /dev/null +++ b/tests/step_defs/server.py @@ -0,0 +1,40 @@ +"""Fixture for setting up a single server.""" + +from asyncio import create_subprocess_exec, get_event_loop, sleep +from pathlib import Path + +class Server: + """Setup a single server.""" + + def __init__(self): + """Initialization of a server.""" + self.settings = {} + self.settings["address"] = "127.0.0.1" + self.settings["port"] = 9090 + self.process = None + self.loop = get_event_loop() + + async def __start(self): + """async start of the server.""" + self.process = await create_subprocess_exec( + Path.cwd().joinpath("target", "release", "morethantext_web") + ) + await sleep(1) + + async def __stop(self): + """async stop of the server.""" + if self.process is not None and self.process.returncode is None: + self.process.terminate() + await self.process.wait() + + def start(self): + """Start the server.""" + self.loop.run_until_complete(self.__start()) + + def stop(self): + """Stop the server.""" + self.loop.run_until_complete(self.__stop()) + + def destroy(self): + """Removes the server instance.""" + self.stop() diff --git a/tests/step_defs/test_server_start.py b/tests/step_defs/test_server_start.py new file mode 100644 index 0000000..50af13a --- /dev/null +++ b/tests/step_defs/test_server_start.py @@ -0,0 +1,25 @@ +"""Interpratures for server start features.""" + +from pytest_bdd import given, scenarios, then, when + +scenarios("../features/server_start.feature") + +@given("a server") +def create_server(server): + """Set up a server.""" + +@given("it is running") +def start_server(server): + """Start up the server.""" + server.start() + +@when("the home page is accessed") +def access_home_page(server, page): + """Access the home page.""" + url = f"http://{server.settings['address']}:{server.settings['port']}/" + page.request_url(url) + +@then("the status should be OK") +def check_for_ok(page): + """Is the page status code 200""" + assert page.get_status_code() == 200