diff --git a/integration/ci.sh b/integration/ci.sh new file mode 100755 index 0000000..ef709ff --- /dev/null +++ b/integration/ci.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +set -Exeuo pipefail + +source integration/lib.sh + + +main() { + docker_compose_down || true + docker_compose_up + setup_env + wait_for_env + gitea_root + support_repo_init + fleet_repo_init + teardown_env + docker_compose_down +} + +main diff --git a/integration/cli.py b/integration/cli.py index 7ca3516..9ffc9be 100644 --- a/integration/cli.py +++ b/integration/cli.py @@ -25,6 +25,7 @@ class Gitea: self.login() self.create_repository() self.install_sso() + self.add_deploy_key() def __add_credentials_parser(self, parser): group = parser.add_argument_group("credentials", "User credentials") @@ -120,6 +121,28 @@ class Gitea: self.install_sso_parser.set_defaults(func=run) + def add_deploy_key(self): + def run(args, c: Session): + gitea = gitea_from_args(args, c=c) + gitea.login() + gitea.add_deploy_key(repo=args.repo_name, key=args.key_file) + + self.add_deploy_key_parser = self.subparser.add_parser( + name="add_deploy_key", + description="Create repository on Gitea", + help="Add deploy key to a repository on Gitea", + ) + self.__add_credentials_parser(self.add_deploy_key_parser) + self.add_deploy_key_parser.add_argument( + "repo_name", + type=str, + help="Name of the repository to which key should be added", + ) + self.add_deploy_key_parser.add_argument( + "key_file", type=str, help="Path of the deploy key. Public key." + ) + self.add_deploy_key_parser.set_defaults(func=run) + def dash_from_args(args, c: Session): from .hostea import Hostea diff --git a/integration/gitea.py b/integration/gitea.py index c842269..8ca28ad 100755 --- a/integration/gitea.py +++ b/integration/gitea.py @@ -70,8 +70,8 @@ class Gitea: Install Gitea, first form that a user sees when a new instance is deployed """ - cwd = os.environ.get("PWD") - user = os.environ.get("USER") + # cwd = os.environ.get("PWD") + # user = os.environ.get("USER") payload = { "db_type": "sqlite3", "db_host": "localhost:3306", @@ -81,16 +81,16 @@ class Gitea: "ssl_mode": "disable", "db_schema": "", "charset": "utf8", - "db_path": f"{cwd}/tmp/gitea/db/gitea.db", + "db_path": "/data/gitea/gitea.db", "app_name": "Gitea:+Git+with+a+cup+of+tea", - "repo_root_path": f"{cwd}/tmp/gitea/repos/", - "lfs_root_path": f"{cwd}/tmp/gitea/lfs/", - "run_user": user, + "repo_root_path": "/data/git/repositories", + "lfs_root_path": "/data/git/lfs", + "run_user": "git", "domain": "localhost", - "ssh_port": "2222", + "ssh_port": "2221", "http_port": "3000", - "app_url": self.get_uri(""), - "log_root_path": f"{cwd}/tmp/gitea/log/", + "app_url": "http://localhost:3000/", + "log_root_path": "/data/gitea/log", "smtp_host": "", "smtp_from": "", "smtp_user": "", @@ -285,6 +285,18 @@ class Gitea: resp = self.c.post(self.get_uri("/admin/auths/new"), data=payload) + def add_deploy_key(self, repo: str, key: str): + url = self.get_api_uri(f"/api/v1/repos/{self.username}/{repo}/keys") + with open(key, "r", encoding="utf-8") as f: + key = f.read() + payload = { + "key": key, + "read_only": False, + "title": f"{self.username}/{repo} Dashboard test key", + } + resp = self.c.post(url, json=payload) + assert resp.status_code == 201 + class ParseSSOLogin(HTMLParser): url: str = None diff --git a/integration/lib.sh b/integration/lib.sh index 91f6384..30f7e61 100755 --- a/integration/lib.sh +++ b/integration/lib.sh @@ -1,5 +1,4 @@ -readonly GITEA_PID_FILE=./tmp/gitea.pid -readonly SERVER_PID_FILE=./tmp/gitea.pid +readonly SERVER_PID_FILE=./tmp/server.pid readonly DASHBOARD_URL="http://localhost:8000" readonly GITEA_URL="http://localhost:3000" @@ -17,11 +16,13 @@ readonly GITEA_ROOT_EMAIL="$GITEA_ROOT_USERNAME@example.org" readonly GITEA_ROOT_PASSOWRD=supercomplicatedpassword readonly GITEA_HOSTEA_SSO_NAME=hostea-sso readonly GITEA_OIDC_CALLBACK="$GITEA_URL/user/oauth2/$GITEA_HOSTEA_SSO_NAME/callback" +readonly GITEA_HOSTEA_FLEET_DEPLOY_KEY="tests/fleet-deploy-key.pub" readonly GITEA_HOSTEA_USERNAME=hostea readonly GITEA_HOSTEA_PASSWORD=supercomplicatedpassword readonly GITEA_HOSTEA_EMAIL="$GITEA_HOSTEA_USERNAME@example.org" readonly GITEA_HOSTEA_SUPPORT_REPO="support" +readonly GITEA_HOSTEA_FLEET_REPO="fleet" readonly HOSTEA_CUSTOMER_USERNAME=batman readonly HOSTEA_CUSTOMER_PASSWORD=supercomplicatedpassword @@ -38,7 +39,6 @@ OIDC_CLIENT_ID="" OIDC_CLIENT_SECRET="" wait_for_env() { - curl $DASHBOARD_URL || true python -m integration \ check_env $GITEA_URL $DASHBOARD_URL $MAILDEV_URL } @@ -76,14 +76,14 @@ gitea_root(){ $GITEA_ROOT_USERNAME $GITEA_ROOT_PASSOWRD \ $GITEA_ROOT_EMAIL \ $GITEA_URL - python -m integration \ - gitea install_sso \ - $GITEA_ROOT_USERNAME $GITEA_ROOT_PASSOWRD \ - $GITEA_ROOT_EMAIL \ - $GITEA_URL \ - $GITEA_HOSTEA_SSO_NAME \ - $OIDC_CLIENT_ID $OIDC_CLIENT_SECRET \ - $DASHBOARD_OIDC_DISCOVERY_URL +# python -m integration \ +# gitea install_sso \ +# $GITEA_ROOT_USERNAME $GITEA_ROOT_PASSOWRD \ +# $GITEA_ROOT_EMAIL \ +# $GITEA_URL \ +# $GITEA_HOSTEA_SSO_NAME \ +# $OIDC_CLIENT_ID $OIDC_CLIENT_SECRET \ +# $DASHBOARD_OIDC_DISCOVERY_URL } @@ -107,6 +107,34 @@ support_repo_init() { $GITEA_HOSTEA_SUPPORT_REPO } +# register user "Hostea" on Gitea and create support repository +fleet_repo_init() { + python -m integration \ + gitea register \ + $GITEA_HOSTEA_USERNAME $GITEA_HOSTEA_PASSWORD \ + $GITEA_HOSTEA_EMAIL \ + $GITEA_URL || true + python -m integration \ + gitea login \ + $GITEA_HOSTEA_USERNAME $GITEA_HOSTEA_PASSWORD \ + $GITEA_HOSTEA_EMAIL \ + $GITEA_URL + python -m integration \ + gitea create_repo \ + $GITEA_HOSTEA_USERNAME $GITEA_HOSTEA_PASSWORD \ + $GITEA_HOSTEA_EMAIL \ + $GITEA_URL \ + $GITEA_HOSTEA_FLEET_REPO + + python -m integration \ + gitea add_deploy_key \ + $GITEA_HOSTEA_USERNAME $GITEA_HOSTEA_PASSWORD \ + $GITEA_HOSTEA_EMAIL \ + $GITEA_URL \ + $GITEA_HOSTEA_FLEET_REPO \ + $GITEA_HOSTEA_FLEET_DEPLOY_KEY +} + # Create user on Hostea to simulate a Hostea customer hostea_customer_simulation() { python -m integration \ @@ -128,39 +156,22 @@ hostea_customer_simulation() { $GITEA_HOSTEA_USERNAME $GITEA_HOSTEA_SUPPORT_REPO } -gitea(){ - readonly BIN=tmp/gitea/bin/gitea - readonly SOURCE="https://github.com/go-gitea/gitea/releases/download/v1.16.5/gitea-1.16.5-linux-amd64" - readonly CONFIG_FILE=gitea/app.ini - - mkdir -p tmp/gitea/bin || true - - for dir in repos db lfs log - do - rm -rf tmp/gitea/$dir || true - mkdir -p tmp/gitea/$dir || true - done - - if [ ! -e $BIN ]; - then - wget --quiet --output-document=$BIN $SOURCE - chmod +x $BIN - fi - - nohup $BIN --config $CONFIG_FILE web > /dev/null 2>&1 & - GITEA_PID=$! - echo $GITEA_PID > $GITEA_PID_FILE -} - setup_env() { mkdir tmp/ || true nohup python manage.py runserver > /dev/null 2>&1 & SERVER_PID=$! echo $SERVER_PID > $SERVER_PID_FILE - gitea } teardown_env() { - kill $(cat $GITEA_PID_FILE) kill $(cat $SERVER_PID_FILE) } + +docker_compose_up() { + docker-compose -f docker-compose-dev-deps.yml up -d +} + +docker_compose_down() { + docker-compose -f docker-compose-dev-deps.yml down + docker-compose -f docker-compose-dev-deps.yml down --remove-orphans +}