瀏覽代碼

Cdp/parser fix (#3567)

Signed-off-by: Christian Petersen <Christian.Petersen2@ibm.com>
Christian Petersen 3 月之前
父節點
當前提交
fbe6cf4135
共有 2 個文件被更改,包括 31 次插入1 次删除
  1. 5 1
      core/pkg/filter/resourcequota/parser.go
  2. 26 0
      core/pkg/filter/resourcequota/parser_test.go

+ 5 - 1
core/pkg/filter/resourcequota/parser.go

@@ -1,6 +1,9 @@
 package resourcequota
 
-import "github.com/opencost/opencost/core/pkg/filter/ast"
+import (
+	"github.com/opencost/opencost/core/pkg/filter/ast"
+	"github.com/opencost/opencost/core/pkg/filter/ops"
+)
 
 var resourceQuotaFilterFields []*ast.Field = []*ast.Field{
 	ast.NewField(FieldClusterID),
@@ -19,6 +22,7 @@ func init() {
 		ff := *f
 		fieldMap[ResourceQuotaField(ff.Name)] = &ff
 	}
+	ops.RegisterDefaultFieldLookup[ResourceQuotaField](DefaultFieldByName)
 }
 
 // DefaultFieldByName returns only default resource quota filter fields by name.

+ 26 - 0
core/pkg/filter/resourcequota/parser_test.go

@@ -4,6 +4,7 @@ import (
 	"testing"
 
 	"github.com/opencost/opencost/core/pkg/filter/ast"
+	"github.com/opencost/opencost/core/pkg/filter/ops"
 )
 
 func TestDefaultFieldByName(t *testing.T) {
@@ -41,3 +42,28 @@ func TestDefaultFieldByName(t *testing.T) {
 	}
 
 }
+
+func TestOpsEqWithResourceQuotaField(t *testing.T) {
+	clusterFilter := ops.Eq(FieldClusterID, "test-cluster")
+
+	equalOp, ok := clusterFilter.(*ast.EqualOp)
+	if !ok {
+		t.Fatalf("expected *ast.EqualOp, got %T", clusterFilter)
+	}
+
+	if equalOp.Left.Field == nil {
+		t.Fatal("expected Field to be non-nil, got nil")
+	}
+
+	if equalOp.Left.Field.Name == "" {
+		t.Fatal("expected Field.Name to be non-empty, got empty string")
+	}
+
+	if equalOp.Left.Field.Name != "cluster" {
+		t.Errorf("expected Field.Name to be 'cluster', got '%s'", equalOp.Left.Field.Name)
+	}
+
+	if equalOp.Right != "test-cluster" {
+		t.Errorf("expected Right to be 'test-cluster', got '%s'", equalOp.Right)
+	}
+}