Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
8 changes: 7 additions & 1 deletion internal/gatewayapi/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ func TestRunner(t *testing.T) {
return false
}
// Ensure ir is empty
return (reflect.DeepEqual(xdsIR.LoadAll(), map[string]*message.XdsIRWithContext{})) && (reflect.DeepEqual(infraIR.LoadAll(), map[string]*ir.Infra{}))
return reflect.DeepEqual(
Comment thread
rudrakhp marked this conversation as resolved.
message.SortedXdsIRMap(xdsIR.LoadAll()),
message.SortedXdsIRMap(xdsIR.LoadAll()),
) && reflect.DeepEqual(
message.SortedInfraIRMap(infraIR.LoadAll()),
message.SortedInfraIRMap(map[string]*ir.Infra{}),
)
}, time.Second*1, time.Millisecond*20)
}

Expand Down
59 changes: 57 additions & 2 deletions internal/message/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ package message

import (
"context"
"reflect"

"github.com/telepresenceio/watchable"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"reflect"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gwapiv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
"sort"

egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
"github.com/envoyproxy/gateway/internal/gatewayapi/resource"
Expand Down Expand Up @@ -120,6 +120,61 @@ type ExtensionStatuses struct {
BackendStatuses watchable.Map[types.NamespacedName, *egv1a1.BackendStatus]
}

// Deterministic entries for IR map comparison
type XdsIREntry struct {
Name string
IR *XdsIRWithContext
}

type InfraIREntry struct {
Name string
IR *ir.Infra
}

// SortedXdsIRMap converts an XdsIR map into a deterministically ordered slice.
// This is required for stable comparisons in tests.
func SortedXdsIRMap(in map[string]*XdsIRWithContext) []XdsIRWithContext {
if in == nil {
return nil
}

keys := make([]string, 0, len(in))
for k := range in {
keys = append(keys, k)
}
sort.Strings(keys)

out := make([]XdsIRWithContext, 0, len(keys))
for _, k := range keys {
if v := in[k]; v != nil {
out = append(out, *v)
} else {
out = append(out, XdsIRWithContext{})
}
}
return out
}

// SortedInfraIRMap converts an InfraIR map into a deterministically ordered slice.
// This is required for stable comparisons in tests.
func SortedInfraIRMap(in map[string]*ir.Infra) []*ir.Infra {
if in == nil {
return nil
}

keys := make([]string, 0, len(in))
for k := range in {
keys = append(keys, k)
}
sort.Strings(keys)

out := make([]*ir.Infra, 0, len(keys))
for _, k := range keys {
out = append(out, in[k])
}
return out
}

func (p *PolicyStatuses) Close() {
p.ClientTrafficPolicyStatuses.Close()
p.BackendTrafficPolicyStatuses.Close()
Expand Down
32 changes: 32 additions & 0 deletions internal/message/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,35 @@ func TestXdsWithContextEqual(t *testing.T) {
assert.True(t, x1.Equal(x2))
assert.True(t, x2.Equal(x1))
}

func TestSortedXdsIRMap(t *testing.T) {
in := map[string]*message.XdsIRWithContext{
"b": {XdsIR: &ir.Xds{}},
Comment thread
Aditya7880900936 marked this conversation as resolved.
"a": {XdsIR: &ir.Xds{}},
}

out := message.SortedXdsIRMap(in)

assert.Len(t, out, 2)
assert.NotNil(t, out[0].XdsIR)
assert.NotNil(t, out[1].XdsIR)
}

func TestSortedXdsIRMapNil(t *testing.T) {
assert.Nil(t, message.SortedXdsIRMap(nil))
}

func TestSortedInfraIRMap(t *testing.T) {
in := map[string]*ir.Infra{
"b": {},
"a": {},
}

out := message.SortedInfraIRMap(in)

assert.Len(t, out, 2)
}

func TestSortedInfraIRMapNil(t *testing.T) {
assert.Nil(t, message.SortedInfraIRMap(nil))
}
Loading