Merge pull request #8075 from hashicorp/expose-cli-testfix

Fix a CLI test failure with namespaces in enterprise
This commit is contained in:
Kyle Havlovitz 2020-06-09 15:25:45 -07:00 committed by GitHub
commit ea720c0724
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 7 deletions

View File

@ -6,6 +6,7 @@ import (
"strings" "strings"
"github.com/hashicorp/consul/agent" "github.com/hashicorp/consul/agent"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/flags" "github.com/hashicorp/consul/command/flags"
"github.com/hashicorp/consul/command/intention/create" "github.com/hashicorp/consul/command/intention/create"
@ -145,7 +146,7 @@ func (c *cmd) Run(args []string) int {
// Make sure the service isn't already exposed in this gateway // Make sure the service isn't already exposed in this gateway
for j, service := range listener.Services { for j, service := range listener.Services {
if service.Name == svc && service.Namespace == svcNamespace { if service.Name == svc && namespaceMatch(service.Namespace, svcNamespace) {
serviceIdx = j serviceIdx = j
c.UI.Output(fmt.Sprintf("Updating service definition for %q on listener with port %d", c.service, listener.Port)) c.UI.Output(fmt.Sprintf("Updating service definition for %q on listener with port %d", c.service, listener.Port))
break break
@ -220,6 +221,19 @@ func (c *cmd) Run(args []string) int {
return 0 return 0
} }
func namespaceMatch(a, b string) bool {
namespaceA := a
namespaceB := b
if namespaceA == "" {
namespaceA = structs.IntentionDefaultNamespace
}
if namespaceB == "" {
namespaceB = structs.IntentionDefaultNamespace
}
return namespaceA == namespaceB
}
func (c *cmd) Synopsis() string { func (c *cmd) Synopsis() string {
return synopsis return synopsis
} }

View File

@ -38,16 +38,19 @@ func TestConnectExpose(t *testing.T) {
// Make sure the config entry and intention have been created. // Make sure the config entry and intention have been created.
entry, _, err := client.ConfigEntries().Get(api.IngressGateway, "ingress", nil) entry, _, err := client.ConfigEntries().Get(api.IngressGateway, "ingress", nil)
require.NoError(err) require.NoError(err)
ns := entry.(*api.IngressGatewayConfigEntry).Namespace
expected := &api.IngressGatewayConfigEntry{ expected := &api.IngressGatewayConfigEntry{
Kind: api.IngressGateway, Kind: api.IngressGateway,
Name: "ingress", Name: "ingress",
Namespace: ns,
Listeners: []api.IngressListener{ Listeners: []api.IngressListener{
{ {
Port: 8888, Port: 8888,
Protocol: "tcp", Protocol: "tcp",
Services: []api.IngressService{ Services: []api.IngressService{
{ {
Name: "foo", Name: "foo",
Namespace: ns,
}, },
}, },
}, },
@ -86,7 +89,8 @@ func TestConnectExpose(t *testing.T) {
Protocol: "tcp", Protocol: "tcp",
Services: []api.IngressService{ Services: []api.IngressService{
{ {
Name: "foo", Name: "foo",
Namespace: ns,
}, },
}, },
}) })
@ -254,6 +258,7 @@ func TestConnectExpose_existingConfig(t *testing.T) {
entry, _, err := client.ConfigEntries().Get(api.IngressGateway, "ingress", nil) entry, _, err := client.ConfigEntries().Get(api.IngressGateway, "ingress", nil)
require.NoError(err) require.NoError(err)
entryConf := entry.(*api.IngressGatewayConfigEntry)
ingressConf.Listeners = append(ingressConf.Listeners, api.IngressListener{ ingressConf.Listeners = append(ingressConf.Listeners, api.IngressListener{
Port: 10000, Port: 10000,
Protocol: "tcp", Protocol: "tcp",
@ -263,6 +268,10 @@ func TestConnectExpose_existingConfig(t *testing.T) {
}, },
}, },
}) })
ingressConf.Namespace = entryConf.Namespace
for i, listener := range ingressConf.Listeners {
listener.Services[0].Namespace = entryConf.Listeners[i].Services[0].Namespace
}
ingressConf.CreateIndex = entry.GetCreateIndex() ingressConf.CreateIndex = entry.GetCreateIndex()
ingressConf.ModifyIndex = entry.GetModifyIndex() ingressConf.ModifyIndex = entry.GetModifyIndex()
require.Equal(ingressConf, entry) require.Equal(ingressConf, entry)
@ -292,9 +301,11 @@ func TestConnectExpose_existingConfig(t *testing.T) {
entry, _, err := client.ConfigEntries().Get(api.IngressGateway, "ingress", nil) entry, _, err := client.ConfigEntries().Get(api.IngressGateway, "ingress", nil)
require.NoError(err) require.NoError(err)
entryConf := entry.(*api.IngressGatewayConfigEntry)
ingressConf.Listeners[1].Services = append(ingressConf.Listeners[1].Services, api.IngressService{ ingressConf.Listeners[1].Services = append(ingressConf.Listeners[1].Services, api.IngressService{
Name: "zoo", Name: "zoo",
Hosts: []string{"foo.com", "foo.net"}, Namespace: entryConf.Listeners[1].Services[1].Namespace,
Hosts: []string{"foo.com", "foo.net"},
}) })
ingressConf.CreateIndex = entry.GetCreateIndex() ingressConf.CreateIndex = entry.GetCreateIndex()
ingressConf.ModifyIndex = entry.GetModifyIndex() ingressConf.ModifyIndex = entry.GetModifyIndex()