generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First version of the Extension comparator
- Loading branch information
Showing
5 changed files
with
210 additions
and
39 deletions.
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
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,74 @@ | ||
package shoot | ||
|
||
import ( | ||
"github.com/gardener/gardener/pkg/apis/core/v1beta1" | ||
. "github.com/onsi/ginkgo/v2" //nolint:revive | ||
. "github.com/onsi/gomega" //nolint:revive | ||
) | ||
|
||
var _ = Describe(":: extension matcher :: ", func() { | ||
var empty []v1beta1.Extension | ||
|
||
DescribeTable( | ||
"checking invalid args :: ", | ||
testExtensionInvalidArgs, | ||
Entry("when actual is nil", nil, empty), | ||
Entry("when expected is nil", "", nil), | ||
) | ||
|
||
//disabledTrue := true | ||
disabledFalse := false | ||
|
||
DescribeTable( | ||
"checking results :: ", | ||
testExtensionResults, | ||
Entry( | ||
"should match empty and zero values", | ||
nil, | ||
empty, | ||
true, | ||
), | ||
Entry( | ||
"should match identical tables", | ||
newExtensionTable(newExtension("test", &disabledFalse)), | ||
newExtensionTable(newExtension("test", &disabledFalse)), | ||
true, | ||
), | ||
Entry( | ||
"should detect extension missing", | ||
newExtensionTable(newExtension("test1", &disabledFalse), newExtension("test2", &disabledFalse)), | ||
newExtensionTable(newExtension("test", &disabledFalse)), | ||
false, | ||
), | ||
Entry( | ||
"should detect redundant extension", | ||
newExtensionTable(newExtension("test", &disabledFalse)), | ||
newExtensionTable(newExtension("test1", &disabledFalse), newExtension("test2", &disabledFalse)), | ||
false, | ||
), | ||
) | ||
}) | ||
|
||
func newExtension(name string, disabled *bool) v1beta1.Extension { | ||
return v1beta1.Extension{ | ||
Type: name, | ||
Disabled: disabled, | ||
} | ||
} | ||
|
||
func newExtensionTable(extensions ...v1beta1.Extension) []v1beta1.Extension { | ||
return extensions | ||
} | ||
|
||
func testExtensionInvalidArgs(actual, expected interface{}) { | ||
matcher := NewExtensionMatcher(expected) | ||
_, err := matcher.Match(actual) | ||
Expect(err).To(HaveOccurred()) | ||
} | ||
|
||
func testExtensionResults(actual, expected interface{}, expectedMatch bool) { | ||
matcher := NewExtensionMatcher(expected) | ||
actualMatch, err := matcher.Match(actual) | ||
Expect(err).ShouldNot(HaveOccurred()) | ||
Expect(actualMatch).Should(Equal(expectedMatch), matcher.FailureMessage(actual)) | ||
} |
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,95 @@ | ||
package shoot | ||
|
||
import ( | ||
"fmt" | ||
"github.com/gardener/gardener/pkg/apis/core/v1beta1" | ||
"github.com/onsi/gomega" | ||
"github.com/onsi/gomega/types" | ||
"github.com/pkg/errors" | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
type ExtensionMatcher struct { | ||
toMatch interface{} | ||
fails []string | ||
} | ||
|
||
func NewExtensionMatcher(i interface{}) types.GomegaMatcher { | ||
return &ExtensionMatcher{ | ||
toMatch: i, | ||
} | ||
} | ||
|
||
func (m *ExtensionMatcher) Match(actual interface{}) (success bool, err error) { | ||
|
||
if err != nil { | ||
return false, err | ||
} | ||
|
||
if actual == nil && m.toMatch != nil { | ||
return false, errors.New("actual is nil") | ||
} | ||
|
||
if actual != nil && m.toMatch == nil { | ||
return false, errors.New("expected is nil") | ||
} | ||
|
||
if actual == nil && m.toMatch == nil { | ||
return true, nil | ||
} | ||
|
||
aExtensions, err := getExtension(actual) | ||
findExtension := func(extensions []v1beta1.Extension, extensionToFind string) v1beta1.Extension { | ||
for _, extension := range extensions { | ||
if extension.Type == extensionToFind { | ||
return extension | ||
} | ||
} | ||
return v1beta1.Extension{} | ||
} | ||
|
||
eExtensions, err := getExtension(m.toMatch) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
for i, eExtension := range eExtensions { | ||
aExtension := findExtension(aExtensions, eExtension.Type) | ||
matcher := gomega.BeComparableTo(eExtension.Type) | ||
ok, err := matcher.Match(aExtension.Type) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
if !ok { | ||
msg := fmt.Sprintf("spec/Extensions[%d]: %s", i, matcher.FailureMessage(aExtension)) | ||
m.fails = append(m.fails, msg) | ||
} | ||
|
||
} | ||
|
||
return false, nil | ||
} | ||
|
||
func (m *ExtensionMatcher) NegatedFailureMessage(_ interface{}) string { | ||
return "expected should not equal actual" | ||
} | ||
|
||
func (m *ExtensionMatcher) FailureMessage(_ interface{}) string { | ||
return strings.Join(m.fails, "\n") | ||
} | ||
|
||
func getExtension(i interface{}) (shoot []v1beta1.Extension, err error) { | ||
if i == nil { | ||
return []v1beta1.Extension{}, fmt.Errorf("invalid value nil") | ||
} | ||
|
||
switch v := i.(type) { | ||
case []v1beta1.Extension: | ||
return v, nil | ||
|
||
default: | ||
return []v1beta1.Extension{}, fmt.Errorf(`%w: %s`, errInvalidType, reflect.TypeOf(v)) | ||
} | ||
} |
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