From c7f1ca945915f60b14c4c7a0ecd8fa03f79b460e Mon Sep 17 00:00:00 2001
From: Conor Mongey <conor@mongey.net>
Date: Thu, 21 Mar 2024 16:32:52 +0000
Subject: [PATCH] Add test

---
 agent/consul/discoverychain/compile_test.go | 65 +++++++++++++++++++++
 1 file changed, 65 insertions(+)

diff --git a/agent/consul/discoverychain/compile_test.go b/agent/consul/discoverychain/compile_test.go
index 8c9c9dfec7f8e..19c7e2d748e53 100644
--- a/agent/consul/discoverychain/compile_test.go
+++ b/agent/consul/discoverychain/compile_test.go
@@ -29,6 +29,7 @@ func TestCompile(t *testing.T) {
 	t.Parallel()
 
 	cases := map[string]compileTestCase{
+		"proxy defaults with router":                       testCase_ProxyDefaultsWithRouter(),
 		"router with defaults":                             testcase_JustRouterWithDefaults(),
 		"router with defaults and resolver":                testcase_RouterWithDefaults_NoSplit_WithResolver(),
 		"router with defaults and noop split":              testcase_RouterWithDefaults_WithNoopSplit_DefaultResolver(),
@@ -161,6 +162,70 @@ func TestCompile(t *testing.T) {
 	}
 }
 
+func testCase_ProxyDefaultsWithRouter() compileTestCase {
+	entries := newEntries()
+	setGlobalProxyProtocol(entries, "http")
+	setServiceProtocol(entries, "main", "")
+
+	entries.AddRouters(
+		&structs.ServiceRouterConfigEntry{
+			Kind: "service-router",
+			Name: "main",
+			Routes: []structs.ServiceRoute{
+				{
+					Match: &structs.ServiceRouteMatch{
+						HTTP: &structs.ServiceRouteHTTPMatch{},
+					},
+					Destination: &structs.ServiceRouteDestination{
+						RequestTimeout:        time.Duration(time.Minute),
+						RetryOnConnectFailure: true,
+					},
+				},
+			},
+		},
+	)
+
+	route := newDefaultServiceRoute("", "", "")
+	route.Destination.RetryOnConnectFailure = true
+	route.Destination.RequestTimeout = time.Duration(time.Minute)
+	route.Match.HTTP.PathPrefix = ""
+
+	expect := &structs.CompiledDiscoveryChain{
+		Protocol:  "http",
+		StartNode: "router:main.default.default",
+		Nodes: map[string]*structs.DiscoveryGraphNode{
+			"router:main.default.default": {
+				Type: structs.DiscoveryGraphNodeTypeRouter,
+				Name: "main.default.default",
+				Routes: []*structs.DiscoveryRoute{
+					{
+						Definition: route,
+						NextNode:   "resolver:main.default.default.dc1",
+					},
+					{
+						Definition: newDefaultServiceRoute("main", "default", "default"),
+						NextNode:   "resolver:main.default.default.dc1",
+					},
+				},
+			},
+			"resolver:main.default.default.dc1": {
+				Type: structs.DiscoveryGraphNodeTypeResolver,
+				Name: "main.default.default.dc1",
+				Resolver: &structs.DiscoveryResolver{
+					Default:        true,
+					ConnectTimeout: 5 * time.Second,
+					Target:         "main.default.default.dc1",
+				},
+			},
+		},
+		Targets: map[string]*structs.DiscoveryTarget{
+			"main.default.default.dc1": newTarget(structs.DiscoveryTargetOpts{Service: "main"}, nil),
+		},
+	}
+
+	return compileTestCase{entries: entries, expect: expect}
+}
+
 func testcase_JustRouterWithDefaults() compileTestCase {
 	entries := newEntries()
 	setServiceProtocol(entries, "main", "http")