Skip to content

Commit

Permalink
Tests changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tauhid621 committed Jan 10, 2024
1 parent 63c9cd0 commit 9336394
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 272 deletions.
10 changes: 8 additions & 2 deletions agent/config_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,15 +793,21 @@ func TestConfig_Exported_Services(t *testing.T) {
require.Len(t, services, 2)
assertIndex(t, resp)

entMeta := acl.DefaultEnterpriseMeta()

expected := []api.ResolvedExportedService{
{
Service: "api",
Service: "api",
Partition: entMeta.PartitionOrEmpty(),
Namespace: entMeta.NamespaceOrEmpty(),
Consumers: api.ResolvedConsumers{
Peers: []string{"east", "west"},
},
},
{
Service: "db",
Service: "db",
Partition: entMeta.PartitionOrEmpty(),
Namespace: entMeta.NamespaceOrEmpty(),
Consumers: api.ResolvedConsumers{
Peers: []string{"east"},
},
Expand Down
2 changes: 1 addition & 1 deletion agent/consul/configentry_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type ConfigEntryBackend struct {

var _ configentry.Backend = (*ConfigEntryBackend)(nil)

// NewPeeringBackend returns a peering.Backend implementation that is bound to the given server.
// NewConfigEntryBackend returns a configentry.Backend implementation that is bound to the given server.
func NewConfigEntryBackend(srv *Server) *ConfigEntryBackend {
return &ConfigEntryBackend{
srv: srv,
Expand Down
159 changes: 159 additions & 0 deletions agent/consul/state/config_entry_exported_services_ce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/proto/private/pbconfigentry"
"github.com/hashicorp/go-memdb"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -48,3 +49,161 @@ func TestStore_prepareExportedServicesResponse(t *testing.T) {

require.ElementsMatch(t, expected, resp)
}

func TestStore_ResolvedExportingServices(t *testing.T) {
s := NewStateStore(nil)
var c indexCounter

{
require.NoError(t, s.EnsureNode(c.Next(), &structs.Node{
Node: "foo", Address: "127.0.0.1",
}))

require.NoError(t, s.EnsureService(c.Next(), "foo", &structs.NodeService{
ID: "db", Service: "db", Port: 5000,
}))

require.NoError(t, s.EnsureService(c.Next(), "foo", &structs.NodeService{
ID: "cache", Service: "cache", Port: 5000,
}))

entry := &structs.ExportedServicesConfigEntry{
Name: "default",
Services: []structs.ExportedService{
{
Name: "db",
Consumers: []structs.ServiceConsumer{
{
Peer: "east",
},
{
Peer: "west",
},
},
},
{
Name: "cache",
Consumers: []structs.ServiceConsumer{
{
Peer: "east",
},
},
},
},
}
err := s.EnsureConfigEntry(c.Next(), entry)
require.NoError(t, err)

// Adding services to check wildcard config later on

require.NoError(t, s.EnsureService(c.Next(), "foo", &structs.NodeService{
ID: "frontend", Service: "frontend", Port: 5000,
}))

require.NoError(t, s.EnsureService(c.Next(), "foo", &structs.NodeService{
ID: "backend", Service: "backend", Port: 5000,
}))

// The consul service should never be exported.
require.NoError(t, s.EnsureService(c.Next(), "foo", &structs.NodeService{
ID: structs.ConsulServiceID, Service: structs.ConsulServiceName, Port: 8000,
}))

}

type testCase struct {
expect []*pbconfigentry.ResolvedExportedService
idx uint64
}

run := func(t *testing.T, tc testCase) {
ws := memdb.NewWatchSet()
defaultMeta := structs.DefaultEnterpriseMetaInDefaultPartition()
idx, services, err := s.ResolvedExportedServices(ws, defaultMeta)
require.NoError(t, err)
require.Equal(t, tc.idx, idx)
require.ElementsMatch(t, tc.expect, services)
}

t.Run("only exported services are included", func(t *testing.T) {
tc := testCase{
expect: []*pbconfigentry.ResolvedExportedService{
{
Service: "db",
Consumers: &pbconfigentry.Consumers{
Peers: []string{"east", "west"},
},
},
{
Service: "cache",
Consumers: &pbconfigentry.Consumers{
Peers: []string{"east"},
},
},
},
idx: 4,
}

run(t, tc)
})

t.Run("wild card includes all services", func(t *testing.T) {
entry := &structs.ExportedServicesConfigEntry{
Name: "default",
Services: []structs.ExportedService{
{
Name: "*",
Consumers: []structs.ServiceConsumer{
{Peer: "west"},
},
},
},
}

err := s.EnsureConfigEntry(c.Next(), entry)
require.NoError(t, err)

tc := testCase{
expect: []*pbconfigentry.ResolvedExportedService{
{
Service: "db",
Consumers: &pbconfigentry.Consumers{
Peers: []string{"west"},
},
},
{
Service: "cache",
Consumers: &pbconfigentry.Consumers{
Peers: []string{"west"},
},
},
{
Service: "frontend",
Consumers: &pbconfigentry.Consumers{
Peers: []string{"west"},
},
},
{
Service: "backend",
Consumers: &pbconfigentry.Consumers{
Peers: []string{"west"},
},
},
},
idx: c.Last(),
}

run(t, tc)
})

t.Run("deleting the config entry clears the services", func(t *testing.T) {
defaultMeta := structs.DefaultEnterpriseMetaInDefaultPartition()
err := s.DeleteConfigEntry(c.Next(), structs.ExportedServices, "default", nil)
require.NoError(t, err)

idx, result, err := s.ResolvedExportedServices(nil, defaultMeta)
require.NoError(t, err)
require.Equal(t, c.Last(), idx)
require.Nil(t, result)
})
}
171 changes: 0 additions & 171 deletions agent/consul/state/config_entry_exported_services_test.go

This file was deleted.

Loading

0 comments on commit 9336394

Please sign in to comment.