Skip to content
This repository has been archived by the owner on Jan 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #645 from wwitzel3/issue-491
Browse files Browse the repository at this point in the history
add Request interface to make plugin testing easier
  • Loading branch information
Sam Foo committed Feb 10, 2020
2 parents a10dbda + 7e2840b commit 57ea697
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 19 deletions.
6 changes: 5 additions & 1 deletion build.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,11 @@ func installTestPlugin() {
dir := pluginDir()
log.Printf("Plugin path: %s", dir)
os.MkdirAll(dir, 0755)
pluginFile := fmt.Sprintf("%s/octant-sample-plugin", dir)
filename := "octant-sample-plugin"
if runtime.GOOS == "windows" {
filename = "octant-sample-plugin.exe"
}
pluginFile := filepath.Join(dir, filename)
runCmd("go", nil, "build", "-o", pluginFile, "github.com/vmware-tanzu/octant/cmd/octant-sample-plugin")
}

Expand Down
3 changes: 3 additions & 0 deletions changelogs/unreleased/645-wwitzel3
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Plugins, service.Request is now an interface. This is a breaking API change.
- HandlerFunc now takes this new interface which allows for fakes/mocks during unit testing.
- Path proprety is now accessed via Path()
6 changes: 3 additions & 3 deletions cmd/octant-sample-plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ func initRoutes(router *service.Router) {
return cardList
}

router.HandleFunc("*", func(request *service.Request) (component.ContentResponse, error) {
router.HandleFunc("*", func(request service.Request) (component.ContentResponse, error) {
// For each page, generate two tabs with a some content.
component1 := gen("Tab 1", "tab1", request.Path)
component2 := gen("Tab 2", "tab2", request.Path)
component1 := gen("Tab 1", "tab1", request.Path())
component2 := gen("Tab 2", "tab2", request.Path())

contentResponse := component.NewContentResponse(component.TitleFromString("Example"))
contentResponse.Add(component1, component2)
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugin/service/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ func (p *Handler) Content(ctx context.Context, contentPath string) (component.Co
return component.ContentResponse{}, nil
}

request := &Request{
request := &request{
baseRequest: newBaseRequest(ctx, p.name),
dashboardClient: p.dashboardClient,
Path: contentPath,
path: contentPath,
}

return handlerFunc(request)
Expand Down
28 changes: 21 additions & 7 deletions pkg/plugin/service/router.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
package service

import (
"context"

"github.com/gobwas/glob"

"github.com/vmware-tanzu/octant/pkg/view/component"
)

// HandleFunc is a function that generates a content response.
type HandleFunc func(request *Request) (component.ContentResponse, error)
type HandleFunc func(request Request) (component.ContentResponse, error)

type Request interface {
Context() context.Context
DashboardClient() Dashboard
Path() string
}

var _ Request = (*request)(nil)

// Request represents a path request from Octant. It will always be a
// GET style request with a path.
type Request struct {
type request struct {
baseRequest

dashboardClient Dashboard

// Path is path that Octant is requesting. It is scoped to the plugin.
// i.e. If Octant wants to render /content/plugin/foo, Path will be
// `/foo`.
Path string
path string
}

// DashboardClient returns a dashboard client for the request.
func (r *Request) DashboardClient() Dashboard {
func (r *request) DashboardClient() Dashboard {
return r.dashboardClient
}

// Path is path that Octant is requesting. It is scoped to the plugin.
// i.e. If Octant wants to render /content/plugin/foo, Path will be
// `/foo`.
func (r *request) Path() string {
return r.path
}

type route struct {
path string
glob glob.Glob
Expand Down
12 changes: 6 additions & 6 deletions pkg/plugin/service/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ func TestRouter_Match(t *testing.T) {
}

router := NewRouter()
router.HandleFunc("/nested1/nested2", func(request *Request) (component.ContentResponse, error) {
router.HandleFunc("/nested1/nested2", func(request Request) (component.ContentResponse, error) {
return genContentResponse("nested2"), nil
})
router.HandleFunc("/nested1", func(request *Request) (component.ContentResponse, error) {
router.HandleFunc("/nested1", func(request Request) (component.ContentResponse, error) {
return genContentResponse("nested1"), nil
})
router.HandleFunc("/glob*", func(request *Request) (component.ContentResponse, error) {
router.HandleFunc("/glob*", func(request Request) (component.ContentResponse, error) {
return genContentResponse("glob"), nil
})
router.HandleFunc("/", func(request *Request) (component.ContentResponse, error) {
router.HandleFunc("/", func(request Request) (component.ContentResponse, error) {
return genContentResponse("root"), nil
})

Expand Down Expand Up @@ -81,10 +81,10 @@ func TestRouter_Match(t *testing.T) {
}
require.True(t, ok)

request := &Request{
request := &request{
baseRequest: newBaseRequest(context.Background(), "plugin-name"),
dashboardClient: nil,
Path: test.path,
path: test.path,
}
got, err := handleFunc(request)
require.NoError(t, err)
Expand Down

0 comments on commit 57ea697

Please sign in to comment.