2021-10-01 13:59:55 +00:00
|
|
|
//go:build !windows
|
2020-01-15 15:29:47 +00:00
|
|
|
// +build !windows
|
2021-10-01 13:59:55 +00:00
|
|
|
|
2020-01-15 15:29:47 +00:00
|
|
|
// todo(shoenig): Once Connect is supported on Windows, we'll need to make this
|
|
|
|
// set of tests work there too.
|
|
|
|
|
2019-08-12 22:41:39 +00:00
|
|
|
package taskrunner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2021-01-20 15:48:12 +00:00
|
|
|
"fmt"
|
2019-08-12 22:41:39 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2021-04-26 19:24:06 +00:00
|
|
|
"time"
|
2019-08-12 22:41:39 +00:00
|
|
|
|
|
|
|
consulapi "github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
|
2019-09-03 15:43:38 +00:00
|
|
|
"github.com/hashicorp/nomad/client/taskenv"
|
2019-08-12 22:41:39 +00:00
|
|
|
"github.com/hashicorp/nomad/client/testutil"
|
|
|
|
agentconsul "github.com/hashicorp/nomad/command/agent/consul"
|
2020-04-02 16:30:38 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2019-09-03 15:43:38 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/args"
|
2019-08-12 22:41:39 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2019-12-18 16:23:16 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2019-08-12 22:41:39 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2020-04-02 16:30:38 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs/config"
|
2019-08-12 22:41:39 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-01-07 15:11:33 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2019-08-12 22:41:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ interfaces.TaskPrestartHook = (*envoyBootstrapHook)(nil)
|
|
|
|
|
2021-03-16 18:22:21 +00:00
|
|
|
const (
|
|
|
|
// consulNamespace is empty string in OSS, because Consul OSS does not like
|
|
|
|
// having even the default namespace set.
|
|
|
|
consulNamespace = ""
|
|
|
|
)
|
|
|
|
|
2019-12-18 16:23:16 +00:00
|
|
|
func writeTmp(t *testing.T, s string, fm os.FileMode) string {
|
|
|
|
dir, err := ioutil.TempDir("", "envoy-")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
fPath := filepath.Join(dir, sidsTokenFile)
|
|
|
|
err = ioutil.WriteFile(fPath, []byte(s), fm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvoyBootstrapHook_maybeLoadSIToken(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2020-01-15 15:29:47 +00:00
|
|
|
// This test fails when running as root because the test case for checking
|
|
|
|
// the error condition when the file is unreadable fails (root can read the
|
|
|
|
// file even though the permissions are set to 0200).
|
2020-01-07 15:11:33 +00:00
|
|
|
if unix.Geteuid() == 0 {
|
|
|
|
t.Skip("test only works as non-root")
|
|
|
|
}
|
|
|
|
|
2019-12-18 16:23:16 +00:00
|
|
|
t.Run("file does not exist", func(t *testing.T) {
|
|
|
|
h := newEnvoyBootstrapHook(&envoyBootstrapHookConfig{logger: testlog.HCLogger(t)})
|
2020-04-02 16:30:38 +00:00
|
|
|
cfg, err := h.maybeLoadSIToken("task1", "/does/not/exist")
|
2019-12-18 16:23:16 +00:00
|
|
|
require.NoError(t, err) // absence of token is not an error
|
2020-04-02 16:30:38 +00:00
|
|
|
require.Equal(t, "", cfg)
|
2019-12-18 16:23:16 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("load token from file", func(t *testing.T) {
|
|
|
|
token := uuid.Generate()
|
|
|
|
f := writeTmp(t, token, 0440)
|
|
|
|
defer cleanupDir(t, f)
|
|
|
|
|
|
|
|
h := newEnvoyBootstrapHook(&envoyBootstrapHookConfig{logger: testlog.HCLogger(t)})
|
2020-04-02 16:30:38 +00:00
|
|
|
cfg, err := h.maybeLoadSIToken("task1", f)
|
2019-12-18 16:23:16 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-02 16:30:38 +00:00
|
|
|
require.Equal(t, token, cfg)
|
2019-12-18 16:23:16 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("file is unreadable", func(t *testing.T) {
|
|
|
|
token := uuid.Generate()
|
|
|
|
f := writeTmp(t, token, 0200)
|
|
|
|
defer cleanupDir(t, f)
|
|
|
|
|
|
|
|
h := newEnvoyBootstrapHook(&envoyBootstrapHookConfig{logger: testlog.HCLogger(t)})
|
2020-04-02 16:30:38 +00:00
|
|
|
cfg, err := h.maybeLoadSIToken("task1", f)
|
2019-12-18 16:23:16 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.False(t, os.IsNotExist(err))
|
2020-04-02 16:30:38 +00:00
|
|
|
require.Equal(t, "", cfg)
|
2019-12-18 16:23:16 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-02 16:30:38 +00:00
|
|
|
func TestEnvoyBootstrapHook_decodeTriState(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
require.Equal(t, "", decodeTriState(nil))
|
|
|
|
require.Equal(t, "true", decodeTriState(helper.BoolToPtr(true)))
|
|
|
|
require.Equal(t, "false", decodeTriState(helper.BoolToPtr(false)))
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2020-05-13 20:15:55 +00:00
|
|
|
consulPlainConfig = consulTransportConfig{
|
2020-04-02 16:30:38 +00:00
|
|
|
HTTPAddr: "2.2.2.2",
|
|
|
|
}
|
|
|
|
|
2020-05-13 20:15:55 +00:00
|
|
|
consulTLSConfig = consulTransportConfig{
|
2020-04-02 16:30:38 +00:00
|
|
|
HTTPAddr: "2.2.2.2", // arg
|
|
|
|
Auth: "user:password", // env
|
|
|
|
SSL: "true", // env
|
|
|
|
VerifySSL: "true", // env
|
|
|
|
CAFile: "/etc/tls/ca-file", // arg
|
|
|
|
CertFile: "/etc/tls/cert-file", // arg
|
|
|
|
KeyFile: "/etc/tls/key-file", // arg
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-12-18 16:23:16 +00:00
|
|
|
func TestEnvoyBootstrapHook_envoyBootstrapArgs(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run("excluding SI token", func(t *testing.T) {
|
|
|
|
ebArgs := envoyBootstrapArgs{
|
|
|
|
sidecarFor: "s1",
|
|
|
|
grpcAddr: "1.1.1.1",
|
2020-04-02 16:30:38 +00:00
|
|
|
consulConfig: consulPlainConfig,
|
2021-07-09 18:44:02 +00:00
|
|
|
envoyAdminBind: "127.0.0.2:19000",
|
|
|
|
envoyReadyBind: "127.0.0.1:19100",
|
2019-12-18 16:23:16 +00:00
|
|
|
}
|
2020-04-02 16:30:38 +00:00
|
|
|
result := ebArgs.args()
|
2019-12-18 16:23:16 +00:00
|
|
|
require.Equal(t, []string{"connect", "envoy",
|
|
|
|
"-grpc-addr", "1.1.1.1",
|
|
|
|
"-http-addr", "2.2.2.2",
|
2021-07-09 18:44:02 +00:00
|
|
|
"-admin-bind", "127.0.0.2:19000",
|
|
|
|
"-address", "127.0.0.1:19100",
|
2019-12-18 16:23:16 +00:00
|
|
|
"-bootstrap",
|
|
|
|
"-sidecar-for", "s1",
|
2020-04-02 16:30:38 +00:00
|
|
|
}, result)
|
2019-12-18 16:23:16 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("including SI token", func(t *testing.T) {
|
|
|
|
token := uuid.Generate()
|
|
|
|
ebArgs := envoyBootstrapArgs{
|
|
|
|
sidecarFor: "s1",
|
|
|
|
grpcAddr: "1.1.1.1",
|
2020-04-02 16:30:38 +00:00
|
|
|
consulConfig: consulPlainConfig,
|
2021-07-09 18:44:02 +00:00
|
|
|
envoyAdminBind: "127.0.0.2:19000",
|
|
|
|
envoyReadyBind: "127.0.0.1:19100",
|
2019-12-18 16:23:16 +00:00
|
|
|
siToken: token,
|
|
|
|
}
|
2020-04-02 16:30:38 +00:00
|
|
|
result := ebArgs.args()
|
2019-12-18 16:23:16 +00:00
|
|
|
require.Equal(t, []string{"connect", "envoy",
|
|
|
|
"-grpc-addr", "1.1.1.1",
|
|
|
|
"-http-addr", "2.2.2.2",
|
2021-07-09 18:44:02 +00:00
|
|
|
"-admin-bind", "127.0.0.2:19000",
|
|
|
|
"-address", "127.0.0.1:19100",
|
2019-12-18 16:23:16 +00:00
|
|
|
"-bootstrap",
|
|
|
|
"-sidecar-for", "s1",
|
|
|
|
"-token", token,
|
2020-04-02 16:30:38 +00:00
|
|
|
}, result)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("including certificates", func(t *testing.T) {
|
|
|
|
ebArgs := envoyBootstrapArgs{
|
|
|
|
sidecarFor: "s1",
|
|
|
|
grpcAddr: "1.1.1.1",
|
|
|
|
consulConfig: consulTLSConfig,
|
2021-07-09 18:44:02 +00:00
|
|
|
envoyAdminBind: "127.0.0.2:19000",
|
|
|
|
envoyReadyBind: "127.0.0.1:19100",
|
2020-04-02 16:30:38 +00:00
|
|
|
}
|
|
|
|
result := ebArgs.args()
|
|
|
|
require.Equal(t, []string{"connect", "envoy",
|
|
|
|
"-grpc-addr", "1.1.1.1",
|
|
|
|
"-http-addr", "2.2.2.2",
|
2021-07-09 18:44:02 +00:00
|
|
|
"-admin-bind", "127.0.0.2:19000",
|
|
|
|
"-address", "127.0.0.1:19100",
|
2020-04-02 16:30:38 +00:00
|
|
|
"-bootstrap",
|
|
|
|
"-sidecar-for", "s1",
|
|
|
|
"-ca-file", "/etc/tls/ca-file",
|
|
|
|
"-client-cert", "/etc/tls/cert-file",
|
|
|
|
"-client-key", "/etc/tls/key-file",
|
|
|
|
}, result)
|
|
|
|
})
|
2020-07-28 20:12:08 +00:00
|
|
|
|
|
|
|
t.Run("ingress gateway", func(t *testing.T) {
|
|
|
|
ebArgs := envoyBootstrapArgs{
|
|
|
|
consulConfig: consulPlainConfig,
|
|
|
|
grpcAddr: "1.1.1.1",
|
2021-07-09 18:44:02 +00:00
|
|
|
envoyAdminBind: "127.0.0.2:19000",
|
|
|
|
envoyReadyBind: "127.0.0.1:19100",
|
2020-07-28 20:12:08 +00:00
|
|
|
gateway: "my-ingress-gateway",
|
2021-01-19 15:46:44 +00:00
|
|
|
proxyID: "_nomad-task-803cb569-881c-b0d8-9222-360bcc33157e-group-ig-ig-8080",
|
2020-07-28 20:12:08 +00:00
|
|
|
}
|
|
|
|
result := ebArgs.args()
|
|
|
|
require.Equal(t, []string{"connect", "envoy",
|
|
|
|
"-grpc-addr", "1.1.1.1",
|
|
|
|
"-http-addr", "2.2.2.2",
|
2021-07-09 18:44:02 +00:00
|
|
|
"-admin-bind", "127.0.0.2:19000",
|
|
|
|
"-address", "127.0.0.1:19100",
|
2020-07-28 20:12:08 +00:00
|
|
|
"-bootstrap",
|
|
|
|
"-gateway", "my-ingress-gateway",
|
2021-01-19 15:46:44 +00:00
|
|
|
"-proxy-id", "_nomad-task-803cb569-881c-b0d8-9222-360bcc33157e-group-ig-ig-8080",
|
2020-07-28 20:12:08 +00:00
|
|
|
}, result)
|
|
|
|
})
|
2021-04-12 19:10:10 +00:00
|
|
|
|
|
|
|
t.Run("mesh gateway", func(t *testing.T) {
|
|
|
|
ebArgs := envoyBootstrapArgs{
|
|
|
|
consulConfig: consulPlainConfig,
|
|
|
|
grpcAddr: "1.1.1.1",
|
2021-07-09 18:44:02 +00:00
|
|
|
envoyAdminBind: "127.0.0.2:19000",
|
|
|
|
envoyReadyBind: "127.0.0.1:19100",
|
2021-04-12 19:10:10 +00:00
|
|
|
gateway: "my-mesh-gateway",
|
|
|
|
proxyID: "_nomad-task-803cb569-881c-b0d8-9222-360bcc33157e-group-mesh-mesh-8080",
|
|
|
|
}
|
|
|
|
result := ebArgs.args()
|
|
|
|
require.Equal(t, []string{"connect", "envoy",
|
|
|
|
"-grpc-addr", "1.1.1.1",
|
|
|
|
"-http-addr", "2.2.2.2",
|
2021-07-09 18:44:02 +00:00
|
|
|
"-admin-bind", "127.0.0.2:19000",
|
|
|
|
"-address", "127.0.0.1:19100",
|
2021-04-12 19:10:10 +00:00
|
|
|
"-bootstrap",
|
|
|
|
"-gateway", "my-mesh-gateway",
|
|
|
|
"-proxy-id", "_nomad-task-803cb569-881c-b0d8-9222-360bcc33157e-group-mesh-mesh-8080",
|
|
|
|
}, result)
|
|
|
|
})
|
2020-04-02 16:30:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvoyBootstrapHook_envoyBootstrapEnv(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
environment := []string{"foo=bar", "baz=1"}
|
|
|
|
|
|
|
|
t.Run("plain consul config", func(t *testing.T) {
|
|
|
|
require.Equal(t, []string{
|
|
|
|
"foo=bar", "baz=1",
|
|
|
|
}, envoyBootstrapArgs{
|
|
|
|
sidecarFor: "s1",
|
|
|
|
grpcAddr: "1.1.1.1",
|
|
|
|
consulConfig: consulPlainConfig,
|
|
|
|
envoyAdminBind: "localhost:3333",
|
|
|
|
}.env(environment))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("tls consul config", func(t *testing.T) {
|
|
|
|
require.Equal(t, []string{
|
|
|
|
"foo=bar", "baz=1",
|
|
|
|
"CONSUL_HTTP_AUTH=user:password",
|
|
|
|
"CONSUL_HTTP_SSL=true",
|
|
|
|
"CONSUL_HTTP_SSL_VERIFY=true",
|
|
|
|
}, envoyBootstrapArgs{
|
|
|
|
sidecarFor: "s1",
|
|
|
|
grpcAddr: "1.1.1.1",
|
|
|
|
consulConfig: consulTLSConfig,
|
|
|
|
envoyAdminBind: "localhost:3333",
|
|
|
|
}.env(environment))
|
2019-12-18 16:23:16 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-28 20:12:08 +00:00
|
|
|
// envoyConfig is used to unmarshal an envoy bootstrap configuration file, so that
|
|
|
|
// we can inspect the contents in tests.
|
2019-12-18 16:23:16 +00:00
|
|
|
type envoyConfig struct {
|
2020-07-28 20:12:08 +00:00
|
|
|
Admin struct {
|
|
|
|
Address struct {
|
|
|
|
SocketAddress struct {
|
|
|
|
Address string `json:"address"`
|
|
|
|
Port int `json:"port_value"`
|
|
|
|
} `json:"socket_address"`
|
|
|
|
} `json:"address"`
|
|
|
|
} `json:"admin"`
|
|
|
|
Node struct {
|
|
|
|
Cluster string `json:"cluster"`
|
2021-01-20 15:48:12 +00:00
|
|
|
ID string `json:"id"`
|
2020-07-28 20:12:08 +00:00
|
|
|
Metadata struct {
|
|
|
|
Namespace string `json:"namespace"`
|
|
|
|
Version string `json:"envoy_version"`
|
|
|
|
}
|
|
|
|
}
|
2019-12-18 16:23:16 +00:00
|
|
|
DynamicResources struct {
|
|
|
|
ADSConfig struct {
|
|
|
|
GRPCServices struct {
|
|
|
|
InitialMetadata []struct {
|
|
|
|
Key string `json:"key"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
} `json:"initial_metadata"`
|
|
|
|
} `json:"grpc_services"`
|
|
|
|
} `json:"ads_config"`
|
|
|
|
} `json:"dynamic_resources"`
|
|
|
|
}
|
|
|
|
|
2020-01-15 14:41:23 +00:00
|
|
|
// TestEnvoyBootstrapHook_with_SI_token asserts the bootstrap file written for
|
|
|
|
// Envoy contains a Consul SI token.
|
2019-12-18 16:23:16 +00:00
|
|
|
func TestEnvoyBootstrapHook_with_SI_token(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
testutil.RequireConsul(t)
|
|
|
|
|
2020-07-28 20:12:08 +00:00
|
|
|
testConsul := getTestConsul(t)
|
|
|
|
defer testConsul.Stop()
|
2019-12-18 16:23:16 +00:00
|
|
|
|
2020-07-28 20:12:08 +00:00
|
|
|
alloc := mock.ConnectAlloc()
|
2019-12-18 16:23:16 +00:00
|
|
|
alloc.AllocatedResources.Shared.Networks = []*structs.NetworkResource{
|
|
|
|
{
|
|
|
|
Mode: "bridge",
|
|
|
|
IP: "10.0.0.1",
|
|
|
|
DynamicPorts: []structs.Port{
|
|
|
|
{
|
|
|
|
Label: "connect-proxy-foo",
|
|
|
|
Value: 9999,
|
|
|
|
To: 9999,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tg := alloc.Job.TaskGroups[0]
|
|
|
|
tg.Services = []*structs.Service{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
PortLabel: "9999", // Just need a valid port, nothing will bind to it
|
|
|
|
Connect: &structs.ConsulConnect{
|
|
|
|
SidecarService: &structs.ConsulSidecarService{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
sidecarTask := &structs.Task{
|
|
|
|
Name: "sidecar",
|
|
|
|
Kind: "connect-proxy:foo",
|
|
|
|
}
|
|
|
|
tg.Tasks = append(tg.Tasks, sidecarTask)
|
|
|
|
|
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
|
2021-10-15 23:56:14 +00:00
|
|
|
allocDir, cleanup := allocdir.TestAllocDir(t, logger, "EnvoyBootstrap", alloc.ID)
|
2019-12-18 16:23:16 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// Register Group Services
|
|
|
|
consulConfig := consulapi.DefaultConfig()
|
2020-07-28 20:12:08 +00:00
|
|
|
consulConfig.Address = testConsul.HTTPAddr
|
2019-12-18 16:23:16 +00:00
|
|
|
consulAPIClient, err := consulapi.NewClient(consulConfig)
|
|
|
|
require.NoError(t, err)
|
2021-06-07 15:54:33 +00:00
|
|
|
namespacesClient := agentconsul.NewNamespacesClient(consulAPIClient.Namespaces(), consulAPIClient.Agent())
|
2019-12-18 16:23:16 +00:00
|
|
|
|
2021-03-16 18:22:21 +00:00
|
|
|
consulClient := agentconsul.NewServiceClient(consulAPIClient.Agent(), namespacesClient, logger, true)
|
2019-12-18 16:23:16 +00:00
|
|
|
go consulClient.Run()
|
|
|
|
defer consulClient.Shutdown()
|
|
|
|
require.NoError(t, consulClient.RegisterWorkload(agentconsul.BuildAllocServices(mock.Node(), alloc, agentconsul.NoopRestarter())))
|
|
|
|
|
|
|
|
// Run Connect bootstrap Hook
|
2020-04-02 16:30:38 +00:00
|
|
|
h := newEnvoyBootstrapHook(newEnvoyBootstrapHookConfig(alloc, &config.ConsulConfig{
|
|
|
|
Addr: consulConfig.Address,
|
2021-03-16 18:22:21 +00:00
|
|
|
}, consulNamespace, logger))
|
2019-12-18 16:23:16 +00:00
|
|
|
req := &interfaces.TaskPrestartRequest{
|
|
|
|
Task: sidecarTask,
|
|
|
|
TaskDir: allocDir.NewTaskDir(sidecarTask.Name),
|
2020-07-28 20:12:08 +00:00
|
|
|
TaskEnv: taskenv.NewEmptyTaskEnv(),
|
2019-12-18 16:23:16 +00:00
|
|
|
}
|
|
|
|
require.NoError(t, req.TaskDir.Build(false, nil))
|
|
|
|
|
|
|
|
// Insert service identity token in the secrets directory
|
|
|
|
token := uuid.Generate()
|
|
|
|
siTokenFile := filepath.Join(req.TaskDir.SecretsDir, sidsTokenFile)
|
|
|
|
err = ioutil.WriteFile(siTokenFile, []byte(token), 0440)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp := &interfaces.TaskPrestartResponse{}
|
|
|
|
|
|
|
|
// Run the hook
|
|
|
|
require.NoError(t, h.Prestart(context.Background(), req, resp))
|
|
|
|
|
|
|
|
// Assert it is Done
|
|
|
|
require.True(t, resp.Done)
|
|
|
|
|
|
|
|
// Ensure the default path matches
|
|
|
|
env := map[string]string{
|
|
|
|
taskenv.SecretsDir: req.TaskDir.SecretsDir,
|
|
|
|
}
|
|
|
|
f, err := os.Open(args.ReplaceEnv(structs.EnvoyBootstrapPath, env))
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
// Assert bootstrap configuration is valid json
|
|
|
|
var out envoyConfig
|
|
|
|
require.NoError(t, json.NewDecoder(f).Decode(&out))
|
|
|
|
|
|
|
|
// Assert the SI token got set
|
|
|
|
key := out.DynamicResources.ADSConfig.GRPCServices.InitialMetadata[0].Key
|
|
|
|
value := out.DynamicResources.ADSConfig.GRPCServices.InitialMetadata[0].Value
|
|
|
|
require.Equal(t, "x-consul-token", key)
|
|
|
|
require.Equal(t, token, value)
|
|
|
|
}
|
|
|
|
|
2020-07-28 20:12:08 +00:00
|
|
|
// TestTaskRunner_EnvoyBootstrapHook_sidecar_ok asserts the EnvoyBootstrapHook
|
2019-08-12 22:41:39 +00:00
|
|
|
// creates Envoy's bootstrap.json configuration based on Connect proxy sidecars
|
|
|
|
// registered for the task.
|
2020-07-28 20:12:08 +00:00
|
|
|
func TestTaskRunner_EnvoyBootstrapHook_sidecar_ok(t *testing.T) {
|
2019-08-12 22:41:39 +00:00
|
|
|
t.Parallel()
|
|
|
|
testutil.RequireConsul(t)
|
|
|
|
|
2020-07-28 20:12:08 +00:00
|
|
|
testConsul := getTestConsul(t)
|
|
|
|
defer testConsul.Stop()
|
2019-08-12 22:41:39 +00:00
|
|
|
|
2020-07-28 20:12:08 +00:00
|
|
|
alloc := mock.ConnectAlloc()
|
2019-08-12 22:41:39 +00:00
|
|
|
alloc.AllocatedResources.Shared.Networks = []*structs.NetworkResource{
|
|
|
|
{
|
|
|
|
Mode: "bridge",
|
|
|
|
IP: "10.0.0.1",
|
|
|
|
DynamicPorts: []structs.Port{
|
|
|
|
{
|
|
|
|
Label: "connect-proxy-foo",
|
|
|
|
Value: 9999,
|
|
|
|
To: 9999,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tg := alloc.Job.TaskGroups[0]
|
|
|
|
tg.Services = []*structs.Service{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
PortLabel: "9999", // Just need a valid port, nothing will bind to it
|
|
|
|
Connect: &structs.ConsulConnect{
|
|
|
|
SidecarService: &structs.ConsulSidecarService{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
sidecarTask := &structs.Task{
|
|
|
|
Name: "sidecar",
|
2020-07-28 20:12:08 +00:00
|
|
|
Kind: structs.NewTaskKind(structs.ConnectProxyPrefix, "foo"),
|
2019-08-12 22:41:39 +00:00
|
|
|
}
|
|
|
|
tg.Tasks = append(tg.Tasks, sidecarTask)
|
|
|
|
|
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
|
2021-10-15 23:56:14 +00:00
|
|
|
allocDir, cleanup := allocdir.TestAllocDir(t, logger, "EnvoyBootstrap", alloc.ID)
|
2019-09-03 15:43:38 +00:00
|
|
|
defer cleanup()
|
2019-08-12 22:41:39 +00:00
|
|
|
|
|
|
|
// Register Group Services
|
|
|
|
consulConfig := consulapi.DefaultConfig()
|
2020-07-28 20:12:08 +00:00
|
|
|
consulConfig.Address = testConsul.HTTPAddr
|
2019-08-12 22:41:39 +00:00
|
|
|
consulAPIClient, err := consulapi.NewClient(consulConfig)
|
|
|
|
require.NoError(t, err)
|
2021-06-07 15:54:33 +00:00
|
|
|
namespacesClient := agentconsul.NewNamespacesClient(consulAPIClient.Namespaces(), consulAPIClient.Agent())
|
2019-12-18 16:23:16 +00:00
|
|
|
|
2021-03-16 18:22:21 +00:00
|
|
|
consulClient := agentconsul.NewServiceClient(consulAPIClient.Agent(), namespacesClient, logger, true)
|
2019-08-12 22:41:39 +00:00
|
|
|
go consulClient.Run()
|
|
|
|
defer consulClient.Shutdown()
|
2019-11-18 18:04:01 +00:00
|
|
|
require.NoError(t, consulClient.RegisterWorkload(agentconsul.BuildAllocServices(mock.Node(), alloc, agentconsul.NoopRestarter())))
|
2019-08-12 22:41:39 +00:00
|
|
|
|
|
|
|
// Run Connect bootstrap Hook
|
2020-04-02 16:30:38 +00:00
|
|
|
h := newEnvoyBootstrapHook(newEnvoyBootstrapHookConfig(alloc, &config.ConsulConfig{
|
|
|
|
Addr: consulConfig.Address,
|
2021-03-16 18:22:21 +00:00
|
|
|
}, consulNamespace, logger))
|
2019-08-12 22:41:39 +00:00
|
|
|
req := &interfaces.TaskPrestartRequest{
|
|
|
|
Task: sidecarTask,
|
|
|
|
TaskDir: allocDir.NewTaskDir(sidecarTask.Name),
|
2020-07-28 20:12:08 +00:00
|
|
|
TaskEnv: taskenv.NewEmptyTaskEnv(),
|
2019-08-12 22:41:39 +00:00
|
|
|
}
|
|
|
|
require.NoError(t, req.TaskDir.Build(false, nil))
|
|
|
|
|
|
|
|
resp := &interfaces.TaskPrestartResponse{}
|
|
|
|
|
|
|
|
// Run the hook
|
|
|
|
require.NoError(t, h.Prestart(context.Background(), req, resp))
|
|
|
|
|
|
|
|
// Assert it is Done
|
|
|
|
require.True(t, resp.Done)
|
|
|
|
|
2020-01-08 18:41:38 +00:00
|
|
|
require.NotNil(t, resp.Env)
|
2021-07-09 18:44:02 +00:00
|
|
|
require.Equal(t, "127.0.0.2:19001", resp.Env[envoyAdminBindEnvPrefix+"foo"])
|
2020-01-08 18:41:38 +00:00
|
|
|
|
2019-09-03 15:43:38 +00:00
|
|
|
// Ensure the default path matches
|
|
|
|
env := map[string]string{
|
|
|
|
taskenv.SecretsDir: req.TaskDir.SecretsDir,
|
|
|
|
}
|
|
|
|
f, err := os.Open(args.ReplaceEnv(structs.EnvoyBootstrapPath, env))
|
2019-08-12 22:41:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
// Assert bootstrap configuration is valid json
|
2019-12-18 16:23:16 +00:00
|
|
|
var out envoyConfig
|
2019-08-12 22:41:39 +00:00
|
|
|
require.NoError(t, json.NewDecoder(f).Decode(&out))
|
2019-12-18 16:23:16 +00:00
|
|
|
|
|
|
|
// Assert no SI token got set
|
|
|
|
key := out.DynamicResources.ADSConfig.GRPCServices.InitialMetadata[0].Key
|
|
|
|
value := out.DynamicResources.ADSConfig.GRPCServices.InitialMetadata[0].Value
|
|
|
|
require.Equal(t, "x-consul-token", key)
|
|
|
|
require.Equal(t, "", value)
|
2019-08-12 22:41:39 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 20:12:08 +00:00
|
|
|
func TestTaskRunner_EnvoyBootstrapHook_gateway_ok(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
|
|
|
|
testConsul := getTestConsul(t)
|
|
|
|
defer testConsul.Stop()
|
|
|
|
|
|
|
|
// Setup an Allocation
|
|
|
|
alloc := mock.ConnectIngressGatewayAlloc("bridge")
|
2021-10-15 23:56:14 +00:00
|
|
|
allocDir, cleanupDir := allocdir.TestAllocDir(t, logger, "EnvoyBootstrapIngressGateway", alloc.ID)
|
2020-07-28 20:12:08 +00:00
|
|
|
defer cleanupDir()
|
|
|
|
|
|
|
|
// Get a Consul client
|
|
|
|
consulConfig := consulapi.DefaultConfig()
|
|
|
|
consulConfig.Address = testConsul.HTTPAddr
|
|
|
|
consulAPIClient, err := consulapi.NewClient(consulConfig)
|
|
|
|
require.NoError(t, err)
|
2021-06-07 15:54:33 +00:00
|
|
|
namespacesClient := agentconsul.NewNamespacesClient(consulAPIClient.Namespaces(), consulAPIClient.Agent())
|
2020-07-28 20:12:08 +00:00
|
|
|
|
|
|
|
// Register Group Services
|
2021-03-16 18:22:21 +00:00
|
|
|
serviceClient := agentconsul.NewServiceClient(consulAPIClient.Agent(), namespacesClient, logger, true)
|
2020-07-28 20:12:08 +00:00
|
|
|
go serviceClient.Run()
|
|
|
|
defer serviceClient.Shutdown()
|
|
|
|
require.NoError(t, serviceClient.RegisterWorkload(agentconsul.BuildAllocServices(mock.Node(), alloc, agentconsul.NoopRestarter())))
|
|
|
|
|
|
|
|
// Register Configuration Entry
|
|
|
|
ceClient := consulAPIClient.ConfigEntries()
|
|
|
|
set, _, err := ceClient.Set(&consulapi.IngressGatewayConfigEntry{
|
|
|
|
Kind: consulapi.IngressGateway,
|
|
|
|
Name: "gateway-service", // matches job
|
|
|
|
Listeners: []consulapi.IngressListener{{
|
|
|
|
Port: 2000,
|
|
|
|
Protocol: "tcp",
|
|
|
|
Services: []consulapi.IngressService{{
|
|
|
|
Name: "service1",
|
|
|
|
}},
|
|
|
|
}},
|
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, set)
|
|
|
|
|
|
|
|
// Run Connect bootstrap hook
|
|
|
|
h := newEnvoyBootstrapHook(newEnvoyBootstrapHookConfig(alloc, &config.ConsulConfig{
|
|
|
|
Addr: consulConfig.Address,
|
2021-03-16 18:22:21 +00:00
|
|
|
}, consulNamespace, logger))
|
2020-07-28 20:12:08 +00:00
|
|
|
|
|
|
|
req := &interfaces.TaskPrestartRequest{
|
|
|
|
Task: alloc.Job.TaskGroups[0].Tasks[0],
|
|
|
|
TaskDir: allocDir.NewTaskDir(alloc.Job.TaskGroups[0].Tasks[0].Name),
|
|
|
|
TaskEnv: taskenv.NewEmptyTaskEnv(),
|
|
|
|
}
|
|
|
|
require.NoError(t, req.TaskDir.Build(false, nil))
|
|
|
|
|
|
|
|
var resp interfaces.TaskPrestartResponse
|
|
|
|
|
|
|
|
// Run the hook
|
|
|
|
require.NoError(t, h.Prestart(context.Background(), req, &resp))
|
|
|
|
|
2020-09-04 17:50:11 +00:00
|
|
|
// Assert the hook is Done
|
2020-07-28 20:12:08 +00:00
|
|
|
require.True(t, resp.Done)
|
|
|
|
require.NotNil(t, resp.Env)
|
|
|
|
|
|
|
|
// Read the Envoy Config file
|
|
|
|
env := map[string]string{
|
|
|
|
taskenv.SecretsDir: req.TaskDir.SecretsDir,
|
|
|
|
}
|
|
|
|
f, err := os.Open(args.ReplaceEnv(structs.EnvoyBootstrapPath, env))
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
var out envoyConfig
|
|
|
|
require.NoError(t, json.NewDecoder(f).Decode(&out))
|
|
|
|
|
2021-01-20 15:48:12 +00:00
|
|
|
// The only interesting thing on bootstrap is the presence of the cluster,
|
|
|
|
// and its associated ID that Nomad sets. Everything is configured at runtime
|
|
|
|
// through xDS.
|
|
|
|
expID := fmt.Sprintf("_nomad-task-%s-group-web-my-ingress-service-9999", alloc.ID)
|
|
|
|
require.Equal(t, expID, out.Node.ID)
|
2021-01-19 15:46:44 +00:00
|
|
|
require.Equal(t, "ingress-gateway", out.Node.Cluster)
|
2020-07-28 20:12:08 +00:00
|
|
|
}
|
|
|
|
|
2019-08-12 22:41:39 +00:00
|
|
|
// TestTaskRunner_EnvoyBootstrapHook_Noop asserts that the Envoy bootstrap hook
|
2020-07-28 20:12:08 +00:00
|
|
|
// is a noop for non-Connect proxy sidecar / gateway tasks.
|
2019-08-12 22:41:39 +00:00
|
|
|
func TestTaskRunner_EnvoyBootstrapHook_Noop(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
task := alloc.Job.LookupTaskGroup(alloc.TaskGroup).Tasks[0]
|
2021-10-15 23:56:14 +00:00
|
|
|
allocDir, cleanup := allocdir.TestAllocDir(t, logger, "EnvoyBootstrap", alloc.ID)
|
|
|
|
defer cleanup()
|
2019-08-12 22:41:39 +00:00
|
|
|
|
|
|
|
// Run Envoy bootstrap Hook. Use invalid Consul address as it should
|
|
|
|
// not get hit.
|
2020-04-02 16:30:38 +00:00
|
|
|
h := newEnvoyBootstrapHook(newEnvoyBootstrapHookConfig(alloc, &config.ConsulConfig{
|
|
|
|
Addr: "http://127.0.0.2:1",
|
2021-03-16 18:22:21 +00:00
|
|
|
}, consulNamespace, logger))
|
2019-08-12 22:41:39 +00:00
|
|
|
req := &interfaces.TaskPrestartRequest{
|
|
|
|
Task: task,
|
|
|
|
TaskDir: allocDir.NewTaskDir(task.Name),
|
|
|
|
}
|
|
|
|
require.NoError(t, req.TaskDir.Build(false, nil))
|
|
|
|
|
|
|
|
resp := &interfaces.TaskPrestartResponse{}
|
|
|
|
|
|
|
|
// Run the hook
|
|
|
|
require.NoError(t, h.Prestart(context.Background(), req, resp))
|
|
|
|
|
|
|
|
// Assert it is Done
|
|
|
|
require.True(t, resp.Done)
|
|
|
|
|
|
|
|
// Assert no file was written
|
2019-09-03 15:43:38 +00:00
|
|
|
_, err := os.Open(filepath.Join(req.TaskDir.SecretsDir, "envoy_bootstrap.json"))
|
2019-08-12 22:41:39 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, os.IsNotExist(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestTaskRunner_EnvoyBootstrapHook_RecoverableError asserts the Envoy
|
|
|
|
// bootstrap hook returns a Recoverable error if the bootstrap command runs but
|
|
|
|
// fails.
|
|
|
|
func TestTaskRunner_EnvoyBootstrapHook_RecoverableError(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
testutil.RequireConsul(t)
|
|
|
|
|
2020-07-28 20:12:08 +00:00
|
|
|
testConsul := getTestConsul(t)
|
|
|
|
defer testConsul.Stop()
|
2019-08-12 22:41:39 +00:00
|
|
|
|
2020-07-28 20:12:08 +00:00
|
|
|
alloc := mock.ConnectAlloc()
|
2019-08-12 22:41:39 +00:00
|
|
|
alloc.AllocatedResources.Shared.Networks = []*structs.NetworkResource{
|
|
|
|
{
|
|
|
|
Mode: "bridge",
|
|
|
|
IP: "10.0.0.1",
|
|
|
|
DynamicPorts: []structs.Port{
|
|
|
|
{
|
|
|
|
Label: "connect-proxy-foo",
|
|
|
|
Value: 9999,
|
|
|
|
To: 9999,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tg := alloc.Job.TaskGroups[0]
|
|
|
|
tg.Services = []*structs.Service{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
PortLabel: "9999", // Just need a valid port, nothing will bind to it
|
|
|
|
Connect: &structs.ConsulConnect{
|
|
|
|
SidecarService: &structs.ConsulSidecarService{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
sidecarTask := &structs.Task{
|
|
|
|
Name: "sidecar",
|
|
|
|
Kind: "connect-proxy:foo",
|
|
|
|
}
|
|
|
|
tg.Tasks = append(tg.Tasks, sidecarTask)
|
|
|
|
|
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
|
2021-10-15 23:56:14 +00:00
|
|
|
allocDir, cleanup := allocdir.TestAllocDir(t, logger, "EnvoyBootstrap", alloc.ID)
|
2019-09-03 15:43:38 +00:00
|
|
|
defer cleanup()
|
2019-08-12 22:41:39 +00:00
|
|
|
|
|
|
|
// Unlike the successful test above, do NOT register the group services
|
|
|
|
// yet. This should cause a recoverable error similar to if Consul was
|
|
|
|
// not running.
|
|
|
|
|
|
|
|
// Run Connect bootstrap Hook
|
2020-04-02 16:30:38 +00:00
|
|
|
h := newEnvoyBootstrapHook(newEnvoyBootstrapHookConfig(alloc, &config.ConsulConfig{
|
2020-07-28 20:12:08 +00:00
|
|
|
Addr: testConsul.HTTPAddr,
|
2021-03-16 18:22:21 +00:00
|
|
|
}, consulNamespace, logger))
|
2021-04-26 19:24:06 +00:00
|
|
|
|
|
|
|
// Lower the allowable wait time for testing
|
|
|
|
h.envoyBootstrapWaitTime = 1 * time.Second
|
|
|
|
h.envoyBoostrapInitialGap = 100 * time.Millisecond
|
|
|
|
|
2019-08-12 22:41:39 +00:00
|
|
|
req := &interfaces.TaskPrestartRequest{
|
|
|
|
Task: sidecarTask,
|
|
|
|
TaskDir: allocDir.NewTaskDir(sidecarTask.Name),
|
2020-07-28 20:12:08 +00:00
|
|
|
TaskEnv: taskenv.NewEmptyTaskEnv(),
|
2019-08-12 22:41:39 +00:00
|
|
|
}
|
|
|
|
require.NoError(t, req.TaskDir.Build(false, nil))
|
|
|
|
|
|
|
|
resp := &interfaces.TaskPrestartResponse{}
|
|
|
|
|
|
|
|
// Run the hook
|
2020-05-13 20:15:55 +00:00
|
|
|
err := h.Prestart(context.Background(), req, resp)
|
2019-12-18 16:23:16 +00:00
|
|
|
require.EqualError(t, err, "error creating bootstrap configuration for Connect proxy sidecar: exit status 1")
|
2019-08-12 22:41:39 +00:00
|
|
|
require.True(t, structs.IsRecoverable(err))
|
|
|
|
|
|
|
|
// Assert it is not Done
|
|
|
|
require.False(t, resp.Done)
|
|
|
|
|
|
|
|
// Assert no file was written
|
|
|
|
_, err = os.Open(filepath.Join(req.TaskDir.SecretsDir, "envoy_bootstrap.json"))
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, os.IsNotExist(err))
|
|
|
|
}
|
2020-07-28 20:12:08 +00:00
|
|
|
|
2021-04-26 19:24:06 +00:00
|
|
|
func TestTaskRunner_EnvoyBootstrapHook_retryTimeout(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
|
|
|
|
testConsul := getTestConsul(t)
|
|
|
|
defer testConsul.Stop()
|
|
|
|
|
|
|
|
begin := time.Now()
|
|
|
|
|
|
|
|
// Setup an Allocation
|
|
|
|
alloc := mock.ConnectAlloc()
|
|
|
|
alloc.AllocatedResources.Shared.Networks = []*structs.NetworkResource{
|
|
|
|
{
|
|
|
|
Mode: "bridge",
|
|
|
|
IP: "10.0.0.1",
|
|
|
|
DynamicPorts: []structs.Port{
|
|
|
|
{
|
|
|
|
Label: "connect-proxy-foo",
|
|
|
|
Value: 9999,
|
|
|
|
To: 9999,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tg := alloc.Job.TaskGroups[0]
|
|
|
|
tg.Services = []*structs.Service{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
PortLabel: "9999", // Just need a valid port, nothing will bind to it
|
|
|
|
Connect: &structs.ConsulConnect{
|
|
|
|
SidecarService: &structs.ConsulSidecarService{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
sidecarTask := &structs.Task{
|
|
|
|
Name: "sidecar",
|
|
|
|
Kind: structs.NewTaskKind(structs.ConnectProxyPrefix, "foo"),
|
|
|
|
}
|
|
|
|
tg.Tasks = append(tg.Tasks, sidecarTask)
|
2021-10-15 23:56:14 +00:00
|
|
|
allocDir, cleanupAlloc := allocdir.TestAllocDir(t, logger, "EnvoyBootstrapRetryTimeout", alloc.ID)
|
2021-04-26 19:24:06 +00:00
|
|
|
defer cleanupAlloc()
|
|
|
|
|
|
|
|
// Get a Consul client
|
|
|
|
consulConfig := consulapi.DefaultConfig()
|
|
|
|
consulConfig.Address = testConsul.HTTPAddr
|
|
|
|
|
|
|
|
// Do NOT register group services, causing the hook to retry until timeout
|
|
|
|
|
|
|
|
// Run Connect bootstrap hook
|
|
|
|
h := newEnvoyBootstrapHook(newEnvoyBootstrapHookConfig(alloc, &config.ConsulConfig{
|
|
|
|
Addr: consulConfig.Address,
|
|
|
|
}, consulNamespace, logger))
|
|
|
|
|
|
|
|
// Keep track of the retry backoff iterations
|
|
|
|
iterations := 0
|
|
|
|
|
|
|
|
// Lower the allowable wait time for testing
|
|
|
|
h.envoyBootstrapWaitTime = 3 * time.Second
|
|
|
|
h.envoyBoostrapInitialGap = 1 * time.Second
|
|
|
|
h.envoyBootstrapExpSleep = func(d time.Duration) {
|
|
|
|
iterations++
|
|
|
|
time.Sleep(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the prestart request
|
|
|
|
req := &interfaces.TaskPrestartRequest{
|
|
|
|
Task: sidecarTask,
|
|
|
|
TaskDir: allocDir.NewTaskDir(sidecarTask.Name),
|
|
|
|
TaskEnv: taskenv.NewEmptyTaskEnv(),
|
|
|
|
}
|
|
|
|
require.NoError(t, req.TaskDir.Build(false, nil))
|
|
|
|
|
|
|
|
var resp interfaces.TaskPrestartResponse
|
|
|
|
|
|
|
|
// Run the hook and get the error
|
|
|
|
err := h.Prestart(context.Background(), req, &resp)
|
|
|
|
require.EqualError(t, err, "error creating bootstrap configuration for Connect proxy sidecar: exit status 1")
|
|
|
|
|
|
|
|
// Current time should be at least start time + total wait time
|
|
|
|
minimum := begin.Add(h.envoyBootstrapWaitTime)
|
|
|
|
require.True(t, time.Now().After(minimum))
|
|
|
|
|
|
|
|
// Should hit at least 2 iterations
|
|
|
|
require.Greater(t, 2, iterations)
|
|
|
|
|
|
|
|
// Make sure we captured the recoverable-ness of the error
|
|
|
|
_, ok := err.(*structs.RecoverableError)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
// Assert the hook is not done (it failed)
|
|
|
|
require.False(t, resp.Done)
|
|
|
|
}
|
|
|
|
|
2020-07-28 20:12:08 +00:00
|
|
|
func TestTaskRunner_EnvoyBootstrapHook_extractNameAndKind(t *testing.T) {
|
|
|
|
t.Run("connect sidecar", func(t *testing.T) {
|
|
|
|
kind, name, err := (*envoyBootstrapHook)(nil).extractNameAndKind(
|
|
|
|
structs.NewTaskKind(structs.ConnectProxyPrefix, "foo"),
|
|
|
|
)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, "connect-proxy", kind)
|
|
|
|
require.Equal(t, "foo", name)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("connect gateway", func(t *testing.T) {
|
|
|
|
kind, name, err := (*envoyBootstrapHook)(nil).extractNameAndKind(
|
|
|
|
structs.NewTaskKind(structs.ConnectIngressPrefix, "foo"),
|
|
|
|
)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, "connect-ingress", kind)
|
|
|
|
require.Equal(t, "foo", name)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("connect native", func(t *testing.T) {
|
|
|
|
_, _, err := (*envoyBootstrapHook)(nil).extractNameAndKind(
|
|
|
|
structs.NewTaskKind(structs.ConnectNativePrefix, "foo"),
|
|
|
|
)
|
|
|
|
require.EqualError(t, err, "envoy must be used as connect sidecar or gateway")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("normal task", func(t *testing.T) {
|
|
|
|
_, _, err := (*envoyBootstrapHook)(nil).extractNameAndKind(
|
|
|
|
structs.TaskKind(""),
|
|
|
|
)
|
|
|
|
require.EqualError(t, err, "envoy must be used as connect sidecar or gateway")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskRunner_EnvoyBootstrapHook_grpcAddress(t *testing.T) {
|
|
|
|
bridgeH := newEnvoyBootstrapHook(newEnvoyBootstrapHookConfig(
|
|
|
|
mock.ConnectIngressGatewayAlloc("bridge"),
|
|
|
|
new(config.ConsulConfig),
|
2021-03-16 18:22:21 +00:00
|
|
|
consulNamespace,
|
2020-07-28 20:12:08 +00:00
|
|
|
testlog.HCLogger(t),
|
|
|
|
))
|
|
|
|
|
|
|
|
hostH := newEnvoyBootstrapHook(newEnvoyBootstrapHookConfig(
|
|
|
|
mock.ConnectIngressGatewayAlloc("host"),
|
|
|
|
new(config.ConsulConfig),
|
2021-03-16 18:22:21 +00:00
|
|
|
consulNamespace,
|
2020-07-28 20:12:08 +00:00
|
|
|
testlog.HCLogger(t),
|
|
|
|
))
|
|
|
|
|
|
|
|
t.Run("environment", func(t *testing.T) {
|
|
|
|
env := map[string]string{
|
|
|
|
grpcConsulVariable: "1.2.3.4:9000",
|
|
|
|
}
|
|
|
|
require.Equal(t, "1.2.3.4:9000", bridgeH.grpcAddress(env))
|
|
|
|
require.Equal(t, "1.2.3.4:9000", hostH.grpcAddress(env))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("defaults", func(t *testing.T) {
|
|
|
|
require.Equal(t, "unix://alloc/tmp/consul_grpc.sock", bridgeH.grpcAddress(nil))
|
|
|
|
require.Equal(t, "127.0.0.1:8502", hostH.grpcAddress(nil))
|
|
|
|
})
|
|
|
|
}
|
2021-04-12 19:10:10 +00:00
|
|
|
|
|
|
|
func TestTaskRunner_EnvoyBootstrapHook_isConnectKind(t *testing.T) {
|
|
|
|
require.True(t, isConnectKind(structs.ConnectProxyPrefix))
|
|
|
|
require.True(t, isConnectKind(structs.ConnectIngressPrefix))
|
|
|
|
require.True(t, isConnectKind(structs.ConnectTerminatingPrefix))
|
|
|
|
require.True(t, isConnectKind(structs.ConnectMeshPrefix))
|
|
|
|
require.False(t, isConnectKind(""))
|
|
|
|
require.False(t, isConnectKind("something"))
|
|
|
|
}
|