2022-07-22 10:52:05 +00:00
|
|
|
package proxycfgglue
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
|
|
|
"github.com/hashicorp/consul/agent/proxycfg"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
"github.com/hashicorp/consul/proto/pbpeering"
|
2022-07-27 15:06:20 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2022-07-28 19:01:52 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-07-22 10:52:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestServerExportedPeeredServices(t *testing.T) {
|
|
|
|
nextIndex := indexGenerator()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
|
|
|
store := state.NewStateStore(nil)
|
2022-10-07 21:45:49 +00:00
|
|
|
require.NoError(t, store.CASetConfig(0, &structs.CAConfiguration{ClusterID: "cluster-id"}))
|
2022-07-22 10:52:05 +00:00
|
|
|
|
|
|
|
for _, peer := range []string{"peer-1", "peer-2", "peer-3"} {
|
2022-08-01 14:33:18 +00:00
|
|
|
require.NoError(t, store.PeeringWrite(nextIndex(), &pbpeering.PeeringWriteRequest{
|
|
|
|
Peering: &pbpeering.Peering{
|
|
|
|
ID: testUUID(t),
|
|
|
|
Name: peer,
|
|
|
|
State: pbpeering.PeeringState_ACTIVE,
|
|
|
|
},
|
2022-07-22 10:52:05 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, store.EnsureConfigEntry(nextIndex(), &structs.ExportedServicesConfigEntry{
|
|
|
|
Name: "default",
|
|
|
|
Services: []structs.ExportedService{
|
|
|
|
{
|
|
|
|
Name: "web",
|
|
|
|
Consumers: []structs.ServiceConsumer{
|
2022-10-04 18:46:15 +00:00
|
|
|
{Peer: "peer-1"},
|
2022-07-22 10:52:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "db",
|
|
|
|
Consumers: []structs.ServiceConsumer{
|
2022-10-04 18:46:15 +00:00
|
|
|
{Peer: "peer-2"},
|
2022-07-22 10:52:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
authz := policyAuthorizer(t, `
|
|
|
|
service "web" { policy = "read" }
|
|
|
|
service "api" { policy = "read" }
|
|
|
|
service "db" { policy = "deny" }
|
|
|
|
`)
|
|
|
|
|
|
|
|
eventCh := make(chan proxycfg.UpdateEvent)
|
|
|
|
dataSource := ServerExportedPeeredServices(ServerDataSourceDeps{
|
|
|
|
GetStore: func() Store { return store },
|
|
|
|
ACLResolver: newStaticResolver(authz),
|
|
|
|
})
|
2022-10-07 21:45:49 +00:00
|
|
|
require.NoError(t, dataSource.Notify(ctx, &structs.DCSpecificRequest{Datacenter: "dc1"}, "", eventCh))
|
2022-07-22 10:52:05 +00:00
|
|
|
|
2022-07-27 15:06:20 +00:00
|
|
|
testutil.RunStep(t, "initial state", func(t *testing.T) {
|
2022-07-22 10:52:05 +00:00
|
|
|
result := getEventResult[*structs.IndexedExportedServiceList](t, eventCh)
|
|
|
|
require.Equal(t,
|
|
|
|
map[string]structs.ServiceList{
|
|
|
|
"peer-1": {structs.NewServiceName("web", nil)},
|
|
|
|
},
|
|
|
|
result.Services,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-07-27 15:06:20 +00:00
|
|
|
testutil.RunStep(t, "update exported services", func(t *testing.T) {
|
2022-07-22 10:52:05 +00:00
|
|
|
require.NoError(t, store.EnsureConfigEntry(nextIndex(), &structs.ExportedServicesConfigEntry{
|
|
|
|
Name: "default",
|
|
|
|
Services: []structs.ExportedService{
|
|
|
|
{
|
|
|
|
Name: "web",
|
|
|
|
Consumers: []structs.ServiceConsumer{
|
2022-10-04 18:46:15 +00:00
|
|
|
{Peer: "peer-1"},
|
2022-07-22 10:52:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "db",
|
|
|
|
Consumers: []structs.ServiceConsumer{
|
2022-10-04 18:46:15 +00:00
|
|
|
{Peer: "peer-2"},
|
2022-07-22 10:52:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "api",
|
|
|
|
Consumers: []structs.ServiceConsumer{
|
2022-10-04 18:46:15 +00:00
|
|
|
{Peer: "peer-1"},
|
|
|
|
{Peer: "peer-3"},
|
2022-07-22 10:52:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
result := getEventResult[*structs.IndexedExportedServiceList](t, eventCh)
|
|
|
|
require.Equal(t,
|
|
|
|
map[string]structs.ServiceList{
|
|
|
|
"peer-1": {
|
|
|
|
structs.NewServiceName("api", nil),
|
|
|
|
structs.NewServiceName("web", nil),
|
|
|
|
},
|
|
|
|
"peer-3": {
|
|
|
|
structs.NewServiceName("api", nil),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
result.Services,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|