2019-08-15 15:22:37 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2019-12-12 23:46:14 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2019-08-15 15:22:37 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-06-22 17:55:59 +00:00
|
|
|
func TestJobEndpointConnect_isSidecarForService(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-08-15 15:22:37 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 17:55:59 +00:00
|
|
|
func TestJobEndpointConnect_groupConnectHook(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-08-15 15:22:37 +00:00
|
|
|
// Test that connect-proxy task is inserted for backend service
|
2019-12-12 23:46:14 +00:00
|
|
|
job := mock.Job()
|
|
|
|
job.TaskGroups[0] = &structs.TaskGroup{
|
2019-08-15 15:22:37 +00:00
|
|
|
Networks: structs.Networks{
|
|
|
|
{
|
|
|
|
Mode: "bridge",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Services: []*structs.Service{
|
|
|
|
{
|
|
|
|
Name: "backend",
|
|
|
|
PortLabel: "8080",
|
|
|
|
Connect: &structs.ConsulConnect{
|
|
|
|
SidecarService: &structs.ConsulSidecarService{},
|
|
|
|
},
|
|
|
|
},
|
2019-11-08 09:33:39 +00:00
|
|
|
{
|
|
|
|
Name: "admin",
|
|
|
|
PortLabel: "9090",
|
|
|
|
Connect: &structs.ConsulConnect{
|
|
|
|
SidecarService: &structs.ConsulSidecarService{},
|
|
|
|
},
|
|
|
|
},
|
2019-08-15 15:22:37 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-12-12 23:46:14 +00:00
|
|
|
// Expected tasks
|
|
|
|
tgOut := job.TaskGroups[0].Copy()
|
2019-08-15 15:22:37 +00:00
|
|
|
tgOut.Tasks = []*structs.Task{
|
2019-12-12 23:46:14 +00:00
|
|
|
newConnectTask(tgOut.Services[0].Name),
|
|
|
|
newConnectTask(tgOut.Services[1].Name),
|
2019-08-15 15:22:37 +00:00
|
|
|
}
|
2019-12-12 23:46:14 +00:00
|
|
|
|
|
|
|
// Expect sidecar tasks to be properly canonicalized
|
|
|
|
tgOut.Tasks[0].Canonicalize(job, tgOut)
|
|
|
|
tgOut.Tasks[1].Canonicalize(job, tgOut)
|
2019-08-15 15:22:37 +00:00
|
|
|
tgOut.Networks[0].DynamicPorts = []structs.Port{
|
|
|
|
{
|
|
|
|
Label: fmt.Sprintf("%s-%s", structs.ConnectProxyPrefix, "backend"),
|
|
|
|
To: -1,
|
|
|
|
},
|
2019-11-08 09:33:39 +00:00
|
|
|
{
|
|
|
|
Label: fmt.Sprintf("%s-%s", structs.ConnectProxyPrefix, "admin"),
|
|
|
|
To: -1,
|
|
|
|
},
|
2019-08-15 15:22:37 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 23:46:14 +00:00
|
|
|
require.NoError(t, groupConnectHook(job, job.TaskGroups[0]))
|
|
|
|
require.Exactly(t, tgOut, job.TaskGroups[0])
|
2019-08-15 15:22:37 +00:00
|
|
|
|
|
|
|
// Test that hook is idempotent
|
2019-12-12 23:46:14 +00:00
|
|
|
require.NoError(t, groupConnectHook(job, job.TaskGroups[0]))
|
|
|
|
require.Exactly(t, tgOut, job.TaskGroups[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestJobEndpoint_ConnectInterpolation asserts that when a Connect sidecar
|
|
|
|
// proxy task is being created for a group service with an interpolated name,
|
|
|
|
// the service name is interpolated *before the task is created.
|
|
|
|
//
|
|
|
|
// See https://github.com/hashicorp/nomad/issues/6853
|
2020-06-22 17:55:59 +00:00
|
|
|
func TestJobEndpointConnect_ConnectInterpolation(t *testing.T) {
|
2019-12-12 23:46:14 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
server := &Server{logger: testlog.HCLogger(t)}
|
|
|
|
jobEndpoint := NewJobEndpoints(server)
|
|
|
|
|
|
|
|
j := mock.ConnectJob()
|
|
|
|
j.TaskGroups[0].Services[0].Name = "${JOB}-api"
|
|
|
|
j, warnings, err := jobEndpoint.admissionMutators(j)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, warnings)
|
|
|
|
|
|
|
|
require.Len(t, j.TaskGroups[0].Tasks, 2)
|
|
|
|
require.Equal(t, "connect-proxy-my-job-api", j.TaskGroups[0].Tasks[1].Name)
|
2019-08-15 15:22:37 +00:00
|
|
|
}
|
2020-06-22 17:55:59 +00:00
|
|
|
|
|
|
|
func TestJobEndpointConnect_groupConnectSidecarValidate(t *testing.T) {
|
|
|
|
t.Run("sidecar 0 networks", func(t *testing.T) {
|
|
|
|
require.EqualError(t, groupConnectSidecarValidate(&structs.TaskGroup{
|
|
|
|
Name: "g1",
|
|
|
|
Networks: nil,
|
|
|
|
}), `Consul Connect sidecars require exactly 1 network, found 0 in group "g1"`)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("sidecar non bridge", func(t *testing.T) {
|
|
|
|
require.EqualError(t, groupConnectSidecarValidate(&structs.TaskGroup{
|
|
|
|
Name: "g2",
|
|
|
|
Networks: structs.Networks{{
|
|
|
|
Mode: "host",
|
|
|
|
}},
|
|
|
|
}), `Consul Connect sidecar requires bridge network, found "host" in group "g2"`)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("sidecar okay", func(t *testing.T) {
|
|
|
|
require.NoError(t, groupConnectSidecarValidate(&structs.TaskGroup{
|
|
|
|
Name: "g3",
|
|
|
|
Networks: structs.Networks{{
|
|
|
|
Mode: "bridge",
|
|
|
|
}},
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJobEndpointConnect_groupConnectNativeValidate(t *testing.T) {
|
|
|
|
t.Run("no task in service", func(t *testing.T) {
|
|
|
|
require.EqualError(t, groupConnectNativeValidate(&structs.TaskGroup{
|
|
|
|
Name: "g1",
|
|
|
|
}, &structs.Service{
|
|
|
|
Name: "s1",
|
|
|
|
TaskName: "",
|
|
|
|
}), `Consul Connect Native service "s1" requires task name`)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("no task for service", func(t *testing.T) {
|
|
|
|
require.EqualError(t, groupConnectNativeValidate(&structs.TaskGroup{
|
|
|
|
Name: "g2",
|
|
|
|
}, &structs.Service{
|
|
|
|
Name: "s2",
|
|
|
|
TaskName: "t1",
|
|
|
|
}), `Consul Connect Native service "s2" requires undefined task "t1" in group "g2"`)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("native okay", func(t *testing.T) {
|
|
|
|
require.NoError(t, groupConnectNativeValidate(&structs.TaskGroup{
|
|
|
|
Name: "g2",
|
|
|
|
Tasks: []*structs.Task{{Name: "t0"}, {Name: "t1"}, {Name: "t3"}},
|
|
|
|
}, &structs.Service{
|
|
|
|
Name: "s2",
|
|
|
|
TaskName: "t1",
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
}
|