consul/command/connect/expose/expose_test.go

364 lines
9.1 KiB
Go
Raw Permalink Normal View History

// Copyright (c) HashiCorp, Inc.
[COMPLIANCE] License changes (#18443) * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2023-08-11 13:12:13 +00:00
// SPDX-License-Identifier: BUSL-1.1
2020-06-05 21:54:29 +00:00
package expose
import (
"testing"
"github.com/mitchellh/cli"
"github.com/stretchr/testify/require"
2020-06-05 21:54:29 +00:00
"github.com/hashicorp/consul/agent"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/testrpc"
)
func TestConnectExpose(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
}
2020-06-05 21:54:29 +00:00
t.Parallel()
a := agent.NewTestAgent(t, ``)
client := a.Client()
defer a.Shutdown()
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
{
ui := cli.NewMockUi()
c := New(ui)
args := []string{
"-http-addr=" + a.HTTPAddr(),
"-service=foo",
"-ingress-gateway=ingress",
"-port=8888",
"-protocol=tcp",
}
code := c.Run(args)
if code != 0 {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
}
}
// Make sure the config entry and intention have been created.
entry, _, err := client.ConfigEntries().Get(api.IngressGateway, "ingress", nil)
require.NoError(t, err)
ns := entry.(*api.IngressGatewayConfigEntry).Namespace
2021-11-12 21:59:29 +00:00
ap := entry.(*api.IngressGatewayConfigEntry).Partition
2020-06-05 21:54:29 +00:00
expected := &api.IngressGatewayConfigEntry{
Kind: api.IngressGateway,
Name: "ingress",
Namespace: ns,
Partition: ap,
2020-06-05 21:54:29 +00:00
Listeners: []api.IngressListener{
{
Port: 8888,
Protocol: "tcp",
Services: []api.IngressService{
{
Name: "foo",
Namespace: ns,
2021-11-08 20:22:10 +00:00
Partition: ap,
2020-06-05 21:54:29 +00:00
},
},
},
},
}
expected.CreateIndex = entry.GetCreateIndex()
expected.ModifyIndex = entry.GetModifyIndex()
require.Equal(t, expected, entry)
2020-06-05 21:54:29 +00:00
ixns, _, err := client.Connect().Intentions(nil)
require.NoError(t, err)
require.Len(t, ixns, 1)
require.Equal(t, "ingress", ixns[0].SourceName)
require.Equal(t, "foo", ixns[0].DestinationName)
2020-06-05 21:54:29 +00:00
// Run the command again with a different port, make sure the config entry
// is updated while intentions are unmodified.
2020-06-05 21:54:29 +00:00
{
ui := cli.NewMockUi()
c := New(ui)
args := []string{
"-http-addr=" + a.HTTPAddr(),
"-service=foo",
"-ingress-gateway=ingress",
"-port=7777",
"-protocol=tcp",
}
code := c.Run(args)
if code != 0 {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
}
expected.Listeners = append(expected.Listeners, api.IngressListener{
Port: 7777,
Protocol: "tcp",
Services: []api.IngressService{
{
Name: "foo",
Namespace: ns,
2021-11-08 20:22:10 +00:00
Partition: ap,
},
},
})
2020-06-05 21:54:29 +00:00
// Make sure the config entry/intention weren't affected.
entry, _, err = client.ConfigEntries().Get(api.IngressGateway, "ingress", nil)
require.NoError(t, err)
expected.ModifyIndex = entry.GetModifyIndex()
require.Equal(t, expected, entry)
2020-06-05 21:54:29 +00:00
ixns, _, err = client.Connect().Intentions(nil)
require.NoError(t, err)
require.Len(t, ixns, 1)
require.Equal(t, "ingress", ixns[0].SourceName)
require.Equal(t, "foo", ixns[0].DestinationName)
2020-06-05 21:54:29 +00:00
}
// Run the command again with a conflicting protocol, should exit with an error and
// cause no changes to config entry/intentions.
{
ui := cli.NewMockUi()
c := New(ui)
args := []string{
"-http-addr=" + a.HTTPAddr(),
"-service=bar",
"-ingress-gateway=ingress",
"-port=8888",
"-protocol=http",
}
code := c.Run(args)
if code != 1 {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
}
require.Contains(t, ui.ErrorWriter.String(), `conflicting protocol "tcp"`)
2020-06-05 21:54:29 +00:00
// Make sure the config entry/intention weren't affected.
entry, _, err = client.ConfigEntries().Get(api.IngressGateway, "ingress", nil)
require.NoError(t, err)
require.Equal(t, expected, entry)
2020-06-05 21:54:29 +00:00
ixns, _, err = client.Connect().Intentions(nil)
require.NoError(t, err)
require.Len(t, ixns, 1)
require.Equal(t, "ingress", ixns[0].SourceName)
require.Equal(t, "foo", ixns[0].DestinationName)
2020-06-05 21:54:29 +00:00
}
}
func TestConnectExpose_invalidFlags(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
}
2020-06-05 21:54:29 +00:00
t.Parallel()
a := agent.NewTestAgent(t, ``)
defer a.Shutdown()
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
t.Run("missing service", func(t *testing.T) {
ui := cli.NewMockUi()
c := New(ui)
args := []string{
"-http-addr=" + a.HTTPAddr(),
}
code := c.Run(args)
if code != 1 {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
}
require.Contains(t, ui.ErrorWriter.String(), "A service name must be given")
2020-06-05 21:54:29 +00:00
})
t.Run("missing gateway", func(t *testing.T) {
ui := cli.NewMockUi()
c := New(ui)
args := []string{
"-http-addr=" + a.HTTPAddr(),
"-service=foo",
}
code := c.Run(args)
if code != 1 {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
}
require.Contains(t, ui.ErrorWriter.String(), "An ingress gateway service must be given")
2020-06-05 21:54:29 +00:00
})
t.Run("missing port", func(t *testing.T) {
ui := cli.NewMockUi()
c := New(ui)
args := []string{
"-http-addr=" + a.HTTPAddr(),
"-service=foo",
"-ingress-gateway=ingress",
}
code := c.Run(args)
if code != 1 {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
}
require.Contains(t, ui.ErrorWriter.String(), "A port must be provided")
2020-06-05 21:54:29 +00:00
})
}
func TestConnectExpose_existingConfig(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
}
2020-06-05 21:54:29 +00:00
t.Parallel()
a := agent.NewTestAgent(t, ``)
client := a.Client()
defer a.Shutdown()
// Create some service config entries to set their protocol.
for _, service := range []string{"bar", "zoo"} {
_, _, err := client.ConfigEntries().Set(&api.ServiceConfigEntry{
Kind: "service-defaults",
Name: service,
Protocol: "http",
}, nil)
require.NoError(t, err)
2020-06-05 21:54:29 +00:00
}
// Create an existing ingress config entry with some services.
ingressConf := &api.IngressGatewayConfigEntry{
Kind: api.IngressGateway,
Name: "ingress",
Listeners: []api.IngressListener{
{
Port: 8888,
Protocol: "tcp",
Services: []api.IngressService{
{
Name: "foo",
},
},
},
{
Port: 9999,
Protocol: "http",
Services: []api.IngressService{
{
Name: "bar",
},
},
},
},
}
_, _, err := client.ConfigEntries().Set(ingressConf, nil)
require.NoError(t, err)
2020-06-05 21:54:29 +00:00
// Add a service on a new port.
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
{
ui := cli.NewMockUi()
c := New(ui)
args := []string{
"-http-addr=" + a.HTTPAddr(),
"-service=baz",
"-ingress-gateway=ingress",
"-port=10000",
"-protocol=tcp",
}
code := c.Run(args)
if code != 0 {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
}
// Make sure the ingress config was updated and existing services preserved.
entry, _, err := client.ConfigEntries().Get(api.IngressGateway, "ingress", nil)
require.NoError(t, err)
2020-06-05 21:54:29 +00:00
entryConf := entry.(*api.IngressGatewayConfigEntry)
2020-06-05 21:54:29 +00:00
ingressConf.Listeners = append(ingressConf.Listeners, api.IngressListener{
Port: 10000,
Protocol: "tcp",
Services: []api.IngressService{
{
Name: "baz",
},
},
})
ingressConf.Partition = entryConf.Partition
ingressConf.Namespace = entryConf.Namespace
for i, listener := range ingressConf.Listeners {
listener.Services[0].Namespace = entryConf.Listeners[i].Services[0].Namespace
listener.Services[0].Partition = entryConf.Listeners[i].Services[0].Partition
}
2020-06-05 21:54:29 +00:00
ingressConf.CreateIndex = entry.GetCreateIndex()
ingressConf.ModifyIndex = entry.GetModifyIndex()
require.Equal(t, ingressConf, entry)
2020-06-05 21:54:29 +00:00
}
// Add an service on a port shared with an existing listener.
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
{
ui := cli.NewMockUi()
c := New(ui)
args := []string{
"-http-addr=" + a.HTTPAddr(),
"-service=zoo",
"-ingress-gateway=ingress",
"-port=9999",
"-protocol=http",
2020-06-08 23:59:47 +00:00
"-host=foo.com",
"-host=foo.net",
2020-06-05 21:54:29 +00:00
}
code := c.Run(args)
if code != 0 {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
}
// Make sure the ingress config was updated and existing services preserved.
entry, _, err := client.ConfigEntries().Get(api.IngressGateway, "ingress", nil)
require.NoError(t, err)
2020-06-05 21:54:29 +00:00
entryConf := entry.(*api.IngressGatewayConfigEntry)
2020-06-05 21:54:29 +00:00
ingressConf.Listeners[1].Services = append(ingressConf.Listeners[1].Services, api.IngressService{
Name: "zoo",
Namespace: entryConf.Listeners[1].Services[1].Namespace,
Partition: entryConf.Listeners[1].Services[1].Partition,
Hosts: []string{"foo.com", "foo.net"},
2020-06-05 21:54:29 +00:00
})
ingressConf.CreateIndex = entry.GetCreateIndex()
ingressConf.ModifyIndex = entry.GetModifyIndex()
require.Equal(t, ingressConf, entry)
2020-06-05 21:54:29 +00:00
}
// Update the bar service and add a custom host.
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
{
ui := cli.NewMockUi()
c := New(ui)
args := []string{
"-http-addr=" + a.HTTPAddr(),
"-service=bar",
"-ingress-gateway=ingress",
"-port=9999",
"-protocol=http",
"-host=bar.com",
}
code := c.Run(args)
if code != 0 {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
}
// Make sure the ingress config was updated and existing services preserved.
entry, _, err := client.ConfigEntries().Get(api.IngressGateway, "ingress", nil)
require.NoError(t, err)
ingressConf.Listeners[1].Services[0].Hosts = []string{"bar.com"}
ingressConf.CreateIndex = entry.GetCreateIndex()
ingressConf.ModifyIndex = entry.GetModifyIndex()
require.Equal(t, ingressConf, entry)
}
2020-06-05 21:54:29 +00:00
}