Skip to content

Commit

Permalink
review: EvaluationMeta as interface{}
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Pana <8968914+acpana@users.noreply.github.com>
  • Loading branch information
acpana committed Nov 30, 2022
1 parent 58f8691 commit 4feb533
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 49 deletions.
21 changes: 4 additions & 17 deletions constraint/pkg/client/drivers/local/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,14 @@ type Driver struct {
clientCertWatcher *certwatcher.CertWatcher
}

// regoResultMeta has rego specific metadata for a result.
type regoResultMeta struct {
// RegoEvaluationMeta has rego specific metadata from evaluation.
type RegoEvaluationMeta struct {
// TemplateRunTime is the number of milliseconds it took to evaluate all constraints for a template.
TemplateRunTime float64 `json:"templateRunTime"`
// ConstraintCount indicates how many constraints were evaluated for an underlying engine eval call.
// ConstraintCount indicates how many constraints were evaluated for an underlying rego engine eval call.
ConstraintCount uint `json:"constraintCount"`
}

func (rm *regoResultMeta) EngineStats() (map[string]interface{}, error) {
var mapInterface map[string]interface{}
marshalb, err := json.Marshal(rm)
if err != nil {
return nil, err
}
err = json.Unmarshal(marshalb, &mapInterface)
if err != nil {
return nil, err
}
return mapInterface, nil
}

// AddTemplate adds templ to Driver. Normalizes modules into usable forms for
// use in queries.
func (d *Driver) AddTemplate(ctx context.Context, templ *templates.ConstraintTemplate) error {
Expand Down Expand Up @@ -304,7 +291,7 @@ func (d *Driver) Query(ctx context.Context, target string, constraints []*unstru
}

for _, result := range kindResults {
result.ResultMeta = &regoResultMeta{
result.EvaluationMeta = RegoEvaluationMeta{
TemplateRunTime: float64(evalEndTime.Nanoseconds()) / 1000000,
ConstraintCount: uint(len(kindResults)),
}
Expand Down
22 changes: 7 additions & 15 deletions constraint/pkg/client/drivers/local/driver_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,25 +172,17 @@ func TestDriver_Query(t *testing.T) {
t.Fatalf("got 0 errors on data-less query; want 1")
}

stats, err := res[0].ResultMeta.EngineStats()
if err != nil {
t.Fatalf("got Query() (#3) error = %v, want %v", err, nil)
stats, ok := res[0].EvaluationMeta.(RegoEvaluationMeta)
if !ok {
t.Fatalf("could not type convert to RegoEvaluationMeta")
}

trt, found := stats["templateRunTime"]
if !found {
t.Fatalf("did not find %v in engine stats", "templateRunTime")
}
if trt == 0 {
t.Fatalf("expected %v's value to be positive was zero", "templateRunTime")
if stats.TemplateRunTime == 0 {
t.Fatalf("expected %v's value to be positive was zero", "TemplateRunTime")
}

cc, found := stats["constraintCount"]
if !found {
t.Fatalf("did not find %v in engine stats", "constraintCount")
}
if cc != 1.0 { // this comes our as as float64
t.Fatalf("expected %v constraint count, got %v", 1, cc)
if stats.ConstraintCount != uint(1) {
t.Fatalf("expected %v constraint count, got %v", 1, "ConstraintCount")
}
}

Expand Down
23 changes: 7 additions & 16 deletions constraint/pkg/client/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,26 +765,17 @@ func TestE2E_Review_ResultMeta(t *testing.T) {

// for each result check that we have the constraintCount == 3 and a positive templateRunTime
for _, result := range results {
stats, err := result.ResultMeta.EngineStats()
if err != nil {
t.Fatalf("expected nil err got %v", err)
}

trt, found := stats["templateRunTime"]
if !found {
t.Fatalf("did not find %v in engine stats", "templateRunTime")
}
if trt == 0 {
t.Fatalf("expected %v's value to be positive was zero", "templateRunTime")
stats, ok := result.EvaluationMeta.(local.RegoEvaluationMeta)
if !ok {
t.Fatalf("could not type convert to RegoEvaluationMeta")
}

cc, found := stats["constraintCount"]
if !found {
t.Fatalf("did not find %v in engine stats", "constraintCount")
if stats.TemplateRunTime == 0 {
t.Fatalf("expected %v's value to be positive was zero", "TemplateRunTime")
}

if cc != float64(numConstrains) { // constraintCount comes out as float64
t.Fatalf("expected %v constraint count, got %v", numConstrains, cc)
if stats.ConstraintCount != uint(numConstrains) {
t.Fatalf("expected %v constraint count, got %v", numConstrains, "ConstraintCount")
}
}
}
3 changes: 2 additions & 1 deletion constraint/pkg/types/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ type Result struct {
// The enforcement action of the constraint
EnforcementAction string `json:"enforcementAction,omitempty"`

ResultMeta ResultMeta
// EvaluationMeta has metadata for a Result's evaluation.
EvaluationMeta interface{} `json:"evaluationMeta,omitempty"`
}

// Response is a collection of Constraint violations for a particular Target.
Expand Down

0 comments on commit 4feb533

Please sign in to comment.