Skip to content

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

chore: add db group cel tests (#13093)

boojack0f4dc5d6bf93
diff --git a/backend/runner/taskrun/database_create_executor.go b/backend/runner/taskrun/database_create_executor.go
index 70adca0243..1a9a20d9d1 100644
--- a/backend/runner/taskrun/database_create_executor.go
+++ b/backend/runner/taskrun/database_create_executor.go
@@ -304,7 +304,7 @@ func (exec *DatabaseCreateExecutor) reconcilePlan(ctx context.Context, project *
if databaseGroup == nil || !databaseGroup.Payload.Multitenancy {
return nil
}
- isMatched, err := utils.CheckDatabaseGroupMatch(ctx, databaseGroup, createdDatabase)
+ isMatched, err := utils.CheckDatabaseGroupMatch(ctx, databaseGroup.Expression.Expression, createdDatabase)
if err != nil || !isMatched {
// continue if current database is not matched.
//nolint:nilerr
@@ -541,7 +541,7 @@ func (exec *DatabaseCreateExecutor) getPeerTenantDatabasesFromDatabaseGroup(ctx
continue
}
- isMatched, err := utils.CheckDatabaseGroupMatch(ctx, dbGroup, database)
+ isMatched, err := utils.CheckDatabaseGroupMatch(ctx, dbGroup.Expression.Expression, database)
if err != nil {
return nil, errors.Wrapf(err, "Failed to get matched and unmatched databases in database group %q", dbGroup.Placeholder)
}
diff --git a/backend/utils/utils.go b/backend/utils/utils.go
index adf1702f2b..e54c18f273 100644
--- a/backend/utils/utils.go
+++ b/backend/utils/utils.go
@@ -664,7 +664,7 @@ func GetMatchedAndUnmatchedDatabasesInDatabaseGroup(ctx context.Context, databas
// DONOT check bb.feature.database-grouping for instance. The API here is read-only in the frontend, we need to show if the instance is matched but missing required license.
// The feature guard will works during issue creation.
for _, database := range allDatabases {
- matched, err := CheckDatabaseGroupMatch(ctx, databaseGroup, database)
+ matched, err := CheckDatabaseGroupMatch(ctx, databaseGroup.Expression.Expression, database)
if err != nil {
return nil, nil, err
}
@@ -677,8 +677,8 @@ func GetMatchedAndUnmatchedDatabasesInDatabaseGroup(ctx context.Context, databas
return matches, unmatches, nil
}
-func CheckDatabaseGroupMatch(ctx context.Context, databaseGroup *store.DatabaseGroupMessage, database *store.DatabaseMessage) (bool, error) {
- prog, err := common.ValidateGroupCELExpr(databaseGroup.Expression.Expression)
+func CheckDatabaseGroupMatch(ctx context.Context, expression string, database *store.DatabaseMessage) (bool, error) {
+ prog, err := common.ValidateGroupCELExpr(expression)
if err != nil {
return false, err
}
diff --git a/backend/utils/utils_test.go b/backend/utils/utils_test.go
index 2128456f68..e09d35e638 100644
--- a/backend/utils/utils_test.go
+++ b/backend/utils/utils_test.go
@@ -1,6 +1,7 @@
package utils
import (
+ "context"
"testing"
"github.com/stretchr/testify/assert"
@@ -466,3 +467,48 @@ func TestConvertBytesToUTF8String(t *testing.T) {
assert.Equal(t, test.expected, actual)
}
}
+
+func TestCheckDatabaseGroupMatch(t *testing.T) {
+ tests := []struct {
+ expression string
+ database *store.DatabaseMessage
+ match bool
+ }{
+ {
+ expression: `resource.labels.unit == "gcp"`,
+ database: &store.DatabaseMessage{
+ Metadata: &storepb.DatabaseMetadata{
+ Labels: map[string]string{
+ "unit": "gcp",
+ },
+ },
+ },
+ match: true,
+ },
+ {
+ expression: `resource.labels.unit == "aws"`,
+ database: &store.DatabaseMessage{
+ Metadata: &storepb.DatabaseMetadata{
+ Labels: map[string]string{
+ "unit": "gcp",
+ },
+ },
+ },
+ match: false,
+ },
+ {
+ expression: `has(resource.labels.unit) && resource.labels.unit == "aws"`,
+ database: &store.DatabaseMessage{
+ Metadata: &storepb.DatabaseMetadata{},
+ },
+ match: false,
+ },
+ }
+
+ ctx := context.Background()
+ for _, test := range tests {
+ match, err := CheckDatabaseGroupMatch(ctx, test.expression, test.database)
+ assert.NoError(t, err)
+ assert.Equal(t, test.match, match)
+ }
+}