-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This creates the `mockprovider`, a way to test resources and fields that behave in non-standard ways, like returning null-values where we would expect to see a resource returned. They avoid polluting providers with fields and data that we may only need for testing. --------- Signed-off-by: Dominik Richter <[email protected]>
- Loading branch information
Showing
10 changed files
with
585 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,3 +63,7 @@ vmknics | |
Vtpm | ||
vulnerabilityassessmentsettings | ||
wil | ||
mgroup | ||
muser | ||
testutils | ||
nullgroup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
providers-sdk/v1/testutils/mockprovider/mockprovider.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// Copyright (c) Mondoo, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package mockprovider | ||
|
||
import ( | ||
"errors" | ||
"strconv" | ||
"strings" | ||
|
||
"go.mondoo.com/cnquery/llx" | ||
"go.mondoo.com/cnquery/providers-sdk/v1/plugin" | ||
"go.mondoo.com/cnquery/providers-sdk/v1/testutils/mockprovider/resources" | ||
) | ||
|
||
var Config = plugin.Provider{ | ||
Name: "mock", | ||
ID: "go.mondoo.com/cnquery/providers-sdk/v1/testutils/mockprovider", | ||
Version: "0.0.0", | ||
Connectors: []plugin.Connector{}, | ||
} | ||
|
||
type Service struct { | ||
runtimes map[uint32]*plugin.Runtime | ||
lastConnectionID uint32 | ||
} | ||
|
||
func Init() *Service { | ||
return &Service{ | ||
runtimes: map[uint32]*plugin.Runtime{}, | ||
} | ||
} | ||
|
||
func (s *Service) ParseCLI(req *plugin.ParseCLIReq) (*plugin.ParseCLIRes, error) { | ||
return nil, errors.New("core doesn't offer any connectors") | ||
} | ||
|
||
func (s *Service) Connect(req *plugin.ConnectReq, callback plugin.ProviderCallback) (*plugin.ConnectRes, error) { | ||
if req == nil || req.Asset == nil { | ||
return nil, errors.New("no connection data provided") | ||
} | ||
|
||
s.lastConnectionID++ | ||
connID := s.lastConnectionID | ||
runtime := &plugin.Runtime{ | ||
Callback: callback, | ||
HasRecording: req.HasRecording, | ||
} | ||
s.runtimes[connID] = runtime | ||
|
||
return &plugin.ConnectRes{ | ||
Id: connID, | ||
Name: "mockprovider", | ||
}, nil | ||
} | ||
|
||
// Shutdown is automatically called when the shell closes. | ||
// It is not necessary to implement this method. | ||
// If you want to do some cleanup, you can do it here. | ||
func (s *Service) Shutdown(req *plugin.ShutdownReq) (*plugin.ShutdownRes, error) { | ||
return &plugin.ShutdownRes{}, nil | ||
} | ||
|
||
func (s *Service) GetData(req *plugin.DataReq) (*plugin.DataRes, error) { | ||
runtime, ok := s.runtimes[req.Connection] | ||
if !ok { | ||
return nil, errors.New("connection " + strconv.FormatUint(uint64(req.Connection), 10) + " not found") | ||
} | ||
|
||
args := plugin.PrimitiveArgsToRawDataArgs(req.Args, runtime) | ||
|
||
if req.ResourceId == "" && req.Field == "" { | ||
res, err := resources.CreateResource(runtime, req.Resource, args) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rd := llx.ResourceData(res, req.Resource).Result() | ||
return &plugin.DataRes{ | ||
Data: rd.Data, | ||
}, nil | ||
} | ||
|
||
resource, ok := runtime.Resources.Get(req.Resource + "\x00" + req.ResourceId) | ||
if !ok { | ||
return nil, errors.New("resource '" + req.Resource + "' (id: " + req.ResourceId + ") doesn't exist") | ||
} | ||
|
||
return resources.GetData(resource, req.Field, args), nil | ||
} | ||
|
||
func (s *Service) StoreData(req *plugin.StoreReq) (*plugin.StoreRes, error) { | ||
runtime, ok := s.runtimes[req.Connection] | ||
if !ok { | ||
return nil, errors.New("connection " + strconv.FormatUint(uint64(req.Connection), 10) + " not found") | ||
} | ||
|
||
var errs []string | ||
for i := range req.Resources { | ||
info := req.Resources[i] | ||
|
||
args, err := plugin.ProtoArgsToRawDataArgs(info.Fields) | ||
if err != nil { | ||
errs = append(errs, "failed to add cached "+info.Name+" (id: "+info.Id+"), failed to parse arguments") | ||
continue | ||
} | ||
|
||
resource, ok := runtime.Resources.Get(info.Name + "\x00" + info.Id) | ||
if !ok { | ||
resource, err = resources.CreateResource(runtime, info.Name, args) | ||
if err != nil { | ||
errs = append(errs, "failed to add cached "+info.Name+" (id: "+info.Id+"), creation failed: "+err.Error()) | ||
continue | ||
} | ||
|
||
runtime.Resources.Set(info.Name+"\x00"+info.Id, resource) | ||
} | ||
|
||
for k, v := range args { | ||
if err := resources.SetData(resource, k, v); err != nil { | ||
errs = append(errs, "failed to add cached "+info.Name+" (id: "+info.Id+"), field error: "+err.Error()) | ||
} | ||
} | ||
} | ||
|
||
if len(errs) != 0 { | ||
return nil, errors.New(strings.Join(errs, ", ")) | ||
} | ||
return &plugin.StoreRes{}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) Mondoo, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package resources | ||
|
||
import ( | ||
"go.mondoo.com/cnquery/llx" | ||
"go.mondoo.com/cnquery/providers-sdk/v1/plugin" | ||
) | ||
|
||
func (c *mqlMuser) id() (string, error) { | ||
return c.Name.Data, nil | ||
} | ||
|
||
func (c *mqlMuser) group() (*mqlMgroup, error) { | ||
o, err := CreateResource(c.MqlRuntime, "mgroup", map[string]*llx.RawData{ | ||
"name": llx.StringData("group one"), | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return o.(*mqlMgroup), nil | ||
} | ||
|
||
func (c *mqlMuser) nullgroup() (*mqlMgroup, error) { | ||
c.Nullgroup.State = plugin.StateIsSet | plugin.StateIsNull | ||
return nil, nil | ||
} | ||
|
||
func (c *mqlMuser) groups() ([]interface{}, error) { | ||
one, err := CreateResource(c.MqlRuntime, "mgroup", map[string]*llx.RawData{ | ||
"name": llx.StringData("group one"), | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return []interface{}{ | ||
one, nil, | ||
}, nil | ||
} | ||
|
||
func (c *mqlMgroup) id() (string, error) { | ||
return c.Name.Data, nil | ||
} |
16 changes: 16 additions & 0 deletions
16
providers-sdk/v1/testutils/mockprovider/resources/mockprovider.lr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (c) Mondoo, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
option provider = "go.mondoo.com/cnquery/providers-sdk/v1/testutils/mockprovider" | ||
option go_package = "go.mondoo.com/cnquery/providers-sdk/v1/testutils/mockprovider" | ||
|
||
muser { | ||
name string | ||
group() mgroup | ||
nullgroup() mgroup | ||
groups() []mgroup | ||
} | ||
|
||
mgroup { | ||
name string | ||
} |
Oops, something went wrong.