go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/explorer/filters_test.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package explorer_test 5 6 import ( 7 "context" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 "go.mondoo.com/cnquery/explorer" 13 "go.mondoo.com/cnquery/providers-sdk/v1/testutils" 14 ) 15 16 func TestNewFilters(t *testing.T) { 17 t.Run("empty", func(t *testing.T) { 18 f := explorer.NewFilters() 19 require.NotNil(t, f) 20 require.Empty(t, f.Items) 21 }) 22 23 t.Run("two filters", func(t *testing.T) { 24 f := explorer.NewFilters("true", "false") 25 require.NotNil(t, f) 26 assert.Equal(t, map[string]*explorer.Mquery{ 27 "0": {Mql: "true"}, 28 "1": {Mql: "false"}, 29 }, f.Items) 30 }) 31 } 32 33 func TestSummarize(t *testing.T) { 34 t.Run("with NewFilters initialization", func(t *testing.T) { 35 f := explorer.NewFilters("true", "asset.platform != ''") 36 assert.Equal(t, "asset.platform != '', true", f.Summarize()) 37 }) 38 39 t.Run("with mixed filters", func(t *testing.T) { 40 f := &explorer.Filters{ 41 Items: map[string]*explorer.Mquery{ 42 "one": {Mql: "asset.name"}, 43 "two": {Title: "filter 2"}, 44 }, 45 } 46 47 assert.Equal(t, "asset.name, filter 2", f.Summarize()) 48 }) 49 } 50 51 func TestBundleAssetFilter(t *testing.T) { 52 // load the raw bundle 53 tester := testutils.InitTester(testutils.LinuxMock()) 54 bundle, err := explorer.BundleFromPaths("../examples/os.mql.yaml") 55 require.NoError(t, err) 56 assert.Equal(t, 1, len(bundle.Packs)) 57 assert.Equal(t, "asset.family.contains(\"unix\")", bundle.Packs[0].Filters.Items["0"].Mql) 58 assert.Equal(t, (*explorer.Filters)(nil), bundle.Packs[0].ComputedFilters) 59 60 // check that the computed asset filters are set 61 pbm, err := bundle.Compile(context.Background(), tester.Runtime.Schema()) 62 require.NoError(t, err) 63 assert.Equal(t, "asset.family.contains(\"unix\")", pbm.Packs["//local.cnquery.io/run/local-execution/querypacks/linux-mixed-queries"].ComputedFilters.Summarize()) 64 }