-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
27fb8ed
commit 796b0a6
Showing
11 changed files
with
1,305 additions
and
149 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,102 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package experimental | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/onsi/gomega" | ||
"github.com/onsi/gomega/types" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// MatchConditions returns a custom matcher to check equality of clusterv1.Conditions. | ||
func MatchConditions(expected []metav1.Condition) types.GomegaMatcher { | ||
return &matchConditions{ | ||
expected: expected, | ||
} | ||
} | ||
|
||
type matchConditions struct { | ||
expected []metav1.Condition | ||
} | ||
|
||
func (m matchConditions) Match(actual interface{}) (success bool, err error) { | ||
elems := []interface{}{} | ||
for _, condition := range m.expected { | ||
elems = append(elems, MatchCondition(condition)) | ||
} | ||
|
||
return gomega.ConsistOf(elems...).Match(actual) | ||
} | ||
|
||
func (m matchConditions) FailureMessage(actual interface{}) (message string) { | ||
return fmt.Sprintf("expected\n\t%#v\nto match\n\t%#v\n", actual, m.expected) | ||
} | ||
|
||
func (m matchConditions) NegatedFailureMessage(actual interface{}) (message string) { | ||
return fmt.Sprintf("expected\n\t%#v\nto not match\n\t%#v\n", actual, m.expected) | ||
} | ||
|
||
// MatchCondition returns a custom matcher to check equality of clusterv1.Condition. | ||
func MatchCondition(expected metav1.Condition) types.GomegaMatcher { | ||
return &matchCondition{ | ||
expected: expected, | ||
} | ||
} | ||
|
||
type matchCondition struct { | ||
expected metav1.Condition | ||
} | ||
|
||
func (m matchCondition) Match(actual interface{}) (success bool, err error) { | ||
actualCondition, ok := actual.(metav1.Condition) | ||
if !ok { | ||
return false, fmt.Errorf("actual should be of type metav1.Condition") | ||
} | ||
|
||
ok, err = gomega.Equal(m.expected.Type).Match(actualCondition.Type) | ||
if !ok { | ||
return ok, err | ||
} | ||
ok, err = gomega.Equal(m.expected.Status).Match(actualCondition.Status) | ||
if !ok { | ||
return ok, err | ||
} | ||
ok, err = gomega.Equal(m.expected.ObservedGeneration).Match(actualCondition.ObservedGeneration) | ||
if !ok { | ||
return ok, err | ||
} | ||
ok, err = gomega.Equal(m.expected.Reason).Match(actualCondition.Reason) | ||
if !ok { | ||
return ok, err | ||
} | ||
ok, err = gomega.Equal(m.expected.Message).Match(actualCondition.Message) | ||
if !ok { | ||
return ok, err | ||
} | ||
|
||
return ok, err | ||
} | ||
|
||
func (m matchCondition) FailureMessage(actual interface{}) (message string) { | ||
return fmt.Sprintf("expected\n\t%#v\nto match\n\t%#v\n", actual, m.expected) | ||
} | ||
|
||
func (m matchCondition) NegatedFailureMessage(actual interface{}) (message string) { | ||
return fmt.Sprintf("expected\n\t%#v\nto not match\n\t%#v\n", actual, m.expected) | ||
} |
Oops, something went wrong.