diff options
| author | crupest <crupest@outlook.com> | 2022-11-28 13:13:27 +0800 | 
|---|---|---|
| committer | crupest <crupest@outlook.com> | 2022-11-28 13:13:27 +0800 | 
| commit | 51bee1e8891ebea829a1a5e9a6502a79e0e5b6db (patch) | |
| tree | 63b194129d445220ce11425cc993fca67788e9ad | |
| parent | 873c9b4e39e47d2e6e1dbf40ee6f2e812c229adb (diff) | |
| download | crupest-51bee1e8891ebea829a1a5e9a6502a79e0e5b6db.tar.gz crupest-51bee1e8891ebea829a1a5e9a6502a79e0e5b6db.tar.bz2 crupest-51bee1e8891ebea829a1a5e9a6502a79e0e5b6db.zip | |
Merge test into aio.
| -rwxr-xr-x | tool/aio.py | 11 | ||||
| -rw-r--r-- | tool/modules/test.py | 31 | ||||
| -rwxr-xr-x | tool/test-crupest-api.py | 39 | 
3 files changed, 42 insertions, 39 deletions
| diff --git a/tool/aio.py b/tool/aio.py index 9a7ab33..e2a9f13 100755 --- a/tool/aio.py +++ b/tool/aio.py @@ -25,6 +25,7 @@ from modules.check import *  from modules.backup import *  from modules.download_tools import *  from modules.helper import * +from modules.test import *  console = Console() @@ -103,6 +104,10 @@ docker_subparsers.add_parser("down", help="Run docker compose down.")  docker_subparsers.add_parser(      "prune", help="Run docker system prune -a -f.") +test_parser = subparsers.add_parser("test", help="Test things.") +test_parser.add_argument( +    "test_action", help="Test action.", choices=["crupest-api"]) +  args = parser.parse_args()  if args.yes: @@ -468,6 +473,12 @@ def run():                      "To renew certs previously created (nginx):", style="cyan")                  console.print(certbot_command_gen(                      domain, 'renew', test=is_test), soft_wrap=True) +        case "test": +            match args.test_action: +                case "crupest-api": +                    test_crupest_api(console) +                case _: +                    console.print("Test action invalid.", style="red")          case _:              console.print("First let's check all the templates...") diff --git a/tool/modules/test.py b/tool/modules/test.py new file mode 100644 index 0000000..d6eb778 --- /dev/null +++ b/tool/modules/test.py @@ -0,0 +1,31 @@ +import json +from http.client import * +from urllib.request import urlopen + + +def test_crupest_api(console): +    def do_the_test(): +        res: HTTPResponse = urlopen("http://localhost:5188/api/todos") +        body = res.read() + +        if res.status != 200: +            raise Exception("Status code is not 200.") +        result = json.loads(body) +        if not isinstance(result,  list): +            raise Exception("Result is not an array.") +        if len(result) == 0: +            raise Exception("Result is an empty array.") +        if not isinstance(result[0], dict): +            raise Exception("Result[0] is not an object.") +        if not isinstance(result[0].get("title"), str): +            raise Exception("Result[0].title is not a string.") +        if not isinstance(result[0].get("status"), str): +            raise Exception("Result[0].status is not a string.") + +    try: +        do_the_test() +        console.print("Test passed!", style="green") +        exit(0) +    except Exception as e: +        console.print(e) +        console.print("Test failed!", style="red") diff --git a/tool/test-crupest-api.py b/tool/test-crupest-api.py deleted file mode 100755 index c89a0f9..0000000 --- a/tool/test-crupest-api.py +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env python3 - -import json -import time -from os.path import * -from urllib.request import urlopen -from http.client import * -from rich.console import Console -from modules.path import * - -console = console = Console() - - -def do_the_test(): -    res: HTTPResponse = urlopen("http://localhost:5188/api/todos") -    body = res.read() - -    if res.status != 200: -        raise Exception("Status code is not 200.") -    result = json.loads(body) -    if not isinstance(result,  list): -        raise Exception("Result is not an array.") -    if len(result) == 0: -        raise Exception("Result is an empty array.") -    if not isinstance(result[0], dict): -        raise Exception("Result[0] is not an object.") -    if not isinstance(result[0].get("title"), str): -        raise Exception("Result[0].title is not a string.") -    if not isinstance(result[0].get("status"), str): -        raise Exception("Result[0].status is not a string.") - - -try: -    do_the_test() -    console.print("Test passed!", style="green") -    exit(0) -except Exception as e: -    console.print(e) -    console.print("Test failed!", style="red") | 
