Skip to content

Commit

Permalink
Add a test for resolving a policy with an ignored query. (#893)
Browse files Browse the repository at this point in the history
* Add a test for resolving a policy with an ignored query.

* improve ignored query test.
  • Loading branch information
preslavgerchev authored Nov 8, 2023
1 parent 5649381 commit d9adc44
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions policy/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,75 @@ policies:
}
}

func TestResolve_IgnoredQuery(t *testing.T) {
b := parseBundle(t, `
owner_mrn: //test.sth
policies:
- uid: policy-1
owner_mrn: //test.sth
groups:
- type: chapter
filters: "true"
checks:
- uid: check1
mql: 1 == 1
- mrn: asset1
owner_mrn: //test.sth
groups:
- policies:
- uid: policy-1
- checks:
- uid: check1
action: 4
`)

_, srv, err := inmemory.NewServices(providers.DefaultRuntime(), nil)
require.NoError(t, err)

ctx := context.Background()
_, err = srv.SetBundle(ctx, b)
require.NoError(t, err)

bundleMap, err := b.Compile(context.Background(), schema, nil)
require.NoError(t, err)

rp, err := srv.Resolve(context.Background(), &policy.ResolveReq{
PolicyMrn: "asset1",
AssetFilters: []*explorer.Mquery{{Mql: "true"}},
})

require.NoError(t, err)
require.NotNil(t, rp)
require.Len(t, rp.CollectorJob.ReportingJobs, 3)

mrnToQueryId := map[string]string{}
for _, q := range bundleMap.Queries {
mrnToQueryId[q.Mrn] = q.CodeId
}

rjTester := frameworkReportingJobTester{
t: t,
queryIdToReportingJob: map[string]*policy.ReportingJob{},
rjIdToReportingJob: rp.CollectorJob.ReportingJobs,
rjIdToDatapointJob: rp.CollectorJob.Datapoints,
dataQueriesMrns: map[string]struct{}{},
}

for _, rj := range rjTester.rjIdToReportingJob {
_, ok := rjTester.queryIdToReportingJob[rj.QrId]
require.False(t, ok)
rjTester.queryIdToReportingJob[rj.QrId] = rj
}

queryRj := rjTester.queryIdToReportingJob[mrnToQueryId[queryMrn("check1")]]
// we ensure that even though ignored, theres an RJ for the query
require.NotNil(t, queryRj)
parent := queryRj.Notify[0]
parentRj := rjTester.rjIdToReportingJob[parent]
require.NotNil(t, parentRj)
require.Equal(t, explorer.ScoringSystem_IGNORE_SCORE, parentRj.ChildJobs[queryRj.Uuid].Scoring)
}

func TestResolve_ExpiredGroups(t *testing.T) {
b := parseBundle(t, `
owner_mrn: //test.sth
Expand Down

0 comments on commit d9adc44

Please sign in to comment.