Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: risk calc regression tests (#16799)

p0ny3258803e1bc6
diff --git a/backend/runner/approval/runner.go b/backend/runner/approval/runner.go
index 2f2711b03f..629741ef1d 100644
--- a/backend/runner/approval/runner.go
+++ b/backend/runner/approval/runner.go
@@ -136,8 +136,7 @@ func (r *Runner) findApprovalTemplateForIssue(ctx context.Context, issue *store.
approvalTemplate, riskLevel, done, err := func() (*storepb.ApprovalTemplate, storepb.IssuePayloadApproval_RiskLevel, bool, error) {
// no need to find if
// - feature is not enabled
- // - approval setting rules are empty
- if r.licenseService.IsFeatureEnabled(v1pb.PlanFeature_FEATURE_APPROVAL_WORKFLOW) != nil || len(approvalSetting.Rules) == 0 {
+ if r.licenseService.IsFeatureEnabled(v1pb.PlanFeature_FEATURE_APPROVAL_WORKFLOW) != nil {
// nolint:nilerr
return nil, 0, true, nil
}
@@ -276,6 +275,10 @@ func (r *Runner) findApprovalTemplateForIssue(ctx context.Context, issue *store.
}
func getApprovalTemplate(approvalSetting *storepb.WorkspaceApprovalSetting, riskLevel int32, riskSource store.RiskSource) (*storepb.ApprovalTemplate, error) {
+ if len(approvalSetting.Rules) == 0 {
+ return nil, nil
+ }
+
e, err := cel.NewEnv(common.ApprovalFactors...)
if err != nil {
return nil, err
diff --git a/backend/tests/risk_calculation_test.go b/backend/tests/risk_calculation_test.go
new file mode 100644
index 0000000000..43def8ff44
--- /dev/null
+++ b/backend/tests/risk_calculation_test.go
@@ -0,0 +1,264 @@
+package tests
+
+import (
+ "context"
+ "fmt"
+ "testing"
+ "time"
+
+ "connectrpc.com/connect"
+ "github.com/google/uuid"
+ "github.com/stretchr/testify/require"
+ "google.golang.org/genproto/googleapis/type/expr"
+
+ v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
+)
+
+func TestRiskLevelCalculation(t *testing.T) {
+ t.Parallel()
+ a := require.New(t)
+ ctx := context.Background()
+ ctl := &controller{}
+
+ ctx, err := ctl.StartServerWithExternalPg(ctx)
+ a.NoError(err)
+ defer ctl.Close(ctx)
+
+ // Create instance
+ instanceDir := t.TempDir()
+ instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
+ InstanceId: generateRandomString("inst"),
+ Instance: &v1pb.Instance{
+ Title: "Prod Instance",
+ Engine: v1pb.Engine_SQLITE,
+ Environment: "environments/prod",
+ Activation: true,
+ DataSources: []*v1pb.DataSource{{
+ Type: v1pb.DataSourceType_ADMIN,
+ Host: instanceDir,
+ Id: "admin",
+ }},
+ },
+ }))
+ a.NoError(err)
+
+ // Create database
+ dbName := generateRandomString("db")
+ err = ctl.createDatabaseV2(ctx, ctl.project, instanceResp.Msg, nil, dbName, "")
+ a.NoError(err)
+
+ // Create a risk rule that evaluates based on environment context
+ // This should work without summary reports
+ _, err = ctl.riskServiceClient.CreateRisk(ctx, connect.NewRequest(&v1pb.CreateRiskRequest{
+ Risk: &v1pb.Risk{
+ Title: "Production DDL Risk",
+ Source: v1pb.Risk_DDL,
+ Level: 300, // HIGH risk level
+ Active: true,
+ Condition: &expr.Expr{
+ Expression: `environment_id == "prod"`,
+ },
+ },
+ }))
+ a.NoError(err)
+
+ // Create sheet with DDL statement
+ sheet, err := ctl.sheetServiceClient.CreateSheet(ctx, connect.NewRequest(&v1pb.CreateSheetRequest{
+ Parent: ctl.project.Name,
+ Sheet: &v1pb.Sheet{
+ Title: "Test DDL Sheet",
+ Content: []byte("CREATE TABLE test_table (id INTEGER PRIMARY KEY, name TEXT);"),
+ },
+ }))
+ a.NoError(err)
+
+ planResp, err := ctl.planServiceClient.CreatePlan(ctx, connect.NewRequest(&v1pb.CreatePlanRequest{
+ Parent: ctl.project.Name,
+ Plan: &v1pb.Plan{
+ Title: "Test DDL Plan",
+ Specs: []*v1pb.Plan_Spec{{
+ Id: uuid.NewString(),
+ Config: &v1pb.Plan_Spec_ChangeDatabaseConfig{
+ ChangeDatabaseConfig: &v1pb.Plan_ChangeDatabaseConfig{
+ Targets: []string{fmt.Sprintf("%s/databases/%s", instanceResp.Msg.Name, dbName)},
+ Sheet: sheet.Msg.Name,
+ Type: v1pb.Plan_ChangeDatabaseConfig_MIGRATE,
+ },
+ },
+ }},
+ },
+ }))
+ a.NoError(err, "Plan creation should succeed even without summary reports")
+
+ // Verify plan was created successfully
+ a.NotNil(planResp.Msg)
+ a.Equal("Test DDL Plan", planResp.Msg.Title)
+
+ // Create issue with the plan - this triggers risk level calculation
+ issueResp, err := ctl.issueServiceClient.CreateIssue(ctx, connect.NewRequest(&v1pb.CreateIssueRequest{
+ Parent: ctl.project.Name,
+ Issue: &v1pb.Issue{
+ Title: "Test Issue for Risk Calculation",
+ Type: v1pb.Issue_DATABASE_CHANGE,
+ Description: "Testing risk calculation without summary reports",
+ Plan: planResp.Msg.Name,
+ },
+ }))
+ a.NoError(err, "Issue creation should succeed even without summary reports")
+
+ // Check issue status 5 times with 3 second intervals
+ // issue.ApprovalFindingDone indicates that the approval flow is generated and issue.RiskLevel is available
+ var issue *v1pb.Issue
+ for i := 0; i < 5; i++ {
+ if i > 0 {
+ time.Sleep(3 * time.Second) // Wait 3 seconds between checks
+ }
+
+ issueGetResp, err := ctl.issueServiceClient.GetIssue(ctx, connect.NewRequest(&v1pb.GetIssueRequest{
+ Name: issueResp.Msg.Name,
+ }))
+ a.NoError(err)
+ issue = issueGetResp.Msg
+
+ // Log the current state for debugging
+ t.Logf("Check %d: ApprovalFindingDone=%v, RiskLevel=%v, ApprovalFindingError='%s'",
+ i+1, issue.ApprovalFindingDone, issue.RiskLevel, issue.ApprovalFindingError)
+
+ // Check if approval finding is complete
+ if issue.ApprovalFindingDone {
+ break
+ }
+ }
+
+ // Verify that the approval finding process completed successfully
+ a.NotNil(issue, "Issue should be retrievable")
+ a.True(issue.ApprovalFindingDone, "Approval finding should complete even without summary reports")
+ a.Empty(issue.ApprovalFindingError, "Approval finding should not have errors despite missing summary reports")
+
+ a.Equal(v1pb.Issue_HIGH, issue.RiskLevel, "Issue risk level should be HIGH")
+}
+
+// TestRiskLevelCalculationWithInvalidSQL tests the fix for issue where
+// risk level calculation should work without requiring summary reports.
+// This is a regression test for PR #16793.
+func TestRiskLevelCalculationWithInvalidSQL(t *testing.T) {
+ t.Parallel()
+ a := require.New(t)
+ ctx := context.Background()
+ ctl := &controller{}
+
+ ctx, err := ctl.StartServerWithExternalPg(ctx)
+ a.NoError(err)
+ defer ctl.Close(ctx)
+
+ // Create instance
+ instanceDir := t.TempDir()
+ instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
+ InstanceId: generateRandomString("inst"),
+ Instance: &v1pb.Instance{
+ Title: "Prod Instance",
+ Engine: v1pb.Engine_SQLITE,
+ Environment: "environments/prod",
+ Activation: true,
+ DataSources: []*v1pb.DataSource{{
+ Type: v1pb.DataSourceType_ADMIN,
+ Host: instanceDir,
+ Id: "admin",
+ }},
+ },
+ }))
+ a.NoError(err)
+
+ // Create database
+ dbName := generateRandomString("db")
+ err = ctl.createDatabaseV2(ctx, ctl.project, instanceResp.Msg, nil, dbName, "")
+ a.NoError(err)
+
+ // Create a risk rule that evaluates based on environment context
+ // This should work without summary reports even for invalid SQL
+ _, err = ctl.riskServiceClient.CreateRisk(ctx, connect.NewRequest(&v1pb.CreateRiskRequest{
+ Risk: &v1pb.Risk{
+ Title: "Production DDL Risk",
+ Source: v1pb.Risk_DDL,
+ Level: 300, // HIGH risk level
+ Active: true,
+ Condition: &expr.Expr{
+ Expression: `environment_id == "prod"`,
+ },
+ },
+ }))
+ a.NoError(err)
+
+ // Create sheet with invalid SQL statement "hh"
+ // This will definitely fail summary report generation
+ sheet, err := ctl.sheetServiceClient.CreateSheet(ctx, connect.NewRequest(&v1pb.CreateSheetRequest{
+ Parent: ctl.project.Name,
+ Sheet: &v1pb.Sheet{
+ Title: "Invalid SQL Sheet",
+ Content: []byte("hh"),
+ },
+ }))
+ a.NoError(err)
+
+ // Create plan with invalid SQL - this should succeed despite summary report failure
+ planResp, err := ctl.planServiceClient.CreatePlan(ctx, connect.NewRequest(&v1pb.CreatePlanRequest{
+ Parent: ctl.project.Name,
+ Plan: &v1pb.Plan{
+ Title: "Invalid SQL Plan",
+ Specs: []*v1pb.Plan_Spec{{
+ Id: uuid.NewString(),
+ Config: &v1pb.Plan_Spec_ChangeDatabaseConfig{
+ ChangeDatabaseConfig: &v1pb.Plan_ChangeDatabaseConfig{
+ Targets: []string{fmt.Sprintf("%s/databases/%s", instanceResp.Msg.Name, dbName)},
+ Sheet: sheet.Msg.Name,
+ Type: v1pb.Plan_ChangeDatabaseConfig_MIGRATE,
+ },
+ },
+ }},
+ },
+ }))
+ a.NoError(err, "Plan creation should succeed even with invalid SQL that breaks summary reports")
+
+ // Create issue with the plan - this triggers risk level calculation
+ issueResp, err := ctl.issueServiceClient.CreateIssue(ctx, connect.NewRequest(&v1pb.CreateIssueRequest{
+ Parent: ctl.project.Name,
+ Issue: &v1pb.Issue{
+ Title: "Test Issue with Invalid SQL",
+ Type: v1pb.Issue_DATABASE_CHANGE,
+ Description: "Testing risk calculation with invalid SQL",
+ Plan: planResp.Msg.Name,
+ },
+ }))
+ a.NoError(err, "Issue creation should succeed even with invalid SQL in plan")
+
+ // Check issue status 5 times with 3 second intervals
+ // issue.ApprovalFindingDone indicates that the approval flow is generated and issue.RiskLevel is available
+ var issue *v1pb.Issue
+ for i := 0; i < 5; i++ {
+ if i > 0 {
+ time.Sleep(3 * time.Second) // Wait 3 seconds between checks
+ }
+
+ issueGetResp, err := ctl.issueServiceClient.GetIssue(ctx, connect.NewRequest(&v1pb.GetIssueRequest{
+ Name: issueResp.Msg.Name,
+ }))
+ a.NoError(err)
+ issue = issueGetResp.Msg
+
+ // Log the current state for debugging
+ t.Logf("Check %d: ApprovalFindingDone=%v, RiskLevel=%v, ApprovalFindingError='%s'",
+ i+1, issue.ApprovalFindingDone, issue.RiskLevel, issue.ApprovalFindingError)
+
+ // Check if approval finding is complete
+ if issue.ApprovalFindingDone {
+ break
+ }
+ }
+
+ // Verify that the approval finding process completed successfully despite invalid SQL
+ a.NotNil(issue, "Issue should be retrievable even with invalid SQL")
+ a.True(issue.ApprovalFindingDone, "Approval finding should complete even with invalid SQL")
+ a.Empty(issue.ApprovalFindingError, "Approval finding should not have errors despite invalid SQL")
+
+ a.Equal(v1pb.Issue_HIGH, issue.RiskLevel, "Issue risk level should be HIGH")
+}
diff --git a/backend/tests/tests.go b/backend/tests/tests.go
index b8d6e24311..faeead4baa 100644
--- a/backend/tests/tests.go
+++ b/backend/tests/tests.go
@@ -112,6 +112,7 @@ type controller struct {
workspaceServiceClient v1connect.WorkspaceServiceClient
releaseServiceClient v1connect.ReleaseServiceClient
revisionServiceClient v1connect.RevisionServiceClient
+ riskServiceClient v1connect.RiskServiceClient
project *v1pb.Project
@@ -297,6 +298,7 @@ func (ctl *controller) start(ctx context.Context, port int) (context.Context, er
ctl.workspaceServiceClient = v1connect.NewWorkspaceServiceClient(ctl.client, baseURL, interceptors)
ctl.releaseServiceClient = v1connect.NewReleaseServiceClient(ctl.client, baseURL, interceptors)
ctl.revisionServiceClient = v1connect.NewRevisionServiceClient(ctl.client, baseURL, interceptors)
+ ctl.riskServiceClient = v1connect.NewRiskServiceClient(ctl.client, baseURL, interceptors)
if err := ctl.waitForHealthz(ctx); err != nil {
return nil, errors.Wrap(err, "failed to wait for healthz")