Skip to content

Commit

Permalink
Add raw extension comparator (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
m00g3n authored Oct 10, 2024
1 parent e305111 commit c9d296a
Show file tree
Hide file tree
Showing 11 changed files with 345 additions and 41 deletions.
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))
}

}
21 changes: 21 additions & 0 deletions hack/shoot-comparator/pkg/runtime/testdata/infra_cfg_azure_11.yaml
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 hack/shoot-comparator/pkg/runtime/testdata/infra_cfg_azure_12.yaml
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 hack/shoot-comparator/pkg/runtime/testdata/infra_cfg_azure_21.yaml
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
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

0 comments on commit c9d296a

Please sign in to comment.