From ed91e8003a26110a1840a8917e79d7e8fbb82f18 Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Fri, 22 Sep 2023 10:11:51 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20remove=20deprecated=20providers/?= =?UTF-8?q?mock=20(#1851)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We already have the mock provider, which is not a standalone provider anymore but part of the providers engine now. You can find it in `providers/mock.go`. The provider in the folder is a relic of earlier iterations now and can be removed. Signed-off-by: Dominik Richter --- cli/shell/shell_test.go | 19 +- .../manager/credentials_query_test.go | 15 +- providers-sdk/v1/testutils/testutils.go | 14 - providers/mock/mock.go | 275 ------------------ 4 files changed, 12 insertions(+), 311 deletions(-) delete mode 100644 providers/mock/mock.go diff --git a/cli/shell/shell_test.go b/cli/shell/shell_test.go index 5762437155..d40903fd3e 100644 --- a/cli/shell/shell_test.go +++ b/cli/shell/shell_test.go @@ -4,13 +4,11 @@ package shell_test import ( - "path/filepath" "testing" "github.com/stretchr/testify/assert" "go.mondoo.com/cnquery/cli/shell" "go.mondoo.com/cnquery/providers-sdk/v1/testutils" - "go.mondoo.com/cnquery/providers/mock" ) func localShell() *shell.Shell { @@ -23,21 +21,6 @@ func localShell() *shell.Shell { return res } -func mockShell(filename string, opts ...shell.ShellOption) *shell.Shell { - path, _ := filepath.Abs(filename) - runtime, err := mock.NewFromTomlFile(path) - if err != nil { - panic(err.Error()) - } - - res, err := shell.New(runtime, opts...) - if err != nil { - panic(err.Error()) - } - - return res -} - func TestShell_RunOnce(t *testing.T) { shell := localShell() assert.NotPanics(t, func() { @@ -65,7 +48,7 @@ func TestShell_Help(t *testing.T) { } func TestShell_Centos8(t *testing.T) { - shell := mockShell("../../mql/testdata/centos8.toml") + shell := localShell() assert.NotPanics(t, func() { shell.RunOnce("platform { title name release arch }") }, "should not panic on partial queries") diff --git a/providers-sdk/v1/inventory/manager/credentials_query_test.go b/providers-sdk/v1/inventory/manager/credentials_query_test.go index aef38f73a2..4ea171b3de 100644 --- a/providers-sdk/v1/inventory/manager/credentials_query_test.go +++ b/providers-sdk/v1/inventory/manager/credentials_query_test.go @@ -8,15 +8,22 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.mondoo.com/cnquery/llx" "go.mondoo.com/cnquery/providers-sdk/v1/inventory" "go.mondoo.com/cnquery/providers-sdk/v1/inventory/manager" + "go.mondoo.com/cnquery/providers-sdk/v1/testutils" "go.mondoo.com/cnquery/providers-sdk/v1/vault" - "go.mondoo.com/cnquery/providers/mock" ) +var runtime llx.Runtime + +func init() { + runtime = testutils.LinuxMock() +} + func TestSecretKeySimple(t *testing.T) { query := `{ type: 'ssh_agent' }` - runner, err := manager.NewCredentialQueryRunner(query, mock.New()) + runner, err := manager.NewCredentialQueryRunner(query, runtime) require.NoError(t, err) cred, err := runner.Run(&inventory.Asset{}) require.NoError(t, err) @@ -31,7 +38,7 @@ func TestSecretKeyIfReturn(t *testing.T) { return {type: 'private_key', secret_id: 'otherkey'} ` - runner, err := manager.NewCredentialQueryRunner(query, mock.New()) + runner, err := manager.NewCredentialQueryRunner(query, runtime) require.NoError(t, err) cred, err := runner.Run(&inventory.Asset{ @@ -53,7 +60,7 @@ func TestSecretKeyIfConditionalReturn(t *testing.T) { return { secret_id: '' }" ` - runner, err := manager.NewCredentialQueryRunner(query, mock.New()) + runner, err := manager.NewCredentialQueryRunner(query, runtime) require.NoError(t, err) // check with provided label diff --git a/providers-sdk/v1/testutils/testutils.go b/providers-sdk/v1/testutils/testutils.go index de32a215a9..2f8c9a3521 100644 --- a/providers-sdk/v1/testutils/testutils.go +++ b/providers-sdk/v1/testutils/testutils.go @@ -26,7 +26,6 @@ import ( "go.mondoo.com/cnquery/providers-sdk/v1/lr/docs" "go.mondoo.com/cnquery/providers-sdk/v1/resources" "go.mondoo.com/cnquery/providers-sdk/v1/testutils/mockprovider" - "go.mondoo.com/cnquery/providers/mock" networkconf "go.mondoo.com/cnquery/providers/network/config" networkprovider "go.mondoo.com/cnquery/providers/network/provider" osconf "go.mondoo.com/cnquery/providers/os/config" @@ -70,15 +69,6 @@ func getEnvFeatures() cnquery.Features { return fts } -func TomlMock(filepath string) llx.Runtime { - trans, err := mock.NewFromTomlFile(filepath) - if err != nil { - panic(err.Error()) - } - - return trans -} - type tester struct { Runtime llx.Runtime } @@ -289,10 +279,6 @@ func RecordingMock(absTestdataPath string) llx.Runtime { return mockRuntimeAbs(absTestdataPath) } -func CustomMock(path string) llx.Runtime { - return TomlMock(path) -} - type SimpleTest struct { Code string ResultIndex int diff --git a/providers/mock/mock.go b/providers/mock/mock.go deleted file mode 100644 index 23c59b850b..0000000000 --- a/providers/mock/mock.go +++ /dev/null @@ -1,275 +0,0 @@ -// Copyright (c) Mondoo, Inc. -// SPDX-License-Identifier: BUSL-1.1 - -package mock - -import ( - "errors" - "os" - - "github.com/BurntSushi/toml" - "go.mondoo.com/cnquery/llx" - "go.mondoo.com/cnquery/providers" - "go.mondoo.com/cnquery/providers-sdk/v1/plugin" - "go.mondoo.com/cnquery/providers-sdk/v1/resources" -) - -// Unlike other providers, we are currently building this into the core -// of the providers library. In that, it is similar to the core providers. -// Both are optional and both will be removed from being built-in in the -// future, at least from some of the builds. -// -// The reason for this decision is that we want to use it for testing and -// recording/playback in all scenarios. Because core needs to support -// parsers at the moment anyway, we get the benefit of having those -// libraries anyway. So the overhead of this additional loader is very -// small. - -type Mock struct { - Inventory map[string]Resources - Providers []string - schema llx.Schema - AssetMrn string -} - -type Resources map[string]Resource - -type Resource struct { - Fields map[string]plugin.DataRes -} - -func NewFromTomlFile(path string) (*Mock, error) { - data, err := os.ReadFile(path) - if err != nil { - return nil, err - } - - return NewFromToml(data) -} - -func loadRawDataRes(raw interface{}) (plugin.DataRes, error) { - switch v := raw.(type) { - case string: - return plugin.DataRes{Data: llx.StringPrimitive(v)}, nil - case int64: - return plugin.DataRes{Data: llx.IntPrimitive(v)}, nil - case bool: - return plugin.DataRes{Data: llx.BoolPrimitive(v)}, nil - default: - return plugin.DataRes{}, errors.New("failed to load value") - } -} - -func loadRawFields(resource *Resource, fields map[string]interface{}) error { - var err error - for field, raw := range fields { - resource.Fields[field], err = loadRawDataRes(raw) - if err != nil { - return err - } - } - return nil -} - -func loadMqlInfo(raw interface{}, m *Mock) error { - info, ok := raw.(map[string]interface{}) - if !ok { - return errors.New("mql info is not a map") - } - - if p, ok := info["providers"]; ok { - list := p.([]interface{}) - for _, v := range list { - m.Providers = append(m.Providers, v.(string)) - } - } - - return nil -} - -func New() *Mock { - return &Mock{ - Inventory: map[string]Resources{}, - Providers: []string{}, - schema: emptySchema{}, - } -} - -func NewFromToml(raw []byte) (*Mock, error) { - var tmp interface{} - err := toml.Unmarshal(raw, &tmp) - if err != nil { - return nil, err - } - - res := Mock{ - Inventory: map[string]Resources{}, - schema: providers.DefaultRuntime().Schema(), - } - err = nil - - rawResources, ok := tmp.(map[string]interface{}) - if !ok { - return nil, errors.New("incorrect structure of recording TOML (outer layer should be resources)") - } - - if mqlInfo, ok := rawResources["mql"]; ok { - loadMqlInfo(mqlInfo, &res) - delete(rawResources, "mql") - } - - for name, v := range rawResources { - resources := Resources{} - res.Inventory[name] = resources - - rawList, ok := v.(map[string]interface{}) - if !ok { - return nil, errors.New("incorrect structure of recording TOML (" + name + " resources should be followed by IDs)") - } - - for id, vv := range rawList { - resource := Resource{ - Fields: map[string]plugin.DataRes{}, - } - - rawFields, ok := vv.(map[string]interface{}) - if !ok { - return nil, errors.New("incorrect structure of recording TOML (resource " + name + " (id: " + id + ") should have fields set)") - } - - if err = loadRawFields(&resource, rawFields); err != nil { - return nil, err - } - - resources[id] = resource - } - } - - return &res, err -} - -func (m *Mock) Unregister(watcherUID string) error { - // nothing will change, so nothing to watch or unregister - return nil -} - -func (m *Mock) AssetMRN() string { - return m.AssetMrn -} - -func (m *Mock) CreateResource(name string, args map[string]*llx.Primitive) (llx.Resource, error) { - resourceCache, ok := m.Inventory[name] - if !ok { - return nil, errors.New("resource '" + name + "' is not in recording") - } - - // FIXME: we currently have no way of generating the ID that we need to get the right resource, - // until we have a solid way to (1) connect the right provider and (2) use it to generate the ID on the fly. - // - // For now, we are just using a few hardcoded workaround... - - switch name { - case "command": - rid, ok := args["command"] - if !ok { - return nil, errors.New("cannot find '" + name + "' ID in recording") - } - - id := string(rid.Value) - _, ok = resourceCache[id] - if !ok { - return nil, errors.New("cannot find " + name + " '" + id + "' in recording") - } - - return &llx.MockResource{Name: name, ID: id}, nil - - case "file": - fid, ok := args["path"] - if !ok { - return nil, errors.New("cannot find '" + name + "' ID in recording") - } - - id := string(fid.Value) - _, ok = resourceCache[id] - if !ok { - return nil, errors.New("cannot find " + name + " '" + id + "' in recording") - } - - return &llx.MockResource{Name: name, ID: id}, nil - - default: - // for all static resources - if _, ok := resourceCache[""]; ok { - return &llx.MockResource{Name: name, ID: ""}, nil - } - } - - return nil, errors.New("cannot create resource '" + name + "' from recording yet") -} - -func (m *Mock) CreateResourceWithID(name string, id string, args map[string]*llx.Primitive) (llx.Resource, error) { - resourceCache, ok := m.Inventory[name] - if !ok { - return nil, errors.New("resource '" + name + "' is not in recording") - } - - _, ok = resourceCache[id] - if !ok { - return nil, errors.New("cannot find " + name + " '" + id + "' in recording") - } - - return &llx.MockResource{Name: name, ID: id}, nil -} - -func (m *Mock) WatchAndUpdate(resource llx.Resource, field string, watcherUID string, callback func(res interface{}, err error)) error { - name := resource.MqlName() - resourceCache, ok := m.Inventory[name] - if !ok { - return errors.New("resource '" + name + "' is not in recording") - } - - id := resource.MqlID() - x, ok := resourceCache[id] - if !ok { - return errors.New("cannot find " + name + " '" + id + "' in recording") - } - - f, ok := x.Fields[field] - if !ok { - return errors.New("cannot find field '" + field + "' in resource " + name + " (id: " + id + ")") - } - - if f.Error != "" { - callback(nil, errors.New(f.Error)) - } else { - callback(f.Data.RawData().Value, nil) - } - - // nothing will change, so nothing to watch or unregister - return nil -} - -func (m *Mock) Resource(name string) (*resources.ResourceInfo, bool) { - panic("not sure how to get resource info from mock yet...") - return nil, false -} - -func (m *Mock) Schema() llx.Schema { - return m.schema -} - -func (m *Mock) Close() {} - -type emptySchema struct{} - -func (e emptySchema) Lookup(resource string) *resources.ResourceInfo { - return nil -} - -func (e emptySchema) LookupField(resource string, field string) (*resources.ResourceInfo, *resources.Field) { - return nil, nil -} - -func (e emptySchema) AllResources() map[string]*resources.ResourceInfo { - return nil -}