Skip to content

Commit

Permalink
add merge function
Browse files Browse the repository at this point in the history
  • Loading branch information
erichaberkorn committed Mar 22, 2023
1 parent 25642ea commit 97a01f0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
28 changes: 28 additions & 0 deletions agent/structs/discovery_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,34 @@ type DiscoveryTargetOpts struct {
Peer string
}

func MergeDiscoveryTargetOpts(o1 DiscoveryTargetOpts, o2 DiscoveryTargetOpts) DiscoveryTargetOpts {
if o2.Service != "" {
o1.Service = o2.Service
}

if o2.ServiceSubset != "" {
o1.ServiceSubset = o2.ServiceSubset
}

if o2.Namespace != "" {
o1.Namespace = o2.Namespace
}

if o2.Partition != "" {
o1.Partition = o2.Partition
}

if o2.Datacenter != "" {
o1.Datacenter = o2.Datacenter
}

if o2.Peer != "" {
o1.Peer = o2.Peer
}

return o1
}

func NewDiscoveryTarget(opts DiscoveryTargetOpts) *DiscoveryTarget {
t := &DiscoveryTarget{
Service: opts.Service,
Expand Down
33 changes: 25 additions & 8 deletions agent/xds/golden_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package xds

import (
"encoding/json"
"flag"
"fmt"
"os"
"path/filepath"
"testing"

"github.com/hashicorp/go-version"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -83,12 +85,16 @@ func golden(t *testing.T, name, subname, latestSubname, got string) string {

golden := filepath.Join("testdata", name+suffix)

// Always load the latest golden file if configured to do so.
latestExpected := ""
if latestSubname != "" && subname != latestSubname {
latestGolden := filepath.Join("testdata", fmt.Sprintf("%s.%s.golden", name, latestSubname))
raw, err := os.ReadFile(latestGolden)
require.NoError(t, err, "%q %q %q", name, subname, latestSubname)
var latestGoldenPath, latestExpected string
isLatest := subname == latestSubname
// Include latestSubname in the latest golden path if it exists.
if latestSubname == "" {
latestGoldenPath = filepath.Join("testdata", fmt.Sprintf("%s.golden", name))
} else {
latestGoldenPath = filepath.Join("testdata", fmt.Sprintf("%s.%s.golden", name, latestSubname))
}

if raw, err := os.ReadFile(latestGoldenPath); err == nil {
latestExpected = string(raw)
}

Expand All @@ -97,8 +103,14 @@ func golden(t *testing.T, name, subname, latestSubname, got string) string {
//
// To trim down PRs, we only create per-version golden files if they differ
// from the latest version.

if *update && got != "" {
if latestExpected == got {
var gotInterface, latestExpectedInterface interface{}
json.Unmarshal([]byte(got), &gotInterface)
json.Unmarshal([]byte(latestExpected), &latestExpectedInterface)

// Remove non-latest golden files if they are the same as the latest one.
if !isLatest && assert.ObjectsAreEqualValues(gotInterface, latestExpectedInterface) {
// In update mode we erase a golden file if it is identical to
// the golden file corresponding to the latest version of
// envoy.
Expand All @@ -109,7 +121,12 @@ func golden(t *testing.T, name, subname, latestSubname, got string) string {
return got
}

require.NoError(t, os.WriteFile(golden, []byte(got), 0644))
// We use require.JSONEq to compare values and ObjectsAreEqualValues is used
// internally by that function to compare the string JSON values. This only
// writes updates the golden file when they actually change.
if !assert.ObjectsAreEqualValues(gotInterface, latestExpectedInterface) {
require.NoError(t, os.WriteFile(golden, []byte(got), 0644))
}
return got
}

Expand Down

0 comments on commit 97a01f0

Please sign in to comment.