forked from maistra/odh-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(test): adds custom matchers for openshift routes
- Loading branch information
1 parent
4a967d9
commit ef6ef2d
Showing
3 changed files
with
139 additions
and
1 deletion.
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,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 |
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,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) | ||
} |