generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add raw extension comparator #413
Merged
akgalwas
merged 1 commit into
kyma-project:main
from
m00g3n:add-raw-extension-comparator
Oct 10, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package errors | ||
|
||
import "fmt" | ||
|
||
var ( | ||
ErrInvalidType = fmt.Errorf("invalid type") | ||
ErrInvalidValue = fmt.Errorf("invalid value") | ||
ErrNilValue = fmt.Errorf("%w: nil", ErrInvalidValue) | ||
) |
63 changes: 63 additions & 0 deletions
63
hack/shoot-comparator/pkg/runtime/raw_extension_matcher.go
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,63 @@ | ||
package runtime | ||
|
||
import ( | ||
"sort" | ||
|
||
"github.com/kyma-project/infrastructure-manager/hack/shoot-comparator/pkg/utilz" | ||
"github.com/onsi/gomega" | ||
"github.com/onsi/gomega/types" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
type RawExtensionMatcher struct { | ||
toMatch interface{} | ||
} | ||
|
||
func NewRawExtensionMatcher(v any) types.GomegaMatcher { | ||
return &RawExtensionMatcher{ | ||
toMatch: v, | ||
} | ||
} | ||
|
||
func (m *RawExtensionMatcher) Match(actual interface{}) (bool, error) { | ||
if actual == nil && m.toMatch == nil { | ||
return true, nil | ||
} | ||
|
||
aRawExtension, err := utilz.Get[runtime.RawExtension](actual) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
eRawExtension, err := utilz.Get[runtime.RawExtension](m.toMatch) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
sort.Sort(sortBytes(aRawExtension.Raw)) | ||
sort.Sort(sortBytes(eRawExtension.Raw)) | ||
|
||
return gomega.BeComparableTo(aRawExtension.Raw).Match(eRawExtension.Raw) | ||
} | ||
|
||
func (m *RawExtensionMatcher) NegatedFailureMessage(_ interface{}) string { | ||
return "expected should not equal actual" | ||
} | ||
|
||
func (m *RawExtensionMatcher) FailureMessage(_ interface{}) string { | ||
return "expected should equal actual" | ||
} | ||
|
||
type sortBytes []byte | ||
|
||
func (s sortBytes) Less(i, j int) bool { | ||
return s[i] < s[j] | ||
} | ||
|
||
func (s sortBytes) Swap(i, j int) { | ||
s[i], s[j] = s[j], s[i] | ||
} | ||
|
||
func (s sortBytes) Len() int { | ||
return len(s) | ||
} |
66 changes: 66 additions & 0 deletions
66
hack/shoot-comparator/pkg/runtime/raw_extension_matcher_test.go
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,66 @@ | ||
package runtime | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
func loadTestdata(t *testing.T, filePath string) []byte { | ||
data, err := os.ReadFile(filePath) | ||
require.NoError(t, err) | ||
require.Greater(t, len(data), 0, "test data is empty") | ||
return data | ||
} | ||
|
||
func TestMatcher(t *testing.T) { | ||
testCases := []struct { | ||
actual interface{} | ||
expected interface{} | ||
isOK bool | ||
hasErr bool | ||
}{ | ||
{ | ||
actual: string(loadTestdata(t, "testdata/infra_cfg_azure_11.yaml")), | ||
expected: string(loadTestdata(t, "testdata/infra_cfg_azure_12.yaml")), | ||
isOK: true, | ||
hasErr: false, | ||
}, | ||
{ | ||
actual: runtime.RawExtension{}, | ||
expected: runtime.RawExtension{}, | ||
isOK: true, | ||
hasErr: false, | ||
}, | ||
{ | ||
actual: runtime.RawExtension{}, | ||
expected: nil, | ||
isOK: false, | ||
hasErr: true, | ||
}, | ||
{ | ||
actual: string(loadTestdata(t, "testdata/infra_cfg_azure_11.yaml")), | ||
expected: string(loadTestdata(t, "testdata/infra_cfg_azure_21.yaml")), | ||
isOK: false, | ||
hasErr: false, | ||
}, | ||
{ | ||
actual: []byte("invalid type"), | ||
expected: []byte("invalid type"), | ||
isOK: false, | ||
hasErr: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
matcher := NewRawExtensionMatcher(tc.actual) | ||
ok, err := matcher.Match(tc.expected) | ||
// THEN | ||
assert.Equal(t, tc.hasErr, err != nil, err) | ||
assert.Equal(t, tc.isOK, ok, matcher.FailureMessage(nil)) | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
hack/shoot-comparator/pkg/runtime/testdata/infra_cfg_azure_11.yaml
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,21 @@ | ||
apiVersion: azure.provider.extensions.gardener.cloud/v1alpha1 | ||
kind: InfrastructureConfig | ||
networks: | ||
vnet: | ||
cidr: 10.250.0.0/22 | ||
zones: | ||
- cidr: 10.250.0.0/25 | ||
name: 2 | ||
natGateway: | ||
enabled: true | ||
idleConnectionTimeoutMinutes: 4 | ||
- cidr: 10.250.0.128/25 | ||
name: 3 | ||
natGateway: | ||
enabled: true | ||
idleConnectionTimeoutMinutes: 4 | ||
- cidr: 10.250.1.0/25 | ||
name: 1 | ||
natGateway: | ||
enabled: true | ||
idleConnectionTimeoutMinutes: 4 |
21 changes: 21 additions & 0 deletions
21
hack/shoot-comparator/pkg/runtime/testdata/infra_cfg_azure_12.yaml
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,21 @@ | ||
apiVersion: azure.provider.extensions.gardener.cloud/v1alpha1 | ||
kind: InfrastructureConfig | ||
networks: | ||
vnet: | ||
cidr: 10.250.0.0/22 | ||
zones: | ||
- cidr: 10.250.0.128/25 | ||
name: 3 | ||
natGateway: | ||
enabled: true | ||
idleConnectionTimeoutMinutes: 4 | ||
- cidr: 10.250.1.0/25 | ||
name: 1 | ||
natGateway: | ||
enabled: true | ||
idleConnectionTimeoutMinutes: 4 | ||
- cidr: 10.250.0.0/25 | ||
name: 2 | ||
natGateway: | ||
enabled: true | ||
idleConnectionTimeoutMinutes: 4 |
11 changes: 11 additions & 0 deletions
11
hack/shoot-comparator/pkg/runtime/testdata/infra_cfg_azure_21.yaml
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,11 @@ | ||
apiVersion: azure.provider.extensions.gardener.cloud/v1alpha1 | ||
kind: InfrastructureConfig | ||
networks: | ||
vnet: | ||
cidr: 10.250.0.0/22 | ||
zones: | ||
- cidr: 10.250.1.0/25 | ||
name: 1 | ||
natGateway: | ||
enabled: true | ||
idleConnectionTimeoutMinutes: 4 |
6 changes: 6 additions & 0 deletions
6
hack/shoot-comparator/pkg/runtime/testdata/infra_cfg_openstack_11.yaml
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,6 @@ | ||
apiVersion: openstack.provider.extensions.gardener.cloud/v1alpha1 | ||
floatingPoolName: FloatingIP-external-kyma-01 | ||
kind: InfrastructureConfig | ||
networks: | ||
worker: '' | ||
workers: 10.250.0.0/22 |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file seems to not be used anywhere.