Skip to content

Commit

Permalink
🧹 Expose loading schemas directly from LR, use in mqlc tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
preslavgerchev committed Sep 19, 2023
1 parent fcee83d commit 29129e6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
9 changes: 5 additions & 4 deletions mqlc/mqlc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ import (
"go.mondoo.com/cnquery/llx"
"go.mondoo.com/cnquery/logger"
"go.mondoo.com/cnquery/mqlc"
"go.mondoo.com/cnquery/providers"
"go.mondoo.com/cnquery/types"

"go.mondoo.com/cnquery/providers-sdk/v1/testutils"
)

var (
features = cnquery.Features{}
os_schema = providers.MustLoadSchemaFromFile("os", "../providers/os/resources/os.resources.json")
core_schema = providers.MustLoadSchemaFromFile("core", "../providers/core/resources/core.resources.json")
azure_schema = providers.MustLoadSchemaFromFile("azure", "../providers/azure/resources/azure.resources.json")
os_schema = testutils.MustLoadSchema(testutils.SchemaProvider{Provider: "os"})
core_schema = testutils.MustLoadSchema(testutils.SchemaProvider{Provider: "core"})
azure_schema = testutils.MustLoadSchema(testutils.SchemaProvider{Provider: "azure"})
conf = mqlc.NewConfig(
core_schema.Add(os_schema).Add(azure_schema),
features,
Expand Down
32 changes: 23 additions & 9 deletions providers-sdk/v1/testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ type tester struct {
Runtime llx.Runtime
}

type SchemaProvider struct {
Provider string
Path string
}

func InitTester(runtime llx.Runtime) *tester {
return &tester{
Runtime: runtime,
Expand Down Expand Up @@ -150,12 +155,21 @@ func (ctx *tester) TestMqlc(t *testing.T, bundle *llx.CodeBundle, props map[stri
return results
}

func mustLoadSchema(provider string) *resources.Schema {
func MustLoadSchema(provider SchemaProvider) *resources.Schema {
if provider.Path == "" && provider.Provider == "" {
panic("cannot load schema without provider name or path")
}
var path string
if provider == "mockprovider" {
path = filepath.Join(TestutilsDir, "mockprovider/resources/mockprovider.lr")
} else {
path = filepath.Join(TestutilsDir, "../../../providers/"+provider+"/resources/"+provider+".lr")
if provider.Provider != "" {
switch provider.Provider {
// special handling for the mockprovider
case "mockprovider":
path = filepath.Join(TestutilsDir, "mockprovider/resources/mockprovider.lr")
default:
path = filepath.Join(TestutilsDir, "../../../providers/"+provider.Provider+"/resources/"+provider.Provider+".lr")
}
} else if provider.Path != "" {
path = provider.Path
}

res, err := lr.Resolve(path, func(path string) ([]byte, error) { return os.ReadFile(path) })
Expand All @@ -172,10 +186,10 @@ func mustLoadSchema(provider string) *resources.Schema {
}

func Local() llx.Runtime {
osSchema := mustLoadSchema("os")
coreSchema := mustLoadSchema("core")
networkSchema := mustLoadSchema("network")
mockSchema := mustLoadSchema("mockprovider")
osSchema := MustLoadSchema(SchemaProvider{Provider: "os"})
coreSchema := MustLoadSchema(SchemaProvider{Provider: "core"})
networkSchema := MustLoadSchema(SchemaProvider{Provider: "network"})
mockSchema := MustLoadSchema(SchemaProvider{Provider: "mockprovider"})

runtime := providers.Coordinator.NewRuntime()

Expand Down

0 comments on commit 29129e6

Please sign in to comment.