965f00b2fc
* nomad: add admission controller framework * nomad: add admission controller framework and Consul Connect hooks * run admission controllers before checking permissions * client: add default node meta for connect configurables * nomad: remove validateJob func since it has been moved to admission controller * nomad: use new TaskKind type * client: use consts for connect sidecar image and log level * Apply suggestions from code review Co-Authored-By: Michael Schurter <mschurter@hashicorp.com> * nomad: add job register test with connect sidecar * Update nomad/job_endpoint_hooks.go Co-Authored-By: Michael Schurter <mschurter@hashicorp.com>
87 lines
1.5 KiB
Go
87 lines
1.5 KiB
Go
package nomad
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func Test_isSidecarForService(t *testing.T) {
|
|
cases := []struct {
|
|
t *structs.Task // task
|
|
s string // service
|
|
r bool // result
|
|
}{
|
|
{
|
|
&structs.Task{},
|
|
"api",
|
|
false,
|
|
},
|
|
{
|
|
&structs.Task{
|
|
Kind: "connect-proxy:api",
|
|
},
|
|
"api",
|
|
true,
|
|
},
|
|
{
|
|
&structs.Task{
|
|
Kind: "connect-proxy:api",
|
|
},
|
|
"db",
|
|
false,
|
|
},
|
|
{
|
|
&structs.Task{
|
|
Kind: "api",
|
|
},
|
|
"api",
|
|
false,
|
|
},
|
|
}
|
|
|
|
for _, c := range cases {
|
|
require.Equal(t, c.r, isSidecarForService(c.t, c.s))
|
|
}
|
|
}
|
|
|
|
func Test_groupConnectHook(t *testing.T) {
|
|
// Test that connect-proxy task is inserted for backend service
|
|
tgIn := &structs.TaskGroup{
|
|
Networks: structs.Networks{
|
|
{
|
|
Mode: "bridge",
|
|
},
|
|
},
|
|
Services: []*structs.Service{
|
|
{
|
|
Name: "backend",
|
|
PortLabel: "8080",
|
|
Connect: &structs.ConsulConnect{
|
|
SidecarService: &structs.ConsulSidecarService{},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
tgOut := tgIn.Copy()
|
|
tgOut.Tasks = []*structs.Task{
|
|
newConnectTask(tgOut.Services[0]),
|
|
}
|
|
tgOut.Networks[0].DynamicPorts = []structs.Port{
|
|
{
|
|
Label: fmt.Sprintf("%s-%s", structs.ConnectProxyPrefix, "backend"),
|
|
To: -1,
|
|
},
|
|
}
|
|
|
|
require.NoError(t, groupConnectHook(tgIn))
|
|
require.Exactly(t, tgOut, tgIn)
|
|
|
|
// Test that hook is idempotent
|
|
require.NoError(t, groupConnectHook(tgIn))
|
|
require.Exactly(t, tgOut, tgIn)
|
|
}
|