Skip to content

Commit

Permalink
feat(test): adds custom matchers for openshift routes
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszmajsak authored and cam-garrison committed Aug 6, 2024
1 parent 4a967d9 commit ef6ef2d
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 1 deletion.
12 changes: 11 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ linters-settings:
rules:
- name: dot-imports
arguments:
- allowedPackages: ["github.com/onsi/ginkgo/v2","github.com/onsi/gomega","github.com/onsi/gomega/gstruct"]
- allowedPackages: [
"github.com/onsi/ginkgo/v2",
"github.com/onsi/gomega",
"github.com/onsi/gomega/gstruct",
"github.com/opendatahub-io/odh-platform/test/matchers"
]
importas:
alias:
- pkg: k8s.io/apimachinery/pkg/api/errors
Expand Down Expand Up @@ -80,6 +85,11 @@ linters-settings:
- generic
# we want to return spi.Interfaces in routing.go and authconfig.go
- spi
# for custom Gomega matchers
- types.GomegaMatcher
varnamelen:
ignore-names:
- g # g Gomega
linters:
enable-all: true
disable:
Expand Down
26 changes: 26 additions & 0 deletions test/data/crds/test.component.crd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: components.opendatahub.io
spec:
group: opendatahub.io
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
name:
type: string
scope: Namespaced
names:
plural: components
singular: component
kind: Component
shortNames:
- comp
102 changes: 102 additions & 0 deletions test/matchers/route.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package matchers

import (
"fmt"

"github.com/onsi/gomega"
"github.com/onsi/gomega/format"
"github.com/onsi/gomega/types"
openshiftroutev1 "github.com/openshift/api/route/v1"
)

func HaveName(name string) types.GomegaMatcher {
return &routeNameMatcher{expectedName: name}
}

type routeNameMatcher struct {
expectedName string
}

func (r *routeNameMatcher) Match(actual any) (bool, error) {
if actual == nil {
return true, nil
}

route, ok := actual.(*openshiftroutev1.Route)
if !ok {
return false, fmt.Errorf("expected Route. Got:\n%s", format.Object(actual, 1)) //nolint:goerr113 //reason helpful in assertions
}

return gomega.Equal(r.expectedName).Match(route.Name)
}

func (r *routeNameMatcher) FailureMessage(actual any) string {
return format.Message(actual, "to have name", r.expectedName)
}

func (r *routeNameMatcher) NegatedFailureMessage(actual any) string {
return format.Message(actual, "to not have name", r.expectedName)
}

func BeAttachedToService(svcName string) types.GomegaMatcher {
return &routeSvcMatcher{expectedSvcName: svcName}
}

type routeSvcMatcher struct {
expectedSvcName string
}

func (r *routeSvcMatcher) Match(actual any) (bool, error) {
if actual == nil {
return true, nil
}

route, ok := actual.(*openshiftroutev1.Route)
if !ok {
return false, fmt.Errorf("expected Route. Got:\n%s", format.Object(actual, 1)) //nolint:goerr113 //reason helpful in assertions
}

match, err := gomega.Equal("Service").Match(route.Spec.To.Kind)
if !match || err != nil {
return match, err
}

return gomega.Equal(r.expectedSvcName).Match(route.Spec.To.Name)
}

func (r *routeSvcMatcher) FailureMessage(actual any) string {
return format.Message(actual, "to be attached to service named", r.expectedSvcName)
}

func (r *routeSvcMatcher) NegatedFailureMessage(actual any) string {
return format.Message(actual, "not to be attached to service named", r.expectedSvcName)
}

func HaveHostPrefix(name string) types.GomegaMatcher {
return &routeHostPrefix{expectedHostPrefix: name}
}

type routeHostPrefix struct {
expectedHostPrefix string
}

func (matcher *routeHostPrefix) Match(actual any) (bool, error) {
if actual == nil {
return true, nil
}

route, ok := actual.(*openshiftroutev1.Route)
if !ok {
return false, fmt.Errorf("expected Route. Got:\n%s", format.Object(actual, 1)) //nolint:goerr113 //reason helpful in assertions
}

return gomega.HavePrefix(matcher.expectedHostPrefix).Match(route.Spec.Host)
}

func (matcher *routeHostPrefix) FailureMessage(actual any) string {
return format.Message(actual, "to have host prefix", matcher.expectedHostPrefix)
}

func (matcher *routeHostPrefix) NegatedFailureMessage(actual any) string {
return format.Message(actual, "to not have host prefix", matcher.expectedHostPrefix)
}

0 comments on commit ef6ef2d

Please sign in to comment.