From c62fe9b3ffe4c32ef768278328cf7f39cf734e1d Mon Sep 17 00:00:00 2001 From: kumarkavish Date: Sun, 18 Feb 2024 18:49:54 +0530 Subject: [PATCH 01/12] [NET-7377] - CT ENT tests for all consul resource supporting partition and ns. - bug fix for map keys, keys did not contain partition and ns, so could have been overridden if same key in a different ns and partition is added as a dependency. --- dependency/catalog_node.go | 6 + dependency/catalog_node_test.go | 489 +++++++++---- dependency/catalog_nodes.go | 6 + dependency/catalog_nodes_test.go | 361 ++++++---- dependency/catalog_service.go | 6 + dependency/catalog_service_test.go | 501 +++++++------ dependency/catalog_services_test.go | 316 ++++---- dependency/consul_common_test.go | 2 +- dependency/dependency_test.go | 239 +++++-- dependency/health_service.go | 6 + dependency/health_service_test.go | 1032 ++++++++++++++++----------- dependency/kv_get.go | 6 + dependency/kv_get_test.go | 393 +++++----- dependency/kv_keys.go | 6 + dependency/kv_keys_test.go | 437 ++++++------ dependency/kv_list.go | 6 + dependency/kv_list_test.go | 490 +++++++------ test/tenancy_helper.go | 145 ++++ 18 files changed, 2736 insertions(+), 1711 deletions(-) create mode 100644 test/tenancy_helper.go diff --git a/dependency/catalog_node.go b/dependency/catalog_node.go index 50fcd57fd..096bc0943 100644 --- a/dependency/catalog_node.go +++ b/dependency/catalog_node.go @@ -165,6 +165,12 @@ func (d *CatalogNodeQuery) String() string { if d.dc != "" { name = name + "@" + d.dc } + if d.partition != "" { + name = name + "@partition=" + d.partition + } + if d.namespace != "" { + name = name + "@ns=" + d.namespace + } if name == "" { return "catalog.node" diff --git a/dependency/catalog_node_test.go b/dependency/catalog_node_test.go index 0ce4096e1..91362253a 100644 --- a/dependency/catalog_node_test.go +++ b/dependency/catalog_node_test.go @@ -5,88 +5,113 @@ package dependency import ( "fmt" + "github.com/hashicorp/consul/proto-public/pbresource" "testing" "github.com/stretchr/testify/assert" ) func TestNewCatalogNodeQuery(t *testing.T) { - cases := []struct { + type testCase struct { name string i string exp *CatalogNodeQuery err bool - }{ - { - "empty", - "", - &CatalogNodeQuery{}, - false, - }, - { - "invalid query param (unsupported key)", - "node?unsupported=foo", - nil, - true, - }, - { - "bad", - "!4d", - nil, - true, - }, - { - "dc_only", - "@dc1", - nil, - true, - }, - { - "query_only", - "?ns=foo", - nil, - true, - }, - { - "node", - "node", - &CatalogNodeQuery{ - name: "node", - }, - false, - }, - { - "dc", - "node@dc1", - &CatalogNodeQuery{ - name: "node", - dc: "dc1", - }, - false, - }, - { - "every_option", - "node?ns=foo&partition=bar@dc1", - &CatalogNodeQuery{ - name: "node", - dc: "dc1", - namespace: "foo", - partition: "bar", - }, - false, - }, - { - "periods", - "node.bar.com@dc1", - &CatalogNodeQuery{ - name: "node.bar.com", - dc: "dc1", - }, - false, - }, } + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("empty", tenancy), + "", + &CatalogNodeQuery{}, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("invalid query param (unsupported key)", tenancy), + "node?unsupported=foo", + nil, + true, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("bad", tenancy), + "!4d", + nil, + true, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("dc_only", tenancy), + "@dc1", + nil, + true, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("query_only", tenancy), + fmt.Sprintf("?ns=%s", tenancy.Namespace), + nil, + true, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("node", tenancy), + "node", + &CatalogNodeQuery{ + name: "node", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("dc", tenancy), + "node@dc1", + &CatalogNodeQuery{ + name: "node", + dc: "dc1", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("every_option", tenancy), + fmt.Sprintf("node?ns=%s&partition=%s@dc1", tenancy.Namespace, tenancy.Partition), + &CatalogNodeQuery{ + name: "node", + dc: "dc1", + namespace: tenancy.Namespace, + partition: tenancy.Partition, + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("partition", tenancy), + fmt.Sprintf("node?&partition=%s@dc1", tenancy.Partition), + &CatalogNodeQuery{ + name: "node", + dc: "dc1", + partition: tenancy.Partition, + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("namespace", tenancy), + fmt.Sprintf("node?ns=%s@dc1", tenancy.Namespace), + &CatalogNodeQuery{ + name: "node", + dc: "dc1", + namespace: tenancy.Namespace, + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("periods", tenancy), + "node.bar.com@dc1", + &CatalogNodeQuery{ + name: "node.bar.com", + dc: "dc1", + }, + false, + }, + } + }) - for i, tc := range cases { + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { act, err := NewCatalogNodeQuery(tc.i) if (err != nil) != tc.err { @@ -103,67 +128,248 @@ func TestNewCatalogNodeQuery(t *testing.T) { } func TestCatalogNodeQuery_Fetch(t *testing.T) { - cases := []struct { + type testCase struct { name string i string exp *CatalogNode - }{ - { - "local", - "", - &CatalogNode{ - Node: &Node{ - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", - TaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", + } + cases := tenancyHelper.GenerateNonDefaultTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("local", tenancy), + "", + &CatalogNode{ + Node: &Node{ + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", + TaggedAddresses: map[string]string{ + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", + }, + Meta: map[string]string{ + //"consul-network-segment": "", + }, + }, + Services: []*CatalogNodeService{ + { + ID: "conn-enabled-service-default-default", + Service: "conn-enabled-service-default-default", + Tags: ServiceTags([]string{}), + Meta: map[string]string{}, + Port: 12345, + }, + { + ID: "conn-enabled-service-proxy-default-default", + Service: "conn-enabled-service-proxy-default-default", + Tags: ServiceTags([]string{}), + Meta: map[string]string{}, + Port: 21999, + }, + { + ID: "consul", + Service: "consul", + Port: testConsul.Config.Ports.Server, + Tags: ServiceTags([]string{}), + Meta: map[string]string{}, + }, + { + ID: "service-meta-default-default", + Service: "service-meta-default-default", + Tags: ServiceTags([]string{"tag1"}), + Meta: map[string]string{ + "meta1": "value1", + }, + }, + { + ID: "service-taggedAddresses-default-default", + Service: "service-taggedAddresses-default-default", + Tags: ServiceTags([]string{}), + Meta: map[string]string{}, + }, + }, + }, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("partition and ns", tenancy), + fmt.Sprintf("%s?partition=%s&ns=%s", testConsul.Config.NodeName, tenancy.Partition, tenancy.Namespace), + &CatalogNode{ + Node: &Node{ + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", + TaggedAddresses: map[string]string{ + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", + }, + Meta: map[string]string{ + //"consul-network-segment": "", + }, }, - Meta: map[string]string{ - "consul-network-segment": "", + Services: []*CatalogNodeService{ + { + ID: fmt.Sprintf("conn-enabled-service-%s-%s", tenancy.Partition, tenancy.Namespace), + Service: fmt.Sprintf("conn-enabled-service-%s-%s", tenancy.Partition, tenancy.Namespace), + Tags: ServiceTags([]string{}), + Meta: map[string]string{}, + Port: 12345, + }, + { + ID: fmt.Sprintf("conn-enabled-service-proxy-%s-%s", tenancy.Partition, tenancy.Namespace), + Service: fmt.Sprintf("conn-enabled-service-proxy-%s-%s", tenancy.Partition, tenancy.Namespace), + Tags: ServiceTags([]string{}), + Meta: map[string]string{}, + Port: 21999, + }, + { + + ID: fmt.Sprintf("service-meta-%s-%s", tenancy.Partition, tenancy.Namespace), + Service: fmt.Sprintf("service-meta-%s-%s", tenancy.Partition, tenancy.Namespace), + Tags: ServiceTags([]string{"tag1"}), + Meta: map[string]string{ + "meta1": "value1", + }, + }, + { + ID: fmt.Sprintf("service-taggedAddresses-%s-%s", tenancy.Partition, tenancy.Namespace), + Service: fmt.Sprintf("service-taggedAddresses-%s-%s", tenancy.Partition, tenancy.Namespace), + Tags: ServiceTags([]string{}), + Meta: map[string]string{}, + }, }, }, - Services: []*CatalogNodeService{ - { - ID: "consul", - Service: "consul", - Port: testConsul.Config.Ports.Server, - Tags: ServiceTags([]string{}), - Meta: map[string]string{}, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("unknown", tenancy), + "not_a_real_node", + &CatalogNode{}, + }, + } + }) + + cases = append(cases, tenancyHelper.GenerateDefaultTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("local", tenancy), + "", + &CatalogNode{ + Node: &Node{ + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", + TaggedAddresses: map[string]string{ + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", + }, + Meta: map[string]string{ + //"consul-network-segment": "", + }, }, - { - ID: "foo", - Service: "foo-sidecar-proxy", - Tags: ServiceTags([]string{}), - Meta: map[string]string{}, - Port: 21999, + Services: []*CatalogNodeService{ + { + ID: "conn-enabled-service-default-default", + Service: "conn-enabled-service-default-default", + Tags: ServiceTags([]string{}), + Meta: map[string]string{}, + Port: 12345, + }, + { + ID: "conn-enabled-service-proxy-default-default", + Service: "conn-enabled-service-proxy-default-default", + Tags: ServiceTags([]string{}), + Meta: map[string]string{}, + Port: 21999, + }, + { + ID: "consul", + Service: "consul", + Port: testConsul.Config.Ports.Server, + Tags: ServiceTags([]string{}), + Meta: map[string]string{}, + }, + { + ID: "service-meta-default-default", + Service: "service-meta-default-default", + Tags: ServiceTags([]string{"tag1"}), + Meta: map[string]string{ + "meta1": "value1", + }, + }, + { + ID: "service-taggedAddresses-default-default", + Service: "service-taggedAddresses-default-default", + Tags: ServiceTags([]string{}), + Meta: map[string]string{}, + }, }, - { - ID: "service-meta", - Service: "service-meta", - Tags: ServiceTags([]string{"tag1"}), + }, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("partition and ns", tenancy), + fmt.Sprintf("%s?partition=%s&ns=%s", testConsul.Config.NodeName, tenancy.Partition, tenancy.Namespace), + &CatalogNode{ + Node: &Node{ + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", + TaggedAddresses: map[string]string{ + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", + }, Meta: map[string]string{ - "meta1": "value1", + //"consul-network-segment": "", }, }, - { - ID: "service-taggedAddresses", - Service: "service-taggedAddresses", - Tags: ServiceTags([]string{}), - Meta: map[string]string{}, + Services: []*CatalogNodeService{ + { + ID: fmt.Sprintf("conn-enabled-service-%s-%s", tenancy.Partition, tenancy.Namespace), + Service: fmt.Sprintf("conn-enabled-service-%s-%s", tenancy.Partition, tenancy.Namespace), + Tags: ServiceTags([]string{}), + Meta: map[string]string{}, + Port: 12345, + }, + { + ID: fmt.Sprintf("conn-enabled-service-proxy-%s-%s", tenancy.Partition, tenancy.Namespace), + Service: fmt.Sprintf("conn-enabled-service-proxy-%s-%s", tenancy.Partition, tenancy.Namespace), + Tags: ServiceTags([]string{}), + Meta: map[string]string{}, + Port: 21999, + }, + { + ID: "consul", + Service: "consul", + Port: testConsul.Config.Ports.Server, + Tags: ServiceTags([]string{}), + Meta: map[string]string{}, + }, + { + + ID: fmt.Sprintf("service-meta-%s-%s", tenancy.Partition, tenancy.Namespace), + Service: fmt.Sprintf("service-meta-%s-%s", tenancy.Partition, tenancy.Namespace), + Tags: ServiceTags([]string{"tag1"}), + Meta: map[string]string{ + "meta1": "value1", + }, + }, + { + ID: fmt.Sprintf("service-taggedAddresses-%s-%s", tenancy.Partition, tenancy.Namespace), + Service: fmt.Sprintf("service-taggedAddresses-%s-%s", tenancy.Partition, tenancy.Namespace), + Tags: ServiceTags([]string{}), + Meta: map[string]string{}, + }, }, }, }, - }, - { - "unknown", - "not_a_real_node", - &CatalogNode{}, - }, - } + testCase{ + tenancyHelper.AppendTenancyInfo("unknown", tenancy), + "not_a_real_node", + &CatalogNode{}, + }, + } + })...) - for i, tc := range cases { + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { d, err := NewCatalogNodeQuery(tc.i) if err != nil { @@ -194,29 +400,42 @@ func TestCatalogNodeQuery_Fetch(t *testing.T) { } func TestCatalogNodeQuery_String(t *testing.T) { - cases := []struct { + type testCase struct { name string i string exp string - }{ - { - "empty", - "", - "catalog.node", - }, - { - "node", - "node1", - "catalog.node(node1)", - }, - { - "datacenter", - "node1@dc1", - "catalog.node(node1@dc1)", - }, } - - for i, tc := range cases { + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("empty", tenancy), + "", + "catalog.node", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("node", tenancy), + "node1", + "catalog.node(node1)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("datacenter", tenancy), + "node1@dc1", + "catalog.node(node1@dc1)", + }, + testCase{ + name: tenancyHelper.AppendTenancyInfo("partition", tenancy), + i: fmt.Sprintf("node1?&partition=%s&ns=%s@dc1", tenancy.Partition, tenancy.Namespace), + exp: fmt.Sprintf("catalog.node(node1@dc1@partition=%s@ns=%s)", tenancy.Partition, tenancy.Namespace), + }, + testCase{ + name: tenancyHelper.AppendTenancyInfo("namespace", tenancy), + i: fmt.Sprintf("node1?&partition=%s@dc1", tenancy.Partition), + exp: fmt.Sprintf("catalog.node(node1@dc1@partition=%s)", tenancy.Partition), + }, + } + }) + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { d, err := NewCatalogNodeQuery(tc.i) if err != nil { diff --git a/dependency/catalog_nodes.go b/dependency/catalog_nodes.go index ea0f062fb..fe8d59a1c 100644 --- a/dependency/catalog_nodes.go +++ b/dependency/catalog_nodes.go @@ -131,6 +131,12 @@ func (d *CatalogNodesQuery) String() string { if d.dc != "" { name = name + "@" + d.dc } + if d.partition != "" { + name = name + "@partition=" + d.partition + } + if d.namespace != "" { + name = name + "@ns=" + d.namespace + } if d.near != "" { name = name + "~" + d.near } diff --git a/dependency/catalog_nodes_test.go b/dependency/catalog_nodes_test.go index 96b3c18a9..dec5cceb0 100644 --- a/dependency/catalog_nodes_test.go +++ b/dependency/catalog_nodes_test.go @@ -5,119 +5,124 @@ package dependency import ( "fmt" + "github.com/hashicorp/consul/proto-public/pbresource" "testing" "github.com/stretchr/testify/assert" ) func TestNewCatalogNodesQuery(t *testing.T) { - cases := []struct { + type testCase struct { name string i string exp *CatalogNodesQuery err bool - }{ - { - "empty", - "", - &CatalogNodesQuery{}, - false, - }, - { - "invalid query param (unsupported key)", - "key?unsupported=foo", - nil, - true, - }, - { - "node", - "node", - nil, - true, - }, - { - "dc", - "@dc1", - &CatalogNodesQuery{ - dc: "dc1", - }, - false, - }, - { - "namespace", - "?ns=foo", - &CatalogNodesQuery{ - namespace: "foo", - }, - false, - }, - { - "partition", - "?partition=foo", - &CatalogNodesQuery{ - partition: "foo", - }, - false, - }, - { - "namespace_and_partition", - "?ns=foo&partition=bar", - &CatalogNodesQuery{ - namespace: "foo", - partition: "bar", - }, - false, - }, - { - "namespace_and_partition_and_near", - "?ns=foo&partition=bar~node1", - &CatalogNodesQuery{ - namespace: "foo", - partition: "bar", - near: "node1", - }, - false, - }, - { - "near", - "~node1", - &CatalogNodesQuery{ - near: "node1", - }, - false, - }, - { - "dc_near", - "@dc1~node1", - &CatalogNodesQuery{ - dc: "dc1", - near: "node1", - }, - false, - }, - { - "query_near", - "?ns=foo~node1", - &CatalogNodesQuery{ - namespace: "foo", - near: "node1", - }, - false, - }, - { - "every_option", - "?ns=foo&partition=bar@dc1~node1", - &CatalogNodesQuery{ - dc: "dc1", - near: "node1", - partition: "bar", - namespace: "foo", - }, - false, - }, } + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("empty", tenancy), + "", + &CatalogNodesQuery{}, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("invalid query param (unsupported key)", tenancy), + "key?unsupported=foo", + nil, + true, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("node", tenancy), + "node", + nil, + true, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("dc", tenancy), + "@dc1", + &CatalogNodesQuery{ + dc: "dc1", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("namespace", tenancy), + fmt.Sprintf("?ns=%s", tenancy.Namespace), + &CatalogNodesQuery{ + namespace: tenancy.Namespace, + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("partition", tenancy), + fmt.Sprintf("?partition=%s", tenancy.Partition), + &CatalogNodesQuery{ + partition: tenancy.Partition, + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("namespace_and_partition", tenancy), + fmt.Sprintf("?ns=%s&partition=%s", tenancy.Namespace, tenancy.Partition), + &CatalogNodesQuery{ + namespace: tenancy.Namespace, + partition: tenancy.Partition, + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("namespace_and_partition_and_near", tenancy), + fmt.Sprintf("?ns=%s&partition=%s~node1", tenancy.Namespace, tenancy.Partition), + &CatalogNodesQuery{ + namespace: tenancy.Namespace, + partition: tenancy.Partition, + near: "node1", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("near", tenancy), + "~node1", + &CatalogNodesQuery{ + near: "node1", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("dc_near", tenancy), + "@dc1~node1", + &CatalogNodesQuery{ + dc: "dc1", + near: "node1", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("query_near", tenancy), + fmt.Sprintf("?ns=%s~node1", tenancy.Namespace), + &CatalogNodesQuery{ + namespace: tenancy.Namespace, + near: "node1", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("every_option", tenancy), + fmt.Sprintf("?ns=%s&partition=%s@dc1~node1", tenancy.Namespace, tenancy.Partition), + &CatalogNodesQuery{ + dc: "dc1", + near: "node1", + partition: tenancy.Partition, + namespace: tenancy.Namespace, + }, + false, + }, + } + }) - for i, tc := range cases { + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { act, err := NewCatalogNodesQuery(tc.i) if (err != nil) != tc.err { @@ -134,32 +139,90 @@ func TestNewCatalogNodesQuery(t *testing.T) { } func TestCatalogNodesQuery_Fetch(t *testing.T) { - cases := []struct { + type testCase struct { name string i string exp []*Node - }{ - { - "all", - "", - []*Node{ - { - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", - TaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", + } + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("all", tenancy), + "", + []*Node{ + { + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", + TaggedAddresses: map[string]string{ + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", + }, + Meta: map[string]string{ + //"consul-network-segment": "", + }, }, - Meta: map[string]string{ - "consul-network-segment": "", + }, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("partition and namespace", tenancy), + fmt.Sprintf("?partition=%s&ns=%s@dc1", tenancy.Partition, tenancy.Namespace), + []*Node{ + { + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", + TaggedAddresses: map[string]string{ + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", + }, + Meta: map[string]string{ + //"consul-network-segment": "", + }, }, }, }, - }, - } + testCase{ + tenancyHelper.AppendTenancyInfo("namespace", tenancy), + fmt.Sprintf("?ns=%s@dc1", tenancy.Namespace), + []*Node{ + { + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", + TaggedAddresses: map[string]string{ + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", + }, + Meta: map[string]string{ + //"consul-network-segment": "", + }, + }, + }, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("partition", tenancy), + fmt.Sprintf("?partition=%s@dc1", tenancy.Partition), + []*Node{ + { + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", + TaggedAddresses: map[string]string{ + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", + }, + Meta: map[string]string{ + //"consul-network-segment": "", + }, + }, + }, + }, + } + }) - for i, tc := range cases { + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { d, err := NewCatalogNodesQuery(tc.i) if err != nil { @@ -185,34 +248,48 @@ func TestCatalogNodesQuery_Fetch(t *testing.T) { } func TestCatalogNodesQuery_String(t *testing.T) { - cases := []struct { + type testCase struct { name string i string exp string - }{ - { - "empty", - "", - "catalog.nodes", - }, - { - "datacenter", - "@dc1", - "catalog.nodes(@dc1)", - }, - { - "near", - "~node1", - "catalog.nodes(~node1)", - }, - { - "datacenter_near", - "@dc1~node1", - "catalog.nodes(@dc1~node1)", - }, } + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("empty", tenancy), + "", + "catalog.nodes", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("datacenter", tenancy), + "@dc1", + "catalog.nodes(@dc1)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("near", tenancy), + "~node1", + "catalog.nodes(~node1)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("datacenter_near", tenancy), + "@dc1~node1", + "catalog.nodes(@dc1~node1)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("partition", tenancy), + fmt.Sprintf("?partition=%s@dc1~node1", tenancy.Partition), + fmt.Sprintf("catalog.nodes(@dc1@partition=%s~node1)", tenancy.Partition), + }, + testCase{ + tenancyHelper.AppendTenancyInfo("namespace and partition", tenancy), + fmt.Sprintf("?partition=%s&ns=%s@dc1~node1", tenancy.Partition, tenancy.Namespace), + fmt.Sprintf("catalog.nodes(@dc1@partition=%s@ns=%s~node1)", tenancy.Partition, tenancy.Namespace), + }, + } + }) - for i, tc := range cases { + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { d, err := NewCatalogNodesQuery(tc.i) if err != nil { diff --git a/dependency/catalog_service.go b/dependency/catalog_service.go index 7831684b1..a86c8578f 100644 --- a/dependency/catalog_service.go +++ b/dependency/catalog_service.go @@ -151,6 +151,12 @@ func (d *CatalogServiceQuery) String() string { if d.dc != "" { name = name + "@" + d.dc } + if d.partition != "" { + name = name + "@partition=" + d.partition + } + if d.namespace != "" { + name = name + "@ns=" + d.namespace + } if d.near != "" { name = name + "~" + d.near } diff --git a/dependency/catalog_service_test.go b/dependency/catalog_service_test.go index 262eb27bf..56692323b 100644 --- a/dependency/catalog_service_test.go +++ b/dependency/catalog_service_test.go @@ -5,163 +5,169 @@ package dependency import ( "fmt" + "github.com/hashicorp/consul/proto-public/pbresource" "testing" "github.com/stretchr/testify/assert" ) func TestNewCatalogServiceQuery(t *testing.T) { - cases := []struct { + type testCase struct { name string i string exp *CatalogServiceQuery err bool - }{ - { - "empty", - "", - nil, - true, - }, - { - "dc_only", - "@dc1", - nil, - true, - }, - { - "query_only", - "?ns=foo", - nil, - true, - }, - { - "invalid query param (unsupported key)", - "name?unsupported=foo", - nil, - true, - }, - { - "near_only", - "~near", - nil, - true, - }, - { - "tag_only", - "tag.", - nil, - true, - }, - { - "name", - "name", - &CatalogServiceQuery{ - name: "name", + } + + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("empty", tenancy), + "", + nil, + true, }, - false, - }, - { - "name_dc", - "name@dc1", - &CatalogServiceQuery{ - dc: "dc1", - name: "name", + testCase{ + tenancyHelper.AppendTenancyInfo("dc_only", tenancy), + "@dc1", + nil, + true, }, - false, - }, - { - "name_query", - "name?ns=foo", - &CatalogServiceQuery{ - name: "name", - namespace: "foo", + testCase{ + tenancyHelper.AppendTenancyInfo("query_only", tenancy), + fmt.Sprintf("?ns=%s", tenancy.Namespace), + nil, + true, }, - false, - }, - { - "name_dc_near", - "name@dc1~near", - &CatalogServiceQuery{ - dc: "dc1", - name: "name", - near: "near", + testCase{ + tenancyHelper.AppendTenancyInfo("invalid query param (unsupported key)", tenancy), + "name?unsupported=foo", + nil, + true, }, - false, - }, - { - "name_query_near", - "name?ns=foo~near", - &CatalogServiceQuery{ - name: "name", - near: "near", - namespace: "foo", + testCase{ + tenancyHelper.AppendTenancyInfo("near_only", tenancy), + "~near", + nil, + true, }, - false, - }, - { - "name_near", - "name~near", - &CatalogServiceQuery{ - name: "name", - near: "near", + testCase{ + tenancyHelper.AppendTenancyInfo("tag_only", tenancy), + "tag.", + nil, + true, }, - false, - }, - { - "tag_name", - "tag.name", - &CatalogServiceQuery{ - name: "name", - tag: "tag", + testCase{ + tenancyHelper.AppendTenancyInfo("name", tenancy), + "name", + &CatalogServiceQuery{ + name: "name", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("name_dc", tenancy), + "name@dc1", + &CatalogServiceQuery{ + dc: "dc1", + name: "name", + }, + false, }, - false, - }, - { - "tag_name_dc", - "tag.name@dc", - &CatalogServiceQuery{ - dc: "dc", - name: "name", - tag: "tag", + testCase{ + tenancyHelper.AppendTenancyInfo("name_query", tenancy), + fmt.Sprintf("name?ns=%s", tenancy.Namespace), + &CatalogServiceQuery{ + name: "name", + namespace: tenancy.Namespace, + }, + false, }, - false, - }, - { - "tag_name_near", - "tag.name~near", - &CatalogServiceQuery{ - name: "name", - near: "near", - tag: "tag", + testCase{ + tenancyHelper.AppendTenancyInfo("name_dc_near", tenancy), + "name@dc1~near", + &CatalogServiceQuery{ + dc: "dc1", + name: "name", + near: "near", + }, + false, }, - false, - }, - { - "every_option", - "tag.name?ns=foo&partition=bar@dc~near", - &CatalogServiceQuery{ - dc: "dc", - name: "name", - near: "near", - tag: "tag", - namespace: "foo", - partition: "bar", + testCase{ + tenancyHelper.AppendTenancyInfo("name_query_near", tenancy), + fmt.Sprintf("name?ns=%s~near", tenancy.Namespace), + &CatalogServiceQuery{ + name: "name", + near: "near", + namespace: tenancy.Namespace, + }, + false, }, - false, - }, - { - "tag_name_with_colon", - "tag:value.name", - &CatalogServiceQuery{ - name: "name", - tag: "tag:value", + testCase{ + tenancyHelper.AppendTenancyInfo("name_near", tenancy), + "name~near", + &CatalogServiceQuery{ + name: "name", + near: "near", + }, + false, }, - false, - }, - } + testCase{ + tenancyHelper.AppendTenancyInfo("tag_name", tenancy), + "tag.name", + &CatalogServiceQuery{ + name: "name", + tag: "tag", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("tag_name_dc", tenancy), + "tag.name@dc", + &CatalogServiceQuery{ + dc: "dc", + name: "name", + tag: "tag", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("tag_name_near", tenancy), + "tag.name~near", + &CatalogServiceQuery{ + name: "name", + near: "near", + tag: "tag", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("every_option", tenancy), + fmt.Sprintf("tag.name?ns=%s&partition=%s@dc~near", tenancy.Namespace, tenancy.Partition), + &CatalogServiceQuery{ + dc: "dc", + name: "name", + near: "near", + tag: "tag", + namespace: tenancy.Namespace, + partition: tenancy.Partition, + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("tag_name_with_colon", tenancy), + "tag:value.name", + &CatalogServiceQuery{ + name: "name", + tag: "tag:value", + }, + false, + }, + } + }) - for i, tc := range cases { + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { act, err := NewCatalogServiceQuery(tc.i) if (err != nil) != tc.err { @@ -178,61 +184,98 @@ func TestNewCatalogServiceQuery(t *testing.T) { } func TestCatalogServiceQuery_Fetch(t *testing.T) { - cases := []struct { + type testCase struct { name string i string exp []*CatalogService - }{ - { - "consul", - "consul", - []*CatalogService{ - { - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", - TaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", - }, - NodeMeta: map[string]string{ - "consul-network-segment": "", + } + cases := tenancyHelper.GenerateDefaultTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("consul", tenancy), + "consul", + []*CatalogService{ + { + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", + TaggedAddresses: map[string]string{ + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", + }, + NodeMeta: map[string]string{ + //"consul-network-segment": "", + }, + ServiceID: "consul", + ServiceName: "consul", + ServiceAddress: "", + ServiceTags: ServiceTags([]string{}), + ServiceMeta: map[string]string{}, + ServicePort: testConsul.Config.Ports.Server, }, - ServiceID: "consul", - ServiceName: "consul", - ServiceAddress: "", - ServiceTags: ServiceTags([]string{}), - ServiceMeta: map[string]string{}, - ServicePort: testConsul.Config.Ports.Server, }, }, - }, - { - "service-meta", - "service-meta", - []*CatalogService{ - { - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", - TaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", + testCase{ + tenancyHelper.AppendTenancyInfo("service-meta", tenancy), + "service-meta-default-default", + []*CatalogService{ + { + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", + TaggedAddresses: map[string]string{ + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", + }, + NodeMeta: map[string]string{ + //"consul-network-segment": "", + }, + ServiceID: "service-meta-default-default", + ServiceName: "service-meta-default-default", + ServiceAddress: "", + ServiceTags: ServiceTags([]string{"tag1"}), + ServiceMeta: map[string]string{"meta1": "value1"}, }, - NodeMeta: map[string]string{ - "consul-network-segment": "", + }, + }, + } + }) + + cases = append(cases, tenancyHelper.GenerateNonDefaultTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("consul", tenancy), + fmt.Sprintf("consul?ns=%s&partition=%s", tenancy.Namespace, tenancy.Partition), + nil, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("service-meta", tenancy), + fmt.Sprintf("service-meta-%s-%s?ns=%s&partition=%s", tenancy.Partition, tenancy.Namespace, tenancy.Namespace, tenancy.Partition), + []*CatalogService{ + { + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", + TaggedAddresses: map[string]string{ + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", + }, + NodeMeta: map[string]string{ + //"consul-network-segment": "", + }, + ServiceID: fmt.Sprintf("service-meta-%s-%s", tenancy.Partition, tenancy.Namespace), + ServiceName: fmt.Sprintf("service-meta-%s-%s", tenancy.Partition, tenancy.Namespace), + ServiceAddress: "", + ServiceTags: ServiceTags([]string{"tag1"}), + ServiceMeta: map[string]string{"meta1": "value1"}, }, - ServiceID: "service-meta", - ServiceName: "service-meta", - ServiceAddress: "", - ServiceTags: ServiceTags([]string{"tag1"}), - ServiceMeta: map[string]string{"meta1": "value1"}, }, }, - }, - } + } + })...) - for i, tc := range cases { + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { d, err := NewCatalogServiceQuery(tc.i) if err != nil { @@ -266,54 +309,68 @@ func TestCatalogServiceQuery_Fetch(t *testing.T) { } func TestCatalogServiceQuery_String(t *testing.T) { - cases := []struct { + type testCase struct { name string i string exp string - }{ - { - "name", - "name", - "catalog.service(name)", - }, - { - "name_dc", - "name@dc", - "catalog.service(name@dc)", - }, - { - "name_near", - "name~near", - "catalog.service(name~near)", - }, - { - "name_dc_near", - "name@dc~near", - "catalog.service(name@dc~near)", - }, - { - "tag_name", - "tag.name", - "catalog.service(tag.name)", - }, - { - "tag_name_dc", - "tag.name@dc", - "catalog.service(tag.name@dc)", - }, - { - "tag_name_near", - "tag.name~near", - "catalog.service(tag.name~near)", - }, - { - "tag_name_dc_near", - "tag.name@dc~near", - "catalog.service(tag.name@dc~near)", - }, } + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("name", tenancy), + "name", + "catalog.service(name)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("name_dc", tenancy), + "name@dc", + "catalog.service(name@dc)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("name_near", tenancy), + "name~near", + "catalog.service(name~near)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("name_dc_near", tenancy), + "name@dc~near", + "catalog.service(name@dc~near)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("tag_name", tenancy), + "tag.name", + "catalog.service(tag.name)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("tag_name_dc", tenancy), + "tag.name@dc", + "catalog.service(tag.name@dc)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("tag_name_near", tenancy), + "tag.name~near", + "catalog.service(tag.name~near)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("tag_name_dc_near", tenancy), + "tag.name@dc~near", + "catalog.service(tag.name@dc~near)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("tag_name_dc_near_ns", tenancy), + fmt.Sprintf("tag.name?ns=%s@dc~near", tenancy.Namespace), + fmt.Sprintf("catalog.service(tag.name@dc@ns=%s~near)", tenancy.Namespace), + }, + testCase{ + tenancyHelper.AppendTenancyInfo("tag_name_dc_near_ns_partiton", tenancy), + fmt.Sprintf("tag.name?ns=%s&partition=%s@dc~near", tenancy.Namespace, tenancy.Partition), + fmt.Sprintf("catalog.service(tag.name@dc@partition=%s@ns=%s~near)", tenancy.Partition, tenancy.Namespace), + }, + } + }) - for i, tc := range cases { + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { d, err := NewCatalogServiceQuery(tc.i) if err != nil { diff --git a/dependency/catalog_services_test.go b/dependency/catalog_services_test.go index 12504b140..d7298d80e 100644 --- a/dependency/catalog_services_test.go +++ b/dependency/catalog_services_test.go @@ -5,82 +5,87 @@ package dependency import ( "fmt" + "github.com/hashicorp/consul/proto-public/pbresource" "testing" "github.com/stretchr/testify/assert" ) func TestNewCatalogServicesQuery(t *testing.T) { - cases := []struct { + type testCase struct { name string i string exp *CatalogServicesQuery err bool - }{ - { - "empty", - "", - &CatalogServicesQuery{}, - false, - }, - { - "invalid query param (unsupported key)", - "?unsupported=foo", - nil, - true, - }, - { - "node", - "node", - nil, - true, - }, - { - "dc", - "@dc1", - &CatalogServicesQuery{ - dc: "dc1", - }, - false, - }, - { - "namespace", - "?ns=foo", - &CatalogServicesQuery{ - namespace: "foo", - }, - false, - }, - { - "partition", - "?partition=foo", - &CatalogServicesQuery{ - partition: "foo", - }, - false, - }, - { - "partition_and_namespace", - "?ns=foo&partition=bar", - &CatalogServicesQuery{ - namespace: "foo", - partition: "bar", - }, - false, - }, - { - "partition_and_namespace_and_dc", - "?ns=foo&partition=bar@dc1", - &CatalogServicesQuery{ - namespace: "foo", - partition: "bar", - dc: "dc1", - }, - false, - }, } + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("empty", tenancy), + "", + &CatalogServicesQuery{}, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("invalid query param (unsupported key)", tenancy), + "?unsupported=foo", + nil, + true, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("node", tenancy), + "node", + nil, + true, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("dc", tenancy), + "@dc1", + &CatalogServicesQuery{ + dc: "dc1", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("namespace", tenancy), + fmt.Sprintf("?ns=%s", tenancy.Namespace), + &CatalogServicesQuery{ + namespace: tenancy.Namespace, + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("partition", tenancy), + fmt.Sprintf("?partition=%s", tenancy.Partition), + &CatalogServicesQuery{ + partition: tenancy.Partition, + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("partition_and_namespace", tenancy), + fmt.Sprintf("?ns=%s&partition=%s", tenancy.Namespace, tenancy.Partition), + &CatalogServicesQuery{ + namespace: tenancy.Namespace, + partition: tenancy.Partition, + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("partition_and_namespace_and_dc", tenancy), + fmt.Sprintf("?ns=%s&partition=%s@dc1", tenancy.Namespace, tenancy.Partition), + &CatalogServicesQuery{ + namespace: tenancy.Namespace, + partition: tenancy.Partition, + dc: "dc1", + }, + false, + }, + } + }) - for i, tc := range cases { + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { act, err := NewCatalogServicesQuery(tc.i) if (err != nil) != tc.err { @@ -97,40 +102,105 @@ func TestNewCatalogServicesQuery(t *testing.T) { } func TestCatalogServicesQuery_Fetch(t *testing.T) { - cases := []struct { + type testCase struct { name string i string opts *QueryOptions exp []*CatalogSnippet err bool - }{ - { - "all", - "", - nil, - []*CatalogSnippet{ - { - Name: "consul", - Tags: ServiceTags([]string{}), - }, - { - Name: "foo-sidecar-proxy", - Tags: ServiceTags([]string{}), + } + cases := tenancyHelper.GenerateDefaultTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("all", tenancy), + "", + nil, + []*CatalogSnippet{ + { + Name: "conn-enabled-service-default-default", + Tags: ServiceTags([]string{}), + }, + { + Name: "conn-enabled-service-proxy-default-default", + Tags: ServiceTags([]string{}), + }, + { + Name: "consul", + Tags: ServiceTags([]string{}), + }, + { + Name: "service-meta-default-default", + Tags: ServiceTags([]string{"tag1"}), + }, + { + Name: "service-taggedAddresses-default-default", + Tags: ServiceTags([]string{}), + }, }, - { - Name: "service-meta", - Tags: ServiceTags([]string{"tag1"}), + false, + }, + } + }) + + cases = append(cases, tenancyHelper.GenerateNonDefaultTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("all", tenancy), + "", + nil, + []*CatalogSnippet{ + { + Name: "conn-enabled-service-default-default", + Tags: ServiceTags([]string{}), + }, + { + Name: "conn-enabled-service-proxy-default-default", + Tags: ServiceTags([]string{}), + }, + { + Name: "consul", + Tags: ServiceTags([]string{}), + }, + { + Name: "service-meta-default-default", + Tags: ServiceTags([]string{"tag1"}), + }, + { + Name: "service-taggedAddresses-default-default", + Tags: ServiceTags([]string{}), + }, }, - { - Name: "service-taggedAddresses", - Tags: ServiceTags([]string{}), + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("partition_and_ns", tenancy), + fmt.Sprintf("?ns=%s&partition=%s", tenancy.Namespace, tenancy.Partition), + nil, + []*CatalogSnippet{ + { + Name: fmt.Sprintf("conn-enabled-service-%s-%s", tenancy.Partition, tenancy.Namespace), + Tags: ServiceTags([]string{}), + }, + { + Name: fmt.Sprintf("conn-enabled-service-proxy-%s-%s", tenancy.Partition, tenancy.Namespace), + Tags: ServiceTags([]string{}), + }, + { + Name: fmt.Sprintf("service-meta-%s-%s", tenancy.Partition, tenancy.Namespace), + Tags: ServiceTags([]string{"tag1"}), + }, + { + Name: fmt.Sprintf("service-taggedAddresses-%s-%s", tenancy.Partition, tenancy.Namespace), + Tags: ServiceTags([]string{}), + }, }, + false, }, - false, - }, - } + } + })...) - for i, tc := range cases { + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { d, err := NewCatalogServicesQuery(tc.i) @@ -153,44 +223,48 @@ func TestCatalogServicesQuery_Fetch(t *testing.T) { } func TestCatalogServicesQuery_String(t *testing.T) { - cases := []struct { + type testCase struct { name string i string exp string - }{ - { - "empty", - "", - "catalog.services", - }, - { - "datacenter", - "@dc1", - "catalog.services(@dc1)", - }, - { - "datacenter+namespace", - "?ns=bar@dc1", - "catalog.services(@dc1@ns=bar)", - }, - { - "datacenter+namespace+partition", - "?partition=foo&ns=bar@dc1", - "catalog.services(@dc1@partition=foo@ns=bar)", - }, - { - "namespace+partition", - "?partition=foo&ns=bar", - "catalog.services(@partition=foo@ns=bar)", - }, - { - "dc+partition", - "?partition=foo@dc1", - "catalog.services(@dc1@partition=foo)", - }, } + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("empty", tenancy), + "", + "catalog.services", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("datacenter", tenancy), + "@dc1", + "catalog.services(@dc1)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("datacenter+namespace", tenancy), + fmt.Sprintf("?ns=%s@dc1", tenancy.Namespace), + fmt.Sprintf("catalog.services(@dc1@ns=%s)", tenancy.Namespace), + }, + testCase{ + tenancyHelper.AppendTenancyInfo("datacenter+namespace+partition", tenancy), + fmt.Sprintf("?partition=%s&ns=%s@dc1", tenancy.Partition, tenancy.Namespace), + fmt.Sprintf("catalog.services(@dc1@partition=%s@ns=%s)", tenancy.Partition, tenancy.Namespace), + }, + testCase{ + tenancyHelper.AppendTenancyInfo("namespace+partition", tenancy), + fmt.Sprintf("?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace), + fmt.Sprintf("catalog.services(@partition=%s@ns=%s)", tenancy.Partition, tenancy.Namespace), + }, + testCase{ + tenancyHelper.AppendTenancyInfo("dc+partition", tenancy), + fmt.Sprintf("?partition=%s@dc1", tenancy.Partition), + fmt.Sprintf("catalog.services(@dc1@partition=%s)", tenancy.Partition), + }, + } + }) - for i, tc := range cases { + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { d, err := NewCatalogServicesQuery(tc.i) if err != nil { diff --git a/dependency/consul_common_test.go b/dependency/consul_common_test.go index 06527d3df..5f156c36c 100644 --- a/dependency/consul_common_test.go +++ b/dependency/consul_common_test.go @@ -21,7 +21,7 @@ func filterVersionMeta(meta map[string]string) map[string]string { filteredMeta := []string{ "raft_version", "serf_protocol_current", "serf_protocol_min", "serf_protocol_max", "version", - "non_voter", "read_replica", "grpc_port", "grpc_tls_port", "consul-version", + "non_voter", "read_replica", "grpc_port", "grpc_tls_port", "consul-version", "consul-network-segment", } return filter(meta, filteredMeta) } diff --git a/dependency/dependency_test.go b/dependency/dependency_test.go index 0948da841..6f24eefb1 100644 --- a/dependency/dependency_test.go +++ b/dependency/dependency_test.go @@ -7,6 +7,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/hashicorp/consul/proto-public/pbresource" "io" "log" "os" @@ -29,10 +30,11 @@ const ( ) var ( - testConsul *testutil.TestServer - testVault *vaultServer - testNomad *nomadServer - testClients *ClientSet + testConsul *testutil.TestServer + testVault *vaultServer + testNomad *nomadServer + testClients *ClientSet + tenancyHelper *test.TenancyHelper ) func TestMain(m *testing.M) { @@ -46,8 +48,20 @@ func TestMain(m *testing.M) { Address: testConsul.HTTPAddr, }); err != nil { testConsul.Stop() + testVault.Stop() + testNomad.Stop() Fatalf("failed to create consul client: %v\n", err) } + + if t, err := test.NewTenancyHelper(clients.Consul()); err != nil { + testConsul.Stop() + testVault.Stop() + testNomad.Stop() + Fatalf("failed to create tenancy helper: %v\n", err) + } else { + tenancyHelper = t + } + if err := clients.CreateVaultClient(&CreateVaultClientInput{ Address: vaultAddr, Token: vaultToken, @@ -64,70 +78,30 @@ func TestMain(m *testing.M) { testNomad.Stop() Fatalf("failed to create nomad client: %v\n", err) } - testClients = clients - setupVaultPKI(clients) + testClients = clients - consul_agent := testClients.consul.client.Agent() - // service with meta data - serviceMetaService := &api.AgentServiceRegistration{ - ID: "service-meta", - Name: "service-meta", - Tags: []string{"tag1"}, - Meta: map[string]string{ - "meta1": "value1", - }, - } - if err := consul_agent.ServiceRegister(serviceMetaService); err != nil { - Fatalf("%v", err) - } - // service with serviceTaggedAddresses - serviceTaggedAddressesService := &api.AgentServiceRegistration{ - ID: "service-taggedAddresses", - Name: "service-taggedAddresses", - TaggedAddresses: map[string]api.ServiceAddress{ - "lan": { - Address: "192.0.2.1", - Port: 80, - }, - "wan": { - Address: "192.0.2.2", - Port: 443, - }, - }, - } - if err := consul_agent.ServiceRegister(serviceTaggedAddressesService); err != nil { - Fatalf("%v", err) - } - // connect enabled service - testService := &api.AgentServiceRegistration{ - Name: "foo", - ID: "foo", - Port: 12345, - Connect: &api.AgentServiceConnect{}, - } - // this is based on what `consul connect proxy` command does at - // consul/command/connect/proxy/register.go (register method) - testConnect := &api.AgentServiceRegistration{ - Kind: api.ServiceKindConnectProxy, - Name: "foo-sidecar-proxy", - ID: "foo", - Port: 21999, - Proxy: &api.AgentServiceConnectProxyConfig{ - DestinationServiceName: "foo", - }, + if err := testClients.createConsulPartitions(); err != nil { + testConsul.Stop() + testVault.Stop() + testNomad.Stop() + Fatalf("failed to create consul partitions: %v\n", err) } - if err := consul_agent.ServiceRegister(testService); err != nil { - Fatalf("%v", err) - } - if err := consul_agent.ServiceRegister(testConnect); err != nil { - Fatalf("%v", err) + if err := testClients.createConsulNs(); err != nil { + testConsul.Stop() + testVault.Stop() + testNomad.Stop() + Fatalf("failed to create consul namespaces: %v\n", err) } - err := createConsulPeerings(clients) - if err != nil { - Fatalf("%v", err) + setupVaultPKI(clients) + + if err := testClients.createConsulTestResources(); err != nil { + testConsul.Stop() + testVault.Stop() + testNomad.Stop() + Fatalf("failed to create consul test resources: %v\n", err) } // Wait for Nomad initialization to finish @@ -164,15 +138,118 @@ func TestMain(m *testing.M) { os.Exit(exit) } -func createConsulPeerings(clients *ClientSet) error { - generateReq := api.PeeringGenerateTokenRequest{PeerName: "foo"} - _, _, err := clients.consul.client.Peerings().GenerateToken(context.Background(), generateReq, &api.WriteOptions{}) +func (c *ClientSet) createConsulTestResources() error { + catalog := testClients.Consul().Catalog() + + node, err := testClients.Consul().Agent().NodeName() if err != nil { return err } - generateReq = api.PeeringGenerateTokenRequest{PeerName: "bar"} - _, _, err = clients.consul.client.Peerings().GenerateToken(context.Background(), generateReq, &api.WriteOptions{}) + for _, tenancy := range tenancyHelper.TestTenancies() { + // service with meta data + serviceMetaService := &api.AgentService{ + ID: fmt.Sprintf("service-meta-%s-%s", tenancy.Partition, tenancy.Namespace), + Service: fmt.Sprintf("service-meta-%s-%s", tenancy.Partition, tenancy.Namespace), + Tags: []string{"tag1"}, + Meta: map[string]string{ + "meta1": "value1", + }, + Namespace: tenancy.Namespace, + Partition: tenancy.Partition, + } + if _, err := catalog.Register(&api.CatalogRegistration{ + Service: serviceMetaService, + Partition: tenancy.Partition, + Node: node, + Address: "127.0.0.1", + }, nil); err != nil { + return err + } + // service with serviceTaggedAddresses + serviceTaggedAddressesService := &api.AgentService{ + ID: fmt.Sprintf("service-taggedAddresses-%s-%s", tenancy.Partition, tenancy.Namespace), + Service: fmt.Sprintf("service-taggedAddresses-%s-%s", tenancy.Partition, tenancy.Namespace), + TaggedAddresses: map[string]api.ServiceAddress{ + "lan": { + Address: "192.0.2.1", + Port: 80, + }, + "wan": { + Address: "192.0.2.2", + Port: 443, + }, + }, + Namespace: tenancy.Namespace, + Partition: tenancy.Partition, + } + if _, err := catalog.Register(&api.CatalogRegistration{ + Service: serviceTaggedAddressesService, + Partition: tenancy.Partition, + Node: node, + Address: "127.0.0.1", + }, nil); err != nil { + return err + } + + // connect enabled service + testService := &api.AgentService{ + ID: fmt.Sprintf("conn-enabled-service-%s-%s", tenancy.Partition, tenancy.Namespace), + Service: fmt.Sprintf("conn-enabled-service-%s-%s", tenancy.Partition, tenancy.Namespace), + Port: 12345, + Connect: &api.AgentServiceConnect{}, + Namespace: tenancy.Namespace, + Partition: tenancy.Partition, + } + // this is based on what `consul connect proxy` command does at + // consul/command/connect/proxy/register.go (register method) + testConnect := &api.AgentService{ + Kind: api.ServiceKindConnectProxy, + ID: fmt.Sprintf("conn-enabled-service-proxy-%s-%s", tenancy.Partition, tenancy.Namespace), + Service: fmt.Sprintf("conn-enabled-service-proxy-%s-%s", tenancy.Partition, tenancy.Namespace), + Port: 21999, + Proxy: &api.AgentServiceConnectProxyConfig{ + DestinationServiceName: fmt.Sprintf("conn-enabled-service-%s-%s", tenancy.Partition, tenancy.Namespace), + }, + Namespace: tenancy.Namespace, + Partition: tenancy.Partition, + } + + if _, err := catalog.Register(&api.CatalogRegistration{ + Service: testService, + Partition: tenancy.Partition, + Node: node, + Address: "127.0.0.1", + }, nil); err != nil { + return err + } + + if _, err := catalog.Register(&api.CatalogRegistration{ + Service: testConnect, + Partition: tenancy.Partition, + Node: node, + Address: "127.0.0.1", + }, nil); err != nil { + return err + } + + if err := testClients.createConsulPeerings(tenancy); err != nil { + return err + } + } + + return nil +} + +func (c *ClientSet) createConsulPeerings(tenancy *pbresource.Tenancy) error { + generateReq := api.PeeringGenerateTokenRequest{PeerName: "foo", Partition: tenancy.Partition} + _, _, err := c.consul.client.Peerings().GenerateToken(context.Background(), generateReq, &api.WriteOptions{}) + if err != nil { + return err + } + + generateReq = api.PeeringGenerateTokenRequest{PeerName: "bar", Partition: tenancy.Partition} + _, _, err = c.consul.client.Peerings().GenerateToken(context.Background(), generateReq, &api.WriteOptions{}) if err != nil { return err } @@ -466,3 +543,31 @@ func (v *nomadServer) DeleteVariable(path string, opts *nomadapi.WriteOptions) e } return err } + +func (c *ClientSet) createConsulPartitions() error { + for p := range tenancyHelper.GetUniquePartitions() { + if p.Name != "" && p.Name != "default" { + partition := &api.Partition{Name: p.Name} + _, _, err := c.Consul().Partitions().Create(context.Background(), partition, nil) + if err != nil { + return err + } + } + } + + return nil +} + +func (c *ClientSet) createConsulNs() error { + for _, tenancy := range tenancyHelper.TestTenancies() { + if tenancy.Namespace != "" && tenancy.Namespace != "default" { + ns := &api.Namespace{Name: tenancy.Namespace, Partition: tenancy.Partition} + _, _, err := c.Consul().Namespaces().Create(ns, nil) + if err != nil { + return err + } + } + } + + return nil +} diff --git a/dependency/health_service.go b/dependency/health_service.go index 2afe1b7e1..c61b23c28 100644 --- a/dependency/health_service.go +++ b/dependency/health_service.go @@ -252,6 +252,12 @@ func (d *HealthServiceQuery) String() string { if d.dc != "" { name = name + "@" + d.dc } + if d.partition != "" { + name = name + "@partition=" + d.partition + } + if d.namespace != "" { + name = name + "@ns=" + d.namespace + } if d.near != "" { name = name + "~" + d.near } diff --git a/dependency/health_service_test.go b/dependency/health_service_test.go index 5dffe2aad..00e655dfb 100644 --- a/dependency/health_service_test.go +++ b/dependency/health_service_test.go @@ -5,6 +5,7 @@ package dependency import ( "fmt" + "github.com/hashicorp/consul/proto-public/pbresource" "testing" "github.com/hashicorp/consul/api" @@ -12,213 +13,218 @@ import ( ) func TestNewHealthServiceQuery(t *testing.T) { - cases := []struct { + type testCase struct { name string i string exp *HealthServiceQuery err bool - }{ - { - "empty", - "", - nil, - true, - }, - { - "dc_only", - "@dc1", - nil, - true, - }, - { - "near_only", - "~near", - nil, - true, - }, - { - "tag_only", - "tag.", - nil, - true, - }, - { - "query_only", - "?ns=foo", - nil, - true, - }, - { - name: "invalid query param (unsupported key)", - i: "name?unsupported=test", - err: true, - }, - { - "name", - "name", - &HealthServiceQuery{ - filters: []string{"passing"}, - name: "name", - }, - false, - }, - { - "name_dc", - "name@dc1", - &HealthServiceQuery{ - dc: "dc1", - filters: []string{"passing"}, - name: "name", - }, - false, - }, - { - "name_dc_near", - "name@dc1~near", - &HealthServiceQuery{ - dc: "dc1", - filters: []string{"passing"}, - name: "name", - near: "near", - }, - false, - }, - { - "name_near", - "name~near", - &HealthServiceQuery{ - filters: []string{"passing"}, - name: "name", - near: "near", - }, - false, - }, - { - "tag_name", - "tag.name", - &HealthServiceQuery{ - filters: []string{"passing"}, - name: "name", - tag: "tag", - }, - false, - }, - { - "tag_name_dc", - "tag.name@dc", - &HealthServiceQuery{ - dc: "dc", - filters: []string{"passing"}, - name: "name", - tag: "tag", - }, - false, - }, - { - "tag_name_near", - "tag.name~near", - &HealthServiceQuery{ - filters: []string{"passing"}, - name: "name", - near: "near", - tag: "tag", - }, - false, - }, - { - "tag_name_dc_near", - "tag.name@dc~near", - &HealthServiceQuery{ - dc: "dc", - filters: []string{"passing"}, - name: "name", - near: "near", - tag: "tag", - }, - false, - }, - { - "name_partition", - "name?partition=foo", - &HealthServiceQuery{ - filters: []string{"passing"}, - name: "name", - partition: "foo", - }, - false, - }, - { - "name_peer", - "name?peer=foo", - &HealthServiceQuery{ - filters: []string{"passing"}, - name: "name", - peer: "foo", - }, - false, - }, - { - "name_ns", - "name?ns=foo", - &HealthServiceQuery{ - filters: []string{"passing"}, - name: "name", - namespace: "foo", - }, - false, - }, - { - "name_ns_peer_partition", - "name?ns=foo&peer=bar&partition=baz", - &HealthServiceQuery{ - filters: []string{"passing"}, - name: "name", - namespace: "foo", - peer: "bar", - partition: "baz", - }, - false, - }, - { - "namespace set twice should use first", - "name?ns=foo&ns=bar", - &HealthServiceQuery{ - filters: []string{"passing"}, - name: "name", - namespace: "foo", - }, - false, - }, - { - "empty value in query param", - "name?ns=&peer=&partition=", - &HealthServiceQuery{ - filters: []string{"passing"}, - name: "name", - namespace: "", - peer: "", - partition: "", - }, - false, - }, - { - "query with other parameters", - "tag.name?peer=foo&ns=bar&partition=baz@dc2~near", - &HealthServiceQuery{ - filters: []string{"passing"}, - tag: "tag", - name: "name", - dc: "dc2", - near: "near", - peer: "foo", - namespace: "bar", - partition: "baz", - }, - false, - }, } + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("empty", tenancy), + "", + nil, + true, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("dc_only", tenancy), + "@dc1", + nil, + true, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("near_only", tenancy), + "~near", + nil, + true, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("tag_only", tenancy), + "tag.", + nil, + true, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("query_only", tenancy), + fmt.Sprintf("?ns=%s", tenancy.Namespace), + nil, + true, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("invalid query param (unsupported key)", tenancy), + "name?unsupported=test", + nil, + true, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("name", tenancy), + "name", + &HealthServiceQuery{ + filters: []string{"passing"}, + name: "name", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("name_dc", tenancy), + "name@dc1", + &HealthServiceQuery{ + dc: "dc1", + filters: []string{"passing"}, + name: "name", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("name_dc_near", tenancy), + "name@dc1~near", + &HealthServiceQuery{ + dc: "dc1", + filters: []string{"passing"}, + name: "name", + near: "near", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("name_near", tenancy), + "name~near", + &HealthServiceQuery{ + filters: []string{"passing"}, + name: "name", + near: "near", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("tag_name", tenancy), + "tag.name", + &HealthServiceQuery{ + filters: []string{"passing"}, + name: "name", + tag: "tag", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("tag_name_dc", tenancy), + "tag.name@dc", + &HealthServiceQuery{ + dc: "dc", + filters: []string{"passing"}, + name: "name", + tag: "tag", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("tag_name_near", tenancy), + "tag.name~near", + &HealthServiceQuery{ + filters: []string{"passing"}, + name: "name", + near: "near", + tag: "tag", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("tag_name_dc_near", tenancy), + "tag.name@dc~near", + &HealthServiceQuery{ + dc: "dc", + filters: []string{"passing"}, + name: "name", + near: "near", + tag: "tag", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("name_partition", tenancy), + fmt.Sprintf("name?partition=%s", tenancy.Partition), + &HealthServiceQuery{ + filters: []string{"passing"}, + name: "name", + partition: tenancy.Partition, + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("name_peer", tenancy), + "name?peer=foo", + &HealthServiceQuery{ + filters: []string{"passing"}, + name: "name", + peer: "foo", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("name_ns", tenancy), + fmt.Sprintf("name?ns=%s", tenancy.Namespace), + &HealthServiceQuery{ + filters: []string{"passing"}, + name: "name", + namespace: tenancy.Namespace, + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("name_ns_peer_partition", tenancy), + fmt.Sprintf("name?ns=%s&peer=bar&partition=%s", tenancy.Namespace, tenancy.Partition), + &HealthServiceQuery{ + filters: []string{"passing"}, + name: "name", + namespace: tenancy.Namespace, + peer: "bar", + partition: tenancy.Partition, + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("namespace set twice should use first", tenancy), + fmt.Sprintf("name?ns=%s&ns=random", tenancy.Namespace), + &HealthServiceQuery{ + filters: []string{"passing"}, + name: "name", + namespace: tenancy.Namespace, + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("empty value in query param", tenancy), + "name?ns=&peer=&partition=", + &HealthServiceQuery{ + filters: []string{"passing"}, + name: "name", + namespace: "", + peer: "", + partition: "", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("query with other parameters", tenancy), + fmt.Sprintf("tag.name?peer=foo&ns=%s&partition=%s@dc2~near", tenancy.Namespace, tenancy.Partition), + &HealthServiceQuery{ + filters: []string{"passing"}, + tag: "tag", + name: "name", + dc: "dc2", + near: "near", + peer: "foo", + namespace: tenancy.Namespace, + partition: tenancy.Partition, + }, + false, + }, + } + }) - for i, tc := range cases { + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { act, err := NewHealthServiceQuery(tc.i) if (err != nil) != tc.err { @@ -253,35 +259,62 @@ func TestNewHealthServiceQuery(t *testing.T) { } func TestHealthConnectServiceQuery_Fetch(t *testing.T) { - cases := []struct { + type testCase struct { name string in string exp []*HealthService - }{ - { - "connect-service", - "foo", - []*HealthService{ - { - Name: "foo-sidecar-proxy", - ID: "foo", - Port: 21999, - Status: "passing", - Address: "127.0.0.1", - NodeAddress: "127.0.0.1", - Tags: ServiceTags([]string{}), - NodeMeta: map[string]string{ - "consul-network-segment": "", + } + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("connect-service", tenancy), + "conn-enabled-service-default-default", + []*HealthService{ + { + Name: "conn-enabled-service-proxy-default-default", + ID: "conn-enabled-service-proxy-default-default", + Port: 21999, + Status: "passing", + Address: "127.0.0.1", + NodeAddress: "127.0.0.1", + Tags: ServiceTags([]string{}), + NodeMeta: map[string]string{ + //"consul-network-segment": "", + }, + Weights: api.AgentWeights{ + Passing: 1, + Warning: 1, + }, }, - Weights: api.AgentWeights{ - Passing: 1, - Warning: 1, + }, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("connect-service", tenancy), + fmt.Sprintf("conn-enabled-service-%s-%s?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace, tenancy.Partition, tenancy.Namespace), + []*HealthService{ + { + Name: fmt.Sprintf("conn-enabled-service-proxy-%s-%s", tenancy.Partition, tenancy.Namespace), + ID: fmt.Sprintf("conn-enabled-service-proxy-%s-%s", tenancy.Partition, tenancy.Namespace), + Port: 21999, + Status: "passing", + Address: "127.0.0.1", + NodeAddress: "127.0.0.1", + Tags: ServiceTags([]string{}), + NodeMeta: map[string]string{ + //"consul-network-segment": "", + }, + Weights: api.AgentWeights{ + Passing: 1, + Warning: 1, + }, }, }, }, - }, - } - for i, tc := range cases { + } + }) + + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { d, err := NewHealthConnectQuery(tc.in) if err != nil { @@ -312,141 +345,277 @@ func TestHealthConnectServiceQuery_Fetch(t *testing.T) { } func TestHealthServiceQuery_Fetch(t *testing.T) { - cases := []struct { + type testCase struct { name string i string exp []*HealthService - }{ - { - "consul", - "consul", - []*HealthService{ - { - Node: testConsul.Config.NodeName, - NodeAddress: testConsul.Config.Bind, - NodeTaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", - }, - NodeMeta: map[string]string{ - "consul-network-segment": "", - }, - ServiceMeta: map[string]string{}, - Address: testConsul.Config.Bind, - ID: "consul", - Name: "consul", - Tags: []string{}, - Status: "passing", - Port: testConsul.Config.Ports.Server, - Weights: api.AgentWeights{ - Passing: 1, - Warning: 1, + } + cases := tenancyHelper.GenerateDefaultTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("consul", tenancy), + "consul", + []*HealthService{ + { + Node: testConsul.Config.NodeName, + NodeAddress: testConsul.Config.Bind, + NodeTaggedAddresses: map[string]string{ + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", + }, + NodeMeta: map[string]string{ + //"consul-network-segment": "", + }, + ServiceMeta: map[string]string{}, + Address: testConsul.Config.Bind, + ID: "consul", + Name: "consul", + Tags: []string{}, + Status: "passing", + Port: testConsul.Config.Ports.Server, + Weights: api.AgentWeights{ + Passing: 1, + Warning: 1, + }, }, }, }, - }, - { - "filters", - "consul|warning", - []*HealthService{}, - }, - { - "multifilter", - "consul|warning,passing", - []*HealthService{ - { - Node: testConsul.Config.NodeName, - NodeAddress: testConsul.Config.Bind, - NodeTaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", - }, - NodeMeta: map[string]string{ - "consul-network-segment": "", - }, - ServiceMeta: map[string]string{}, - Address: testConsul.Config.Bind, - ID: "consul", - Name: "consul", - Tags: []string{}, - Status: "passing", - Port: testConsul.Config.Ports.Server, - Weights: api.AgentWeights{ - Passing: 1, - Warning: 1, + testCase{ + tenancyHelper.AppendTenancyInfo("filters", tenancy), + "consul|warning", + []*HealthService{}, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("multifilter", tenancy), + "consul|warning,passing", + []*HealthService{ + { + Node: testConsul.Config.NodeName, + NodeAddress: testConsul.Config.Bind, + NodeTaggedAddresses: map[string]string{ + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", + }, + NodeMeta: map[string]string{ + //"consul-network-segment": "", + }, + ServiceMeta: map[string]string{}, + Address: testConsul.Config.Bind, + ID: "consul", + Name: "consul", + Tags: []string{}, + Status: "passing", + Port: testConsul.Config.Ports.Server, + Weights: api.AgentWeights{ + Passing: 1, + Warning: 1, + }, }, }, }, - }, - { - "service-meta", - "service-meta", - []*HealthService{ - { - Node: testConsul.Config.NodeName, - NodeAddress: testConsul.Config.Bind, - NodeTaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", - }, - NodeMeta: map[string]string{ - "consul-network-segment": "", - }, - ServiceMeta: map[string]string{ - "meta1": "value1", + testCase{ + tenancyHelper.AppendTenancyInfo("service-meta", tenancy), + "service-meta-default-default", + []*HealthService{ + { + Node: testConsul.Config.NodeName, + NodeAddress: testConsul.Config.Bind, + NodeTaggedAddresses: map[string]string{ + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", + }, + NodeMeta: map[string]string{ + //"consul-network-segment": "", + }, + ServiceMeta: map[string]string{ + "meta1": "value1", + }, + Address: testConsul.Config.Bind, + ID: "service-meta-default-default", + Name: "service-meta-default-default", + Tags: []string{"tag1"}, + Status: "passing", + Weights: api.AgentWeights{ + Passing: 1, + Warning: 1, + }, }, - Address: testConsul.Config.Bind, - ID: "service-meta", - Name: "service-meta", - Tags: []string{"tag1"}, - Status: "passing", - Weights: api.AgentWeights{ - Passing: 1, - Warning: 1, + }, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("service-taggedAddresses", tenancy), + "service-taggedAddresses-default-default", + []*HealthService{ + { + Node: testConsul.Config.NodeName, + NodeAddress: testConsul.Config.Bind, + NodeTaggedAddresses: map[string]string{ + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", + }, + NodeMeta: map[string]string{ + //"consul-network-segment": "", + }, + ServiceMeta: map[string]string{}, + Address: testConsul.Config.Bind, + ServiceTaggedAddresses: map[string]api.ServiceAddress{ + "lan": { + Address: "192.0.2.1", + Port: 80, + }, + "wan": { + Address: "192.0.2.2", + Port: 443, + }, + }, + ID: "service-taggedAddresses-default-default", + Name: "service-taggedAddresses-default-default", + Tags: []string{}, + Status: "passing", + Weights: api.AgentWeights{ + Passing: 1, + Warning: 1, + }, }, }, }, - }, - { - "service-taggedAddresses", - "service-taggedAddresses", - []*HealthService{ - { - Node: testConsul.Config.NodeName, - NodeAddress: testConsul.Config.Bind, - NodeTaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", + } + }) + + cases = append(cases, tenancyHelper.GenerateNonDefaultTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("consul", tenancy), + "consul", + []*HealthService{ + { + Node: testConsul.Config.NodeName, + NodeAddress: testConsul.Config.Bind, + NodeTaggedAddresses: map[string]string{ + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", + }, + NodeMeta: map[string]string{ + //"consul-network-segment": "", + }, + ServiceMeta: map[string]string{}, + Address: testConsul.Config.Bind, + ID: "consul", + Name: "consul", + Tags: []string{}, + Status: "passing", + Port: testConsul.Config.Ports.Server, + Weights: api.AgentWeights{ + Passing: 1, + Warning: 1, + }, }, - NodeMeta: map[string]string{ - "consul-network-segment": "", + }, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("filters", tenancy), + "consul|warning", + []*HealthService{}, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("multifilter", tenancy), + "consul|warning,passing", + []*HealthService{ + { + Node: testConsul.Config.NodeName, + NodeAddress: testConsul.Config.Bind, + NodeTaggedAddresses: map[string]string{ + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", + }, + NodeMeta: map[string]string{ + //"consul-network-segment": "", + }, + ServiceMeta: map[string]string{}, + Address: testConsul.Config.Bind, + ID: "consul", + Name: "consul", + Tags: []string{}, + Status: "passing", + Port: testConsul.Config.Ports.Server, + Weights: api.AgentWeights{ + Passing: 1, + Warning: 1, + }, }, - ServiceMeta: map[string]string{}, - Address: testConsul.Config.Bind, - ServiceTaggedAddresses: map[string]api.ServiceAddress{ - "lan": { - Address: "192.0.2.1", - Port: 80, + }, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("service-meta", tenancy), + fmt.Sprintf("service-meta-%s-%s?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace, tenancy.Partition, tenancy.Namespace), + []*HealthService{ + { + Node: testConsul.Config.NodeName, + NodeAddress: testConsul.Config.Bind, + NodeTaggedAddresses: map[string]string{ + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", }, - "wan": { - Address: "192.0.2.2", - Port: 443, + NodeMeta: map[string]string{ + //"consul-network-segment": "", + }, + ServiceMeta: map[string]string{ + "meta1": "value1", + }, + Address: testConsul.Config.Bind, + ID: fmt.Sprintf("service-meta-%s-%s", tenancy.Partition, tenancy.Namespace), + Name: fmt.Sprintf("service-meta-%s-%s", tenancy.Partition, tenancy.Namespace), + Tags: []string{"tag1"}, + Status: "passing", + Weights: api.AgentWeights{ + Passing: 1, + Warning: 1, }, }, - ID: "service-taggedAddresses", - Name: "service-taggedAddresses", - Tags: []string{}, - Status: "passing", - Weights: api.AgentWeights{ - Passing: 1, - Warning: 1, + }, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("service-taggedAddresses", tenancy), + fmt.Sprintf("service-taggedAddresses-%s-%s?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace, tenancy.Partition, tenancy.Namespace), + []*HealthService{ + { + Node: testConsul.Config.NodeName, + NodeAddress: testConsul.Config.Bind, + NodeTaggedAddresses: map[string]string{ + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", + }, + NodeMeta: map[string]string{ + //"consul-network-segment": "", + }, + ServiceMeta: map[string]string{}, + Address: testConsul.Config.Bind, + ServiceTaggedAddresses: map[string]api.ServiceAddress{ + "lan": { + Address: "192.0.2.1", + Port: 80, + }, + "wan": { + Address: "192.0.2.2", + Port: 443, + }, + }, + ID: fmt.Sprintf("service-taggedAddresses-%s-%s", tenancy.Partition, tenancy.Namespace), + Name: fmt.Sprintf("service-taggedAddresses-%s-%s", tenancy.Partition, tenancy.Namespace), + Tags: []string{}, + Status: "passing", + Weights: api.AgentWeights{ + Passing: 1, + Warning: 1, + }, }, }, }, - }, - } + } + })...) - for i, tc := range cases { + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { d, err := NewHealthServiceQuery(tc.i) if err != nil { @@ -477,74 +646,93 @@ func TestHealthServiceQuery_Fetch(t *testing.T) { } func TestHealthServiceQuery_String(t *testing.T) { - cases := []struct { + type testCase struct { name string i string exp string - }{ - { - "name", - "name", - "health.service(name|passing)", - }, - { - "name_dc", - "name@dc", - "health.service(name@dc|passing)", - }, - { - "name_filter", - "name|any", - "health.service(name|any)", - }, - { - "name_multifilter", - "name|warning,passing", - "health.service(name|passing,warning)", - }, - { - "name_near", - "name~near", - "health.service(name~near|passing)", - }, - { - "name_near_filter", - "name~near|any", - "health.service(name~near|any)", - }, - { - "name_dc_near", - "name@dc~near", - "health.service(name@dc~near|passing)", - }, - { - "name_dc_near_filter", - "name@dc~near|any", - "health.service(name@dc~near|any)", - }, - { - "tag_name", - "tag.name", - "health.service(tag.name|passing)", - }, - { - "tag_name_dc", - "tag.name@dc", - "health.service(tag.name@dc|passing)", - }, - { - "tag_name_near", - "tag.name~near", - "health.service(tag.name~near|passing)", - }, - { - "tag_name_dc_near", - "tag.name@dc~near", - "health.service(tag.name@dc~near|passing)", - }, } + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("name", tenancy), + "name", + "health.service(name|passing)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("name_dc", tenancy), + "name@dc", + "health.service(name@dc|passing)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("name_filter", tenancy), + "name|any", + "health.service(name|any)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("name_multifilter", tenancy), + "name|warning,passing", + "health.service(name|passing,warning)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("name_near", tenancy), + "name~near", + "health.service(name~near|passing)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("name_near_filter", tenancy), + "name~near|any", + "health.service(name~near|any)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("name_dc_near", tenancy), + "name@dc~near", + "health.service(name@dc~near|passing)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("name_dc_near_filter", tenancy), + "name@dc~near|any", + "health.service(name@dc~near|any)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("tag_name", tenancy), + "tag.name", + "health.service(tag.name|passing)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("tag_name_dc", tenancy), + "tag.name@dc", + "health.service(tag.name@dc|passing)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("tag_name_near", tenancy), + "tag.name~near", + "health.service(tag.name~near|passing)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("tag_name_dc_near", tenancy), + "tag.name@dc~near", + "health.service(tag.name@dc~near|passing)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("tag_name_dc_near_partition", tenancy), + fmt.Sprintf("tag.name?partition=%s@dc~near", tenancy.Partition), + fmt.Sprintf("health.service(tag.name@dc@partition=%s~near|passing)", tenancy.Partition), + }, + testCase{ + tenancyHelper.AppendTenancyInfo("tag_name_dc_near_partition_ns", tenancy), + fmt.Sprintf("tag.name?partition=%s&ns=%s@dc~near", tenancy.Partition, tenancy.Namespace), + fmt.Sprintf("health.service(tag.name@dc@partition=%s@ns=%s~near|passing)", tenancy.Partition, tenancy.Namespace), + }, + testCase{ + tenancyHelper.AppendTenancyInfo("partition_ns", tenancy), + fmt.Sprintf("tag.name?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace), + fmt.Sprintf("health.service(tag.name@partition=%s@ns=%s|passing)", tenancy.Partition, tenancy.Namespace), + }, + } + }) - for i, tc := range cases { + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { d, err := NewHealthServiceQuery(tc.i) if err != nil { @@ -556,27 +744,43 @@ func TestHealthServiceQuery_String(t *testing.T) { } func TestHealthServiceQueryConnect_String(t *testing.T) { - cases := []struct { + type testCase struct { name string fact func(string) (*HealthServiceQuery, error) in string exp string - }{ - { - "name", - NewHealthServiceQuery, - "name", - "health.service(name|passing)", - }, - { - "name", - NewHealthConnectQuery, - "name", - "health.connect(name|passing)", - }, } + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("name", tenancy), + NewHealthServiceQuery, + "name", + "health.service(name|passing)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("name", tenancy), + NewHealthConnectQuery, + "name", + "health.connect(name|passing)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("name_ns_partition", tenancy), + NewHealthServiceQuery, + fmt.Sprintf("name?ns=%s&partition=%s", tenancy.Namespace, tenancy.Partition), + fmt.Sprintf("health.service(name@partition=%s@ns=%s|passing)", tenancy.Partition, tenancy.Namespace), + }, + testCase{ + tenancyHelper.AppendTenancyInfo("name_ns_partition", tenancy), + NewHealthConnectQuery, + fmt.Sprintf("name?ns=%s&partition=%s", tenancy.Namespace, tenancy.Partition), + fmt.Sprintf("health.connect(name@partition=%s@ns=%s|passing)", tenancy.Partition, tenancy.Namespace), + }, + } + }) - for i, tc := range cases { + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { d, err := tc.fact(tc.in) if err != nil { diff --git a/dependency/kv_get.go b/dependency/kv_get.go index 938ef8ba3..2518f92da 100644 --- a/dependency/kv_get.go +++ b/dependency/kv_get.go @@ -110,6 +110,12 @@ func (d *KVGetQuery) String() string { if d.dc != "" { key = key + "@" + d.dc } + if d.partition != "" { + key = key + "@partition=" + d.partition + } + if d.namespace != "" { + key = key + "@ns=" + d.namespace + } if d.blockOnNil { return fmt.Sprintf("kv.block(%s)", key) diff --git a/dependency/kv_get_test.go b/dependency/kv_get_test.go index eff836d73..4e6247325 100644 --- a/dependency/kv_get_test.go +++ b/dependency/kv_get_test.go @@ -5,6 +5,7 @@ package dependency import ( "fmt" + "github.com/hashicorp/consul/proto-public/pbresource" "testing" "time" @@ -12,170 +13,174 @@ import ( ) func TestNewKVGetQuery(t *testing.T) { - cases := []struct { + type testCase struct { name string i string exp *KVGetQuery err bool - }{ - { - "empty", - "", - &KVGetQuery{}, - false, - }, + } + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("empty", tenancy), + "", + &KVGetQuery{}, + false, + }, - { - "dc_only", - "@dc1", - nil, - true, - }, - { - "query_only", - "?ns=foo", - nil, - true, - }, - { - "invalid query param (unsupported key)", - "key?unsupported=foo", - nil, - true, - }, - { - "key", - "key", - &KVGetQuery{ - key: "key", + testCase{ + tenancyHelper.AppendTenancyInfo("dc_only", tenancy), + "@dc1", + nil, + true, }, - false, - }, - { - "dc", - "key@dc1", - &KVGetQuery{ - key: "key", - dc: "dc1", + testCase{ + tenancyHelper.AppendTenancyInfo("query_only", tenancy), + fmt.Sprintf("?ns=%s", tenancy.Namespace), + nil, + true, }, - false, - }, - { - "partition", - "key?partition=foo", - &KVGetQuery{ - key: "key", - partition: "foo", + testCase{ + tenancyHelper.AppendTenancyInfo("invalid query param (unsupported key)", tenancy), + "key?unsupported=foo", + nil, + true, }, - false, - }, - { - "namespace", - "key?ns=foo", - &KVGetQuery{ - key: "key", - namespace: "foo", + testCase{ + tenancyHelper.AppendTenancyInfo("key", tenancy), + "key", + &KVGetQuery{ + key: "key", + }, + false, }, - false, - }, - { - "namespace_and_partition", - "key?ns=foo&partition=bar", - &KVGetQuery{ - key: "key", - namespace: "foo", - partition: "bar", + testCase{ + tenancyHelper.AppendTenancyInfo("dc", tenancy), + "key@dc1", + &KVGetQuery{ + key: "key", + dc: "dc1", + }, + false, }, - false, - }, - { - "namespace_and_partition_and_dc", - "key?ns=foo&partition=bar@dc1", - &KVGetQuery{ - key: "key", - namespace: "foo", - partition: "bar", - dc: "dc1", + testCase{ + tenancyHelper.AppendTenancyInfo("partition", tenancy), + fmt.Sprintf("key?partition=%s", tenancy.Partition), + &KVGetQuery{ + key: "key", + partition: tenancy.Partition, + }, + false, }, - false, - }, - { - "empty_query", - "key?ns=&partition=", - &KVGetQuery{ - key: "key", - namespace: "", - partition: "", + testCase{ + tenancyHelper.AppendTenancyInfo("namespace", tenancy), + fmt.Sprintf("key?ns=%s", tenancy.Namespace), + &KVGetQuery{ + key: "key", + namespace: tenancy.Namespace, + }, + false, }, - false, - }, - { - "dots", - "key.with.dots", - &KVGetQuery{ - key: "key.with.dots", + testCase{ + tenancyHelper.AppendTenancyInfo("namespace_and_partition", tenancy), + fmt.Sprintf("key?ns=%s&partition=%s", tenancy.Namespace, tenancy.Partition), + &KVGetQuery{ + key: "key", + namespace: tenancy.Namespace, + partition: tenancy.Partition, + }, + false, }, - false, - }, - { - "slashes", - "key/with/slashes", - &KVGetQuery{ - key: "key/with/slashes", + testCase{ + tenancyHelper.AppendTenancyInfo("namespace_and_partition_and_dc", tenancy), + fmt.Sprintf("key?ns=%s&partition=%s@dc1", tenancy.Namespace, tenancy.Partition), + &KVGetQuery{ + key: "key", + namespace: tenancy.Namespace, + partition: tenancy.Partition, + dc: "dc1", + }, + false, }, - false, - }, - { - "dashes", - "key-with-dashes", - &KVGetQuery{ - key: "key-with-dashes", + testCase{ + tenancyHelper.AppendTenancyInfo("empty_query", tenancy), + "key?ns=&partition=", + &KVGetQuery{ + key: "key", + namespace: "", + partition: "", + }, + false, }, - false, - }, - { - "leading_slash", - "/leading/slash", - &KVGetQuery{ - key: "leading/slash", + testCase{ + tenancyHelper.AppendTenancyInfo("dots", tenancy), + "key.with.dots", + &KVGetQuery{ + key: "key.with.dots", + }, + false, }, - false, - }, - { - "trailing_slash", - "trailing/slash/", - &KVGetQuery{ - key: "trailing/slash/", + testCase{ + tenancyHelper.AppendTenancyInfo("slashes", tenancy), + "key/with/slashes", + &KVGetQuery{ + key: "key/with/slashes", + }, + false, }, - false, - }, - { - "underscores", - "key_with_underscores", - &KVGetQuery{ - key: "key_with_underscores", + testCase{ + tenancyHelper.AppendTenancyInfo("dashes", tenancy), + "key-with-dashes", + &KVGetQuery{ + key: "key-with-dashes", + }, + false, }, - false, - }, - { - "special_characters", - "config/facet:größe-lf-si", - &KVGetQuery{ - key: "config/facet:größe-lf-si", + testCase{ + tenancyHelper.AppendTenancyInfo("leading_slash", tenancy), + "/leading/slash", + &KVGetQuery{ + key: "leading/slash", + }, + false, }, - false, - }, - { - "splat", - "config/*/timeouts/", - &KVGetQuery{ - key: "config/*/timeouts/", + testCase{ + tenancyHelper.AppendTenancyInfo("trailing_slash", tenancy), + "trailing/slash/", + &KVGetQuery{ + key: "trailing/slash/", + }, + false, }, - false, - }, - } + testCase{ + tenancyHelper.AppendTenancyInfo("underscores", tenancy), + "key_with_underscores", + &KVGetQuery{ + key: "key_with_underscores", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("special_characters", tenancy), + "config/facet:größe-lf-si", + &KVGetQuery{ + key: "config/facet:größe-lf-si", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("splat", tenancy), + "config/*/timeouts/", + &KVGetQuery{ + key: "config/*/timeouts/", + }, + false, + }, + } + }) - for i, tc := range cases { + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { act, err := NewKVGetQuery(tc.i) if (err != nil) != tc.err { @@ -192,32 +197,40 @@ func TestNewKVGetQuery(t *testing.T) { } func TestKVGetQuery_Fetch(t *testing.T) { - testConsul.SetKVString(t, "test-kv-get/key", "value") - testConsul.SetKVString(t, "test-kv-get/key_empty", "") - cases := []struct { + for _, tenancy := range tenancyHelper.TestTenancies() { + testConsul.SetKVString(t, fmt.Sprintf("test-kv-get/key?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace), fmt.Sprintf("value-%s-%s", tenancy.Partition, tenancy.Namespace)) + testConsul.SetKVString(t, fmt.Sprintf("test-kv-get/key_empty?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace), "") + } + + type testCase struct { name string i string exp interface{} - }{ - { - "exists", - "test-kv-get/key", - "value", - }, - { - "exists_empty_string", - "test-kv-get/key_empty", - "", - }, - { - "no_exist", - "test-kv-get/not/a/real/key/like/ever", - nil, - }, } - for i, tc := range cases { + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("exists", tenancy), + fmt.Sprintf("test-kv-get/key?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace), + fmt.Sprintf("value-%s-%s", tenancy.Partition, tenancy.Namespace), + }, + testCase{ + tenancyHelper.AppendTenancyInfo("exists_empty_string", tenancy), + fmt.Sprintf("test-kv-get/key_empty?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace), + "", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("no_exist", tenancy), + fmt.Sprintf("test-kv-get/not/a/real/key/like/ever?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace), + nil, + }, + } + }) + + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { d, err := NewKVGetQuery(tc.i) if err != nil { @@ -233,8 +246,8 @@ func TestKVGetQuery_Fetch(t *testing.T) { }) } - t.Run("stops", func(t *testing.T) { - d, err := NewKVGetQuery("test-kv-get/key") + tenancyHelper.RunWithTenancies(func(tenancy *pbresource.Tenancy) { + d, err := NewKVGetQuery(fmt.Sprintf("test-kv-get/key?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace)) if err != nil { t.Fatal(err) } @@ -268,10 +281,10 @@ func TestKVGetQuery_Fetch(t *testing.T) { case <-time.After(100 * time.Millisecond): t.Errorf("did not stop") } - }) + }, t, "stops") - t.Run("fires_changes", func(t *testing.T) { - d, err := NewKVGetQuery("test-kv-get/key") + tenancyHelper.RunWithTenancies(func(tenancy *pbresource.Tenancy) { + d, err := NewKVGetQuery(fmt.Sprintf("test-kv-get/key?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace)) if err != nil { t.Fatal(err) } @@ -292,7 +305,7 @@ func TestKVGetQuery_Fetch(t *testing.T) { dataCh <- data }() - testConsul.SetKVString(t, "test-kv-get/key", "new-value") + testConsul.SetKVString(t, fmt.Sprintf("test-kv-get/key?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace), "new-value") select { case err := <-errCh: @@ -300,28 +313,42 @@ func TestKVGetQuery_Fetch(t *testing.T) { case data := <-dataCh: assert.Equal(t, data, "new-value") } - }) + }, t, "fires_changes") } func TestKVGetQuery_String(t *testing.T) { - cases := []struct { + type testCase struct { name string i string exp string - }{ - { - "key", - "key", - "kv.get(key)", - }, - { - "dc", - "key@dc1", - "kv.get(key@dc1)", - }, } + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("key", tenancy), + "key", + "kv.get(key)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("dc", tenancy), + "key@dc1", + "kv.get(key@dc1)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("dc_and_partition", tenancy), + fmt.Sprintf("key?partition=%s@dc1", tenancy.Partition), + fmt.Sprintf("kv.get(key@dc1@partition=%s)", tenancy.Partition), + }, + testCase{ + tenancyHelper.AppendTenancyInfo("dc_and_partition_and_ns", tenancy), + fmt.Sprintf("key?partition=%s&ns=%s@dc1", tenancy.Partition, tenancy.Namespace), + fmt.Sprintf("kv.get(key@dc1@partition=%s@ns=%s)", tenancy.Partition, tenancy.Namespace), + }, + } + }) - for i, tc := range cases { + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { d, err := NewKVGetQuery(tc.i) if err != nil { diff --git a/dependency/kv_keys.go b/dependency/kv_keys.go index cd20a6ef6..2b80c3205 100644 --- a/dependency/kv_keys.go +++ b/dependency/kv_keys.go @@ -104,6 +104,12 @@ func (d *KVKeysQuery) String() string { if d.dc != "" { prefix = prefix + "@" + d.dc } + if d.partition != "" { + prefix = prefix + "@partition=" + d.partition + } + if d.namespace != "" { + prefix = prefix + "@ns=" + d.namespace + } return fmt.Sprintf("kv.keys(%s)", prefix) } diff --git a/dependency/kv_keys_test.go b/dependency/kv_keys_test.go index 6e018665f..883cda053 100644 --- a/dependency/kv_keys_test.go +++ b/dependency/kv_keys_test.go @@ -5,6 +5,7 @@ package dependency import ( "fmt" + "github.com/hashicorp/consul/proto-public/pbresource" "testing" "time" @@ -12,185 +13,189 @@ import ( ) func TestNewKVKeysQuery(t *testing.T) { - cases := []struct { + type testCase struct { name string i string exp *KVKeysQuery err bool - }{ - { - "empty", - "", - &KVKeysQuery{}, - false, - }, - { - "dc_only", - "@dc1", - nil, - true, - }, - { - "query_only", - "?ns=foo", - nil, - true, - }, - { - "invalid query param (unsupported key)", - "prefix?unsupported=foo", - nil, - true, - }, - { - "prefix", - "prefix", - &KVKeysQuery{ - prefix: "prefix", + } + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("empty", tenancy), + "", + &KVKeysQuery{}, + false, }, - false, - }, - { - "dc", - "prefix@dc1", - &KVKeysQuery{ - prefix: "prefix", - dc: "dc1", + testCase{ + tenancyHelper.AppendTenancyInfo("dc_only", tenancy), + "@dc1", + nil, + true, }, - false, - }, - { - "partition", - "prefix?partition=foo", - &KVKeysQuery{ - prefix: "prefix", - partition: "foo", + testCase{ + tenancyHelper.AppendTenancyInfo("query_only", tenancy), + fmt.Sprintf("?ns=%s", tenancy.Namespace), + nil, + true, }, - false, - }, - { - "namespace", - "prefix?ns=foo", - &KVKeysQuery{ - prefix: "prefix", - namespace: "foo", + testCase{ + tenancyHelper.AppendTenancyInfo("invalid query param (unsupported key)", tenancy), + "prefix?unsupported=foo", + nil, + true, }, - false, - }, - { - "namespace_and_partition", - "prefix?ns=foo&partition=bar", - &KVKeysQuery{ - prefix: "prefix", - namespace: "foo", - partition: "bar", + testCase{ + tenancyHelper.AppendTenancyInfo("prefix", tenancy), + "prefix", + &KVKeysQuery{ + prefix: "prefix", + }, + false, }, - false, - }, - { - "namespace_and_partition_and_dc", - "prefix?ns=foo&partition=bar@dc1", - &KVKeysQuery{ - prefix: "prefix", - namespace: "foo", - partition: "bar", - dc: "dc1", + testCase{ + tenancyHelper.AppendTenancyInfo("dc", tenancy), + "prefix@dc1", + &KVKeysQuery{ + prefix: "prefix", + dc: "dc1", + }, + false, }, - false, - }, - { - "empty_query", - "prefix?ns=&partition=", - &KVKeysQuery{ - prefix: "prefix", - namespace: "", - partition: "", + testCase{ + tenancyHelper.AppendTenancyInfo("partition", tenancy), + fmt.Sprintf("prefix?partition=%s", tenancy.Partition), + &KVKeysQuery{ + prefix: "prefix", + partition: tenancy.Partition, + }, + false, }, - false, - }, - { - "dots", - "prefix.with.dots", - &KVKeysQuery{ - prefix: "prefix.with.dots", + testCase{ + tenancyHelper.AppendTenancyInfo("namespace", tenancy), + fmt.Sprintf("prefix?ns=%s", tenancy.Namespace), + &KVKeysQuery{ + prefix: "prefix", + namespace: tenancy.Namespace, + }, + false, }, - false, - }, - { - "slashes", - "prefix/with/slashes", - &KVKeysQuery{ - prefix: "prefix/with/slashes", + testCase{ + tenancyHelper.AppendTenancyInfo("namespace_and_partition", tenancy), + "prefix?ns=foo&partition=bar", + &KVKeysQuery{ + prefix: "prefix", + namespace: "foo", + partition: "bar", + }, + false, }, - false, - }, - { - "dashes", - "prefix-with-dashes", - &KVKeysQuery{ - prefix: "prefix-with-dashes", + testCase{ + tenancyHelper.AppendTenancyInfo("dc_namespace_and_partition", tenancy), + fmt.Sprintf("prefix?ns=%s&partition=%s@dc1", tenancy.Namespace, tenancy.Partition), + &KVKeysQuery{ + prefix: "prefix", + namespace: tenancy.Namespace, + partition: tenancy.Partition, + dc: "dc1", + }, + false, }, - false, - }, - { - "leading_slash", - "/leading/slash", - &KVKeysQuery{ - prefix: "leading/slash", + testCase{ + tenancyHelper.AppendTenancyInfo("empty_query", tenancy), + "prefix?ns=&partition=", + &KVKeysQuery{ + prefix: "prefix", + namespace: "", + partition: "", + }, + false, }, - false, - }, - { - "trailing_slash", - "trailing/slash/", - &KVKeysQuery{ - prefix: "trailing/slash/", + testCase{ + tenancyHelper.AppendTenancyInfo("dots", tenancy), + "prefix.with.dots", + &KVKeysQuery{ + prefix: "prefix.with.dots", + }, + false, }, - false, - }, - { - "underscores", - "prefix_with_underscores", - &KVKeysQuery{ - prefix: "prefix_with_underscores", + testCase{ + tenancyHelper.AppendTenancyInfo("slashes", tenancy), + "prefix/with/slashes", + &KVKeysQuery{ + prefix: "prefix/with/slashes", + }, + false, }, - false, - }, - { - "special_characters", - "config/facet:größe-lf-si", - &KVKeysQuery{ - prefix: "config/facet:größe-lf-si", + testCase{ + tenancyHelper.AppendTenancyInfo("dashes", tenancy), + "prefix-with-dashes", + &KVKeysQuery{ + prefix: "prefix-with-dashes", + }, + false, }, - false, - }, - { - "splat", - "config/*/timeouts/", - &KVKeysQuery{ - prefix: "config/*/timeouts/", + testCase{ + tenancyHelper.AppendTenancyInfo("leading_slash", tenancy), + "/leading/slash", + &KVKeysQuery{ + prefix: "leading/slash", + }, + false, }, - false, - }, - { - "slash", - "/", - &KVKeysQuery{ - prefix: "/", + testCase{ + tenancyHelper.AppendTenancyInfo("trailing_slash", tenancy), + "trailing/slash/", + &KVKeysQuery{ + prefix: "trailing/slash/", + }, + false, }, - false, - }, - { - "slash-slash", - "//", - &KVKeysQuery{ - prefix: "/", + testCase{ + tenancyHelper.AppendTenancyInfo("underscores", tenancy), + "prefix_with_underscores", + &KVKeysQuery{ + prefix: "prefix_with_underscores", + }, + false, }, - false, - }, - } + testCase{ + tenancyHelper.AppendTenancyInfo("special_characters", tenancy), + "config/facet:größe-lf-si", + &KVKeysQuery{ + prefix: "config/facet:größe-lf-si", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("splat", tenancy), + "config/*/timeouts/", + &KVKeysQuery{ + prefix: "config/*/timeouts/", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("slash", tenancy), + "/", + &KVKeysQuery{ + prefix: "/", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("slash-slash", tenancy), + "//", + &KVKeysQuery{ + prefix: "/", + }, + false, + }, + } + }) - for i, tc := range cases { + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { act, err := NewKVKeysQuery(tc.i) if (err != nil) != tc.err { @@ -207,33 +212,48 @@ func TestNewKVKeysQuery(t *testing.T) { } func TestKVKeysQuery_Fetch(t *testing.T) { - testConsul.SetKVString(t, "test-kv-keys/prefix/foo", "bar") - testConsul.SetKVString(t, "test-kv-keys/prefix/zip", "zap") - testConsul.SetKVString(t, "test-kv-keys/prefix/wave/ocean", "sleek") + for _, tenancy := range tenancyHelper.TestTenancies() { + testConsul.SetKVString(t, fmt.Sprintf("test-kv-keys/prefix/foo-%s-%s?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace, tenancy.Partition, tenancy.Namespace), "bar") + testConsul.SetKVString(t, fmt.Sprintf("test-kv-keys/prefix/zip-%s-%s?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace, tenancy.Partition, tenancy.Namespace), "zap") + testConsul.SetKVString(t, fmt.Sprintf("test-kv-keys/prefix/wave/ocean-%s-%s?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace, tenancy.Partition, tenancy.Namespace), "sleek") + } - cases := []struct { + type testCase struct { name string i string exp []string - }{ - { - "exists", - "test-kv-keys/prefix", - []string{"foo", "wave/ocean", "zip"}, - }, - { - "trailing", - "test-kv-keys/prefix/", - []string{"foo", "wave/ocean", "zip"}, - }, - { - "no_exist", - "test-kv-keys/not/a/real/prefix/like/ever", - []string{}, - }, } - for i, tc := range cases { + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("exists", tenancy), + fmt.Sprintf("test-kv-keys/prefix?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace), + []string{ + fmt.Sprintf("foo-%s-%s", tenancy.Partition, tenancy.Namespace), + fmt.Sprintf("wave/ocean-%s-%s", tenancy.Partition, tenancy.Namespace), + fmt.Sprintf("zip-%s-%s", tenancy.Partition, tenancy.Namespace), + }, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("trailing", tenancy), + fmt.Sprintf("test-kv-keys/prefix/?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace), + []string{ + fmt.Sprintf("foo-%s-%s", tenancy.Partition, tenancy.Namespace), + fmt.Sprintf("wave/ocean-%s-%s", tenancy.Partition, tenancy.Namespace), + fmt.Sprintf("zip-%s-%s", tenancy.Partition, tenancy.Namespace), + }, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("no_exist", tenancy), + fmt.Sprintf("test-kv-keys/prefix/not/a/real/key/like/ever?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace), + []string{}, + }, + } + }) + + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { d, err := NewKVKeysQuery(tc.i) if err != nil { @@ -249,8 +269,8 @@ func TestKVKeysQuery_Fetch(t *testing.T) { }) } - t.Run("stops", func(t *testing.T) { - d, err := NewKVKeysQuery("test-kv-keys/prefix") + tenancyHelper.RunWithTenancies(func(tenancy *pbresource.Tenancy) { + d, err := NewKVKeysQuery(fmt.Sprintf("test-kv-keys/prefix?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace)) if err != nil { t.Fatal(err) } @@ -284,10 +304,10 @@ func TestKVKeysQuery_Fetch(t *testing.T) { case <-time.After(100 * time.Millisecond): t.Errorf("did not stop") } - }) + }, t, "stops") - t.Run("fires_changes", func(t *testing.T) { - d, err := NewKVKeysQuery("test-kv-keys/prefix/") + tenancyHelper.RunWithTenancies(func(tenancy *pbresource.Tenancy) { + d, err := NewKVKeysQuery(fmt.Sprintf("test-kv-keys/prefix?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace)) if err != nil { t.Fatal(err) } @@ -308,37 +328,56 @@ func TestKVKeysQuery_Fetch(t *testing.T) { dataCh <- data }() - testConsul.SetKVString(t, "test-kv-keys/prefix/zebra", "value") + testConsul.SetKVString(t, fmt.Sprintf("test-kv-keys/prefix/zebra-%s-%s?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace, tenancy.Partition, tenancy.Namespace), "value") select { case err := <-errCh: t.Fatal(err) case act := <-dataCh: - exp := []string{"foo", "wave/ocean", "zebra", "zip"} + exp := []string{ + fmt.Sprintf("foo-%s-%s", tenancy.Partition, tenancy.Namespace), + fmt.Sprintf("wave/ocean-%s-%s", tenancy.Partition, tenancy.Namespace), + fmt.Sprintf("zebra-%s-%s", tenancy.Partition, tenancy.Namespace), + fmt.Sprintf("zip-%s-%s", tenancy.Partition, tenancy.Namespace), + } assert.Equal(t, exp, act) } - }) + }, t, "fires_changes") } func TestKVKeysQuery_String(t *testing.T) { - cases := []struct { + type testCase struct { name string i string exp string - }{ - { - "prefix", - "prefix", - "kv.keys(prefix)", - }, - { - "dc", - "prefix@dc1", - "kv.keys(prefix@dc1)", - }, } + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("prefix", tenancy), + "prefix", + "kv.keys(prefix)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("dc", tenancy), + "prefix@dc1", + "kv.keys(prefix@dc1)", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("dc_partition", tenancy), + fmt.Sprintf("prefix?partition=%s@dc1", tenancy.Partition), + fmt.Sprintf("kv.keys(prefix@dc1@partition=%s)", tenancy.Partition), + }, + testCase{ + tenancyHelper.AppendTenancyInfo("dc_partition_ns", tenancy), + fmt.Sprintf("prefix?partition=%s&ns=%s@dc1", tenancy.Partition, tenancy.Namespace), + fmt.Sprintf("kv.keys(prefix@dc1@partition=%s@ns=%s)", tenancy.Partition, tenancy.Namespace), + }, + } + }) - for i, tc := range cases { + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { d, err := NewKVKeysQuery(tc.i) if err != nil { diff --git a/dependency/kv_list.go b/dependency/kv_list.go index 91de6efbc..76cbbd3e5 100644 --- a/dependency/kv_list.go +++ b/dependency/kv_list.go @@ -133,6 +133,12 @@ func (d *KVListQuery) String() string { if d.dc != "" { prefix = prefix + "@" + d.dc } + if d.partition != "" { + prefix = prefix + "@partition=" + d.partition + } + if d.namespace != "" { + prefix = prefix + "@ns=" + d.namespace + } return fmt.Sprintf("kv.list(%s)", prefix) } diff --git a/dependency/kv_list_test.go b/dependency/kv_list_test.go index d6201496c..b91c3ef3f 100644 --- a/dependency/kv_list_test.go +++ b/dependency/kv_list_test.go @@ -5,6 +5,7 @@ package dependency import ( "fmt" + "github.com/hashicorp/consul/proto-public/pbresource" "testing" "time" @@ -12,185 +13,189 @@ import ( ) func TestNewKVListQuery(t *testing.T) { - cases := []struct { + type testCase struct { name string i string exp *KVListQuery err bool - }{ - { - "empty", - "", - &KVListQuery{}, - false, - }, - { - "dc_only", - "@dc1", - nil, - true, - }, - { - "query_only", - "?ns=foo", - nil, - true, - }, - { - "invalid query param (unsupported key)", - "prefix?unsupported=foo", - nil, - true, - }, - { - "prefix", - "prefix", - &KVListQuery{ - prefix: "prefix", + } + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("empty", tenancy), + "", + &KVListQuery{}, + false, }, - false, - }, - { - "dc", - "prefix@dc1", - &KVListQuery{ - prefix: "prefix", - dc: "dc1", + testCase{ + tenancyHelper.AppendTenancyInfo("dc_only", tenancy), + "@dc1", + nil, + true, }, - false, - }, - { - "partition", - "prefix?partition=foo", - &KVListQuery{ - prefix: "prefix", - partition: "foo", + testCase{ + tenancyHelper.AppendTenancyInfo("query_only", tenancy), + fmt.Sprintf("?ns=%s", tenancy.Namespace), + nil, + true, }, - false, - }, - { - "namespace", - "prefix?ns=foo", - &KVListQuery{ - prefix: "prefix", - namespace: "foo", + testCase{ + tenancyHelper.AppendTenancyInfo("invalid query param (unsupported key)", tenancy), + "prefix?unsupported=foo", + nil, + true, }, - false, - }, - { - "namespace_and_partition", - "prefix?ns=foo&partition=bar", - &KVListQuery{ - prefix: "prefix", - namespace: "foo", - partition: "bar", + testCase{ + tenancyHelper.AppendTenancyInfo("prefix", tenancy), + "prefix", + &KVListQuery{ + prefix: "prefix", + }, + false, }, - false, - }, - { - "namespace_and_partition_and_dc", - "prefix?ns=foo&partition=bar@dc1", - &KVListQuery{ - prefix: "prefix", - namespace: "foo", - partition: "bar", - dc: "dc1", + testCase{ + tenancyHelper.AppendTenancyInfo("dc", tenancy), + "prefix@dc1", + &KVListQuery{ + prefix: "prefix", + dc: "dc1", + }, + false, }, - false, - }, - { - "empty_query", - "prefix?ns=&partition=", - &KVListQuery{ - prefix: "prefix", - namespace: "", - partition: "", + testCase{ + tenancyHelper.AppendTenancyInfo("partition", tenancy), + fmt.Sprintf("prefix?partition=%s", tenancy.Partition), + &KVListQuery{ + prefix: "prefix", + partition: tenancy.Partition, + }, + false, }, - false, - }, - { - "dots", - "prefix.with.dots", - &KVListQuery{ - prefix: "prefix.with.dots", + testCase{ + tenancyHelper.AppendTenancyInfo("namespace", tenancy), + fmt.Sprintf("prefix?ns=%s", tenancy.Namespace), + &KVListQuery{ + prefix: "prefix", + namespace: tenancy.Namespace, + }, + false, }, - false, - }, - { - "slashes", - "prefix/with/slashes", - &KVListQuery{ - prefix: "prefix/with/slashes", + testCase{ + tenancyHelper.AppendTenancyInfo("namespace_and_partition", tenancy), + fmt.Sprintf("prefix?ns=%s&partition=%s", tenancy.Namespace, tenancy.Partition), + &KVListQuery{ + prefix: "prefix", + namespace: tenancy.Namespace, + partition: tenancy.Partition, + }, + false, }, - false, - }, - { - "dashes", - "prefix-with-dashes", - &KVListQuery{ - prefix: "prefix-with-dashes", + testCase{ + tenancyHelper.AppendTenancyInfo("dc_namespace_and_partition", tenancy), + fmt.Sprintf("prefix?ns=%s&partition=%s@dc1", tenancy.Namespace, tenancy.Partition), + &KVListQuery{ + prefix: "prefix", + namespace: tenancy.Namespace, + partition: tenancy.Partition, + dc: "dc1", + }, + false, }, - false, - }, - { - "leading_slash", - "/leading/slash", - &KVListQuery{ - prefix: "leading/slash", + testCase{ + tenancyHelper.AppendTenancyInfo("empty_query", tenancy), + "prefix?ns=&partition=", + &KVListQuery{ + prefix: "prefix", + namespace: "", + partition: "", + }, + false, }, - false, - }, - { - "trailing_slash", - "trailing/slash/", - &KVListQuery{ - prefix: "trailing/slash/", + testCase{ + tenancyHelper.AppendTenancyInfo("dots", tenancy), + "prefix.with.dots", + &KVListQuery{ + prefix: "prefix.with.dots", + }, + false, }, - false, - }, - { - "underscores", - "prefix_with_underscores", - &KVListQuery{ - prefix: "prefix_with_underscores", + testCase{ + tenancyHelper.AppendTenancyInfo("slashes", tenancy), + "prefix/with/slashes", + &KVListQuery{ + prefix: "prefix/with/slashes", + }, + false, }, - false, - }, - { - "special_characters", - "config/facet:größe-lf-si", - &KVListQuery{ - prefix: "config/facet:größe-lf-si", + testCase{ + tenancyHelper.AppendTenancyInfo("dashes", tenancy), + "prefix-with-dashes", + &KVListQuery{ + prefix: "prefix-with-dashes", + }, + false, }, - false, - }, - { - "splat", - "config/*/timeouts/", - &KVListQuery{ - prefix: "config/*/timeouts/", + testCase{ + tenancyHelper.AppendTenancyInfo("leading_slash", tenancy), + "/leading/slash", + &KVListQuery{ + prefix: "leading/slash", + }, + false, }, - false, - }, - { - "slash", - "/", - &KVListQuery{ - prefix: "/", + testCase{ + tenancyHelper.AppendTenancyInfo("trailing_slash", tenancy), + "trailing/slash/", + &KVListQuery{ + prefix: "trailing/slash/", + }, + false, }, - false, - }, - { - "slash-slash", - "//", - &KVListQuery{ - prefix: "/", + testCase{ + tenancyHelper.AppendTenancyInfo("underscores", tenancy), + "prefix_with_underscores", + &KVListQuery{ + prefix: "prefix_with_underscores", + }, + false, }, - false, - }, - } + testCase{ + tenancyHelper.AppendTenancyInfo("special_characters", tenancy), + "config/facet:größe-lf-si", + &KVListQuery{ + prefix: "config/facet:größe-lf-si", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("splat", tenancy), + "config/*/timeouts/", + &KVListQuery{ + prefix: "config/*/timeouts/", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("slash", tenancy), + "/", + &KVListQuery{ + prefix: "/", + }, + false, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("slash-slash", tenancy), + "//", + &KVListQuery{ + prefix: "/", + }, + false, + }, + } + }) - for i, tc := range cases { + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { act, err := NewKVListQuery(tc.i) if (err != nil) != tc.err { @@ -207,65 +212,72 @@ func TestNewKVListQuery(t *testing.T) { } func TestKVListQuery_Fetch(t *testing.T) { - testConsul.SetKVString(t, "test-kv-list/prefix/foo", "bar") - testConsul.SetKVString(t, "test-kv-list/prefix/zip", "zap") - testConsul.SetKVString(t, "test-kv-list/prefix/wave/ocean", "sleek") + for _, tenancy := range tenancyHelper.TestTenancies() { + testConsul.SetKVString(t, fmt.Sprintf("test-kv-list/prefix/foo?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace), fmt.Sprintf("bar-%s-%s", tenancy.Partition, tenancy.Namespace)) + testConsul.SetKVString(t, fmt.Sprintf("test-kv-list/prefix/zip?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace), fmt.Sprintf("zap-%s-%s", tenancy.Partition, tenancy.Namespace)) + testConsul.SetKVString(t, fmt.Sprintf("test-kv-list/prefix/wave/ocean?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace), fmt.Sprintf("sleek-%s-%s", tenancy.Partition, tenancy.Namespace)) + } - cases := []struct { + type testCase struct { name string i string exp []*KeyPair - }{ - { - "exists", - "test-kv-list/prefix", - []*KeyPair{ - { - Path: "test-kv-list/prefix/foo", - Key: "foo", - Value: "bar", - }, - { - Path: "test-kv-list/prefix/wave/ocean", - Key: "wave/ocean", - Value: "sleek", - }, - { - Path: "test-kv-list/prefix/zip", - Key: "zip", - Value: "zap", + } + + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + "exists", + fmt.Sprintf("test-kv-list/prefix?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace), + []*KeyPair{ + { + Path: "test-kv-list/prefix/foo", + Key: "foo", + Value: fmt.Sprintf("bar-%s-%s", tenancy.Partition, tenancy.Namespace), + }, + { + Path: "test-kv-list/prefix/wave/ocean", + Key: "wave/ocean", + Value: fmt.Sprintf("sleek-%s-%s", tenancy.Partition, tenancy.Namespace), + }, + { + Path: "test-kv-list/prefix/zip", + Key: "zip", + Value: fmt.Sprintf("zap-%s-%s", tenancy.Partition, tenancy.Namespace), + }, }, }, - }, - { - "trailing", - "test-kv-list/prefix/", - []*KeyPair{ - { - Path: "test-kv-list/prefix/foo", - Key: "foo", - Value: "bar", - }, - { - Path: "test-kv-list/prefix/wave/ocean", - Key: "wave/ocean", - Value: "sleek", - }, - { - Path: "test-kv-list/prefix/zip", - Key: "zip", - Value: "zap", + testCase{ + "trailing", + fmt.Sprintf("test-kv-list/prefix/?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace), + []*KeyPair{ + { + Path: "test-kv-list/prefix/foo", + Key: "foo", + Value: fmt.Sprintf("bar-%s-%s", tenancy.Partition, tenancy.Namespace), + }, + { + Path: "test-kv-list/prefix/wave/ocean", + Key: "wave/ocean", + Value: fmt.Sprintf("sleek-%s-%s", tenancy.Partition, tenancy.Namespace), + }, + { + Path: "test-kv-list/prefix/zip", + Key: "zip", + Value: fmt.Sprintf("zap-%s-%s", tenancy.Partition, tenancy.Namespace), + }, }, }, - }, - { - "no_exist", - "test-kv-list/not/a/real/prefix/like/ever", - []*KeyPair{}, - }, - } + testCase{ + "no_exist", + fmt.Sprintf("test-kv-list/not/a/real/prefix/like/ever?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace), + []*KeyPair{}, + }, + } + }) - for i, tc := range cases { + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { d, err := NewKVListQuery(tc.i) if err != nil { @@ -286,8 +298,8 @@ func TestKVListQuery_Fetch(t *testing.T) { }) } - t.Run("stops", func(t *testing.T) { - d, err := NewKVListQuery("test-kv-list/prefix") + tenancyHelper.RunWithTenancies(func(tenancy *pbresource.Tenancy) { + d, err := NewKVListQuery(fmt.Sprintf("test-kv-list/prefix?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace)) if err != nil { t.Fatal(err) } @@ -321,10 +333,10 @@ func TestKVListQuery_Fetch(t *testing.T) { case <-time.After(250 * time.Millisecond): t.Errorf("did not stop") } - }) + }, t, "stops") - t.Run("fires_changes", func(t *testing.T) { - d, err := NewKVListQuery("test-kv-list/prefix/") + tenancyHelper.RunWithTenancies(func(tenancy *pbresource.Tenancy) { + d, err := NewKVListQuery(fmt.Sprintf("test-kv-list/prefix?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace)) if err != nil { t.Fatal(err) } @@ -345,7 +357,7 @@ func TestKVListQuery_Fetch(t *testing.T) { dataCh <- data }() - testConsul.SetKVString(t, "test-kv-list/prefix/foo", "new-bar") + testConsul.SetKVString(t, fmt.Sprintf("test-kv-list/prefix/foo?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace), "new-bar") select { case err := <-errCh: @@ -368,28 +380,52 @@ func TestKVListQuery_Fetch(t *testing.T) { assert.Equal(t, exp, act) } - }) + }, t, "fires_changes") } func TestKVListQuery_String(t *testing.T) { - cases := []struct { + type testCase struct { name string i string exp string - }{ - { - "prefix", - "prefix", - "kv.list(prefix)", - }, - { - "dc", - "prefix@dc1", - "kv.list(prefix@dc1)", - }, } + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + return []interface{}{ + testCase{ + "prefix", + "prefix", + "kv.list(prefix)", + }, + testCase{ + "dc", + "prefix@dc1", + "kv.list(prefix@dc1)", + }, + testCase{ + "dc_partition", + fmt.Sprintf("prefix?partition=%s@dc1", tenancy.Partition), + fmt.Sprintf("kv.list(prefix@dc1@partition=%s)", tenancy.Partition), + }, + testCase{ + "dc_namespace", + fmt.Sprintf("prefix?ns=%s@dc1", tenancy.Namespace), + fmt.Sprintf("kv.list(prefix@dc1@ns=%s)", tenancy.Namespace), + }, + testCase{ + "dc_partition_namespace", + fmt.Sprintf("prefix?partition=%s&ns=%s@dc1", tenancy.Partition, tenancy.Namespace), + fmt.Sprintf("kv.list(prefix@dc1@partition=%s@ns=%s)", tenancy.Partition, tenancy.Namespace), + }, + testCase{ + "partition_namespace", + fmt.Sprintf("prefix?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace), + fmt.Sprintf("kv.list(prefix@partition=%s@ns=%s)", tenancy.Partition, tenancy.Namespace), + }, + } + }) - for i, tc := range cases { + for i, test := range cases { + tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { d, err := NewKVListQuery(tc.i) if err != nil { diff --git a/test/tenancy_helper.go b/test/tenancy_helper.go new file mode 100644 index 000000000..048e00f03 --- /dev/null +++ b/test/tenancy_helper.go @@ -0,0 +1,145 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package test + +import ( + "fmt" + "strings" + "sync" + "testing" + + "github.com/hashicorp/consul/api" + "github.com/hashicorp/consul/proto-public/pbresource" +) + +type TenancyHelper struct { + once sync.Once + isConsulEnterprise bool + consulClient *api.Client +} + +func NewTenancyHelper(consulClient *api.Client) (*TenancyHelper, error) { + t := &TenancyHelper{ + consulClient: consulClient, + } + + err := t.init() + if err != nil { + return nil, err + } + + return t, nil +} + +// TestTenancies returns a list of tenancies which represent +// the namespace and partition combinations that can be used in unit tests +func (t *TenancyHelper) TestTenancies() []*pbresource.Tenancy { + tenancies := []*pbresource.Tenancy{ + t.Tenancy("default.default"), + } + + if t.isConsulEnterprise { + tenancies = append(tenancies, t.Tenancy("default.bar"), t.Tenancy("foo.default"), t.Tenancy("foo.bar")) + } + + return tenancies +} + +// Tenancy constructs a pbresource.Tenancy from a concise string representation +// suitable for use in unit tests. +// +// - "" : partition="" namespace="" +// - "foo" : partition="foo" namespace="" +// - "foo.bar" : partition="foo" namespace="bar" +// - : partition="BAD" namespace="BAD" +func (t *TenancyHelper) Tenancy(s string) *pbresource.Tenancy { + parts := strings.Split(s, ".") + switch len(parts) { + case 0: + return &pbresource.Tenancy{} + case 1: + return &pbresource.Tenancy{ + Partition: parts[0], + } + case 2: + return &pbresource.Tenancy{ + Partition: parts[0], + Namespace: parts[1], + } + default: + return &pbresource.Tenancy{Partition: "BAD", Namespace: "BAD"} + } +} + +func (t *TenancyHelper) init() error { + var versionErr error + + t.once.Do(func() { + v, err := t.consulClient.Agent().Version() + if err != nil { + versionErr = err + return + } + + if version, ok := v["HumanVersion"].(string); ok { + // if type is string & the key is present + // then check whether the version contains "ent" + // example: "1.8.0+ent" is enterprise + // otherwise it is CE + t.isConsulEnterprise = strings.Contains(version, "ent") + } + }) + + return versionErr +} + +func (t *TenancyHelper) AppendTenancyInfo(name string, tenancy *pbresource.Tenancy) string { + return fmt.Sprintf("%s_%s_Namespace_%s_Partition", name, tenancy.Namespace, tenancy.Partition) +} + +func (t *TenancyHelper) RunWithTenancies(testFunc func(tenancy *pbresource.Tenancy), test *testing.T, testName string) { + for _, tenancy := range t.TestTenancies() { + test.Run(t.AppendTenancyInfo(testName, tenancy), func(t *testing.T) { + testFunc(tenancy) + }) + } +} + +func (t *TenancyHelper) GenerateTenancyTests(generationFunc func(tenancy *pbresource.Tenancy) []interface{}) []interface{} { + cases := make([]interface{}, 0) + for _, tenancy := range t.TestTenancies() { + cases = append(cases, generationFunc(tenancy)...) + } + return cases +} + +func (t *TenancyHelper) GenerateNonDefaultTenancyTests(generationFunc func(tenancy *pbresource.Tenancy) []interface{}) []interface{} { + cases := make([]interface{}, 0) + for _, tenancy := range t.TestTenancies() { + if tenancy.Partition != "default" || tenancy.Namespace != "default" { + cases = append(cases, generationFunc(tenancy)...) + } + } + return cases +} + +func (t *TenancyHelper) GenerateDefaultTenancyTests(generationFunc func(tenancy *pbresource.Tenancy) []interface{}) []interface{} { + cases := make([]interface{}, 0) + for _, tenancy := range t.TestTenancies() { + if tenancy.Partition == "default" && tenancy.Namespace == "default" { + cases = append(cases, generationFunc(tenancy)...) + } + } + return cases +} + +func (t *TenancyHelper) GetUniquePartitions() map[*api.Partition]interface{} { + partitions := make(map[*api.Partition]interface{}) + for _, tenancy := range t.TestTenancies() { + partitions[&api.Partition{ + Name: tenancy.Partition, + }] = nil + } + return partitions +} From 706640eebeeb9c77b25b4ea32afde394bfa68561 Mon Sep 17 00:00:00 2001 From: kumarkavish Date: Sun, 18 Feb 2024 18:54:16 +0530 Subject: [PATCH 02/12] [NET-7377] - CT ENT tests for all consul resource supporting partition and ns. - bug fix for map keys, keys did not contain partition and ns, so could have been overridden if same key in a different ns and partition is added as a dependency. --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a24660238..92059550d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,11 @@ NEW FEATURES: * Add support for listing Consul peers [NET-6966](https://hashicorp.atlassian.net/browse/NET-6966) +* Add ENT test cases such that all unit tests could run on different combinations of namespace and partition [NET-7377](https://hashicorp.atlassian.net/browse/NET-7377) BUG FIXES: * Fetch services query not overriding opts correctly [NET-7571](https://hashicorp.atlassian.net/browse/NET-7571) +* All consul resources which support namespace and partition should also have namespace and partition in the key represented by `String` function.[NET-7571](https://hashicorp.atlassian.net/browse/NET-7571) * Consul-template now correctly renders KVv2 secrets with `delete_version_after` set [NET-3777](https://hashicorp.atlassian.net/browse/NET-3777) ## v0.36.0 (January 3, 2024) From 3793e2643dfe3e9a50d79f6c8a197ea9650b10e7 Mon Sep 17 00:00:00 2001 From: kumarkavish Date: Sun, 18 Feb 2024 22:17:17 +0530 Subject: [PATCH 03/12] [NET-7377] - CT ENT tests for all consul resource supporting partition and ns. - bug fix for map keys, keys did not contain partition and ns, so could have been overridden if same key in a different ns and partition is added as a dependency. --- go.mod | 6 ++++++ go.sum | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/go.mod b/go.mod index 1060580b5..2ab6975c2 100644 --- a/go.mod +++ b/go.mod @@ -31,6 +31,7 @@ require ( require ( github.com/Masterminds/sprig/v3 v3.2.3 + github.com/hashicorp/consul/proto-public v0.5.1 github.com/hashicorp/vault/api/auth/kubernetes v0.5.0 golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 golang.org/x/text v0.14.0 @@ -43,6 +44,7 @@ require ( github.com/cenkalti/backoff/v3 v3.2.2 // indirect github.com/fatih/color v1.14.1 // indirect github.com/go-jose/go-jose/v3 v3.0.1 // indirect + github.com/golang/protobuf v1.5.3 // indirect github.com/google/uuid v1.3.0 // indirect github.com/gorilla/websocket v1.5.0 // indirect github.com/hashicorp/cronexpr v1.1.1 // indirect @@ -58,6 +60,7 @@ require ( github.com/huandu/xstrings v1.4.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.17 // indirect + github.com/miekg/dns v1.1.50 // indirect github.com/mitchellh/copystructure v1.2.0 // indirect github.com/mitchellh/reflectwalk v1.0.2 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect @@ -66,5 +69,8 @@ require ( github.com/spf13/cast v1.5.0 // indirect golang.org/x/net v0.17.0 // indirect golang.org/x/time v0.3.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect + google.golang.org/grpc v1.56.3 // indirect + google.golang.org/protobuf v1.31.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 13347e456..404f7c87d 100644 --- a/go.sum +++ b/go.sum @@ -54,12 +54,16 @@ github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7a github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= +github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.1 h1:gK4Kx5IaGY9CD5sPJ36FHiBJ6ZXl0kilRiiCj+jdYp4= github.com/google/btree v1.0.1/go.mod h1:xXMiIv4Fb/0kKde4SpL7qlzvu5cMJDRkFDxJfI9uaxA= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= @@ -336,9 +340,22 @@ golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190907020128-2ca718005c18/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.12.1-0.20230815132531-74c255bcf846 h1:Vve/L0v7CXXuxUmaMGIEK/dEeq7uiqb5qBgQrZzIE7E= +golang.org/x/tools v0.12.1-0.20230815132531-74c255bcf846/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc h1:XSJ8Vk1SWuNr8S18z1NZSziL0CPIXLCCMDOEFtHBOFc= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA= +google.golang.org/grpc v1.56.3 h1:8I4C0Yq1EjstUzUJzpcRVbuYA2mODtEmpWiQoN/b2nc= +google.golang.org/grpc v1.56.3/go.mod h1:I9bI3vqKfayGqPUAwGdOSu7kt6oIJLixfffKrpXqQ9s= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= From 5cf6b6af8348e2a99accac40f51a969b787eee42 Mon Sep 17 00:00:00 2001 From: kumarkavish Date: Sun, 18 Feb 2024 22:26:13 +0530 Subject: [PATCH 04/12] [NET-7377] - CT ENT tests for all consul resource supporting partition and ns. - bug fix for map keys, keys did not contain partition and ns, so could have been overridden if same key in a different ns and partition is added as a dependency. --- .golangci.yml | 1 + dependency/catalog_node_test.go | 3 ++- dependency/catalog_nodes_test.go | 3 ++- dependency/catalog_service_test.go | 3 ++- dependency/catalog_services_test.go | 3 ++- dependency/dependency_test.go | 2 +- dependency/health_service_test.go | 2 +- dependency/kv_get_test.go | 3 ++- dependency/kv_keys_test.go | 3 ++- dependency/kv_list_test.go | 3 ++- 10 files changed, 17 insertions(+), 9 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 14827cfe6..14d587e78 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -68,6 +68,7 @@ linters-settings: - github.com/hashicorp/consul-template - github.com/hashicorp/consul/api - github.com/hashicorp/consul/sdk/testutil + - github.com/hashicorp/consul/proto-public/pbresource - github.com/hashicorp/go-gatedio - github.com/hashicorp/go-hclog - github.com/hashicorp/go-multierror diff --git a/dependency/catalog_node_test.go b/dependency/catalog_node_test.go index 91362253a..dbf1d390e 100644 --- a/dependency/catalog_node_test.go +++ b/dependency/catalog_node_test.go @@ -5,10 +5,11 @@ package dependency import ( "fmt" - "github.com/hashicorp/consul/proto-public/pbresource" "testing" "github.com/stretchr/testify/assert" + + "github.com/hashicorp/consul/proto-public/pbresource" ) func TestNewCatalogNodeQuery(t *testing.T) { diff --git a/dependency/catalog_nodes_test.go b/dependency/catalog_nodes_test.go index dec5cceb0..4f62739bc 100644 --- a/dependency/catalog_nodes_test.go +++ b/dependency/catalog_nodes_test.go @@ -5,10 +5,11 @@ package dependency import ( "fmt" - "github.com/hashicorp/consul/proto-public/pbresource" "testing" "github.com/stretchr/testify/assert" + + "github.com/hashicorp/consul/proto-public/pbresource" ) func TestNewCatalogNodesQuery(t *testing.T) { diff --git a/dependency/catalog_service_test.go b/dependency/catalog_service_test.go index 56692323b..ac977b233 100644 --- a/dependency/catalog_service_test.go +++ b/dependency/catalog_service_test.go @@ -5,10 +5,11 @@ package dependency import ( "fmt" - "github.com/hashicorp/consul/proto-public/pbresource" "testing" "github.com/stretchr/testify/assert" + + "github.com/hashicorp/consul/proto-public/pbresource" ) func TestNewCatalogServiceQuery(t *testing.T) { diff --git a/dependency/catalog_services_test.go b/dependency/catalog_services_test.go index d7298d80e..a87a99ce3 100644 --- a/dependency/catalog_services_test.go +++ b/dependency/catalog_services_test.go @@ -5,10 +5,11 @@ package dependency import ( "fmt" - "github.com/hashicorp/consul/proto-public/pbresource" "testing" "github.com/stretchr/testify/assert" + + "github.com/hashicorp/consul/proto-public/pbresource" ) func TestNewCatalogServicesQuery(t *testing.T) { diff --git a/dependency/dependency_test.go b/dependency/dependency_test.go index 6f24eefb1..7d5815952 100644 --- a/dependency/dependency_test.go +++ b/dependency/dependency_test.go @@ -7,7 +7,6 @@ import ( "context" "encoding/json" "fmt" - "github.com/hashicorp/consul/proto-public/pbresource" "io" "log" "os" @@ -19,6 +18,7 @@ import ( "github.com/hashicorp/consul-template/test" "github.com/hashicorp/consul/api" + "github.com/hashicorp/consul/proto-public/pbresource" "github.com/hashicorp/consul/sdk/testutil" nomadapi "github.com/hashicorp/nomad/api" vapi "github.com/hashicorp/vault/api" diff --git a/dependency/health_service_test.go b/dependency/health_service_test.go index 00e655dfb..3d3369cb1 100644 --- a/dependency/health_service_test.go +++ b/dependency/health_service_test.go @@ -5,10 +5,10 @@ package dependency import ( "fmt" - "github.com/hashicorp/consul/proto-public/pbresource" "testing" "github.com/hashicorp/consul/api" + "github.com/hashicorp/consul/proto-public/pbresource" "github.com/stretchr/testify/assert" ) diff --git a/dependency/kv_get_test.go b/dependency/kv_get_test.go index 4e6247325..d7c668ef4 100644 --- a/dependency/kv_get_test.go +++ b/dependency/kv_get_test.go @@ -5,11 +5,12 @@ package dependency import ( "fmt" - "github.com/hashicorp/consul/proto-public/pbresource" "testing" "time" "github.com/stretchr/testify/assert" + + "github.com/hashicorp/consul/proto-public/pbresource" ) func TestNewKVGetQuery(t *testing.T) { diff --git a/dependency/kv_keys_test.go b/dependency/kv_keys_test.go index 883cda053..81755e10a 100644 --- a/dependency/kv_keys_test.go +++ b/dependency/kv_keys_test.go @@ -5,11 +5,12 @@ package dependency import ( "fmt" - "github.com/hashicorp/consul/proto-public/pbresource" "testing" "time" "github.com/stretchr/testify/assert" + + "github.com/hashicorp/consul/proto-public/pbresource" ) func TestNewKVKeysQuery(t *testing.T) { diff --git a/dependency/kv_list_test.go b/dependency/kv_list_test.go index b91c3ef3f..d613598d2 100644 --- a/dependency/kv_list_test.go +++ b/dependency/kv_list_test.go @@ -5,11 +5,12 @@ package dependency import ( "fmt" - "github.com/hashicorp/consul/proto-public/pbresource" "testing" "time" "github.com/stretchr/testify/assert" + + "github.com/hashicorp/consul/proto-public/pbresource" ) func TestNewKVListQuery(t *testing.T) { From 220bd4cf331c2dbe4e594926fc96d1f26e6cce21 Mon Sep 17 00:00:00 2001 From: kumarkavish Date: Sun, 18 Feb 2024 22:33:50 +0530 Subject: [PATCH 05/12] [NET-7377] - CT ENT tests for all consul resource supporting partition and ns. - bug fix for map keys, keys did not contain partition and ns, so could have been overridden if same key in a different ns and partition is added as a dependency. --- go.sum | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/go.sum b/go.sum index 404f7c87d..e8cecf596 100644 --- a/go.sum +++ b/go.sum @@ -260,11 +260,13 @@ github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1F github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190923035154-9ee001bba392/go.mod h1:/lpIB1dKB+9EgE3H3cr1v9wB50oz8l4C4h62xy7jSTY= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58= @@ -272,14 +274,19 @@ golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 h1:m64FZMko/V45gv0bNmrNYoDEq8U5YUhetc9cBWKS1TQ= golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63/go.mod h1:0v4NqG35kSWCMzLaMeX+IQrlSnVE/bqGSyC2cz/9Le8= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= +golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1/go.mod h1:9tjilg8BloeKEkVJvy7fQ90B1CfIiPueXVOjqfkSzI8= +golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= @@ -309,6 +316,8 @@ golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210303074136-134d130e1a04/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= From c9486b2a270046f9bf9fa5eaf115726a1647278f Mon Sep 17 00:00:00 2001 From: kumarkavish Date: Sun, 18 Feb 2024 22:53:09 +0530 Subject: [PATCH 06/12] [NET-7377] - CT ENT tests for all consul resource supporting partition and ns. - bug fix for map keys, keys did not contain partition and ns, so could have been overridden if same key in a different ns and partition is added as a dependency. --- .golangci.yml | 1 - dependency/catalog_node_test.go | 11 +++++----- dependency/catalog_nodes_test.go | 9 ++++---- dependency/catalog_service_test.go | 11 +++++----- dependency/catalog_services_test.go | 11 +++++----- dependency/dependency_test.go | 3 +-- dependency/health_service_test.go | 14 ++++++------- dependency/kv_get_test.go | 13 ++++++------ dependency/kv_keys_test.go | 13 ++++++------ dependency/kv_list_test.go | 13 ++++++------ go.mod | 5 ----- go.sum | 14 ------------- test/tenancy_helper.go | 32 ++++++++++++++++------------- 13 files changed, 63 insertions(+), 87 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 14d587e78..14827cfe6 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -68,7 +68,6 @@ linters-settings: - github.com/hashicorp/consul-template - github.com/hashicorp/consul/api - github.com/hashicorp/consul/sdk/testutil - - github.com/hashicorp/consul/proto-public/pbresource - github.com/hashicorp/go-gatedio - github.com/hashicorp/go-hclog - github.com/hashicorp/go-multierror diff --git a/dependency/catalog_node_test.go b/dependency/catalog_node_test.go index dbf1d390e..8e36fee8a 100644 --- a/dependency/catalog_node_test.go +++ b/dependency/catalog_node_test.go @@ -5,11 +5,10 @@ package dependency import ( "fmt" + "github.com/hashicorp/consul-template/test" "testing" "github.com/stretchr/testify/assert" - - "github.com/hashicorp/consul/proto-public/pbresource" ) func TestNewCatalogNodeQuery(t *testing.T) { @@ -19,7 +18,7 @@ func TestNewCatalogNodeQuery(t *testing.T) { exp *CatalogNodeQuery err bool } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("empty", tenancy), @@ -134,7 +133,7 @@ func TestCatalogNodeQuery_Fetch(t *testing.T) { i string exp *CatalogNode } - cases := tenancyHelper.GenerateNonDefaultTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateNonDefaultTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("local", tenancy), @@ -248,7 +247,7 @@ func TestCatalogNodeQuery_Fetch(t *testing.T) { } }) - cases = append(cases, tenancyHelper.GenerateDefaultTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases = append(cases, tenancyHelper.GenerateDefaultTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("local", tenancy), @@ -406,7 +405,7 @@ func TestCatalogNodeQuery_String(t *testing.T) { i string exp string } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("empty", tenancy), diff --git a/dependency/catalog_nodes_test.go b/dependency/catalog_nodes_test.go index 4f62739bc..9dc78ff22 100644 --- a/dependency/catalog_nodes_test.go +++ b/dependency/catalog_nodes_test.go @@ -5,11 +5,10 @@ package dependency import ( "fmt" + "github.com/hashicorp/consul-template/test" "testing" "github.com/stretchr/testify/assert" - - "github.com/hashicorp/consul/proto-public/pbresource" ) func TestNewCatalogNodesQuery(t *testing.T) { @@ -19,7 +18,7 @@ func TestNewCatalogNodesQuery(t *testing.T) { exp *CatalogNodesQuery err bool } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("empty", tenancy), @@ -145,7 +144,7 @@ func TestCatalogNodesQuery_Fetch(t *testing.T) { i string exp []*Node } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("all", tenancy), @@ -254,7 +253,7 @@ func TestCatalogNodesQuery_String(t *testing.T) { i string exp string } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("empty", tenancy), diff --git a/dependency/catalog_service_test.go b/dependency/catalog_service_test.go index ac977b233..4f3d3c668 100644 --- a/dependency/catalog_service_test.go +++ b/dependency/catalog_service_test.go @@ -5,11 +5,10 @@ package dependency import ( "fmt" + "github.com/hashicorp/consul-template/test" "testing" "github.com/stretchr/testify/assert" - - "github.com/hashicorp/consul/proto-public/pbresource" ) func TestNewCatalogServiceQuery(t *testing.T) { @@ -20,7 +19,7 @@ func TestNewCatalogServiceQuery(t *testing.T) { err bool } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("empty", tenancy), @@ -190,7 +189,7 @@ func TestCatalogServiceQuery_Fetch(t *testing.T) { i string exp []*CatalogService } - cases := tenancyHelper.GenerateDefaultTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateDefaultTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("consul", tenancy), @@ -242,7 +241,7 @@ func TestCatalogServiceQuery_Fetch(t *testing.T) { } }) - cases = append(cases, tenancyHelper.GenerateNonDefaultTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases = append(cases, tenancyHelper.GenerateNonDefaultTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("consul", tenancy), @@ -315,7 +314,7 @@ func TestCatalogServiceQuery_String(t *testing.T) { i string exp string } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("name", tenancy), diff --git a/dependency/catalog_services_test.go b/dependency/catalog_services_test.go index a87a99ce3..88169b6b3 100644 --- a/dependency/catalog_services_test.go +++ b/dependency/catalog_services_test.go @@ -5,11 +5,10 @@ package dependency import ( "fmt" + "github.com/hashicorp/consul-template/test" "testing" "github.com/stretchr/testify/assert" - - "github.com/hashicorp/consul/proto-public/pbresource" ) func TestNewCatalogServicesQuery(t *testing.T) { @@ -19,7 +18,7 @@ func TestNewCatalogServicesQuery(t *testing.T) { exp *CatalogServicesQuery err bool } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("empty", tenancy), @@ -110,7 +109,7 @@ func TestCatalogServicesQuery_Fetch(t *testing.T) { exp []*CatalogSnippet err bool } - cases := tenancyHelper.GenerateDefaultTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateDefaultTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("all", tenancy), @@ -143,7 +142,7 @@ func TestCatalogServicesQuery_Fetch(t *testing.T) { } }) - cases = append(cases, tenancyHelper.GenerateNonDefaultTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases = append(cases, tenancyHelper.GenerateNonDefaultTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("all", tenancy), @@ -229,7 +228,7 @@ func TestCatalogServicesQuery_String(t *testing.T) { i string exp string } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("empty", tenancy), diff --git a/dependency/dependency_test.go b/dependency/dependency_test.go index 7d5815952..097e03e3c 100644 --- a/dependency/dependency_test.go +++ b/dependency/dependency_test.go @@ -18,7 +18,6 @@ import ( "github.com/hashicorp/consul-template/test" "github.com/hashicorp/consul/api" - "github.com/hashicorp/consul/proto-public/pbresource" "github.com/hashicorp/consul/sdk/testutil" nomadapi "github.com/hashicorp/nomad/api" vapi "github.com/hashicorp/vault/api" @@ -241,7 +240,7 @@ func (c *ClientSet) createConsulTestResources() error { return nil } -func (c *ClientSet) createConsulPeerings(tenancy *pbresource.Tenancy) error { +func (c *ClientSet) createConsulPeerings(tenancy *test.Tenancy) error { generateReq := api.PeeringGenerateTokenRequest{PeerName: "foo", Partition: tenancy.Partition} _, _, err := c.consul.client.Peerings().GenerateToken(context.Background(), generateReq, &api.WriteOptions{}) if err != nil { diff --git a/dependency/health_service_test.go b/dependency/health_service_test.go index 3d3369cb1..3c58ca6e1 100644 --- a/dependency/health_service_test.go +++ b/dependency/health_service_test.go @@ -5,10 +5,10 @@ package dependency import ( "fmt" + "github.com/hashicorp/consul-template/test" "testing" "github.com/hashicorp/consul/api" - "github.com/hashicorp/consul/proto-public/pbresource" "github.com/stretchr/testify/assert" ) @@ -19,7 +19,7 @@ func TestNewHealthServiceQuery(t *testing.T) { exp *HealthServiceQuery err bool } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("empty", tenancy), @@ -264,7 +264,7 @@ func TestHealthConnectServiceQuery_Fetch(t *testing.T) { in string exp []*HealthService } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("connect-service", tenancy), @@ -350,7 +350,7 @@ func TestHealthServiceQuery_Fetch(t *testing.T) { i string exp []*HealthService } - cases := tenancyHelper.GenerateDefaultTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateDefaultTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("consul", tenancy), @@ -482,7 +482,7 @@ func TestHealthServiceQuery_Fetch(t *testing.T) { } }) - cases = append(cases, tenancyHelper.GenerateNonDefaultTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases = append(cases, tenancyHelper.GenerateNonDefaultTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("consul", tenancy), @@ -651,7 +651,7 @@ func TestHealthServiceQuery_String(t *testing.T) { i string exp string } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("name", tenancy), @@ -750,7 +750,7 @@ func TestHealthServiceQueryConnect_String(t *testing.T) { in string exp string } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("name", tenancy), diff --git a/dependency/kv_get_test.go b/dependency/kv_get_test.go index d7c668ef4..d8cef6277 100644 --- a/dependency/kv_get_test.go +++ b/dependency/kv_get_test.go @@ -5,12 +5,11 @@ package dependency import ( "fmt" + "github.com/hashicorp/consul-template/test" "testing" "time" "github.com/stretchr/testify/assert" - - "github.com/hashicorp/consul/proto-public/pbresource" ) func TestNewKVGetQuery(t *testing.T) { @@ -20,7 +19,7 @@ func TestNewKVGetQuery(t *testing.T) { exp *KVGetQuery err bool } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("empty", tenancy), @@ -210,7 +209,7 @@ func TestKVGetQuery_Fetch(t *testing.T) { exp interface{} } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("exists", tenancy), @@ -247,7 +246,7 @@ func TestKVGetQuery_Fetch(t *testing.T) { }) } - tenancyHelper.RunWithTenancies(func(tenancy *pbresource.Tenancy) { + tenancyHelper.RunWithTenancies(func(tenancy *test.Tenancy) { d, err := NewKVGetQuery(fmt.Sprintf("test-kv-get/key?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace)) if err != nil { t.Fatal(err) @@ -284,7 +283,7 @@ func TestKVGetQuery_Fetch(t *testing.T) { } }, t, "stops") - tenancyHelper.RunWithTenancies(func(tenancy *pbresource.Tenancy) { + tenancyHelper.RunWithTenancies(func(tenancy *test.Tenancy) { d, err := NewKVGetQuery(fmt.Sprintf("test-kv-get/key?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace)) if err != nil { t.Fatal(err) @@ -323,7 +322,7 @@ func TestKVGetQuery_String(t *testing.T) { i string exp string } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("key", tenancy), diff --git a/dependency/kv_keys_test.go b/dependency/kv_keys_test.go index 81755e10a..866f4df65 100644 --- a/dependency/kv_keys_test.go +++ b/dependency/kv_keys_test.go @@ -5,12 +5,11 @@ package dependency import ( "fmt" + "github.com/hashicorp/consul-template/test" "testing" "time" "github.com/stretchr/testify/assert" - - "github.com/hashicorp/consul/proto-public/pbresource" ) func TestNewKVKeysQuery(t *testing.T) { @@ -20,7 +19,7 @@ func TestNewKVKeysQuery(t *testing.T) { exp *KVKeysQuery err bool } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("empty", tenancy), @@ -225,7 +224,7 @@ func TestKVKeysQuery_Fetch(t *testing.T) { exp []string } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("exists", tenancy), @@ -270,7 +269,7 @@ func TestKVKeysQuery_Fetch(t *testing.T) { }) } - tenancyHelper.RunWithTenancies(func(tenancy *pbresource.Tenancy) { + tenancyHelper.RunWithTenancies(func(tenancy *test.Tenancy) { d, err := NewKVKeysQuery(fmt.Sprintf("test-kv-keys/prefix?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace)) if err != nil { t.Fatal(err) @@ -307,7 +306,7 @@ func TestKVKeysQuery_Fetch(t *testing.T) { } }, t, "stops") - tenancyHelper.RunWithTenancies(func(tenancy *pbresource.Tenancy) { + tenancyHelper.RunWithTenancies(func(tenancy *test.Tenancy) { d, err := NewKVKeysQuery(fmt.Sprintf("test-kv-keys/prefix?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace)) if err != nil { t.Fatal(err) @@ -352,7 +351,7 @@ func TestKVKeysQuery_String(t *testing.T) { i string exp string } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("prefix", tenancy), diff --git a/dependency/kv_list_test.go b/dependency/kv_list_test.go index d613598d2..c44433b66 100644 --- a/dependency/kv_list_test.go +++ b/dependency/kv_list_test.go @@ -5,12 +5,11 @@ package dependency import ( "fmt" + "github.com/hashicorp/consul-template/test" "testing" "time" "github.com/stretchr/testify/assert" - - "github.com/hashicorp/consul/proto-public/pbresource" ) func TestNewKVListQuery(t *testing.T) { @@ -20,7 +19,7 @@ func TestNewKVListQuery(t *testing.T) { exp *KVListQuery err bool } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("empty", tenancy), @@ -225,7 +224,7 @@ func TestKVListQuery_Fetch(t *testing.T) { exp []*KeyPair } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ "exists", @@ -299,7 +298,7 @@ func TestKVListQuery_Fetch(t *testing.T) { }) } - tenancyHelper.RunWithTenancies(func(tenancy *pbresource.Tenancy) { + tenancyHelper.RunWithTenancies(func(tenancy *test.Tenancy) { d, err := NewKVListQuery(fmt.Sprintf("test-kv-list/prefix?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace)) if err != nil { t.Fatal(err) @@ -336,7 +335,7 @@ func TestKVListQuery_Fetch(t *testing.T) { } }, t, "stops") - tenancyHelper.RunWithTenancies(func(tenancy *pbresource.Tenancy) { + tenancyHelper.RunWithTenancies(func(tenancy *test.Tenancy) { d, err := NewKVListQuery(fmt.Sprintf("test-kv-list/prefix?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace)) if err != nil { t.Fatal(err) @@ -390,7 +389,7 @@ func TestKVListQuery_String(t *testing.T) { i string exp string } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *pbresource.Tenancy) []interface{} { + cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ "prefix", diff --git a/go.mod b/go.mod index 2ab6975c2..2c7dc50b3 100644 --- a/go.mod +++ b/go.mod @@ -31,7 +31,6 @@ require ( require ( github.com/Masterminds/sprig/v3 v3.2.3 - github.com/hashicorp/consul/proto-public v0.5.1 github.com/hashicorp/vault/api/auth/kubernetes v0.5.0 golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 golang.org/x/text v0.14.0 @@ -44,7 +43,6 @@ require ( github.com/cenkalti/backoff/v3 v3.2.2 // indirect github.com/fatih/color v1.14.1 // indirect github.com/go-jose/go-jose/v3 v3.0.1 // indirect - github.com/golang/protobuf v1.5.3 // indirect github.com/google/uuid v1.3.0 // indirect github.com/gorilla/websocket v1.5.0 // indirect github.com/hashicorp/cronexpr v1.1.1 // indirect @@ -69,8 +67,5 @@ require ( github.com/spf13/cast v1.5.0 // indirect golang.org/x/net v0.17.0 // indirect golang.org/x/time v0.3.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect - google.golang.org/grpc v1.56.3 // indirect - google.golang.org/protobuf v1.31.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index e8cecf596..902520140 100644 --- a/go.sum +++ b/go.sum @@ -54,16 +54,12 @@ github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7a github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= -github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.1 h1:gK4Kx5IaGY9CD5sPJ36FHiBJ6ZXl0kilRiiCj+jdYp4= github.com/google/btree v1.0.1/go.mod h1:xXMiIv4Fb/0kKde4SpL7qlzvu5cMJDRkFDxJfI9uaxA= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= @@ -179,7 +175,6 @@ github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPn github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/miekg/dns v1.1.26/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso= -github.com/miekg/dns v1.1.41 h1:WMszZWJG0XmzbK9FEmzH2TVcqYzFesusSIB41b8KHxY= github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJysuI= github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= github.com/mitchellh/cli v1.1.0/go.mod h1:xcISNoH86gajksDmfB23e/pu+B+GeFRMYmoHXxx3xhI= @@ -357,14 +352,6 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc h1:XSJ8Vk1SWuNr8S18z1NZSziL0CPIXLCCMDOEFtHBOFc= -google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA= -google.golang.org/grpc v1.56.3 h1:8I4C0Yq1EjstUzUJzpcRVbuYA2mODtEmpWiQoN/b2nc= -google.golang.org/grpc v1.56.3/go.mod h1:I9bI3vqKfayGqPUAwGdOSu7kt6oIJLixfffKrpXqQ9s= -google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= -google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= @@ -378,6 +365,5 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/test/tenancy_helper.go b/test/tenancy_helper.go index 048e00f03..0688f3547 100644 --- a/test/tenancy_helper.go +++ b/test/tenancy_helper.go @@ -10,9 +10,13 @@ import ( "testing" "github.com/hashicorp/consul/api" - "github.com/hashicorp/consul/proto-public/pbresource" ) +type Tenancy struct { + Partition string + Namespace string +} + type TenancyHelper struct { once sync.Once isConsulEnterprise bool @@ -34,8 +38,8 @@ func NewTenancyHelper(consulClient *api.Client) (*TenancyHelper, error) { // TestTenancies returns a list of tenancies which represent // the namespace and partition combinations that can be used in unit tests -func (t *TenancyHelper) TestTenancies() []*pbresource.Tenancy { - tenancies := []*pbresource.Tenancy{ +func (t *TenancyHelper) TestTenancies() []*Tenancy { + tenancies := []*Tenancy{ t.Tenancy("default.default"), } @@ -46,29 +50,29 @@ func (t *TenancyHelper) TestTenancies() []*pbresource.Tenancy { return tenancies } -// Tenancy constructs a pbresource.Tenancy from a concise string representation +// Tenancy constructs a Tenancy from a concise string representation // suitable for use in unit tests. // // - "" : partition="" namespace="" // - "foo" : partition="foo" namespace="" // - "foo.bar" : partition="foo" namespace="bar" // - : partition="BAD" namespace="BAD" -func (t *TenancyHelper) Tenancy(s string) *pbresource.Tenancy { +func (t *TenancyHelper) Tenancy(s string) *Tenancy { parts := strings.Split(s, ".") switch len(parts) { case 0: - return &pbresource.Tenancy{} + return &Tenancy{} case 1: - return &pbresource.Tenancy{ + return &Tenancy{ Partition: parts[0], } case 2: - return &pbresource.Tenancy{ + return &Tenancy{ Partition: parts[0], Namespace: parts[1], } default: - return &pbresource.Tenancy{Partition: "BAD", Namespace: "BAD"} + return &Tenancy{Partition: "BAD", Namespace: "BAD"} } } @@ -94,11 +98,11 @@ func (t *TenancyHelper) init() error { return versionErr } -func (t *TenancyHelper) AppendTenancyInfo(name string, tenancy *pbresource.Tenancy) string { +func (t *TenancyHelper) AppendTenancyInfo(name string, tenancy *Tenancy) string { return fmt.Sprintf("%s_%s_Namespace_%s_Partition", name, tenancy.Namespace, tenancy.Partition) } -func (t *TenancyHelper) RunWithTenancies(testFunc func(tenancy *pbresource.Tenancy), test *testing.T, testName string) { +func (t *TenancyHelper) RunWithTenancies(testFunc func(tenancy *Tenancy), test *testing.T, testName string) { for _, tenancy := range t.TestTenancies() { test.Run(t.AppendTenancyInfo(testName, tenancy), func(t *testing.T) { testFunc(tenancy) @@ -106,7 +110,7 @@ func (t *TenancyHelper) RunWithTenancies(testFunc func(tenancy *pbresource.Tenan } } -func (t *TenancyHelper) GenerateTenancyTests(generationFunc func(tenancy *pbresource.Tenancy) []interface{}) []interface{} { +func (t *TenancyHelper) GenerateTenancyTests(generationFunc func(tenancy *Tenancy) []interface{}) []interface{} { cases := make([]interface{}, 0) for _, tenancy := range t.TestTenancies() { cases = append(cases, generationFunc(tenancy)...) @@ -114,7 +118,7 @@ func (t *TenancyHelper) GenerateTenancyTests(generationFunc func(tenancy *pbreso return cases } -func (t *TenancyHelper) GenerateNonDefaultTenancyTests(generationFunc func(tenancy *pbresource.Tenancy) []interface{}) []interface{} { +func (t *TenancyHelper) GenerateNonDefaultTenancyTests(generationFunc func(tenancy *Tenancy) []interface{}) []interface{} { cases := make([]interface{}, 0) for _, tenancy := range t.TestTenancies() { if tenancy.Partition != "default" || tenancy.Namespace != "default" { @@ -124,7 +128,7 @@ func (t *TenancyHelper) GenerateNonDefaultTenancyTests(generationFunc func(tenan return cases } -func (t *TenancyHelper) GenerateDefaultTenancyTests(generationFunc func(tenancy *pbresource.Tenancy) []interface{}) []interface{} { +func (t *TenancyHelper) GenerateDefaultTenancyTests(generationFunc func(tenancy *Tenancy) []interface{}) []interface{} { cases := make([]interface{}, 0) for _, tenancy := range t.TestTenancies() { if tenancy.Partition == "default" && tenancy.Namespace == "default" { From 0a289fad61f87fe18b487d6d7f4574c089afbdaf Mon Sep 17 00:00:00 2001 From: kumarkavish Date: Sun, 18 Feb 2024 23:02:13 +0530 Subject: [PATCH 07/12] [NET-7377] - CT ENT tests for all consul resource supporting partition and ns. - bug fix for map keys, keys did not contain partition and ns, so could have been overridden if same key in a different ns and partition is added as a dependency. --- dependency/catalog_node_test.go | 48 ++++++++--------- dependency/catalog_nodes_test.go | 48 ++++++++--------- dependency/catalog_service_test.go | 36 ++++++------- dependency/consul_common_test.go | 2 +- dependency/health_service_test.go | 84 +++++++++++++++--------------- 5 files changed, 109 insertions(+), 109 deletions(-) diff --git a/dependency/catalog_node_test.go b/dependency/catalog_node_test.go index 8e36fee8a..41934cb29 100644 --- a/dependency/catalog_node_test.go +++ b/dependency/catalog_node_test.go @@ -140,15 +140,15 @@ func TestCatalogNodeQuery_Fetch(t *testing.T) { "", &CatalogNode{ Node: &Node{ - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", TaggedAddresses: map[string]string{ - //"lan": "127.0.0.1", - //"wan": "127.0.0.1", + "lan": "127.0.0.1", + "wan": "127.0.0.1", }, Meta: map[string]string{ - //"consul-network-segment": "", + "consul-network-segment": "", }, }, Services: []*CatalogNodeService{ @@ -195,15 +195,15 @@ func TestCatalogNodeQuery_Fetch(t *testing.T) { fmt.Sprintf("%s?partition=%s&ns=%s", testConsul.Config.NodeName, tenancy.Partition, tenancy.Namespace), &CatalogNode{ Node: &Node{ - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", TaggedAddresses: map[string]string{ - //"lan": "127.0.0.1", - //"wan": "127.0.0.1", + "lan": "127.0.0.1", + "wan": "127.0.0.1", }, Meta: map[string]string{ - //"consul-network-segment": "", + "consul-network-segment": "", }, }, Services: []*CatalogNodeService{ @@ -254,15 +254,15 @@ func TestCatalogNodeQuery_Fetch(t *testing.T) { "", &CatalogNode{ Node: &Node{ - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", TaggedAddresses: map[string]string{ - //"lan": "127.0.0.1", - //"wan": "127.0.0.1", + "lan": "127.0.0.1", + "wan": "127.0.0.1", }, Meta: map[string]string{ - //"consul-network-segment": "", + "consul-network-segment": "", }, }, Services: []*CatalogNodeService{ @@ -309,15 +309,15 @@ func TestCatalogNodeQuery_Fetch(t *testing.T) { fmt.Sprintf("%s?partition=%s&ns=%s", testConsul.Config.NodeName, tenancy.Partition, tenancy.Namespace), &CatalogNode{ Node: &Node{ - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", TaggedAddresses: map[string]string{ - //"lan": "127.0.0.1", - //"wan": "127.0.0.1", + "lan": "127.0.0.1", + "wan": "127.0.0.1", }, Meta: map[string]string{ - //"consul-network-segment": "", + "consul-network-segment": "", }, }, Services: []*CatalogNodeService{ diff --git a/dependency/catalog_nodes_test.go b/dependency/catalog_nodes_test.go index 9dc78ff22..6ee4c07bc 100644 --- a/dependency/catalog_nodes_test.go +++ b/dependency/catalog_nodes_test.go @@ -151,15 +151,15 @@ func TestCatalogNodesQuery_Fetch(t *testing.T) { "", []*Node{ { - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", TaggedAddresses: map[string]string{ - //"lan": "127.0.0.1", - //"wan": "127.0.0.1", + "lan": "127.0.0.1", + "wan": "127.0.0.1", }, Meta: map[string]string{ - //"consul-network-segment": "", + "consul-network-segment": "", }, }, }, @@ -169,15 +169,15 @@ func TestCatalogNodesQuery_Fetch(t *testing.T) { fmt.Sprintf("?partition=%s&ns=%s@dc1", tenancy.Partition, tenancy.Namespace), []*Node{ { - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", TaggedAddresses: map[string]string{ - //"lan": "127.0.0.1", - //"wan": "127.0.0.1", + "lan": "127.0.0.1", + "wan": "127.0.0.1", }, Meta: map[string]string{ - //"consul-network-segment": "", + "consul-network-segment": "", }, }, }, @@ -187,15 +187,15 @@ func TestCatalogNodesQuery_Fetch(t *testing.T) { fmt.Sprintf("?ns=%s@dc1", tenancy.Namespace), []*Node{ { - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", TaggedAddresses: map[string]string{ - //"lan": "127.0.0.1", - //"wan": "127.0.0.1", + "lan": "127.0.0.1", + "wan": "127.0.0.1", }, Meta: map[string]string{ - //"consul-network-segment": "", + "consul-network-segment": "", }, }, }, @@ -205,15 +205,15 @@ func TestCatalogNodesQuery_Fetch(t *testing.T) { fmt.Sprintf("?partition=%s@dc1", tenancy.Partition), []*Node{ { - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", TaggedAddresses: map[string]string{ - //"lan": "127.0.0.1", - //"wan": "127.0.0.1", + "lan": "127.0.0.1", + "wan": "127.0.0.1", }, Meta: map[string]string{ - //"consul-network-segment": "", + "consul-network-segment": "", }, }, }, diff --git a/dependency/catalog_service_test.go b/dependency/catalog_service_test.go index 4f3d3c668..f43ac92e4 100644 --- a/dependency/catalog_service_test.go +++ b/dependency/catalog_service_test.go @@ -196,15 +196,15 @@ func TestCatalogServiceQuery_Fetch(t *testing.T) { "consul", []*CatalogService{ { - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", TaggedAddresses: map[string]string{ - //"lan": "127.0.0.1", - //"wan": "127.0.0.1", + "lan": "127.0.0.1", + "wan": "127.0.0.1", }, NodeMeta: map[string]string{ - //"consul-network-segment": "", + "consul-network-segment": "", }, ServiceID: "consul", ServiceName: "consul", @@ -220,15 +220,15 @@ func TestCatalogServiceQuery_Fetch(t *testing.T) { "service-meta-default-default", []*CatalogService{ { - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", TaggedAddresses: map[string]string{ - //"lan": "127.0.0.1", - //"wan": "127.0.0.1", + "lan": "127.0.0.1", + "wan": "127.0.0.1", }, NodeMeta: map[string]string{ - //"consul-network-segment": "", + "consul-network-segment": "", }, ServiceID: "service-meta-default-default", ServiceName: "service-meta-default-default", @@ -253,15 +253,15 @@ func TestCatalogServiceQuery_Fetch(t *testing.T) { fmt.Sprintf("service-meta-%s-%s?ns=%s&partition=%s", tenancy.Partition, tenancy.Namespace, tenancy.Namespace, tenancy.Partition), []*CatalogService{ { - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", TaggedAddresses: map[string]string{ - //"lan": "127.0.0.1", - //"wan": "127.0.0.1", + "lan": "127.0.0.1", + "wan": "127.0.0.1", }, NodeMeta: map[string]string{ - //"consul-network-segment": "", + "consul-network-segment": "", }, ServiceID: fmt.Sprintf("service-meta-%s-%s", tenancy.Partition, tenancy.Namespace), ServiceName: fmt.Sprintf("service-meta-%s-%s", tenancy.Partition, tenancy.Namespace), diff --git a/dependency/consul_common_test.go b/dependency/consul_common_test.go index 5f156c36c..06527d3df 100644 --- a/dependency/consul_common_test.go +++ b/dependency/consul_common_test.go @@ -21,7 +21,7 @@ func filterVersionMeta(meta map[string]string) map[string]string { filteredMeta := []string{ "raft_version", "serf_protocol_current", "serf_protocol_min", "serf_protocol_max", "version", - "non_voter", "read_replica", "grpc_port", "grpc_tls_port", "consul-version", "consul-network-segment", + "non_voter", "read_replica", "grpc_port", "grpc_tls_port", "consul-version", } return filter(meta, filteredMeta) } diff --git a/dependency/health_service_test.go b/dependency/health_service_test.go index 3c58ca6e1..de32d613f 100644 --- a/dependency/health_service_test.go +++ b/dependency/health_service_test.go @@ -278,8 +278,8 @@ func TestHealthConnectServiceQuery_Fetch(t *testing.T) { Address: "127.0.0.1", NodeAddress: "127.0.0.1", Tags: ServiceTags([]string{}), - NodeMeta: map[string]string{ - //"consul-network-segment": "", + NodeMeta: map[string]string{ + "consul-network-segment": "", }, Weights: api.AgentWeights{ Passing: 1, @@ -357,14 +357,14 @@ func TestHealthServiceQuery_Fetch(t *testing.T) { "consul", []*HealthService{ { - Node: testConsul.Config.NodeName, - NodeAddress: testConsul.Config.Bind, + Node: testConsul.Config.NodeName, + NodeAddress: testConsul.Config.Bind, NodeTaggedAddresses: map[string]string{ - //"lan": "127.0.0.1", - //"wan": "127.0.0.1", + "lan": "127.0.0.1", + "wan": "127.0.0.1", }, NodeMeta: map[string]string{ - //"consul-network-segment": "", + "consul-network-segment": "", }, ServiceMeta: map[string]string{}, Address: testConsul.Config.Bind, @@ -390,14 +390,14 @@ func TestHealthServiceQuery_Fetch(t *testing.T) { "consul|warning,passing", []*HealthService{ { - Node: testConsul.Config.NodeName, - NodeAddress: testConsul.Config.Bind, + Node: testConsul.Config.NodeName, + NodeAddress: testConsul.Config.Bind, NodeTaggedAddresses: map[string]string{ - //"lan": "127.0.0.1", - //"wan": "127.0.0.1", + "lan": "127.0.0.1", + "wan": "127.0.0.1", }, NodeMeta: map[string]string{ - //"consul-network-segment": "", + "consul-network-segment": "", }, ServiceMeta: map[string]string{}, Address: testConsul.Config.Bind, @@ -418,14 +418,14 @@ func TestHealthServiceQuery_Fetch(t *testing.T) { "service-meta-default-default", []*HealthService{ { - Node: testConsul.Config.NodeName, - NodeAddress: testConsul.Config.Bind, + Node: testConsul.Config.NodeName, + NodeAddress: testConsul.Config.Bind, NodeTaggedAddresses: map[string]string{ - //"lan": "127.0.0.1", - //"wan": "127.0.0.1", + "lan": "127.0.0.1", + "wan": "127.0.0.1", }, NodeMeta: map[string]string{ - //"consul-network-segment": "", + "consul-network-segment": "", }, ServiceMeta: map[string]string{ "meta1": "value1", @@ -447,14 +447,14 @@ func TestHealthServiceQuery_Fetch(t *testing.T) { "service-taggedAddresses-default-default", []*HealthService{ { - Node: testConsul.Config.NodeName, - NodeAddress: testConsul.Config.Bind, + Node: testConsul.Config.NodeName, + NodeAddress: testConsul.Config.Bind, NodeTaggedAddresses: map[string]string{ - //"lan": "127.0.0.1", - //"wan": "127.0.0.1", + "lan": "127.0.0.1", + "wan": "127.0.0.1", }, NodeMeta: map[string]string{ - //"consul-network-segment": "", + "consul-network-segment": "", }, ServiceMeta: map[string]string{}, Address: testConsul.Config.Bind, @@ -489,14 +489,14 @@ func TestHealthServiceQuery_Fetch(t *testing.T) { "consul", []*HealthService{ { - Node: testConsul.Config.NodeName, - NodeAddress: testConsul.Config.Bind, + Node: testConsul.Config.NodeName, + NodeAddress: testConsul.Config.Bind, NodeTaggedAddresses: map[string]string{ - //"lan": "127.0.0.1", - //"wan": "127.0.0.1", + "lan": "127.0.0.1", + "wan": "127.0.0.1", }, NodeMeta: map[string]string{ - //"consul-network-segment": "", + "consul-network-segment": "", }, ServiceMeta: map[string]string{}, Address: testConsul.Config.Bind, @@ -522,14 +522,14 @@ func TestHealthServiceQuery_Fetch(t *testing.T) { "consul|warning,passing", []*HealthService{ { - Node: testConsul.Config.NodeName, - NodeAddress: testConsul.Config.Bind, + Node: testConsul.Config.NodeName, + NodeAddress: testConsul.Config.Bind, NodeTaggedAddresses: map[string]string{ - //"lan": "127.0.0.1", - //"wan": "127.0.0.1", + "lan": "127.0.0.1", + "wan": "127.0.0.1", }, NodeMeta: map[string]string{ - //"consul-network-segment": "", + "consul-network-segment": "", }, ServiceMeta: map[string]string{}, Address: testConsul.Config.Bind, @@ -550,14 +550,14 @@ func TestHealthServiceQuery_Fetch(t *testing.T) { fmt.Sprintf("service-meta-%s-%s?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace, tenancy.Partition, tenancy.Namespace), []*HealthService{ { - Node: testConsul.Config.NodeName, - NodeAddress: testConsul.Config.Bind, + Node: testConsul.Config.NodeName, + NodeAddress: testConsul.Config.Bind, NodeTaggedAddresses: map[string]string{ - //"lan": "127.0.0.1", - //"wan": "127.0.0.1", + "lan": "127.0.0.1", + "wan": "127.0.0.1", }, NodeMeta: map[string]string{ - //"consul-network-segment": "", + "consul-network-segment": "", }, ServiceMeta: map[string]string{ "meta1": "value1", @@ -579,14 +579,14 @@ func TestHealthServiceQuery_Fetch(t *testing.T) { fmt.Sprintf("service-taggedAddresses-%s-%s?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace, tenancy.Partition, tenancy.Namespace), []*HealthService{ { - Node: testConsul.Config.NodeName, - NodeAddress: testConsul.Config.Bind, + Node: testConsul.Config.NodeName, + NodeAddress: testConsul.Config.Bind, NodeTaggedAddresses: map[string]string{ - //"lan": "127.0.0.1", - //"wan": "127.0.0.1", + "lan": "127.0.0.1", + "wan": "127.0.0.1", }, NodeMeta: map[string]string{ - //"consul-network-segment": "", + "consul-network-segment": "", }, ServiceMeta: map[string]string{}, Address: testConsul.Config.Bind, From 9393a50a6f86bb756e71c66ecc723100417a72ff Mon Sep 17 00:00:00 2001 From: kumarkavish Date: Sun, 18 Feb 2024 23:13:26 +0530 Subject: [PATCH 08/12] [NET-7377] - CT ENT tests for all consul resource supporting partition and ns. - bug fix for map keys, keys did not contain partition and ns, so could have been overridden if same key in a different ns and partition is added as a dependency. --- dependency/catalog_node_test.go | 48 ++++++++--------- dependency/catalog_nodes_test.go | 48 ++++++++--------- dependency/catalog_service_test.go | 36 ++++++------- dependency/health_service_test.go | 84 +++++++++++++++--------------- 4 files changed, 108 insertions(+), 108 deletions(-) diff --git a/dependency/catalog_node_test.go b/dependency/catalog_node_test.go index 41934cb29..8e36fee8a 100644 --- a/dependency/catalog_node_test.go +++ b/dependency/catalog_node_test.go @@ -140,15 +140,15 @@ func TestCatalogNodeQuery_Fetch(t *testing.T) { "", &CatalogNode{ Node: &Node{ - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", TaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", }, Meta: map[string]string{ - "consul-network-segment": "", + //"consul-network-segment": "", }, }, Services: []*CatalogNodeService{ @@ -195,15 +195,15 @@ func TestCatalogNodeQuery_Fetch(t *testing.T) { fmt.Sprintf("%s?partition=%s&ns=%s", testConsul.Config.NodeName, tenancy.Partition, tenancy.Namespace), &CatalogNode{ Node: &Node{ - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", TaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", }, Meta: map[string]string{ - "consul-network-segment": "", + //"consul-network-segment": "", }, }, Services: []*CatalogNodeService{ @@ -254,15 +254,15 @@ func TestCatalogNodeQuery_Fetch(t *testing.T) { "", &CatalogNode{ Node: &Node{ - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", TaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", }, Meta: map[string]string{ - "consul-network-segment": "", + //"consul-network-segment": "", }, }, Services: []*CatalogNodeService{ @@ -309,15 +309,15 @@ func TestCatalogNodeQuery_Fetch(t *testing.T) { fmt.Sprintf("%s?partition=%s&ns=%s", testConsul.Config.NodeName, tenancy.Partition, tenancy.Namespace), &CatalogNode{ Node: &Node{ - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", TaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", }, Meta: map[string]string{ - "consul-network-segment": "", + //"consul-network-segment": "", }, }, Services: []*CatalogNodeService{ diff --git a/dependency/catalog_nodes_test.go b/dependency/catalog_nodes_test.go index 6ee4c07bc..9dc78ff22 100644 --- a/dependency/catalog_nodes_test.go +++ b/dependency/catalog_nodes_test.go @@ -151,15 +151,15 @@ func TestCatalogNodesQuery_Fetch(t *testing.T) { "", []*Node{ { - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", TaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", }, Meta: map[string]string{ - "consul-network-segment": "", + //"consul-network-segment": "", }, }, }, @@ -169,15 +169,15 @@ func TestCatalogNodesQuery_Fetch(t *testing.T) { fmt.Sprintf("?partition=%s&ns=%s@dc1", tenancy.Partition, tenancy.Namespace), []*Node{ { - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", TaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", }, Meta: map[string]string{ - "consul-network-segment": "", + //"consul-network-segment": "", }, }, }, @@ -187,15 +187,15 @@ func TestCatalogNodesQuery_Fetch(t *testing.T) { fmt.Sprintf("?ns=%s@dc1", tenancy.Namespace), []*Node{ { - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", TaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", }, Meta: map[string]string{ - "consul-network-segment": "", + //"consul-network-segment": "", }, }, }, @@ -205,15 +205,15 @@ func TestCatalogNodesQuery_Fetch(t *testing.T) { fmt.Sprintf("?partition=%s@dc1", tenancy.Partition), []*Node{ { - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", TaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", }, Meta: map[string]string{ - "consul-network-segment": "", + //"consul-network-segment": "", }, }, }, diff --git a/dependency/catalog_service_test.go b/dependency/catalog_service_test.go index f43ac92e4..4f3d3c668 100644 --- a/dependency/catalog_service_test.go +++ b/dependency/catalog_service_test.go @@ -196,15 +196,15 @@ func TestCatalogServiceQuery_Fetch(t *testing.T) { "consul", []*CatalogService{ { - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", TaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", }, NodeMeta: map[string]string{ - "consul-network-segment": "", + //"consul-network-segment": "", }, ServiceID: "consul", ServiceName: "consul", @@ -220,15 +220,15 @@ func TestCatalogServiceQuery_Fetch(t *testing.T) { "service-meta-default-default", []*CatalogService{ { - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", TaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", }, NodeMeta: map[string]string{ - "consul-network-segment": "", + //"consul-network-segment": "", }, ServiceID: "service-meta-default-default", ServiceName: "service-meta-default-default", @@ -253,15 +253,15 @@ func TestCatalogServiceQuery_Fetch(t *testing.T) { fmt.Sprintf("service-meta-%s-%s?ns=%s&partition=%s", tenancy.Partition, tenancy.Namespace, tenancy.Namespace, tenancy.Partition), []*CatalogService{ { - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", TaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", }, NodeMeta: map[string]string{ - "consul-network-segment": "", + //"consul-network-segment": "", }, ServiceID: fmt.Sprintf("service-meta-%s-%s", tenancy.Partition, tenancy.Namespace), ServiceName: fmt.Sprintf("service-meta-%s-%s", tenancy.Partition, tenancy.Namespace), diff --git a/dependency/health_service_test.go b/dependency/health_service_test.go index de32d613f..3c58ca6e1 100644 --- a/dependency/health_service_test.go +++ b/dependency/health_service_test.go @@ -278,8 +278,8 @@ func TestHealthConnectServiceQuery_Fetch(t *testing.T) { Address: "127.0.0.1", NodeAddress: "127.0.0.1", Tags: ServiceTags([]string{}), - NodeMeta: map[string]string{ - "consul-network-segment": "", + NodeMeta: map[string]string{ + //"consul-network-segment": "", }, Weights: api.AgentWeights{ Passing: 1, @@ -357,14 +357,14 @@ func TestHealthServiceQuery_Fetch(t *testing.T) { "consul", []*HealthService{ { - Node: testConsul.Config.NodeName, - NodeAddress: testConsul.Config.Bind, + Node: testConsul.Config.NodeName, + NodeAddress: testConsul.Config.Bind, NodeTaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", }, NodeMeta: map[string]string{ - "consul-network-segment": "", + //"consul-network-segment": "", }, ServiceMeta: map[string]string{}, Address: testConsul.Config.Bind, @@ -390,14 +390,14 @@ func TestHealthServiceQuery_Fetch(t *testing.T) { "consul|warning,passing", []*HealthService{ { - Node: testConsul.Config.NodeName, - NodeAddress: testConsul.Config.Bind, + Node: testConsul.Config.NodeName, + NodeAddress: testConsul.Config.Bind, NodeTaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", }, NodeMeta: map[string]string{ - "consul-network-segment": "", + //"consul-network-segment": "", }, ServiceMeta: map[string]string{}, Address: testConsul.Config.Bind, @@ -418,14 +418,14 @@ func TestHealthServiceQuery_Fetch(t *testing.T) { "service-meta-default-default", []*HealthService{ { - Node: testConsul.Config.NodeName, - NodeAddress: testConsul.Config.Bind, + Node: testConsul.Config.NodeName, + NodeAddress: testConsul.Config.Bind, NodeTaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", }, NodeMeta: map[string]string{ - "consul-network-segment": "", + //"consul-network-segment": "", }, ServiceMeta: map[string]string{ "meta1": "value1", @@ -447,14 +447,14 @@ func TestHealthServiceQuery_Fetch(t *testing.T) { "service-taggedAddresses-default-default", []*HealthService{ { - Node: testConsul.Config.NodeName, - NodeAddress: testConsul.Config.Bind, + Node: testConsul.Config.NodeName, + NodeAddress: testConsul.Config.Bind, NodeTaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", }, NodeMeta: map[string]string{ - "consul-network-segment": "", + //"consul-network-segment": "", }, ServiceMeta: map[string]string{}, Address: testConsul.Config.Bind, @@ -489,14 +489,14 @@ func TestHealthServiceQuery_Fetch(t *testing.T) { "consul", []*HealthService{ { - Node: testConsul.Config.NodeName, - NodeAddress: testConsul.Config.Bind, + Node: testConsul.Config.NodeName, + NodeAddress: testConsul.Config.Bind, NodeTaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", }, NodeMeta: map[string]string{ - "consul-network-segment": "", + //"consul-network-segment": "", }, ServiceMeta: map[string]string{}, Address: testConsul.Config.Bind, @@ -522,14 +522,14 @@ func TestHealthServiceQuery_Fetch(t *testing.T) { "consul|warning,passing", []*HealthService{ { - Node: testConsul.Config.NodeName, - NodeAddress: testConsul.Config.Bind, + Node: testConsul.Config.NodeName, + NodeAddress: testConsul.Config.Bind, NodeTaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", }, NodeMeta: map[string]string{ - "consul-network-segment": "", + //"consul-network-segment": "", }, ServiceMeta: map[string]string{}, Address: testConsul.Config.Bind, @@ -550,14 +550,14 @@ func TestHealthServiceQuery_Fetch(t *testing.T) { fmt.Sprintf("service-meta-%s-%s?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace, tenancy.Partition, tenancy.Namespace), []*HealthService{ { - Node: testConsul.Config.NodeName, - NodeAddress: testConsul.Config.Bind, + Node: testConsul.Config.NodeName, + NodeAddress: testConsul.Config.Bind, NodeTaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", }, NodeMeta: map[string]string{ - "consul-network-segment": "", + //"consul-network-segment": "", }, ServiceMeta: map[string]string{ "meta1": "value1", @@ -579,14 +579,14 @@ func TestHealthServiceQuery_Fetch(t *testing.T) { fmt.Sprintf("service-taggedAddresses-%s-%s?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace, tenancy.Partition, tenancy.Namespace), []*HealthService{ { - Node: testConsul.Config.NodeName, - NodeAddress: testConsul.Config.Bind, + Node: testConsul.Config.NodeName, + NodeAddress: testConsul.Config.Bind, NodeTaggedAddresses: map[string]string{ - "lan": "127.0.0.1", - "wan": "127.0.0.1", + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", }, NodeMeta: map[string]string{ - "consul-network-segment": "", + //"consul-network-segment": "", }, ServiceMeta: map[string]string{}, Address: testConsul.Config.Bind, From d9cb747ffbaca48b77779c658678870ff3d87602 Mon Sep 17 00:00:00 2001 From: kumarkavish Date: Sun, 18 Feb 2024 23:35:51 +0530 Subject: [PATCH 09/12] [NET-7377] - CT ENT tests for all consul resource supporting partition and ns. - bug fix for map keys, keys did not contain partition and ns, so could have been overridden if same key in a different ns and partition is added as a dependency. --- dependency/consul_common_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dependency/consul_common_test.go b/dependency/consul_common_test.go index 06527d3df..9444d5f95 100644 --- a/dependency/consul_common_test.go +++ b/dependency/consul_common_test.go @@ -22,6 +22,7 @@ func filterVersionMeta(meta map[string]string) map[string]string { "raft_version", "serf_protocol_current", "serf_protocol_min", "serf_protocol_max", "version", "non_voter", "read_replica", "grpc_port", "grpc_tls_port", "consul-version", + "consul-network-segment", } return filter(meta, filteredMeta) } @@ -29,6 +30,6 @@ func filterVersionMeta(meta map[string]string) map[string]string { // filterAddresses filters out consul >1.7 ipv4/ipv6 specific entries // from TaggedAddresses entries on nodes, catlog and health services. func filterAddresses(addrs map[string]string) map[string]string { - ipvKeys := []string{"lan_ipv4", "wan_ipv4", "lan_ipv6", "wan_ipv6"} + ipvKeys := []string{"lan_ipv4", "wan_ipv4", "lan_ipv6", "wan_ipv6", "lan", "wan"} return filter(addrs, ipvKeys) } From 12ae445c07470b9d9a9e2d8fa4250aa882360e48 Mon Sep 17 00:00:00 2001 From: kumarkavish Date: Mon, 19 Feb 2024 11:52:11 +0530 Subject: [PATCH 10/12] [NET-7377] - CT ENT tests for all consul resource supporting partition and ns. - bug fix for map keys, keys did not contain partition and ns, so could have been overridden if same key in a different ns and partition is added as a dependency. --- test/tenancy_helper.go | 6 +- test/tenancy_helper_test.go | 199 ++++++++++++++++++++++++++++++++++++ 2 files changed, 202 insertions(+), 3 deletions(-) create mode 100644 test/tenancy_helper_test.go diff --git a/test/tenancy_helper.go b/test/tenancy_helper.go index 0688f3547..376eecffb 100644 --- a/test/tenancy_helper.go +++ b/test/tenancy_helper.go @@ -138,10 +138,10 @@ func (t *TenancyHelper) GenerateDefaultTenancyTests(generationFunc func(tenancy return cases } -func (t *TenancyHelper) GetUniquePartitions() map[*api.Partition]interface{} { - partitions := make(map[*api.Partition]interface{}) +func (t *TenancyHelper) GetUniquePartitions() map[api.Partition]interface{} { + partitions := make(map[api.Partition]interface{}) for _, tenancy := range t.TestTenancies() { - partitions[&api.Partition{ + partitions[api.Partition{ Name: tenancy.Partition, }] = nil } diff --git a/test/tenancy_helper_test.go b/test/tenancy_helper_test.go new file mode 100644 index 000000000..632611047 --- /dev/null +++ b/test/tenancy_helper_test.go @@ -0,0 +1,199 @@ +package test + +import "testing" + +func TestGetUniquePartitions(t *testing.T) { + cases := []struct { + name string + isConsulEnterprise bool + expectedPartitions int + err bool + }{ + {name: "CE", isConsulEnterprise: false, expectedPartitions: 1, err: false}, + {name: "ENT", isConsulEnterprise: true, expectedPartitions: 2, err: false}, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + tenancyHelper := &TenancyHelper{isConsulEnterprise: c.isConsulEnterprise} + partitions := tenancyHelper.GetUniquePartitions() + if len(partitions) != c.expectedPartitions && !c.err { + t.Fatalf("expected %d partitions, got %d", c.expectedPartitions, len(partitions)) + } + }) + } +} + +func TestAppendTenancyInfo(t *testing.T) { + cases := []struct { + name string + namespace string + partition string + expected string + }{ + {name: "empty", namespace: "", partition: "", expected: "empty__Namespace__Partition"}, + {name: "namespace", namespace: "foo", partition: "", expected: "namespace_foo_Namespace__Partition"}, + {name: "partition", namespace: "", partition: "bar", expected: "partition__Namespace_bar_Partition"}, + {name: "both", namespace: "foo", partition: "bar", expected: "both_foo_Namespace_bar_Partition"}, + } + + tenancyHelper := &TenancyHelper{} + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + tenancy := &Tenancy{Namespace: c.namespace, Partition: c.partition} + result := tenancyHelper.AppendTenancyInfo(c.name, tenancy) + if result != c.expected { + t.Fatalf("expected %q, got %q", c.expected, result) + } + }) + } +} + +func TestTenancy(t *testing.T) { + cases := []struct { + name string + input string + expected *Tenancy + }{ + {name: "empty", input: "", expected: &Tenancy{}}, + {name: "partition", input: "foo", expected: &Tenancy{Partition: "foo"}}, + {name: "both", input: "foo.bar", expected: &Tenancy{Partition: "foo", Namespace: "bar"}}, + {name: "bad", input: "foo.bar.baz", expected: &Tenancy{Partition: "BAD", Namespace: "BAD"}}, + } + + tenancyHelper := &TenancyHelper{} + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + result := tenancyHelper.Tenancy(c.input) + if *result != *c.expected { + t.Fatalf("expected %v, got %v", c.expected, result) + } + }) + } +} + +func TestTenancies(t *testing.T) { + cases := []struct { + name string + isConsulEnterprise bool + expectedTenancies int + }{ + {name: "CE", isConsulEnterprise: false, expectedTenancies: 1}, + {name: "ENT", isConsulEnterprise: true, expectedTenancies: 4}, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + tenancyHelper := &TenancyHelper{isConsulEnterprise: c.isConsulEnterprise} + tenancies := tenancyHelper.TestTenancies() + if len(tenancies) != c.expectedTenancies { + t.Fatalf("expected %d tenancies, got %d", c.expectedTenancies, len(tenancies)) + } + }) + } +} + +func TestGenerateTenancyTests(t *testing.T) { + type fakeTest struct { + name string + } + cases := []struct { + name string + isConsulEnterprise bool + expectedTests int + input func(tenancy *Tenancy) []interface{} + }{ + {name: "CE", isConsulEnterprise: false, expectedTests: 2, input: func(tenancy *Tenancy) []interface{} { + return []interface{}{ + fakeTest{name: "CE1"}, + fakeTest{name: "CE2"}, + } + }}, + {name: "ENT", isConsulEnterprise: true, expectedTests: 8, input: func(tenancy *Tenancy) []interface{} { + return []interface{}{ + fakeTest{name: "ENT1"}, + fakeTest{name: "ENT2"}, + } + }}, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + tenancyHelper := &TenancyHelper{isConsulEnterprise: c.isConsulEnterprise} + tests := tenancyHelper.GenerateTenancyTests(c.input) + if len(tests) != c.expectedTests { + t.Fatalf("expected %d tests, got %d", c.expectedTests, len(tests)) + } + }) + } +} + +func TestGenerateDefaultTenancyTests(t *testing.T) { + type fakeTest struct { + name string + } + cases := []struct { + name string + isConsulEnterprise bool + expectedTests int + input func(tenancy *Tenancy) []interface{} + }{ + {name: "CE", isConsulEnterprise: false, expectedTests: 2, input: func(tenancy *Tenancy) []interface{} { + return []interface{}{ + fakeTest{name: "CE1"}, + fakeTest{name: "CE2"}, + } + }}, + {name: "ENT", isConsulEnterprise: true, expectedTests: 2, input: func(tenancy *Tenancy) []interface{} { + return []interface{}{ + fakeTest{name: "ENT1"}, + fakeTest{name: "ENT2"}, + } + }}, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + tenancyHelper := &TenancyHelper{isConsulEnterprise: c.isConsulEnterprise} + tests := tenancyHelper.GenerateDefaultTenancyTests(c.input) + if len(tests) != c.expectedTests { + t.Fatalf("expected %d tests, got %d", c.expectedTests, len(tests)) + } + }) + } +} + +func TestGenerateNonDefaultTenancyTests(t *testing.T) { + type fakeTest struct { + name string + } + cases := []struct { + name string + isConsulEnterprise bool + expectedTests int + input func(tenancy *Tenancy) []interface{} + }{ + {name: "CE", isConsulEnterprise: false, expectedTests: 0, input: func(tenancy *Tenancy) []interface{} { + return []interface{}{ + fakeTest{name: "CE1"}, + fakeTest{name: "CE2"}, + } + }}, + {name: "ENT", isConsulEnterprise: true, expectedTests: 6, input: func(tenancy *Tenancy) []interface{} { + return []interface{}{ + fakeTest{name: "ENT1"}, + fakeTest{name: "ENT2"}, + } + }}, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + tenancyHelper := &TenancyHelper{isConsulEnterprise: c.isConsulEnterprise} + tests := tenancyHelper.GenerateNonDefaultTenancyTests(c.input) + if len(tests) != c.expectedTests { + t.Fatalf("expected %d tests, got %d", c.expectedTests, len(tests)) + } + }) + } +} From b5c1e98e741ebc8e4e86294f5f44112f6a3979f9 Mon Sep 17 00:00:00 2001 From: kumarkavish Date: Mon, 19 Feb 2024 14:22:00 +0530 Subject: [PATCH 11/12] [NET-7377] - CT ENT tests for all consul resource supporting partition and ns. - bug fix for map keys, keys did not contain partition and ns, so could have been overridden if same key in a different ns and partition is added as a dependency. --- dependency/catalog_node_test.go | 56 ------------------ dependency/catalog_nodes_test.go | 25 +++++++- dependency/dependency_test.go | 30 ++++++---- dependency/health_service_test.go | 29 ++++++++- dependency/kv_get_test.go | 49 ++++++++++++++-- dependency/kv_keys_test.go | 68 ++++++++++++++++++--- dependency/kv_list_test.go | 98 +++++++++++++++++++++++++++---- test/tenancy_helper.go | 4 ++ 8 files changed, 266 insertions(+), 93 deletions(-) diff --git a/dependency/catalog_node_test.go b/dependency/catalog_node_test.go index 8e36fee8a..75b8e0f89 100644 --- a/dependency/catalog_node_test.go +++ b/dependency/catalog_node_test.go @@ -304,62 +304,6 @@ func TestCatalogNodeQuery_Fetch(t *testing.T) { }, }, }, - testCase{ - tenancyHelper.AppendTenancyInfo("partition and ns", tenancy), - fmt.Sprintf("%s?partition=%s&ns=%s", testConsul.Config.NodeName, tenancy.Partition, tenancy.Namespace), - &CatalogNode{ - Node: &Node{ - Node: testConsul.Config.NodeName, - Address: testConsul.Config.Bind, - Datacenter: "dc1", - TaggedAddresses: map[string]string{ - //"lan": "127.0.0.1", - //"wan": "127.0.0.1", - }, - Meta: map[string]string{ - //"consul-network-segment": "", - }, - }, - Services: []*CatalogNodeService{ - { - ID: fmt.Sprintf("conn-enabled-service-%s-%s", tenancy.Partition, tenancy.Namespace), - Service: fmt.Sprintf("conn-enabled-service-%s-%s", tenancy.Partition, tenancy.Namespace), - Tags: ServiceTags([]string{}), - Meta: map[string]string{}, - Port: 12345, - }, - { - ID: fmt.Sprintf("conn-enabled-service-proxy-%s-%s", tenancy.Partition, tenancy.Namespace), - Service: fmt.Sprintf("conn-enabled-service-proxy-%s-%s", tenancy.Partition, tenancy.Namespace), - Tags: ServiceTags([]string{}), - Meta: map[string]string{}, - Port: 21999, - }, - { - ID: "consul", - Service: "consul", - Port: testConsul.Config.Ports.Server, - Tags: ServiceTags([]string{}), - Meta: map[string]string{}, - }, - { - - ID: fmt.Sprintf("service-meta-%s-%s", tenancy.Partition, tenancy.Namespace), - Service: fmt.Sprintf("service-meta-%s-%s", tenancy.Partition, tenancy.Namespace), - Tags: ServiceTags([]string{"tag1"}), - Meta: map[string]string{ - "meta1": "value1", - }, - }, - { - ID: fmt.Sprintf("service-taggedAddresses-%s-%s", tenancy.Partition, tenancy.Namespace), - Service: fmt.Sprintf("service-taggedAddresses-%s-%s", tenancy.Partition, tenancy.Namespace), - Tags: ServiceTags([]string{}), - Meta: map[string]string{}, - }, - }, - }, - }, testCase{ tenancyHelper.AppendTenancyInfo("unknown", tenancy), "not_a_real_node", diff --git a/dependency/catalog_nodes_test.go b/dependency/catalog_nodes_test.go index 9dc78ff22..e8b14ac94 100644 --- a/dependency/catalog_nodes_test.go +++ b/dependency/catalog_nodes_test.go @@ -144,7 +144,7 @@ func TestCatalogNodesQuery_Fetch(t *testing.T) { i string exp []*Node } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { + cases := tenancyHelper.GenerateNonDefaultTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("all", tenancy), @@ -221,6 +221,29 @@ func TestCatalogNodesQuery_Fetch(t *testing.T) { } }) + cases = append(cases, tenancyHelper.GenerateDefaultTenancyTests(func(tenancy *test.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("all", tenancy), + "", + []*Node{ + { + Node: testConsul.Config.NodeName, + Address: testConsul.Config.Bind, + Datacenter: "dc1", + TaggedAddresses: map[string]string{ + //"lan": "127.0.0.1", + //"wan": "127.0.0.1", + }, + Meta: map[string]string{ + //"consul-network-segment": "", + }, + }, + }, + }, + } + })...) + for i, test := range cases { tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { diff --git a/dependency/dependency_test.go b/dependency/dependency_test.go index 097e03e3c..08ff2a2d4 100644 --- a/dependency/dependency_test.go +++ b/dependency/dependency_test.go @@ -146,6 +146,12 @@ func (c *ClientSet) createConsulTestResources() error { } for _, tenancy := range tenancyHelper.TestTenancies() { + partition := "" + namespace := "" + if tenancyHelper.IsConsulEnterprise() { + partition = tenancy.Partition + namespace = tenancy.Namespace + } // service with meta data serviceMetaService := &api.AgentService{ ID: fmt.Sprintf("service-meta-%s-%s", tenancy.Partition, tenancy.Namespace), @@ -154,12 +160,12 @@ func (c *ClientSet) createConsulTestResources() error { Meta: map[string]string{ "meta1": "value1", }, - Namespace: tenancy.Namespace, - Partition: tenancy.Partition, + Partition: partition, + Namespace: namespace, } if _, err := catalog.Register(&api.CatalogRegistration{ Service: serviceMetaService, - Partition: tenancy.Partition, + Partition: partition, Node: node, Address: "127.0.0.1", }, nil); err != nil { @@ -179,12 +185,12 @@ func (c *ClientSet) createConsulTestResources() error { Port: 443, }, }, - Namespace: tenancy.Namespace, - Partition: tenancy.Partition, + Partition: partition, + Namespace: namespace, } if _, err := catalog.Register(&api.CatalogRegistration{ Service: serviceTaggedAddressesService, - Partition: tenancy.Partition, + Partition: partition, Node: node, Address: "127.0.0.1", }, nil); err != nil { @@ -197,8 +203,8 @@ func (c *ClientSet) createConsulTestResources() error { Service: fmt.Sprintf("conn-enabled-service-%s-%s", tenancy.Partition, tenancy.Namespace), Port: 12345, Connect: &api.AgentServiceConnect{}, - Namespace: tenancy.Namespace, - Partition: tenancy.Partition, + Partition: partition, + Namespace: namespace, } // this is based on what `consul connect proxy` command does at // consul/command/connect/proxy/register.go (register method) @@ -210,13 +216,13 @@ func (c *ClientSet) createConsulTestResources() error { Proxy: &api.AgentServiceConnectProxyConfig{ DestinationServiceName: fmt.Sprintf("conn-enabled-service-%s-%s", tenancy.Partition, tenancy.Namespace), }, - Namespace: tenancy.Namespace, - Partition: tenancy.Partition, + Partition: partition, + Namespace: namespace, } if _, err := catalog.Register(&api.CatalogRegistration{ Service: testService, - Partition: tenancy.Partition, + Partition: partition, Node: node, Address: "127.0.0.1", }, nil); err != nil { @@ -225,7 +231,7 @@ func (c *ClientSet) createConsulTestResources() error { if _, err := catalog.Register(&api.CatalogRegistration{ Service: testConnect, - Partition: tenancy.Partition, + Partition: partition, Node: node, Address: "127.0.0.1", }, nil); err != nil { diff --git a/dependency/health_service_test.go b/dependency/health_service_test.go index 3c58ca6e1..51abcc1c4 100644 --- a/dependency/health_service_test.go +++ b/dependency/health_service_test.go @@ -264,7 +264,7 @@ func TestHealthConnectServiceQuery_Fetch(t *testing.T) { in string exp []*HealthService } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { + cases := tenancyHelper.GenerateNonDefaultTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("connect-service", tenancy), @@ -313,6 +313,33 @@ func TestHealthConnectServiceQuery_Fetch(t *testing.T) { } }) + cases = append(cases, tenancyHelper.GenerateDefaultTenancyTests(func(tenancy *test.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("connect-service", tenancy), + "conn-enabled-service-default-default", + []*HealthService{ + { + Name: "conn-enabled-service-proxy-default-default", + ID: "conn-enabled-service-proxy-default-default", + Port: 21999, + Status: "passing", + Address: "127.0.0.1", + NodeAddress: "127.0.0.1", + Tags: ServiceTags([]string{}), + NodeMeta: map[string]string{ + //"consul-network-segment": "", + }, + Weights: api.AgentWeights{ + Passing: 1, + Warning: 1, + }, + }, + }, + }, + } + })...) + for i, test := range cases { tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { diff --git a/dependency/kv_get_test.go b/dependency/kv_get_test.go index d8cef6277..9c7a40bd3 100644 --- a/dependency/kv_get_test.go +++ b/dependency/kv_get_test.go @@ -199,8 +199,17 @@ func TestNewKVGetQuery(t *testing.T) { func TestKVGetQuery_Fetch(t *testing.T) { for _, tenancy := range tenancyHelper.TestTenancies() { - testConsul.SetKVString(t, fmt.Sprintf("test-kv-get/key?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace), fmt.Sprintf("value-%s-%s", tenancy.Partition, tenancy.Namespace)) - testConsul.SetKVString(t, fmt.Sprintf("test-kv-get/key_empty?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace), "") + key := fmt.Sprintf("test-kv-get/key") + if tenancyHelper.IsConsulEnterprise() { + key += fmt.Sprintf("?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace) + } + testConsul.SetKVString(t, key, fmt.Sprintf("value-%s-%s", tenancy.Partition, tenancy.Namespace)) + + emptyKey := fmt.Sprintf("test-kv-get/key_empty") + if tenancyHelper.IsConsulEnterprise() { + emptyKey += fmt.Sprintf("?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace) + } + testConsul.SetKVString(t, emptyKey, "") } type testCase struct { @@ -209,7 +218,7 @@ func TestKVGetQuery_Fetch(t *testing.T) { exp interface{} } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { + cases := tenancyHelper.GenerateNonDefaultTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("exists", tenancy), @@ -229,6 +238,26 @@ func TestKVGetQuery_Fetch(t *testing.T) { } }) + cases = append(cases, tenancyHelper.GenerateDefaultTenancyTests(func(tenancy *test.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("exists", tenancy), + fmt.Sprintf("test-kv-get/key"), + fmt.Sprintf("value-%s-%s", tenancy.Partition, tenancy.Namespace), + }, + testCase{ + tenancyHelper.AppendTenancyInfo("exists_empty_string", tenancy), + fmt.Sprintf("test-kv-get/key_empty"), + "", + }, + testCase{ + tenancyHelper.AppendTenancyInfo("no_exist", tenancy), + fmt.Sprintf("test-kv-get/not/a/real/key/like/ever"), + nil, + }, + } + })...) + for i, test := range cases { tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { @@ -247,7 +276,11 @@ func TestKVGetQuery_Fetch(t *testing.T) { } tenancyHelper.RunWithTenancies(func(tenancy *test.Tenancy) { - d, err := NewKVGetQuery(fmt.Sprintf("test-kv-get/key?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace)) + kvQuery := fmt.Sprintf("test-kv-get/key") + if tenancyHelper.IsConsulEnterprise() { + kvQuery += fmt.Sprintf("?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace) + } + d, err := NewKVGetQuery(kvQuery) if err != nil { t.Fatal(err) } @@ -284,7 +317,11 @@ func TestKVGetQuery_Fetch(t *testing.T) { }, t, "stops") tenancyHelper.RunWithTenancies(func(tenancy *test.Tenancy) { - d, err := NewKVGetQuery(fmt.Sprintf("test-kv-get/key?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace)) + kvQuery := fmt.Sprintf("test-kv-get/key") + if tenancyHelper.IsConsulEnterprise() { + kvQuery += fmt.Sprintf("?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace) + } + d, err := NewKVGetQuery(kvQuery) if err != nil { t.Fatal(err) } @@ -305,7 +342,7 @@ func TestKVGetQuery_Fetch(t *testing.T) { dataCh <- data }() - testConsul.SetKVString(t, fmt.Sprintf("test-kv-get/key?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace), "new-value") + testConsul.SetKVString(t, kvQuery, "new-value") select { case err := <-errCh: diff --git a/dependency/kv_keys_test.go b/dependency/kv_keys_test.go index 866f4df65..9aa817f0b 100644 --- a/dependency/kv_keys_test.go +++ b/dependency/kv_keys_test.go @@ -213,9 +213,23 @@ func TestNewKVKeysQuery(t *testing.T) { func TestKVKeysQuery_Fetch(t *testing.T) { for _, tenancy := range tenancyHelper.TestTenancies() { - testConsul.SetKVString(t, fmt.Sprintf("test-kv-keys/prefix/foo-%s-%s?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace, tenancy.Partition, tenancy.Namespace), "bar") - testConsul.SetKVString(t, fmt.Sprintf("test-kv-keys/prefix/zip-%s-%s?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace, tenancy.Partition, tenancy.Namespace), "zap") - testConsul.SetKVString(t, fmt.Sprintf("test-kv-keys/prefix/wave/ocean-%s-%s?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace, tenancy.Partition, tenancy.Namespace), "sleek") + fooKey := fmt.Sprintf("test-kv-keys/prefix/foo-%s-%s", tenancy.Partition, tenancy.Namespace) + if tenancyHelper.IsConsulEnterprise() { + fooKey += fmt.Sprintf("?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace) + } + testConsul.SetKVString(t, fooKey, "bar") + + zipKey := fmt.Sprintf("test-kv-keys/prefix/zip-%s-%s", tenancy.Partition, tenancy.Namespace) + if tenancyHelper.IsConsulEnterprise() { + zipKey += fmt.Sprintf("?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace) + } + testConsul.SetKVString(t, zipKey, "zap") + + oceanKey := fmt.Sprintf("test-kv-keys/prefix/wave/ocean-%s-%s", tenancy.Partition, tenancy.Namespace) + if tenancyHelper.IsConsulEnterprise() { + oceanKey += fmt.Sprintf("?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace) + } + testConsul.SetKVString(t, oceanKey, "sleek") } type testCase struct { @@ -224,7 +238,7 @@ func TestKVKeysQuery_Fetch(t *testing.T) { exp []string } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { + cases := tenancyHelper.GenerateNonDefaultTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("exists", tenancy), @@ -252,6 +266,34 @@ func TestKVKeysQuery_Fetch(t *testing.T) { } }) + cases = append(cases, tenancyHelper.GenerateDefaultTenancyTests(func(tenancy *test.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("exists", tenancy), + fmt.Sprintf("test-kv-keys/prefix"), + []string{ + fmt.Sprintf("foo-%s-%s", tenancy.Partition, tenancy.Namespace), + fmt.Sprintf("wave/ocean-%s-%s", tenancy.Partition, tenancy.Namespace), + fmt.Sprintf("zip-%s-%s", tenancy.Partition, tenancy.Namespace), + }, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("trailing", tenancy), + fmt.Sprintf("test-kv-keys/prefix/"), + []string{ + fmt.Sprintf("foo-%s-%s", tenancy.Partition, tenancy.Namespace), + fmt.Sprintf("wave/ocean-%s-%s", tenancy.Partition, tenancy.Namespace), + fmt.Sprintf("zip-%s-%s", tenancy.Partition, tenancy.Namespace), + }, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("no_exist", tenancy), + fmt.Sprintf("test-kv-keys/prefix/not/a/real/key/like/ever"), + []string{}, + }, + } + })...) + for i, test := range cases { tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { @@ -270,7 +312,11 @@ func TestKVKeysQuery_Fetch(t *testing.T) { } tenancyHelper.RunWithTenancies(func(tenancy *test.Tenancy) { - d, err := NewKVKeysQuery(fmt.Sprintf("test-kv-keys/prefix?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace)) + kvQuery := fmt.Sprintf("test-kv-keys/prefix") + if tenancyHelper.IsConsulEnterprise() { + kvQuery += fmt.Sprintf("?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace) + } + d, err := NewKVKeysQuery(kvQuery) if err != nil { t.Fatal(err) } @@ -307,7 +353,11 @@ func TestKVKeysQuery_Fetch(t *testing.T) { }, t, "stops") tenancyHelper.RunWithTenancies(func(tenancy *test.Tenancy) { - d, err := NewKVKeysQuery(fmt.Sprintf("test-kv-keys/prefix?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace)) + kvQuery := fmt.Sprintf("test-kv-keys/prefix") + if tenancyHelper.IsConsulEnterprise() { + kvQuery += fmt.Sprintf("?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace) + } + d, err := NewKVKeysQuery(kvQuery) if err != nil { t.Fatal(err) } @@ -328,7 +378,11 @@ func TestKVKeysQuery_Fetch(t *testing.T) { dataCh <- data }() - testConsul.SetKVString(t, fmt.Sprintf("test-kv-keys/prefix/zebra-%s-%s?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace, tenancy.Partition, tenancy.Namespace), "value") + zebraKey := fmt.Sprintf("test-kv-keys/prefix/zebra-%s-%s", tenancy.Partition, tenancy.Namespace) + if tenancyHelper.IsConsulEnterprise() { + zebraKey += fmt.Sprintf("?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace) + } + testConsul.SetKVString(t, zebraKey, "value") select { case err := <-errCh: diff --git a/dependency/kv_list_test.go b/dependency/kv_list_test.go index c44433b66..c8cfdfa03 100644 --- a/dependency/kv_list_test.go +++ b/dependency/kv_list_test.go @@ -213,9 +213,23 @@ func TestNewKVListQuery(t *testing.T) { func TestKVListQuery_Fetch(t *testing.T) { for _, tenancy := range tenancyHelper.TestTenancies() { - testConsul.SetKVString(t, fmt.Sprintf("test-kv-list/prefix/foo?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace), fmt.Sprintf("bar-%s-%s", tenancy.Partition, tenancy.Namespace)) - testConsul.SetKVString(t, fmt.Sprintf("test-kv-list/prefix/zip?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace), fmt.Sprintf("zap-%s-%s", tenancy.Partition, tenancy.Namespace)) - testConsul.SetKVString(t, fmt.Sprintf("test-kv-list/prefix/wave/ocean?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace), fmt.Sprintf("sleek-%s-%s", tenancy.Partition, tenancy.Namespace)) + fooKey := fmt.Sprintf("test-kv-list/prefix/foo") + if tenancyHelper.IsConsulEnterprise() { + fooKey += fmt.Sprintf("?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace) + } + testConsul.SetKVString(t, fooKey, fmt.Sprintf("bar-%s-%s", tenancy.Partition, tenancy.Namespace)) + + zipKey := fmt.Sprintf("test-kv-list/prefix/zip") + if tenancyHelper.IsConsulEnterprise() { + zipKey += fmt.Sprintf("?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace) + } + testConsul.SetKVString(t, zipKey, fmt.Sprintf("zap-%s-%s", tenancy.Partition, tenancy.Namespace)) + + oceanKey := fmt.Sprintf("test-kv-list/prefix/wave/ocean") + if tenancyHelper.IsConsulEnterprise() { + oceanKey += fmt.Sprintf("?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace) + } + testConsul.SetKVString(t, oceanKey, fmt.Sprintf("sleek-%s-%s", tenancy.Partition, tenancy.Namespace)) } type testCase struct { @@ -224,10 +238,10 @@ func TestKVListQuery_Fetch(t *testing.T) { exp []*KeyPair } - cases := tenancyHelper.GenerateTenancyTests(func(tenancy *test.Tenancy) []interface{} { + cases := tenancyHelper.GenerateNonDefaultTenancyTests(func(tenancy *test.Tenancy) []interface{} { return []interface{}{ testCase{ - "exists", + tenancyHelper.AppendTenancyInfo("exists", tenancy), fmt.Sprintf("test-kv-list/prefix?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace), []*KeyPair{ { @@ -248,7 +262,7 @@ func TestKVListQuery_Fetch(t *testing.T) { }, }, testCase{ - "trailing", + tenancyHelper.AppendTenancyInfo("trailing", tenancy), fmt.Sprintf("test-kv-list/prefix/?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace), []*KeyPair{ { @@ -269,13 +283,65 @@ func TestKVListQuery_Fetch(t *testing.T) { }, }, testCase{ - "no_exist", + tenancyHelper.AppendTenancyInfo("no_exist", tenancy), fmt.Sprintf("test-kv-list/not/a/real/prefix/like/ever?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace), []*KeyPair{}, }, } }) + cases = append(cases, tenancyHelper.GenerateDefaultTenancyTests(func(tenancy *test.Tenancy) []interface{} { + return []interface{}{ + testCase{ + tenancyHelper.AppendTenancyInfo("exists", tenancy), + fmt.Sprintf("test-kv-list/prefix"), + []*KeyPair{ + { + Path: "test-kv-list/prefix/foo", + Key: "foo", + Value: fmt.Sprintf("bar-%s-%s", tenancy.Partition, tenancy.Namespace), + }, + { + Path: "test-kv-list/prefix/wave/ocean", + Key: "wave/ocean", + Value: fmt.Sprintf("sleek-%s-%s", tenancy.Partition, tenancy.Namespace), + }, + { + Path: "test-kv-list/prefix/zip", + Key: "zip", + Value: fmt.Sprintf("zap-%s-%s", tenancy.Partition, tenancy.Namespace), + }, + }, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("trailing", tenancy), + fmt.Sprintf("test-kv-list/prefix/"), + []*KeyPair{ + { + Path: "test-kv-list/prefix/foo", + Key: "foo", + Value: fmt.Sprintf("bar-%s-%s", tenancy.Partition, tenancy.Namespace), + }, + { + Path: "test-kv-list/prefix/wave/ocean", + Key: "wave/ocean", + Value: fmt.Sprintf("sleek-%s-%s", tenancy.Partition, tenancy.Namespace), + }, + { + Path: "test-kv-list/prefix/zip", + Key: "zip", + Value: fmt.Sprintf("zap-%s-%s", tenancy.Partition, tenancy.Namespace), + }, + }, + }, + testCase{ + tenancyHelper.AppendTenancyInfo("no_exist", tenancy), + fmt.Sprintf("test-kv-list/not/a/real/prefix/like/ever?partition"), + []*KeyPair{}, + }, + } + })...) + for i, test := range cases { tc := test.(testCase) t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { @@ -299,7 +365,11 @@ func TestKVListQuery_Fetch(t *testing.T) { } tenancyHelper.RunWithTenancies(func(tenancy *test.Tenancy) { - d, err := NewKVListQuery(fmt.Sprintf("test-kv-list/prefix?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace)) + kvQuery := fmt.Sprintf("test-kv-list/prefix") + if tenancyHelper.IsConsulEnterprise() { + kvQuery += fmt.Sprintf("?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace) + } + d, err := NewKVListQuery(kvQuery) if err != nil { t.Fatal(err) } @@ -336,7 +406,11 @@ func TestKVListQuery_Fetch(t *testing.T) { }, t, "stops") tenancyHelper.RunWithTenancies(func(tenancy *test.Tenancy) { - d, err := NewKVListQuery(fmt.Sprintf("test-kv-list/prefix?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace)) + kvQuery := fmt.Sprintf("test-kv-list/prefix") + if tenancyHelper.IsConsulEnterprise() { + kvQuery += fmt.Sprintf("?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace) + } + d, err := NewKVListQuery(kvQuery) if err != nil { t.Fatal(err) } @@ -357,7 +431,11 @@ func TestKVListQuery_Fetch(t *testing.T) { dataCh <- data }() - testConsul.SetKVString(t, fmt.Sprintf("test-kv-list/prefix/foo?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace), "new-bar") + fooKey := fmt.Sprintf("test-kv-list/prefix/foo") + if tenancyHelper.IsConsulEnterprise() { + fooKey += fmt.Sprintf("?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace) + } + testConsul.SetKVString(t, fooKey, "new-bar") select { case err := <-errCh: diff --git a/test/tenancy_helper.go b/test/tenancy_helper.go index 376eecffb..056d26431 100644 --- a/test/tenancy_helper.go +++ b/test/tenancy_helper.go @@ -50,6 +50,10 @@ func (t *TenancyHelper) TestTenancies() []*Tenancy { return tenancies } +func (t *TenancyHelper) IsConsulEnterprise() bool { + return t.isConsulEnterprise +} + // Tenancy constructs a Tenancy from a concise string representation // suitable for use in unit tests. // From ccbe62ec4ad593af0c0f3f9e4a0199f20a48d617 Mon Sep 17 00:00:00 2001 From: kumarkavish Date: Mon, 19 Feb 2024 14:26:18 +0530 Subject: [PATCH 12/12] [NET-7377] - CT ENT tests for all consul resource supporting partition and ns. - bug fix for map keys, keys did not contain partition and ns, so could have been overridden if same key in a different ns and partition is added as a dependency. --- dependency/kv_get_test.go | 14 +++++++------- dependency/kv_keys_test.go | 10 +++++----- dependency/kv_list_test.go | 18 +++++++++--------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/dependency/kv_get_test.go b/dependency/kv_get_test.go index 9c7a40bd3..114e4e877 100644 --- a/dependency/kv_get_test.go +++ b/dependency/kv_get_test.go @@ -199,13 +199,13 @@ func TestNewKVGetQuery(t *testing.T) { func TestKVGetQuery_Fetch(t *testing.T) { for _, tenancy := range tenancyHelper.TestTenancies() { - key := fmt.Sprintf("test-kv-get/key") + key := "test-kv-get/key" if tenancyHelper.IsConsulEnterprise() { key += fmt.Sprintf("?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace) } testConsul.SetKVString(t, key, fmt.Sprintf("value-%s-%s", tenancy.Partition, tenancy.Namespace)) - emptyKey := fmt.Sprintf("test-kv-get/key_empty") + emptyKey := "test-kv-get/key_empty" if tenancyHelper.IsConsulEnterprise() { emptyKey += fmt.Sprintf("?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace) } @@ -242,17 +242,17 @@ func TestKVGetQuery_Fetch(t *testing.T) { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("exists", tenancy), - fmt.Sprintf("test-kv-get/key"), + "test-kv-get/key", fmt.Sprintf("value-%s-%s", tenancy.Partition, tenancy.Namespace), }, testCase{ tenancyHelper.AppendTenancyInfo("exists_empty_string", tenancy), - fmt.Sprintf("test-kv-get/key_empty"), + "test-kv-get/key_empty", "", }, testCase{ tenancyHelper.AppendTenancyInfo("no_exist", tenancy), - fmt.Sprintf("test-kv-get/not/a/real/key/like/ever"), + "test-kv-get/not/a/real/key/like/ever", nil, }, } @@ -276,7 +276,7 @@ func TestKVGetQuery_Fetch(t *testing.T) { } tenancyHelper.RunWithTenancies(func(tenancy *test.Tenancy) { - kvQuery := fmt.Sprintf("test-kv-get/key") + kvQuery := "test-kv-get/key" if tenancyHelper.IsConsulEnterprise() { kvQuery += fmt.Sprintf("?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace) } @@ -317,7 +317,7 @@ func TestKVGetQuery_Fetch(t *testing.T) { }, t, "stops") tenancyHelper.RunWithTenancies(func(tenancy *test.Tenancy) { - kvQuery := fmt.Sprintf("test-kv-get/key") + kvQuery := "test-kv-get/key" if tenancyHelper.IsConsulEnterprise() { kvQuery += fmt.Sprintf("?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace) } diff --git a/dependency/kv_keys_test.go b/dependency/kv_keys_test.go index 9aa817f0b..6f6174724 100644 --- a/dependency/kv_keys_test.go +++ b/dependency/kv_keys_test.go @@ -270,7 +270,7 @@ func TestKVKeysQuery_Fetch(t *testing.T) { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("exists", tenancy), - fmt.Sprintf("test-kv-keys/prefix"), + "test-kv-keys/prefix", []string{ fmt.Sprintf("foo-%s-%s", tenancy.Partition, tenancy.Namespace), fmt.Sprintf("wave/ocean-%s-%s", tenancy.Partition, tenancy.Namespace), @@ -279,7 +279,7 @@ func TestKVKeysQuery_Fetch(t *testing.T) { }, testCase{ tenancyHelper.AppendTenancyInfo("trailing", tenancy), - fmt.Sprintf("test-kv-keys/prefix/"), + "test-kv-keys/prefix/", []string{ fmt.Sprintf("foo-%s-%s", tenancy.Partition, tenancy.Namespace), fmt.Sprintf("wave/ocean-%s-%s", tenancy.Partition, tenancy.Namespace), @@ -288,7 +288,7 @@ func TestKVKeysQuery_Fetch(t *testing.T) { }, testCase{ tenancyHelper.AppendTenancyInfo("no_exist", tenancy), - fmt.Sprintf("test-kv-keys/prefix/not/a/real/key/like/ever"), + "test-kv-keys/prefix/not/a/real/key/like/ever", []string{}, }, } @@ -312,7 +312,7 @@ func TestKVKeysQuery_Fetch(t *testing.T) { } tenancyHelper.RunWithTenancies(func(tenancy *test.Tenancy) { - kvQuery := fmt.Sprintf("test-kv-keys/prefix") + kvQuery := "test-kv-keys/prefix" if tenancyHelper.IsConsulEnterprise() { kvQuery += fmt.Sprintf("?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace) } @@ -353,7 +353,7 @@ func TestKVKeysQuery_Fetch(t *testing.T) { }, t, "stops") tenancyHelper.RunWithTenancies(func(tenancy *test.Tenancy) { - kvQuery := fmt.Sprintf("test-kv-keys/prefix") + kvQuery := "test-kv-keys/prefix" if tenancyHelper.IsConsulEnterprise() { kvQuery += fmt.Sprintf("?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace) } diff --git a/dependency/kv_list_test.go b/dependency/kv_list_test.go index c8cfdfa03..7d6501a2e 100644 --- a/dependency/kv_list_test.go +++ b/dependency/kv_list_test.go @@ -213,19 +213,19 @@ func TestNewKVListQuery(t *testing.T) { func TestKVListQuery_Fetch(t *testing.T) { for _, tenancy := range tenancyHelper.TestTenancies() { - fooKey := fmt.Sprintf("test-kv-list/prefix/foo") + fooKey := "test-kv-list/prefix/foo" if tenancyHelper.IsConsulEnterprise() { fooKey += fmt.Sprintf("?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace) } testConsul.SetKVString(t, fooKey, fmt.Sprintf("bar-%s-%s", tenancy.Partition, tenancy.Namespace)) - zipKey := fmt.Sprintf("test-kv-list/prefix/zip") + zipKey := "test-kv-list/prefix/zip" if tenancyHelper.IsConsulEnterprise() { zipKey += fmt.Sprintf("?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace) } testConsul.SetKVString(t, zipKey, fmt.Sprintf("zap-%s-%s", tenancy.Partition, tenancy.Namespace)) - oceanKey := fmt.Sprintf("test-kv-list/prefix/wave/ocean") + oceanKey := "test-kv-list/prefix/wave/ocean" if tenancyHelper.IsConsulEnterprise() { oceanKey += fmt.Sprintf("?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace) } @@ -294,7 +294,7 @@ func TestKVListQuery_Fetch(t *testing.T) { return []interface{}{ testCase{ tenancyHelper.AppendTenancyInfo("exists", tenancy), - fmt.Sprintf("test-kv-list/prefix"), + "test-kv-list/prefix", []*KeyPair{ { Path: "test-kv-list/prefix/foo", @@ -315,7 +315,7 @@ func TestKVListQuery_Fetch(t *testing.T) { }, testCase{ tenancyHelper.AppendTenancyInfo("trailing", tenancy), - fmt.Sprintf("test-kv-list/prefix/"), + "test-kv-list/prefix/", []*KeyPair{ { Path: "test-kv-list/prefix/foo", @@ -336,7 +336,7 @@ func TestKVListQuery_Fetch(t *testing.T) { }, testCase{ tenancyHelper.AppendTenancyInfo("no_exist", tenancy), - fmt.Sprintf("test-kv-list/not/a/real/prefix/like/ever?partition"), + "test-kv-list/not/a/real/prefix/like/ever?partition", []*KeyPair{}, }, } @@ -365,7 +365,7 @@ func TestKVListQuery_Fetch(t *testing.T) { } tenancyHelper.RunWithTenancies(func(tenancy *test.Tenancy) { - kvQuery := fmt.Sprintf("test-kv-list/prefix") + kvQuery := "test-kv-list/prefix" if tenancyHelper.IsConsulEnterprise() { kvQuery += fmt.Sprintf("?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace) } @@ -406,7 +406,7 @@ func TestKVListQuery_Fetch(t *testing.T) { }, t, "stops") tenancyHelper.RunWithTenancies(func(tenancy *test.Tenancy) { - kvQuery := fmt.Sprintf("test-kv-list/prefix") + kvQuery := "test-kv-list/prefix" if tenancyHelper.IsConsulEnterprise() { kvQuery += fmt.Sprintf("?partition=%s&ns=%s", tenancy.Partition, tenancy.Namespace) } @@ -431,7 +431,7 @@ func TestKVListQuery_Fetch(t *testing.T) { dataCh <- data }() - fooKey := fmt.Sprintf("test-kv-list/prefix/foo") + fooKey := "test-kv-list/prefix/foo" if tenancyHelper.IsConsulEnterprise() { fooKey += fmt.Sprintf("?partition=%s&namespace=%s", tenancy.Partition, tenancy.Namespace) }