Skip to content

Commit

Permalink
Initialize test helpers for testing api-gateway resource generation
Browse files Browse the repository at this point in the history
  • Loading branch information
nathancoleman committed Feb 15, 2023
1 parent 2b69d79 commit e229b33
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
135 changes: 135 additions & 0 deletions agent/proxycfg/testing_api_gateway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package proxycfg

import (
"fmt"

"github.com/mitchellh/go-testing-interface"

"github.com/hashicorp/consul/agent/connect"
"github.com/hashicorp/consul/agent/consul/discoverychain"
"github.com/hashicorp/consul/agent/structs"
)

func TestConfigSnapshotAPIGateway(t testing.T) *ConfigSnapshot {
roots, placeholderLeaf := TestCerts(t)

entries := []structs.ConfigEntry{
&structs.ProxyConfigEntry{
Kind: structs.ProxyDefaults,
Name: structs.ProxyConfigGlobal,
Config: map[string]interface{}{
"protocol": "tcp",
},
},
&structs.ServiceResolverConfigEntry{
Kind: structs.ServiceResolver,
Name: "api-gateway",
},
}

baseEvents := []UpdateEvent{
{
CorrelationID: rootsWatchID,
Result: roots,
},
{
CorrelationID: leafWatchID,
Result: placeholderLeaf,
},
{
CorrelationID: gatewayConfigWatchID,
Result: &structs.ConfigEntryResponse{
Entry: &structs.APIGatewayConfigEntry{
Kind: structs.APIGateway,
Name: "api-gateway",
Listeners: []structs.APIGatewayListener{
{
Name: "",
Hostname: "",
Port: 8080,
Protocol: structs.ListenerProtocolTCP,
TLS: structs.APIGatewayTLSConfiguration{
Certificates: []structs.ResourceReference{
{
Kind: structs.InlineCertificate,
Name: "my-inline-certificate",
},
},
},
},
},
},
},
},
{
CorrelationID: gatewayConfigWatchID,
Result: &structs.ConfigEntryResponse{
Entry: &structs.BoundAPIGatewayConfigEntry{
Kind: structs.BoundAPIGateway,
Name: "api-gateway",
Listeners: []structs.BoundAPIGatewayListener{
{
Name: "",
Certificates: []structs.ResourceReference{
{
Kind: structs.InlineCertificate,
Name: "my-inline-certificate",
},
},
Routes: []structs.ResourceReference{
{
Kind: structs.TCPRoute,
Name: "my-tcp-route",
},
},
},
},
},
},
},
{
CorrelationID: routeConfigWatchID,
Result: &structs.ConfigEntryResponse{
Entry: &structs.TCPRouteConfigEntry{
Kind: structs.TCPRoute,
Name: "my-tcp-route",
Parents: []structs.ResourceReference{
{
Kind: structs.APIGateway,
Name: "api-gateway",
},
},
Services: []structs.TCPService{
{Name: "my-tcp-service"},
},
},
},
},
{
CorrelationID: inlineCertificateConfigWatchID,
Result: &structs.ConfigEntryResponse{
Entry: &structs.InlineCertificateConfigEntry{
Kind: structs.InlineCertificate,
Name: "my-inline-certificate",
Certificate: "certificate",
PrivateKey: "private key",
},
},
},
{
CorrelationID: fmt.Sprintf("discovery-chain:%s", UpstreamIDString("","","my-tcp-service",nil, "")),
Result: &structs.DiscoveryChainResponse{
Chain: discoverychain.TestCompileConfigEntries(t,"my-tcp-service","default","default","dc1", connect.TestClusterID+".consul",nil,entries...),
},
},
}

return testConfigSnapshotFixture(t, &structs.NodeService{
Kind: structs.ServiceKindAPIGateway,
Service: "api-gateway",
Port: 9999,
Address: "1.2.3.4",
Meta: nil,
TaggedAddresses: nil,
}, nil, nil, testSpliceEvents(baseEvents, nil))
}
12 changes: 12 additions & 0 deletions agent/xds/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func TestAllResourcesFromSnapshot(t *testing.T) {
tests = append(tests, getMeshGatewayPeeringGoldenTestCases()...)
tests = append(tests, getTrafficControlPeeringGoldenTestCases()...)
tests = append(tests, getEnterpriseGoldenTestCases()...)
tests = append(tests, getAPIGatewayGoldenTestCases()...)

latestEnvoyVersion := xdscommon.EnvoyVersions[0]
for _, envoyVersion := range xdscommon.EnvoyVersions {
Expand Down Expand Up @@ -262,3 +263,14 @@ func getTrafficControlPeeringGoldenTestCases() []goldenTestCase {
},
}
}

func getAPIGatewayGoldenTestCases() []goldenTestCase {
return []goldenTestCase{
{
name:"api-gateway-with-tcp-route-and-inline-certificate",
create: func(t testinf.T) *proxycfg.ConfigSnapshot {
return proxycfg.TestConfigSnapshotAPIGateway(t)
},
},
}
}

0 comments on commit e229b33

Please sign in to comment.