Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jm96441n committed May 23, 2024
1 parent 647c1c9 commit 2687db2
Showing 1 changed file with 134 additions and 30 deletions.
164 changes: 134 additions & 30 deletions dependency/consul_exported_services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,148 @@ import (
)

func TestListExportedServicesQuery_Fetch(t *testing.T) {
_, _, err := testClients.Consul().ConfigEntries().Set(&capi.ExportedServicesConfigEntry{
Name: "default",
Partition: "default",
Services: []capi.ExportedService{
{
Name: "service1",
Consumers: []capi.ServiceConsumer{
testCases := map[string]struct {
partition string
exportedServices *capi.ExportedServicesConfigEntry
expected []ExportedService
}{
//"no services": {},
"default partition - one exported service - partitions set": {
partition: "default",
exportedServices: &capi.ExportedServicesConfigEntry{
Name: "default",
Partition: "default",
Services: []capi.ExportedService{
{
Partition: "default.bar",
Name: "service1",
Consumers: []capi.ServiceConsumer{
{
Partition: "foo",
},
},
},
},
},
expected: []ExportedService{
{
Service: "service1",
Consumers: ResolvedConsumers{
Peers: []string{},
Partitions: []string{"foo"},
SamenessGroups: []string{},
},
},
},
},
"default partition - multiple exported services - partitions set": {
partition: "default",
exportedServices: &capi.ExportedServicesConfigEntry{
Name: "default",
Partition: "default",
Services: []capi.ExportedService{
{
Name: "service1",
Consumers: []capi.ServiceConsumer{
{
Partition: "foo",
},
},
},
{
Name: "service2",
Consumers: []capi.ServiceConsumer{
{
Partition: "foo",
},
},
},
},
},
expected: []ExportedService{
{
Service: "service1",
Consumers: ResolvedConsumers{
Peers: []string{},
Partitions: []string{"foo"},
SamenessGroups: []string{},
},
},
{
Service: "service2",
Consumers: ResolvedConsumers{
Peers: []string{},
Partitions: []string{"foo"},
SamenessGroups: []string{},
},
},
},
},
"non default partition - multiple exported services - partitions set": {
partition: "foo",
exportedServices: &capi.ExportedServicesConfigEntry{
Name: "foo",
Partition: "foo",
Services: []capi.ExportedService{
{
Name: "service1",
Consumers: []capi.ServiceConsumer{
{
Partition: "default",
},
},
},
{
Name: "service2",
Consumers: []capi.ServiceConsumer{
{
Partition: "default",
},
},
},
},
},
expected: []ExportedService{
{
Service: "service1",
Consumers: ResolvedConsumers{
Peers: []string{},
Partitions: []string{"default"},
SamenessGroups: []string{},
},
},
{
Service: "service2",
Consumers: ResolvedConsumers{
Peers: []string{},
Partitions: []string{"default"},
SamenessGroups: []string{},
},
},
},
},
}, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

q := &ListExportedServicesQuery{
stopCh: make(chan struct{}),
partition: "default",
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
_, _, err := testClients.Consul().ConfigEntries().Set(tc.exportedServices, &capi.WriteOptions{Partition: tc.partition})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

actual, _, err := q.Fetch(testClients, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
q := &ListExportedServicesQuery{
stopCh: make(chan struct{}),
partition: tc.partition,
}

expected := []ExportedService{
{
Service: "service1",
Consumers: ResolvedConsumers{
Peers: []string{},
Partitions: []string{"default.bar"},
SamenessGroups: []string{},
},
},
}
actual, _, err := q.Fetch(testClients, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

require.ElementsMatch(t, expected, actual)
require.ElementsMatch(t, tc.expected, actual)

// need to clean up because we use a single shared consul instance
_, err = testClients.Consul().ConfigEntries().Delete(capi.ExportedServices, tc.exportedServices.Name, &capi.WriteOptions{Partition: tc.partition})

Check failure on line 153 in dependency/consul_exported_services_test.go

View workflow job for this annotation

GitHub Actions / Run linters

ineffectual assignment to err (ineffassign)
})
}
}

0 comments on commit 2687db2

Please sign in to comment.