2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-09-04 17:50:11 +00:00
|
|
|
package taskrunner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-04-02 00:24:02 +00:00
|
|
|
"errors"
|
2020-09-04 17:50:11 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2020-09-04 17:50:11 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
|
|
|
ifs "github.com/hashicorp/nomad/client/allocrunner/interfaces"
|
|
|
|
"github.com/hashicorp/nomad/client/taskenv"
|
|
|
|
"github.com/hashicorp/nomad/command/agent/consul"
|
2020-12-11 15:57:13 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/envoy"
|
2020-09-04 17:50:11 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2023-04-11 14:56:03 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2020-09-04 17:50:11 +00:00
|
|
|
)
|
|
|
|
|
2020-12-11 15:57:13 +00:00
|
|
|
var (
|
2020-12-14 17:56:34 +00:00
|
|
|
taskEnvDefault = taskenv.NewTaskEnv(nil, nil, nil, map[string]string{
|
2020-12-11 15:57:13 +00:00
|
|
|
"meta.connect.sidecar_image": envoy.ImageFormat,
|
|
|
|
"meta.connect.gateway_image": envoy.ImageFormat,
|
2020-12-14 17:56:34 +00:00
|
|
|
}, "", "")
|
2020-12-11 15:57:13 +00:00
|
|
|
)
|
|
|
|
|
2020-09-04 17:50:11 +00:00
|
|
|
func TestEnvoyVersionHook_semver(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-09-04 17:50:11 +00:00
|
|
|
|
|
|
|
t.Run("with v", func(t *testing.T) {
|
|
|
|
result, err := semver("v1.2.3")
|
2023-04-11 14:56:03 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.Eq(t, "1.2.3", result)
|
2020-09-04 17:50:11 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("without v", func(t *testing.T) {
|
|
|
|
result, err := semver("1.2.3")
|
2023-04-11 14:56:03 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.Eq(t, "1.2.3", result)
|
2020-09-04 17:50:11 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("unexpected", func(t *testing.T) {
|
|
|
|
_, err := semver("foo")
|
2023-04-11 14:56:03 +00:00
|
|
|
must.ErrorContains(t, err, "unexpected envoy version format: Malformed version: foo")
|
2020-09-04 17:50:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvoyVersionHook_taskImage(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-09-04 17:50:11 +00:00
|
|
|
|
|
|
|
t.Run("absent", func(t *testing.T) {
|
|
|
|
result := (*envoyVersionHook)(nil).taskImage(map[string]interface{}{
|
|
|
|
// empty
|
|
|
|
})
|
2023-04-11 14:56:03 +00:00
|
|
|
must.Eq(t, envoy.ImageFormat, result)
|
2020-09-04 17:50:11 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("not a string", func(t *testing.T) {
|
|
|
|
result := (*envoyVersionHook)(nil).taskImage(map[string]interface{}{
|
|
|
|
"image": 7, // not a string
|
|
|
|
})
|
2023-04-11 14:56:03 +00:00
|
|
|
must.Eq(t, envoy.ImageFormat, result)
|
2020-09-04 17:50:11 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
result := (*envoyVersionHook)(nil).taskImage(map[string]interface{}{
|
|
|
|
"image": "custom/envoy:latest",
|
|
|
|
})
|
2023-04-11 14:56:03 +00:00
|
|
|
must.Eq(t, "custom/envoy:latest", result)
|
2020-09-04 17:50:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvoyVersionHook_tweakImage(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-12-11 15:57:13 +00:00
|
|
|
|
|
|
|
image := envoy.ImageFormat
|
2020-09-04 17:50:11 +00:00
|
|
|
|
|
|
|
t.Run("legacy", func(t *testing.T) {
|
|
|
|
result, err := (*envoyVersionHook)(nil).tweakImage(image, nil)
|
2023-04-11 14:56:03 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.Eq(t, envoy.FallbackImage, result)
|
2020-09-04 17:50:11 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("unexpected", func(t *testing.T) {
|
|
|
|
_, err := (*envoyVersionHook)(nil).tweakImage(image, map[string][]string{
|
|
|
|
"envoy": {"foo", "bar", "baz"},
|
|
|
|
})
|
2023-04-11 14:56:03 +00:00
|
|
|
must.ErrorContains(t, err, "unexpected envoy version format: Malformed version: foo")
|
2020-09-04 17:50:11 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("standard envoy", func(t *testing.T) {
|
|
|
|
result, err := (*envoyVersionHook)(nil).tweakImage(image, map[string][]string{
|
|
|
|
"envoy": {"1.15.0", "1.14.4", "1.13.4", "1.12.6"},
|
|
|
|
})
|
2023-04-11 14:56:03 +00:00
|
|
|
must.NoError(t, err)
|
2023-05-02 14:27:48 +00:00
|
|
|
must.Eq(t, "docker.io/envoyproxy/envoy:v1.15.0", result)
|
2020-09-04 17:50:11 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("custom image", func(t *testing.T) {
|
|
|
|
custom := "custom-${NOMAD_envoy_version}/envoy:${NOMAD_envoy_version}"
|
|
|
|
result, err := (*envoyVersionHook)(nil).tweakImage(custom, map[string][]string{
|
|
|
|
"envoy": {"1.15.0", "1.14.4", "1.13.4", "1.12.6"},
|
|
|
|
})
|
2023-04-11 14:56:03 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.Eq(t, "custom-1.15.0/envoy:1.15.0", result)
|
2020-09-04 17:50:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-11 15:57:13 +00:00
|
|
|
func TestEnvoyVersionHook_interpolateImage(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-12-11 15:57:13 +00:00
|
|
|
|
|
|
|
hook := (*envoyVersionHook)(nil)
|
|
|
|
|
|
|
|
t.Run("default sidecar", func(t *testing.T) {
|
|
|
|
task := &structs.Task{
|
|
|
|
Config: map[string]interface{}{"image": envoy.SidecarConfigVar},
|
|
|
|
}
|
|
|
|
hook.interpolateImage(task, taskEnvDefault)
|
2023-04-11 14:56:03 +00:00
|
|
|
must.Eq(t, envoy.ImageFormat, task.Config["image"])
|
2020-12-11 15:57:13 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("default gateway", func(t *testing.T) {
|
|
|
|
task := &structs.Task{
|
|
|
|
Config: map[string]interface{}{"image": envoy.GatewayConfigVar},
|
|
|
|
}
|
|
|
|
hook.interpolateImage(task, taskEnvDefault)
|
2023-04-11 14:56:03 +00:00
|
|
|
must.Eq(t, envoy.ImageFormat, task.Config["image"])
|
2020-12-11 15:57:13 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("custom static", func(t *testing.T) {
|
|
|
|
task := &structs.Task{
|
|
|
|
Config: map[string]interface{}{"image": "custom/envoy"},
|
|
|
|
}
|
|
|
|
hook.interpolateImage(task, taskEnvDefault)
|
2023-04-11 14:56:03 +00:00
|
|
|
must.Eq(t, "custom/envoy", task.Config["image"])
|
2020-12-11 15:57:13 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("custom interpolated", func(t *testing.T) {
|
|
|
|
task := &structs.Task{
|
|
|
|
Config: map[string]interface{}{"image": "${MY_ENVOY}"},
|
|
|
|
}
|
|
|
|
hook.interpolateImage(task, taskenv.NewTaskEnv(map[string]string{
|
|
|
|
"MY_ENVOY": "my/envoy",
|
2020-12-14 17:56:34 +00:00
|
|
|
}, map[string]string{
|
|
|
|
"MY_ENVOY": "my/envoy",
|
|
|
|
}, nil, nil, "", ""))
|
2023-04-11 14:56:03 +00:00
|
|
|
must.Eq(t, "my/envoy", task.Config["image"])
|
2020-12-11 15:57:13 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("no image", func(t *testing.T) {
|
|
|
|
task := &structs.Task{
|
|
|
|
Config: map[string]interface{}{},
|
|
|
|
}
|
|
|
|
hook.interpolateImage(task, taskEnvDefault)
|
2023-04-11 14:56:03 +00:00
|
|
|
must.MapEmpty(t, task.Config)
|
2020-12-11 15:57:13 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-04 17:50:11 +00:00
|
|
|
func TestEnvoyVersionHook_skip(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-09-04 17:50:11 +00:00
|
|
|
|
|
|
|
h := new(envoyVersionHook)
|
|
|
|
|
|
|
|
t.Run("not docker", func(t *testing.T) {
|
|
|
|
skip := h.skip(&ifs.TaskPrestartRequest{
|
|
|
|
Task: &structs.Task{
|
|
|
|
Driver: "exec",
|
|
|
|
Config: nil,
|
|
|
|
},
|
|
|
|
})
|
2023-04-11 14:56:03 +00:00
|
|
|
must.True(t, skip)
|
2020-09-04 17:50:11 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("not connect", func(t *testing.T) {
|
|
|
|
skip := h.skip(&ifs.TaskPrestartRequest{
|
|
|
|
Task: &structs.Task{
|
|
|
|
Driver: "docker",
|
|
|
|
Kind: "",
|
|
|
|
},
|
|
|
|
})
|
2023-04-11 14:56:03 +00:00
|
|
|
must.True(t, skip)
|
2020-09-04 17:50:11 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("version not needed", func(t *testing.T) {
|
|
|
|
skip := h.skip(&ifs.TaskPrestartRequest{
|
|
|
|
Task: &structs.Task{
|
|
|
|
Driver: "docker",
|
|
|
|
Kind: structs.NewTaskKind(structs.ConnectProxyPrefix, "task"),
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"image": "custom/envoy:latest",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2023-04-11 14:56:03 +00:00
|
|
|
must.True(t, skip)
|
2020-09-04 17:50:11 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("version needed custom", func(t *testing.T) {
|
|
|
|
skip := h.skip(&ifs.TaskPrestartRequest{
|
|
|
|
Task: &structs.Task{
|
|
|
|
Driver: "docker",
|
|
|
|
Kind: structs.NewTaskKind(structs.ConnectProxyPrefix, "task"),
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"image": "custom/envoy:v${NOMAD_envoy_version}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2023-04-11 14:56:03 +00:00
|
|
|
must.False(t, skip)
|
2020-09-04 17:50:11 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("version needed standard", func(t *testing.T) {
|
|
|
|
skip := h.skip(&ifs.TaskPrestartRequest{
|
|
|
|
Task: &structs.Task{
|
|
|
|
Driver: "docker",
|
|
|
|
Kind: structs.NewTaskKind(structs.ConnectProxyPrefix, "task"),
|
|
|
|
Config: map[string]interface{}{
|
2020-12-11 15:57:13 +00:00
|
|
|
"image": envoy.ImageFormat,
|
2020-09-04 17:50:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2023-04-11 14:56:03 +00:00
|
|
|
must.False(t, skip)
|
2020-09-04 17:50:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskRunner_EnvoyVersionHook_Prestart_standard(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-09-04 17:50:11 +00:00
|
|
|
|
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
|
|
|
|
// Setup an Allocation
|
|
|
|
alloc := mock.ConnectAlloc()
|
|
|
|
alloc.Job.TaskGroups[0].Tasks[0] = mock.ConnectSidecarTask()
|
2021-10-18 17:32:41 +00:00
|
|
|
allocDir, cleanupDir := allocdir.TestAllocDir(t, logger, "EnvoyVersionHook", alloc.ID)
|
2020-09-04 17:50:11 +00:00
|
|
|
defer cleanupDir()
|
|
|
|
|
|
|
|
// Setup a mock for Consul API
|
|
|
|
spAPI := consul.MockSupportedProxiesAPI{
|
|
|
|
Value: map[string][]string{
|
|
|
|
"envoy": {"1.15.0", "1.14.4"},
|
|
|
|
},
|
|
|
|
Error: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run envoy_version hook
|
|
|
|
h := newEnvoyVersionHook(newEnvoyVersionHookConfig(alloc, spAPI, logger))
|
|
|
|
|
|
|
|
// Create a prestart request
|
|
|
|
request := &ifs.TaskPrestartRequest{
|
|
|
|
Task: alloc.Job.TaskGroups[0].Tasks[0],
|
|
|
|
TaskDir: allocDir.NewTaskDir(alloc.Job.TaskGroups[0].Tasks[0].Name),
|
2020-12-11 15:57:13 +00:00
|
|
|
TaskEnv: taskEnvDefault,
|
2020-09-04 17:50:11 +00:00
|
|
|
}
|
2023-04-11 14:56:03 +00:00
|
|
|
must.NoError(t, request.TaskDir.Build(false, nil))
|
2020-09-04 17:50:11 +00:00
|
|
|
|
|
|
|
// Prepare a response
|
|
|
|
var response ifs.TaskPrestartResponse
|
|
|
|
|
|
|
|
// Run the hook
|
2023-04-11 14:56:03 +00:00
|
|
|
must.NoError(t, h.Prestart(context.Background(), request, &response))
|
2020-09-04 17:50:11 +00:00
|
|
|
|
|
|
|
// Assert the Task.Config[image] is concrete
|
2023-05-02 14:27:48 +00:00
|
|
|
must.Eq(t, "docker.io/envoyproxy/envoy:v1.15.0", request.Task.Config["image"])
|
2020-09-04 17:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskRunner_EnvoyVersionHook_Prestart_custom(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-09-04 17:50:11 +00:00
|
|
|
|
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
|
|
|
|
// Setup an Allocation
|
|
|
|
alloc := mock.ConnectAlloc()
|
|
|
|
alloc.Job.TaskGroups[0].Tasks[0] = mock.ConnectSidecarTask()
|
2023-05-01 20:07:35 +00:00
|
|
|
alloc.Job.TaskGroups[0].Tasks[0].Driver = "podman"
|
2020-09-04 17:50:11 +00:00
|
|
|
alloc.Job.TaskGroups[0].Tasks[0].Config["image"] = "custom-${NOMAD_envoy_version}:latest"
|
2021-10-15 23:56:14 +00:00
|
|
|
allocDir, cleanupDir := allocdir.TestAllocDir(t, logger, "EnvoyVersionHook", alloc.ID)
|
2020-09-04 17:50:11 +00:00
|
|
|
defer cleanupDir()
|
|
|
|
|
|
|
|
// Setup a mock for Consul API
|
|
|
|
spAPI := consul.MockSupportedProxiesAPI{
|
|
|
|
Value: map[string][]string{
|
|
|
|
"envoy": {"1.14.1", "1.13.3"},
|
|
|
|
},
|
|
|
|
Error: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run envoy_version hook
|
|
|
|
h := newEnvoyVersionHook(newEnvoyVersionHookConfig(alloc, spAPI, logger))
|
|
|
|
|
|
|
|
// Create a prestart request
|
|
|
|
request := &ifs.TaskPrestartRequest{
|
|
|
|
Task: alloc.Job.TaskGroups[0].Tasks[0],
|
|
|
|
TaskDir: allocDir.NewTaskDir(alloc.Job.TaskGroups[0].Tasks[0].Name),
|
2020-12-11 15:57:13 +00:00
|
|
|
TaskEnv: taskEnvDefault,
|
2020-09-04 17:50:11 +00:00
|
|
|
}
|
2023-04-11 14:56:03 +00:00
|
|
|
must.NoError(t, request.TaskDir.Build(false, nil))
|
2020-09-04 17:50:11 +00:00
|
|
|
|
|
|
|
// Prepare a response
|
|
|
|
var response ifs.TaskPrestartResponse
|
|
|
|
|
|
|
|
// Run the hook
|
2023-04-11 14:56:03 +00:00
|
|
|
must.NoError(t, h.Prestart(context.Background(), request, &response))
|
2020-09-04 17:50:11 +00:00
|
|
|
|
|
|
|
// Assert the Task.Config[image] is concrete
|
2023-04-11 14:56:03 +00:00
|
|
|
must.Eq(t, "custom-1.14.1:latest", request.Task.Config["image"])
|
2020-09-04 17:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskRunner_EnvoyVersionHook_Prestart_skip(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-09-04 17:50:11 +00:00
|
|
|
|
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
|
|
|
|
// Setup an Allocation
|
|
|
|
alloc := mock.ConnectAlloc()
|
|
|
|
alloc.Job.TaskGroups[0].Tasks[0] = mock.ConnectSidecarTask()
|
|
|
|
alloc.Job.TaskGroups[0].Tasks[0].Driver = "exec"
|
|
|
|
alloc.Job.TaskGroups[0].Tasks[0].Config = map[string]interface{}{
|
|
|
|
"command": "/sidecar",
|
|
|
|
}
|
2021-10-15 23:56:14 +00:00
|
|
|
allocDir, cleanupDir := allocdir.TestAllocDir(t, logger, "EnvoyVersionHook", alloc.ID)
|
2020-09-04 17:50:11 +00:00
|
|
|
defer cleanupDir()
|
|
|
|
|
|
|
|
// Setup a mock for Consul API
|
|
|
|
spAPI := consul.MockSupportedProxiesAPI{
|
|
|
|
Value: map[string][]string{
|
|
|
|
"envoy": {"1.14.1", "1.13.3"},
|
|
|
|
},
|
|
|
|
Error: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run envoy_version hook
|
|
|
|
h := newEnvoyVersionHook(newEnvoyVersionHookConfig(alloc, spAPI, logger))
|
|
|
|
|
|
|
|
// Create a prestart request
|
|
|
|
request := &ifs.TaskPrestartRequest{
|
|
|
|
Task: alloc.Job.TaskGroups[0].Tasks[0],
|
|
|
|
TaskDir: allocDir.NewTaskDir(alloc.Job.TaskGroups[0].Tasks[0].Name),
|
2020-12-11 15:57:13 +00:00
|
|
|
TaskEnv: taskEnvDefault,
|
2020-09-04 17:50:11 +00:00
|
|
|
}
|
2023-04-11 14:56:03 +00:00
|
|
|
must.NoError(t, request.TaskDir.Build(false, nil))
|
2020-09-04 17:50:11 +00:00
|
|
|
|
|
|
|
// Prepare a response
|
|
|
|
var response ifs.TaskPrestartResponse
|
|
|
|
|
|
|
|
// Run the hook
|
2023-04-11 14:56:03 +00:00
|
|
|
must.NoError(t, h.Prestart(context.Background(), request, &response))
|
2020-09-04 17:50:11 +00:00
|
|
|
|
|
|
|
// Assert the Task.Config[image] does not get set
|
2023-04-11 14:56:03 +00:00
|
|
|
must.MapNotContainsKey(t, request.Task.Config, "image")
|
2020-09-04 17:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskRunner_EnvoyVersionHook_Prestart_fallback(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-09-04 17:50:11 +00:00
|
|
|
|
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
|
|
|
|
// Setup an Allocation
|
|
|
|
alloc := mock.ConnectAlloc()
|
|
|
|
alloc.Job.TaskGroups[0].Tasks[0] = mock.ConnectSidecarTask()
|
2021-10-15 23:56:14 +00:00
|
|
|
allocDir, cleanupDir := allocdir.TestAllocDir(t, logger, "EnvoyVersionHook", alloc.ID)
|
2020-09-04 17:50:11 +00:00
|
|
|
defer cleanupDir()
|
|
|
|
|
|
|
|
// Setup a mock for Consul API
|
|
|
|
spAPI := consul.MockSupportedProxiesAPI{
|
|
|
|
Value: nil, // old consul, no .xDS.SupportedProxies
|
|
|
|
Error: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run envoy_version hook
|
|
|
|
h := newEnvoyVersionHook(newEnvoyVersionHookConfig(alloc, spAPI, logger))
|
|
|
|
|
|
|
|
// Create a prestart request
|
|
|
|
request := &ifs.TaskPrestartRequest{
|
|
|
|
Task: alloc.Job.TaskGroups[0].Tasks[0],
|
|
|
|
TaskDir: allocDir.NewTaskDir(alloc.Job.TaskGroups[0].Tasks[0].Name),
|
2020-12-11 15:57:13 +00:00
|
|
|
TaskEnv: taskEnvDefault,
|
2020-09-04 17:50:11 +00:00
|
|
|
}
|
2023-04-11 14:56:03 +00:00
|
|
|
must.NoError(t, request.TaskDir.Build(false, nil))
|
2020-09-04 17:50:11 +00:00
|
|
|
|
|
|
|
// Prepare a response
|
|
|
|
var response ifs.TaskPrestartResponse
|
|
|
|
|
|
|
|
// Run the hook
|
2023-04-11 14:56:03 +00:00
|
|
|
must.NoError(t, h.Prestart(context.Background(), request, &response))
|
2020-09-04 17:50:11 +00:00
|
|
|
|
|
|
|
// Assert the Task.Config[image] is the fallback image
|
2023-04-11 14:56:03 +00:00
|
|
|
must.Eq(t, "envoyproxy/envoy:v1.11.2@sha256:a7769160c9c1a55bb8d07a3b71ce5d64f72b1f665f10d81aa1581bc3cf850d09", request.Task.Config["image"])
|
2020-09-04 17:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskRunner_EnvoyVersionHook_Prestart_error(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-09-04 17:50:11 +00:00
|
|
|
|
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
|
|
|
|
// Setup an Allocation
|
|
|
|
alloc := mock.ConnectAlloc()
|
|
|
|
alloc.Job.TaskGroups[0].Tasks[0] = mock.ConnectSidecarTask()
|
2021-10-15 23:56:14 +00:00
|
|
|
allocDir, cleanupDir := allocdir.TestAllocDir(t, logger, "EnvoyVersionHook", alloc.ID)
|
2020-09-04 17:50:11 +00:00
|
|
|
defer cleanupDir()
|
|
|
|
|
|
|
|
// Setup a mock for Consul API
|
|
|
|
spAPI := consul.MockSupportedProxiesAPI{
|
|
|
|
Value: nil,
|
|
|
|
Error: errors.New("some consul error"),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run envoy_version hook
|
|
|
|
h := newEnvoyVersionHook(newEnvoyVersionHookConfig(alloc, spAPI, logger))
|
|
|
|
|
|
|
|
// Create a prestart request
|
|
|
|
request := &ifs.TaskPrestartRequest{
|
|
|
|
Task: alloc.Job.TaskGroups[0].Tasks[0],
|
|
|
|
TaskDir: allocDir.NewTaskDir(alloc.Job.TaskGroups[0].Tasks[0].Name),
|
2020-12-11 15:57:13 +00:00
|
|
|
TaskEnv: taskEnvDefault,
|
2020-09-04 17:50:11 +00:00
|
|
|
}
|
2023-04-11 14:56:03 +00:00
|
|
|
must.NoError(t, request.TaskDir.Build(false, nil))
|
2020-09-04 17:50:11 +00:00
|
|
|
|
|
|
|
// Prepare a response
|
|
|
|
var response ifs.TaskPrestartResponse
|
|
|
|
|
|
|
|
// Run the hook, error should be recoverable
|
|
|
|
err := h.Prestart(context.Background(), request, &response)
|
2023-04-11 14:56:03 +00:00
|
|
|
must.ErrorContains(t, err, "error retrieving supported Envoy versions from Consul: some consul error")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskRunner_EnvoyVersionHook_Prestart_restart(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
|
|
|
|
// Setup an Allocation
|
|
|
|
alloc := mock.ConnectAlloc()
|
|
|
|
alloc.Job.TaskGroups[0].Tasks[0] = mock.ConnectSidecarTask()
|
|
|
|
allocDir, cleanupDir := allocdir.TestAllocDir(t, logger, "EnvoyVersionHook", alloc.ID)
|
|
|
|
defer cleanupDir()
|
|
|
|
|
|
|
|
// Set up a mock for Consul API.
|
|
|
|
mockProxiesAPI := consul.MockSupportedProxiesAPI{
|
|
|
|
Value: map[string][]string{
|
|
|
|
"envoy": {"1.15.0", "1.14.4"},
|
|
|
|
},
|
|
|
|
Error: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run envoy_version hook
|
|
|
|
h := newEnvoyVersionHook(newEnvoyVersionHookConfig(alloc, mockProxiesAPI, logger))
|
|
|
|
|
|
|
|
// Create a prestart request
|
|
|
|
request := &ifs.TaskPrestartRequest{
|
|
|
|
Task: alloc.Job.TaskGroups[0].Tasks[0],
|
|
|
|
TaskDir: allocDir.NewTaskDir(alloc.Job.TaskGroups[0].Tasks[0].Name),
|
|
|
|
TaskEnv: taskEnvDefault,
|
|
|
|
}
|
|
|
|
must.NoError(t, request.TaskDir.Build(false, nil))
|
|
|
|
|
|
|
|
// Prepare a response
|
|
|
|
var response ifs.TaskPrestartResponse
|
|
|
|
|
|
|
|
// Run the hook and ensure the tasks image has been modified.
|
|
|
|
must.NoError(t, h.Prestart(context.Background(), request, &response))
|
2023-05-02 14:27:48 +00:00
|
|
|
must.Eq(t, "docker.io/envoyproxy/envoy:v1.15.0", request.Task.Config["image"])
|
2023-04-11 14:56:03 +00:00
|
|
|
|
|
|
|
// Overwrite the previously modified image. This is the same behaviour that
|
|
|
|
// occurs when the server sends a non-destructive allocation update.
|
|
|
|
request.Task.Config["image"] = "${meta.connect.sidecar_image}"
|
|
|
|
|
|
|
|
// Run the Prestart hook function again, and ensure the image is updated.
|
|
|
|
must.NoError(t, h.Prestart(context.Background(), request, &response))
|
2023-05-02 14:27:48 +00:00
|
|
|
must.Eq(t, "docker.io/envoyproxy/envoy:v1.15.0", request.Task.Config["image"])
|
2020-09-04 17:50:11 +00:00
|
|
|
|
2023-04-11 14:56:03 +00:00
|
|
|
// Run the hook again, and ensure the config is still the same mimicking
|
|
|
|
// a non-user initiated restart.
|
|
|
|
must.NoError(t, h.Prestart(context.Background(), request, &response))
|
2023-05-02 14:27:48 +00:00
|
|
|
must.Eq(t, "docker.io/envoyproxy/envoy:v1.15.0", request.Task.Config["image"])
|
2020-09-04 17:50:11 +00:00
|
|
|
}
|