Skip to content

Commit

Permalink
重构 audit 相关代码, 改为面向对象实现 (#2348)
Browse files Browse the repository at this point in the history
* 重构 audit 相关代码, 改为面向对象实现

* 使用 pytst 运行测试

* fix tests

* fix coverage generation

* more coverage

* 修复 audit_groups 的类型错误

* 不抛错给用户, 改为输出日志
  • Loading branch information
LeoQuote authored Nov 3, 2023
1 parent 3cdfba0 commit ba985e9
Show file tree
Hide file tree
Showing 20 changed files with 1,502 additions and 1,439 deletions.
8 changes: 0 additions & 8 deletions .coveragerc

This file was deleted.

6 changes: 3 additions & 3 deletions .github/workflows/django.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ jobs:
run: |
sudo apt-get update && sudo apt-get install libsasl2-dev libkrb5-dev libldap2-dev libssl-dev unixodbc unixodbc-dev
python -m pip install --upgrade pip
pip install codecov coverage flake8 -r requirements.txt
pip install -r requirements.txt
pip install -r dev-requirements.txt
- name: Init Table
run: |
Expand All @@ -83,8 +84,7 @@ jobs:
run: |
python manage.py makemigrations
python manage.py makemigrations sql
coverage run manage.py test -v 3 --keepdb
coverage xml
pytest --cov --cov-report xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ sonar-project.properties
.scannerwork
.env
local_settings.py
src/docker-compose-dev
src/docker-compose-dev
.coverage
3 changes: 3 additions & 0 deletions archery/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"sql.notify:GenericWebhookNotifier",
],
),
CURRENT_AUDITOR=(str, "sql.utils.workflow_audit:AuditV2"),
)

# SECURITY WARNING: keep the secret key used in production secret!
Expand Down Expand Up @@ -103,6 +104,8 @@

ENABLED_ENGINES = env("ENABLED_ENGINES")

CURRENT_AUDITOR = env("CURRENT_AUDITOR")

# Application definition
INSTALLED_APPS = (
"django.contrib.admin",
Expand Down
12 changes: 12 additions & 0 deletions common/utils/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ class WorkflowStatus(models.IntegerChoices):
ABORTED = 3, "审核取消"


class WorkflowAction(models.IntegerChoices):
"""工单操作列表, 必须是动词, 不是一种状态"""

SUBMIT = 0, "提交"
PASS = 1, "审核通过"
REJECT = 2, "审核不通过"
ABORT = 3, "审核取消"
EXECUTE_SET_TIME = 4, "设置定时执行"
EXECUTE_START = 5, "开始执行"
EXECUTE_END = 6, "执行结束"


class SQLTuning:
SYS_PARM_FILTER = [
"BINLOG_CACHE_SIZE",
Expand Down
129 changes: 129 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import datetime

import pytest

from common.utils.const import WorkflowStatus
from sql.models import (
Instance,
ResourceGroup,
SqlWorkflow,
SqlWorkflowContent,
QueryPrivilegesApply,
ArchiveConfig,
)
from common.config import SysConfig


@pytest.fixture
def normal_user(django_user_model):
user = django_user_model.objects.create(
username="test_user", display="中文显示", is_active=True
)
yield user
user.delete()


@pytest.fixture
def super_user(django_user_model):
user = django_user_model.objects.create(
username="super_user", display="超级用户", is_active=True, is_superuser=True
)
yield user
user.delete()


@pytest.fixture
def db_instance(db):
ins = Instance.objects.create(
instance_name="some_ins",
type="slave",
db_type="mysql",
host="some_host",
port=3306,
user="ins_user",
password="some_str",
)
yield ins
ins.delete()


@pytest.fixture
def resource_group(db) -> ResourceGroup:
res_group = ResourceGroup.objects.create(group_id=1, group_name="group_name")
yield res_group
res_group.delete()


@pytest.fixture
def sql_workflow(db_instance):
wf = SqlWorkflow.objects.create(
workflow_name="some_name",
group_id=1,
group_name="g1",
engineer_display="",
audit_auth_groups="some_audit_group",
create_time=datetime.datetime.now(),
status="workflow_timingtask",
is_backup=True,
instance=db_instance,
db_name="some_db",
syntax_type=1,
)
wf_content = SqlWorkflowContent.objects.create(
workflow=wf, sql_content="some_sql", execute_result=""
)
yield wf, wf_content
wf.delete()
wf_content.delete()


@pytest.fixture
def sql_query_apply(db_instance):
tomorrow = datetime.datetime.today() + datetime.timedelta(days=1)
query_apply_1 = QueryPrivilegesApply.objects.create(
group_id=1,
group_name="some_name",
title="some_title1",
user_name="some_user",
instance=db_instance,
db_list="some_db,some_db2",
limit_num=100,
valid_date=tomorrow,
priv_type=1,
status=0,
audit_auth_groups="some_audit_group",
)
yield query_apply_1
query_apply_1.delete()


@pytest.fixture
def archive_apply(db_instance, resource_group):
archive_apply_1 = ArchiveConfig.objects.create(
title="title",
resource_group=resource_group,
audit_auth_groups="",
src_instance=db_instance,
src_db_name="src_db_name",
src_table_name="src_table_name",
dest_instance=db_instance,
dest_db_name="src_db_name",
dest_table_name="src_table_name",
condition="1=1",
mode="file",
no_delete=True,
sleep=1,
status=WorkflowStatus.WAITING,
state=False,
user_name="some_user",
user_display="display",
)
yield archive_apply_1
archive_apply_1.delete()


@pytest.fixture
def setup_sys_config(db):
sys_config = SysConfig()
yield sys_config
sys_config.purge()
6 changes: 6 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pytest
pytest-django
pytest-mock
pytest-cov
codecov
flake8
28 changes: 28 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "archery.settings"
python_files = "tests.py test_*.py *_tests.py"

[tool.coverage.run]
source = [
"."
]
omit = [
# omit anything in a .local directory anywhere
"src*",
# omit everything in /usr
"downloads*",
# omit this single file
"sql/migrations/*",
"venv*"
]

[tool.coverage.report]
omit = [
# omit anything in a .local directory anywhere
"src*",
# omit everything in /usr
"downloads*",
# omit this single file
"sql/migrations/*",
"venv*"
]
Loading

0 comments on commit ba985e9

Please sign in to comment.