-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add experimental test manage func (#704)
* add test manage func * flesh out * fix linter * adjust test to use experimental package * regenerate * delete dir * Experimental: OAuth Token Retriever (#702) Co-authored-by: Mihaly Gyongyosi <[email protected]> * fix golden file * update package name * fix test * fix golden file * undo commit * fix comment * shutdown for plugin --------- Co-authored-by: Andres Martinez Gotor <[email protected]> Co-authored-by: Mihaly Gyongyosi <[email protected]>
- Loading branch information
1 parent
6be9232
commit 792f8fd
Showing
12 changed files
with
258 additions
and
435 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
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,96 @@ | ||
package datasourcetest | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io" | ||
|
||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/credentials/insecure" | ||
"google.golang.org/grpc/status" | ||
|
||
"github.com/grafana/grafana-plugin-sdk-go/backend" | ||
"github.com/grafana/grafana-plugin-sdk-go/genproto/pluginv2" | ||
) | ||
|
||
type TestPluginClient struct { | ||
DataClient pluginv2.DataClient | ||
DiagnosticsClient pluginv2.DiagnosticsClient | ||
ResourceClient pluginv2.ResourceClient | ||
|
||
conn *grpc.ClientConn | ||
} | ||
|
||
func newTestPluginClient(addr string) (*TestPluginClient, error) { | ||
c, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &TestPluginClient{ | ||
conn: c, | ||
DiagnosticsClient: pluginv2.NewDiagnosticsClient(c), | ||
DataClient: pluginv2.NewDataClient(c), | ||
ResourceClient: pluginv2.NewResourceClient(c), | ||
}, nil | ||
} | ||
|
||
func (p *TestPluginClient) QueryData(ctx context.Context, r *backend.QueryDataRequest) (*backend.QueryDataResponse, error) { | ||
req := backend.ToProto().QueryDataRequest(r) | ||
|
||
resp, err := p.DataClient.QueryData(ctx, req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return backend.FromProto().QueryDataResponse(resp) | ||
} | ||
|
||
func (p *TestPluginClient) CheckHealth(ctx context.Context, r *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) { | ||
req := &pluginv2.CheckHealthRequest{ | ||
PluginContext: backend.ToProto().PluginContext(r.PluginContext), | ||
} | ||
|
||
resp, err := p.DiagnosticsClient.CheckHealth(ctx, req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return backend.FromProto().CheckHealthResponse(resp), nil | ||
} | ||
|
||
func (p *TestPluginClient) CallResource(ctx context.Context, r *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error { | ||
protoReq := backend.ToProto().CallResourceRequest(r) | ||
protoStream, err := p.ResourceClient.CallResource(ctx, protoReq) | ||
if err != nil { | ||
if status.Code(err) == codes.Unimplemented { | ||
return errors.New("method not implemented") | ||
} | ||
|
||
return err | ||
} | ||
|
||
for { | ||
protoResp, err := protoStream.Recv() | ||
if err != nil { | ||
if status.Code(err) == codes.Unimplemented { | ||
return errors.New("method not implemented") | ||
} | ||
|
||
if errors.Is(err, io.EOF) { | ||
return nil | ||
} | ||
|
||
return err | ||
} | ||
|
||
if err = sender.Send(backend.FromProto().CallResourceResponse(protoResp)); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
func (p *TestPluginClient) shutdown() error { | ||
return p.conn.Close() | ||
} |
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,51 @@ | ||
package datasourcetest | ||
|
||
import ( | ||
"github.com/grafana/grafana-plugin-sdk-go/backend" | ||
"github.com/grafana/grafana-plugin-sdk-go/backend/datasource" | ||
"github.com/grafana/grafana-plugin-sdk-go/internal/automanagement" | ||
) | ||
|
||
type ManageOpts struct { | ||
Address string | ||
} | ||
|
||
type TestPlugin struct { | ||
Client *TestPluginClient | ||
Server *TestPluginServer | ||
} | ||
|
||
func (p *TestPlugin) Shutdown() error { | ||
if p.Server != nil { | ||
p.Server.shutdown() | ||
} | ||
|
||
if p.Client != nil { | ||
return p.Client.shutdown() | ||
} | ||
return nil | ||
} | ||
|
||
func Manage(instanceFactory datasource.InstanceFactoryFunc, opts ManageOpts) (TestPlugin, error) { | ||
handler := automanagement.NewManager(datasource.NewInstanceManager(instanceFactory)) | ||
s, err := backend.TestStandaloneServe(backend.ServeOpts{ | ||
CheckHealthHandler: handler, | ||
CallResourceHandler: handler, | ||
QueryDataHandler: handler, | ||
StreamHandler: handler, | ||
}, opts.Address) | ||
|
||
if err != nil { | ||
return TestPlugin{}, err | ||
} | ||
|
||
c, err := newTestPluginClient(opts.Address) | ||
if err != nil { | ||
return TestPlugin{}, err | ||
} | ||
|
||
return TestPlugin{ | ||
Client: c, | ||
Server: newTestPluginServer(s), | ||
}, 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,16 @@ | ||
package datasourcetest | ||
|
||
import "google.golang.org/grpc" | ||
|
||
type TestPluginServer struct { | ||
srv *grpc.Server | ||
} | ||
|
||
func newTestPluginServer(s *grpc.Server) *TestPluginServer { | ||
return &TestPluginServer{ | ||
srv: s, | ||
} | ||
} | ||
func (s *TestPluginServer) shutdown() { | ||
s.srv.Stop() | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.