Skip to content
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
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions hack/shoot-comparator/pkg/errors/errors.go
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 hack/shoot-comparator/pkg/runtime/raw_extension_matcher.go
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 hack/shoot-comparator/pkg/runtime/raw_extension_matcher_test.go
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))
}

}
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
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
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: openstack.provider.extensions.gardener.cloud/v1alpha1
Copy link
Contributor

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.

floatingPoolName: FloatingIP-external-kyma-01
kind: InfrastructureConfig
networks:
worker: ''
workers: 10.250.0.0/22
3 changes: 2 additions & 1 deletion hack/shoot-comparator/pkg/shoot/extensionmatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/gardener/gardener/pkg/apis/core/v1beta1"
"github.com/kyma-project/infrastructure-manager/hack/shoot-comparator/pkg/errors"
"github.com/onsi/gomega"
"github.com/onsi/gomega/types"
)
Expand Down Expand Up @@ -45,7 +46,7 @@ func getExtension(i interface{}) ([]v1beta1.Extension, error) {
return v, nil

default:
return []v1beta1.Extension{}, fmt.Errorf(`%w: %s`, errInvalidType, reflect.TypeOf(v))
return []v1beta1.Extension{}, fmt.Errorf(`%w: %s`, errors.ErrInvalidType, reflect.TypeOf(v))
}
}

Expand Down
Loading
Loading