2022-03-03 10:26:14 +00:00
|
|
|
package structs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-10-11 13:21:42 +00:00
|
|
|
"github.com/hashicorp/go-set"
|
|
|
|
"github.com/shoenig/test/must"
|
2022-03-03 10:26:14 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestServiceRegistrationsRequest_StaleReadSupport(t *testing.T) {
|
|
|
|
req := &AllocServiceRegistrationsRequest{}
|
|
|
|
require.True(t, req.IsRead())
|
|
|
|
}
|
2022-03-14 11:42:12 +00:00
|
|
|
|
|
|
|
func TestJob_RequiresNativeServiceDiscovery(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2022-10-11 13:21:42 +00:00
|
|
|
name string
|
|
|
|
inputJob *Job
|
|
|
|
expBasic []string
|
|
|
|
expChecks []string
|
2022-03-14 11:42:12 +00:00
|
|
|
}{
|
|
|
|
{
|
2022-10-11 13:21:42 +00:00
|
|
|
name: "multiple group services with Nomad provider",
|
2022-03-14 11:42:12 +00:00
|
|
|
inputJob: &Job{
|
|
|
|
TaskGroups: []*TaskGroup{
|
|
|
|
{
|
|
|
|
Name: "group1",
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "nomad"},
|
|
|
|
{Provider: "nomad"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "group2",
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "nomad"},
|
|
|
|
{Provider: "nomad"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-11 13:21:42 +00:00
|
|
|
expBasic: []string{"group1", "group2"},
|
|
|
|
expChecks: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple group services with Nomad provider with checks",
|
|
|
|
inputJob: &Job{
|
|
|
|
TaskGroups: []*TaskGroup{
|
|
|
|
{
|
|
|
|
Name: "group1",
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "nomad", Checks: []*ServiceCheck{{Name: "c1"}}},
|
|
|
|
{Provider: "nomad"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "group2",
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "nomad"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "group3",
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "nomad"},
|
|
|
|
{Provider: "nomad", Checks: []*ServiceCheck{{Name: "c2"}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expBasic: []string{"group1", "group2", "group3"},
|
|
|
|
expChecks: []string{"group1", "group3"},
|
2022-03-14 11:42:12 +00:00
|
|
|
},
|
|
|
|
{
|
2022-10-11 13:21:42 +00:00
|
|
|
name: "multiple task services with Nomad provider",
|
2022-03-14 11:42:12 +00:00
|
|
|
inputJob: &Job{
|
|
|
|
TaskGroups: []*TaskGroup{
|
|
|
|
{
|
|
|
|
Name: "group1",
|
|
|
|
Tasks: []*Task{
|
|
|
|
{
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "nomad"},
|
|
|
|
{Provider: "nomad"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "nomad"},
|
|
|
|
{Provider: "nomad"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "group2",
|
|
|
|
Tasks: []*Task{
|
|
|
|
{
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "nomad"},
|
|
|
|
{Provider: "nomad"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "nomad"},
|
2022-10-11 13:21:42 +00:00
|
|
|
{Provider: "nomad", Checks: []*ServiceCheck{{Name: "c1"}}},
|
2022-03-14 11:42:12 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-11 13:21:42 +00:00
|
|
|
expBasic: []string{"group1", "group2"},
|
|
|
|
expChecks: []string{"group2"},
|
2022-03-14 11:42:12 +00:00
|
|
|
},
|
|
|
|
{
|
2022-10-11 13:21:42 +00:00
|
|
|
name: "multiple group services with Consul provider",
|
2022-03-14 11:42:12 +00:00
|
|
|
inputJob: &Job{
|
|
|
|
TaskGroups: []*TaskGroup{
|
|
|
|
{
|
|
|
|
Name: "group1",
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "consul"},
|
|
|
|
{Provider: "consul"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "group2",
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "consul"},
|
|
|
|
{Provider: "consul"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-11 13:21:42 +00:00
|
|
|
expBasic: nil,
|
|
|
|
expChecks: nil,
|
2022-03-14 11:42:12 +00:00
|
|
|
},
|
|
|
|
{
|
2022-10-11 13:21:42 +00:00
|
|
|
name: "multiple task services with Consul provider",
|
2022-03-14 11:42:12 +00:00
|
|
|
inputJob: &Job{
|
|
|
|
TaskGroups: []*TaskGroup{
|
|
|
|
{
|
|
|
|
Name: "group1",
|
|
|
|
Tasks: []*Task{
|
|
|
|
{
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "consul"},
|
|
|
|
{Provider: "consul"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "consul"},
|
|
|
|
{Provider: "consul"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "group2",
|
|
|
|
Tasks: []*Task{
|
|
|
|
{
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "consul"},
|
|
|
|
{Provider: "consul"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "consul"},
|
|
|
|
{Provider: "consul"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-11 13:21:42 +00:00
|
|
|
expBasic: nil,
|
|
|
|
expChecks: nil,
|
2022-03-14 11:42:12 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-10-11 13:21:42 +00:00
|
|
|
nsdUsage := tc.inputJob.RequiredNativeServiceDiscovery()
|
|
|
|
must.Equal(t, set.From(tc.expBasic), nsdUsage.Basic)
|
|
|
|
must.Equal(t, set.From(tc.expChecks), nsdUsage.Checks)
|
2022-03-14 11:42:12 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-04-20 12:09:13 +00:00
|
|
|
|
|
|
|
func TestJob_RequiredConsulServiceDiscovery(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
inputJob *Job
|
|
|
|
expectedOutput map[string]bool
|
|
|
|
name string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
inputJob: &Job{
|
|
|
|
TaskGroups: []*TaskGroup{
|
|
|
|
{
|
|
|
|
Name: "group1",
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "consul"},
|
|
|
|
{Provider: "consul"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "group2",
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "consul"},
|
|
|
|
{Provider: "consul"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedOutput: map[string]bool{"group1": true, "group2": true},
|
|
|
|
name: "multiple group services with Consul provider",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
inputJob: &Job{
|
|
|
|
TaskGroups: []*TaskGroup{
|
|
|
|
{
|
|
|
|
Name: "group1",
|
|
|
|
Tasks: []*Task{
|
|
|
|
{
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "consul"},
|
|
|
|
{Provider: "consul"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "consul"},
|
|
|
|
{Provider: "consul"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "group2",
|
|
|
|
Tasks: []*Task{
|
|
|
|
{
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "consul"},
|
|
|
|
{Provider: "consul"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "consul"},
|
|
|
|
{Provider: "consul"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedOutput: map[string]bool{"group1": true, "group2": true},
|
|
|
|
name: "multiple task services with Consul provider",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
inputJob: &Job{
|
|
|
|
TaskGroups: []*TaskGroup{
|
|
|
|
{
|
|
|
|
Name: "group1",
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "nomad"},
|
|
|
|
{Provider: "nomad"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "group2",
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "nomad"},
|
|
|
|
{Provider: "nomad"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedOutput: map[string]bool{},
|
|
|
|
name: "multiple group services with Nomad provider",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
inputJob: &Job{
|
|
|
|
TaskGroups: []*TaskGroup{
|
|
|
|
{
|
|
|
|
Name: "group1",
|
|
|
|
Tasks: []*Task{
|
|
|
|
{
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "nomad"},
|
|
|
|
{Provider: "nomad"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "nomad"},
|
|
|
|
{Provider: "nomad"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "group2",
|
|
|
|
Tasks: []*Task{
|
|
|
|
{
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "nomad"},
|
|
|
|
{Provider: "nomad"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Services: []*Service{
|
|
|
|
{Provider: "nomad"},
|
|
|
|
{Provider: "nomad"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedOutput: map[string]bool{},
|
|
|
|
name: "multiple task services with Nomad provider",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
actualOutput := tc.inputJob.RequiredConsulServiceDiscovery()
|
|
|
|
require.Equal(t, tc.expectedOutput, actualOutput)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|