Add unit tests to airshipui plugin
- Remove globbed url route - Create unit tests for register.go - Add test and coverage-html targets to Makefile - Add test coverage output to gitignore Change-Id: I980e28204afc83b67a8a4d2d6af500caa9e530c2
This commit is contained in:
parent
01350d74b0
commit
3575299373
|
@ -0,0 +1,2 @@
|
|||
# Coverage File
|
||||
coverage.out
|
13
Makefile
13
Makefile
|
@ -19,8 +19,8 @@ else
|
|||
OCTANT_PLUGINSTUB_DIR ?= ${HOME}/.config/octant/plugins
|
||||
endif
|
||||
|
||||
DIRS = internal pkg
|
||||
RECURSIVE_DIRS = $(addsuffix /...,$(DIRS))
|
||||
DIRS = internal
|
||||
RECURSIVE_DIRS = $(addprefix ./, $(addsuffix /..., $(DIRS)))
|
||||
|
||||
.PHONY: install-plugins
|
||||
install-plugins: $(PLUGINS)
|
||||
|
@ -30,7 +30,11 @@ $(PLUGINS):
|
|||
|
||||
.PHONY: test
|
||||
test: generate
|
||||
go test -v $(RECURSIVE_DIRS)
|
||||
go test $(RECURSIVE_DIRS) -v -coverprofile=coverage.out
|
||||
|
||||
.PHONY: coverage-html
|
||||
coverage-html: test
|
||||
go tool cover -html=coverage.out
|
||||
|
||||
.PHONY: vet
|
||||
vet:
|
||||
|
@ -46,3 +50,6 @@ clean:
|
|||
|
||||
.PHONY: ci
|
||||
ci: test vet
|
||||
|
||||
# The golang-unit zuul job calls the env target, so create one
|
||||
.PHONY: env
|
||||
|
|
1
go.sum
1
go.sum
|
@ -97,6 +97,7 @@ github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4er
|
|||
github.com/golang/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E=
|
||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||
github.com/golang/mock v1.3.1 h1:qGJ6qTW+x6xX/my+8YUVl4WNpX9B7+/l2tRsHGZ7f2s=
|
||||
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
|
||||
github.com/golang/protobuf v0.0.0-20161109072736-4bd1920723d7/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
|
|
|
@ -0,0 +1,94 @@
|
|||
package plugin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/vmware-tanzu/octant/pkg/plugin/service"
|
||||
"github.com/vmware-tanzu/octant/pkg/view/component"
|
||||
)
|
||||
|
||||
func TestRegister(t *testing.T) {
|
||||
p, err := Register("airship-ui", "Airship UI test version")
|
||||
if err != nil {
|
||||
t.Fatalf("Registering the plugin returned an error")
|
||||
}
|
||||
|
||||
p.Validate()
|
||||
}
|
||||
|
||||
func TestRoutes(t *testing.T) {
|
||||
router := service.NewRouter()
|
||||
|
||||
initRoutes(router)
|
||||
|
||||
tests := []struct {
|
||||
path string
|
||||
exists bool
|
||||
}{
|
||||
{
|
||||
path: "",
|
||||
exists: true,
|
||||
},
|
||||
{
|
||||
path: "/not-real",
|
||||
exists: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(fmt.Sprintf("Path='%s'", test.path), func(t *testing.T) {
|
||||
_, found := router.Match(test.path)
|
||||
|
||||
if test.exists != found {
|
||||
if found {
|
||||
t.Errorf("Found path '%s' when it should not exist.", test.path)
|
||||
} else {
|
||||
t.Errorf("Didn't find path '%s' when it should exist.", test.path)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
func TestRouteHandles(t *testing.T) {
|
||||
router := service.NewRouter()
|
||||
|
||||
initRoutes(router)
|
||||
|
||||
tests := []struct {
|
||||
path string
|
||||
title string
|
||||
}{
|
||||
{
|
||||
path: "",
|
||||
title: "Airship UI",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(fmt.Sprintf("Path='%s'", test.path), func(t *testing.T) {
|
||||
handleFunc, found := router.Match(test.path)
|
||||
|
||||
if !found {
|
||||
t.Fatalf("Path '%s' was not found.", test.path)
|
||||
}
|
||||
|
||||
request := &service.Request{
|
||||
Path: test.path,
|
||||
}
|
||||
contentResponse, err := handleFunc(request)
|
||||
if err != nil {
|
||||
t.Fatalf("handleFunc for path '%s' returned an error.", test.path)
|
||||
}
|
||||
|
||||
title, err := component.TitleFromTitleComponent(contentResponse.Title)
|
||||
if err != nil {
|
||||
t.Fatalf("Getting the Title from the TitleComponents returned an error.")
|
||||
}
|
||||
|
||||
if title != test.title {
|
||||
t.Errorf("Title is not correct. Got: '%s', Expected: '%s'", title, test.title)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue