Skip to content

Commit

Permalink
Merge pull request #432 from OrenZhang/main
Browse files Browse the repository at this point in the history
chore: 增加单元测试和代码扫描 --story=119460096
  • Loading branch information
OrenZhang authored Sep 9, 2024
2 parents 0369ebd + e1fe296 commit a1c084b
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 7 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: "CodeQL"

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "*" ]

jobs:
analyze:
name: Analyze
runs-on: 'ubuntu-latest'
timeout-minutes: 360
permissions:
actions: read
contents: read
security-events: write
strategy:
fail-fast: false
matrix:
language: [ "python", "javascript" ]
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Initialize CodeQL
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
- name: Autobuild
uses: github/codeql-action/autobuild@v3
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
with:
category: "/language:${{matrix.language}}"
98 changes: 98 additions & 0 deletions .github/workflows/unittest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: Unittest

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "*" ]

jobs:
unittest:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:8
env:
MYSQL_ROOT_PASSWORD: root
ports:
- 3306:3306
options: >-
--health-cmd="mysqladmin ping --silent"
--health-interval=10s
--health-timeout=5s
--health-retries=3
redis:
image: redis:latest
ports:
- 6379:6379
options: >-
--health-cmd="redis-cli ping"
--health-interval=10s
--health-timeout=5s
--health-retries=3
env:
BKPAAS_APP_MODULE_NAME: "api"
BKAPP_DEPLOY_SERVICE: "web"
DJANGO_SETTINGS_MODULE: "settings"
BKAPP_IS_KUBERNETES: "True"
BKPAAS_MAJOR_VERSION: "3"
BKPAAS_ENGINE_REGION: "open"
BKPAAS_URL: "https://paas.example.com"
BKPAAS_BK_DOMAIN: "paas.example.com"
BKPAAS_APP_ID: "bk_audit"
BKPAAS_APP_SECRET: "bk_audit"
BKAPP_BK_APIGW_NAME: "bk-audit"
BK_API_URL_TMPL: "https://bkapi.paas.example.com/api/{api_name}"
BK_COMPONENT_API_URL: "https://bkapi.paas.example.com"
BKAPP_IAM_SYSTEM_ID: "bk-audit"
BKAPP_IAM_SYSTEM_NAME: "审计中心"
MYSQL_HOST: "127.0.0.1"
MYSQL_PORT: "3306"
MYSQL_NAME: "bk_audit"
MYSQL_USER: "root"
MYSQL_PASSWORD: "root"
REDIS_HOST: "127.0.0.1"
REDIS_PORT: "6379"
REDIS_DB: "0"
REDIS_PASSWORD: ""
REDIS_KEY_PREFIX: "bk_audit"
BKAPP_PLATFORM_AUTH_ACCESS_USERNAME: "admin"
BKAPP_PLATFORM_AUTH_ACCESS_TOKEN: "admin"
BKAPP_BK_IAM_APIGATEWAY_URL: "https://bkapi.paas.example.com/api/bk-iam/prod"
BKAPP_BK_IAM_RESOURCE_API_HOST: "https://bkaudit-api.paas.example.com/"
BKPAAS_IAM_URL: "http://bkiam.paas.example.com"
BKAPP_BK_AUDIT_SAAS_URL: "http://bkaudit.paas.example.com"
BKAPP_BK_AUDIT_PULLER_SAAS_URL: "http://bkaudit-puller.paas.example.com"
BKAPP_BK_ITSM_SAAS_URL: "http://apps.paas.example.com/bk--itsm"
BKAPP_BK_SOPS_SAAS_URL: "http://apps.paas.example.com/bk--sops"
BKAPP_BK_DATAWEB_SAAS_URL: "http://apps.paas.example.com/bk--dataweb"
BKAPP_ADMIN_USERNAMES: "admin"
BKAPP_SKIP_IAM_MIGRATION: "True"
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.10'
- name: Install dependencies
run: |
cd src/backend
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -r requirements_dev.txt
- name: Create DB
run: |
mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASSWORD -P$MYSQL_PORT -e "CREATE DATABASE $MYSQL_NAME CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
- name: Run migrations
run: |
cd src/backend
python manage.py migrate
- name: Run tests
run: |
cd src/backend
pytest --cov=.
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
13 changes: 8 additions & 5 deletions src/backend/config/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,14 @@
DATABASES = {
"default": {
"ENGINE": "django.db.backends.mysql",
"NAME": APP_CODE,
"USER": "root",
"PASSWORD": "",
"HOST": "localhost",
"PORT": "3306",
"NAME": os.getenv("MYSQL_NAME", APP_CODE),
"USER": os.getenv("MYSQL_USER", "root"),
"PASSWORD": os.getenv("MYSQL_PASSWORD", ""),
"HOST": os.getenv("MYSQL_HOST", "localhost"),
"PORT": os.getenv("MYSQL_PORT", "3306"),
"OPTIONS": {
"charset": "utf8mb4",
},
},
}

Expand Down
7 changes: 7 additions & 0 deletions src/backend/services/web/risk/migrations/0010_iam_migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@
to the current version of the project delivered to anyone in the future.
"""

import os

from django.db import migrations
from iam.contrib.iam_migration.migrator import IAMMigrator

from core.utils.distutils import strtobool


def iam_migrate(*args, **kwargs):
if strtobool(os.getenv("BKAPP_SKIP_IAM_MIGRATION", "False")):
return

migrator = IAMMigrator("initial.json")
migrator.migrate()

Expand Down
7 changes: 7 additions & 0 deletions src/backend/services/web/vision/migrations/0001_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@
to the current version of the project delivered to anyone in the future.
"""

import os

from django.db import migrations
from iam.contrib.iam_migration.migrator import IAMMigrator

from core.utils.distutils import strtobool


def iam_migrate(*args, **kwargs):
if strtobool(os.getenv("BKAPP_SKIP_IAM_MIGRATION", "False")):
return

migrator = IAMMigrator("initial.json")
migrator.migrate()

Expand Down
5 changes: 3 additions & 2 deletions src/backend/tests/version/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
We undertake not to change the open source license (MIT license) applicable
to the current version of the project delivered to anyone in the future.
"""

from unittest import mock

from blueapps.account.components.bk_ticket.models import UserProxy
from django.contrib.auth import get_user_model


class GetLocalRequestMock(mock.MagicMock):
COOKIES = {"bk_token": ""}
user = UserProxy(username="admin")
user = get_user_model()(username="admin")


# Version Info
Expand Down

0 comments on commit a1c084b

Please sign in to comment.