Skip to content

Commit

Permalink
🧹 ensure example pass our own lint requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-rock committed Jan 21, 2024
1 parent 7676446 commit 7dd4497
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 8 deletions.
1 change: 1 addition & 0 deletions examples/compliance.mql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
policies:
- uid: ssh-policy
name: SSH Policy
version: 1.0.0
groups:
- filters: return true
checks:
Expand Down
2 changes: 1 addition & 1 deletion examples/directory/example1.mql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ policies:
- name: Mondoo
email: hello@mondoo.com
groups:
- filters: platform.family.contains('unix')
- filters: asset.family.contains('unix')
checks:
- uid: sshd-01
- uid: sshd-02
Expand Down
2 changes: 1 addition & 1 deletion examples/directory/example2.mql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ policies:
name: Another policy
version: 1.0.0
groups:
- filters: platform.family.contains('unix')
- filters: asset.family.contains('unix')
checks:
- uid: linux-1
policies:
Expand Down
59 changes: 59 additions & 0 deletions examples/lint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1

package examples

import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mondoo.com/cnquery/v10/providers"
"go.mondoo.com/cnspec/v10/internal/bundle"
"os"
"testing"
)

func ensureProviders(ids []string) error {
for _, id := range ids {
_, err := providers.EnsureProvider(providers.ProviderLookup{ID: id}, true, nil)
if err != nil {
return err
}
}
return nil
}

func TestMain(m *testing.M) {
dir := ".lint-providers"
providers.CustomProviderPath = dir
providers.DefaultPath = dir

err := ensureProviders([]string{
"go.mondoo.com/cnquery/providers/os",
})
if err != nil {
panic(err)
}

exitVal := m.Run()

// cleanup custom provider path to ensure no leftovers and other tests are not affected
err = os.RemoveAll(dir)
if err != nil {
panic(err)
}

os.Exit(exitVal)
}

func TestExampleLint(t *testing.T) {
files := []string{
"./complex.mql.yaml",
"./example.mql.yaml",
"./props.mql.yaml",
}

runtime := providers.DefaultRuntime()
result, err := bundle.Lint(runtime.Schema(), files...)
require.NoError(t, err)
assert.False(t, result.HasError())
}
13 changes: 7 additions & 6 deletions examples/props.mql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,27 @@ policies:

queries:
- uid: variant-check
title: Variant check
variants:
- uid: var1
- uid: var2
- uid: var3
- uid: variant-1
- uid: variant-2
- uid: variant-3

- uid: var1
- uid: variant-1
mql: props.home + " on 1"
filters: asset.family.contains("unix")
props:
- uid: home
mql: return "p1"

- uid: var2
- uid: variant-2
mql: props.home + " on 2"
filters: asset.family.contains("unix")
props:
- uid: home
mql: return "p2"

- uid: var3
- uid: variant-3
mql: props.homeDir + " on 3"
filters: asset.family.contains("unix")
props:
Expand Down

0 comments on commit 7dd4497

Please sign in to comment.