2019-01-26 05:06:50 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2020-08-18 22:37:02 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-01-26 05:06:50 +00:00
|
|
|
|
2021-04-14 23:02:42 +00:00
|
|
|
api "github.com/hashicorp/nomad/api"
|
2019-01-26 05:06:50 +00:00
|
|
|
"github.com/hashicorp/nomad/e2e/e2eutil"
|
|
|
|
"github.com/hashicorp/nomad/e2e/framework"
|
2020-08-18 22:37:02 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2019-01-26 05:06:50 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2020-08-18 22:37:02 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2019-01-26 05:06:50 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-08-18 22:37:02 +00:00
|
|
|
const (
|
|
|
|
consulJobBasic = "consul/input/consul_example.nomad"
|
|
|
|
consulJobCanaryTags = "consul/input/canary_tags.nomad"
|
2021-01-04 22:06:47 +00:00
|
|
|
|
|
|
|
consulJobRegisterOnUpdatePart1 = "consul/input/services_empty.nomad"
|
|
|
|
consulJobRegisterOnUpdatePart2 = "consul/input/services_present.nomad"
|
2020-08-18 22:37:02 +00:00
|
|
|
)
|
|
|
|
|
2021-04-14 23:02:42 +00:00
|
|
|
const (
|
|
|
|
// unless otherwise set, tests should just use the default consul namespace
|
|
|
|
consulNamespace = "default"
|
|
|
|
)
|
|
|
|
|
2019-01-26 05:06:50 +00:00
|
|
|
type ConsulE2ETest struct {
|
|
|
|
framework.TC
|
|
|
|
jobIds []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
framework.AddSuites(&framework.TestSuite{
|
|
|
|
Component: "Consul",
|
|
|
|
CanRunLocal: true,
|
|
|
|
Consul: true,
|
|
|
|
Cases: []framework.TestCase{
|
|
|
|
new(ConsulE2ETest),
|
2019-09-03 20:47:53 +00:00
|
|
|
new(ScriptChecksE2ETest),
|
2021-01-21 21:05:39 +00:00
|
|
|
new(CheckRestartE2ETest),
|
2021-02-04 15:18:03 +00:00
|
|
|
new(OnUpdateChecksTest),
|
2021-04-14 23:02:42 +00:00
|
|
|
new(ConsulNamespacesE2ETest),
|
2019-01-26 05:06:50 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *ConsulE2ETest) BeforeAll(f *framework.F) {
|
|
|
|
e2eutil.WaitForLeader(f.T(), tc.Nomad())
|
|
|
|
e2eutil.WaitForNodesReady(f.T(), tc.Nomad(), 1)
|
|
|
|
}
|
|
|
|
|
2020-08-18 22:37:02 +00:00
|
|
|
func (tc *ConsulE2ETest) AfterEach(f *framework.F) {
|
|
|
|
if os.Getenv("NOMAD_TEST_SKIPCLEANUP") == "1" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, id := range tc.jobIds {
|
2021-01-04 22:06:47 +00:00
|
|
|
_, _, err := tc.Nomad().Jobs().Deregister(id, true, nil)
|
|
|
|
require.NoError(f.T(), err)
|
2020-08-18 22:37:02 +00:00
|
|
|
}
|
|
|
|
tc.jobIds = []string{}
|
2021-01-04 22:06:47 +00:00
|
|
|
require.NoError(f.T(), tc.Nomad().System().GarbageCollect())
|
2019-01-28 02:15:35 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 22:37:02 +00:00
|
|
|
// TestConsulRegistration asserts that a job registers services with tags in Consul.
|
2019-01-26 05:06:50 +00:00
|
|
|
func (tc *ConsulE2ETest) TestConsulRegistration(f *framework.F) {
|
2020-08-18 22:37:02 +00:00
|
|
|
t := f.T()
|
2021-01-04 22:06:47 +00:00
|
|
|
r := require.New(t)
|
2020-08-18 22:37:02 +00:00
|
|
|
|
2019-01-26 05:06:50 +00:00
|
|
|
nomadClient := tc.Nomad()
|
2021-01-04 22:06:47 +00:00
|
|
|
jobId := "consul" + uuid.Short()
|
2019-01-26 05:06:50 +00:00
|
|
|
tc.jobIds = append(tc.jobIds, jobId)
|
|
|
|
|
2021-01-04 22:06:47 +00:00
|
|
|
allocations := e2eutil.RegisterAndWaitForAllocs(f.T(), nomadClient, consulJobBasic, jobId, "")
|
|
|
|
require.Equal(t, 3, len(allocations))
|
|
|
|
allocIDs := e2eutil.AllocIDsFromAllocationListStubs(allocations)
|
2020-08-18 22:37:02 +00:00
|
|
|
e2eutil.WaitForAllocsRunning(t, tc.Nomad(), allocIDs)
|
2019-01-26 05:06:50 +00:00
|
|
|
|
2020-08-18 22:37:02 +00:00
|
|
|
expectedTags := []string{
|
|
|
|
"cache",
|
|
|
|
"global",
|
2019-01-26 05:06:50 +00:00
|
|
|
}
|
2020-08-18 22:37:02 +00:00
|
|
|
|
|
|
|
// Assert services get registered
|
2021-04-14 23:02:42 +00:00
|
|
|
e2eutil.RequireConsulRegistered(r, tc.Consul(), consulNamespace, "consul-example", 3)
|
2021-01-04 22:06:47 +00:00
|
|
|
services, _, err := tc.Consul().Catalog().Service("consul-example", "", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
for _, s := range services {
|
|
|
|
// If we've made it this far the tags should *always* match
|
|
|
|
require.ElementsMatch(t, expectedTags, s.ServiceTags)
|
|
|
|
}
|
2020-08-18 22:37:02 +00:00
|
|
|
|
|
|
|
// Stop the job
|
|
|
|
e2eutil.WaitForJobStopped(t, nomadClient, jobId)
|
2019-01-26 05:06:50 +00:00
|
|
|
|
2021-01-04 22:06:47 +00:00
|
|
|
// Verify that services were de-registered in Consul
|
2021-04-14 23:02:42 +00:00
|
|
|
e2eutil.RequireConsulDeregistered(r, tc.Consul(), consulNamespace, "consul-example")
|
2021-01-04 22:06:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *ConsulE2ETest) TestConsulRegisterOnUpdate(f *framework.F) {
|
|
|
|
t := f.T()
|
|
|
|
r := require.New(t)
|
|
|
|
|
|
|
|
nomadClient := tc.Nomad()
|
|
|
|
catalog := tc.Consul().Catalog()
|
|
|
|
jobID := "consul" + uuid.Short()
|
|
|
|
tc.jobIds = append(tc.jobIds, jobID)
|
|
|
|
|
|
|
|
// Initial job has no services for task.
|
|
|
|
allocations := e2eutil.RegisterAndWaitForAllocs(t, nomadClient, consulJobRegisterOnUpdatePart1, jobID, "")
|
|
|
|
require.Equal(t, 1, len(allocations))
|
|
|
|
allocIDs := e2eutil.AllocIDsFromAllocationListStubs(allocations)
|
|
|
|
e2eutil.WaitForAllocsRunning(t, tc.Nomad(), allocIDs)
|
|
|
|
|
|
|
|
// Assert service not yet registered.
|
|
|
|
results, _, err := catalog.Service("nc-service", "", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, results)
|
|
|
|
|
|
|
|
// On update, add services for task.
|
|
|
|
allocations = e2eutil.RegisterAndWaitForAllocs(t, nomadClient, consulJobRegisterOnUpdatePart2, jobID, "")
|
|
|
|
require.Equal(t, 1, len(allocations))
|
|
|
|
allocIDs = e2eutil.AllocIDsFromAllocationListStubs(allocations)
|
|
|
|
e2eutil.WaitForAllocsRunning(t, tc.Nomad(), allocIDs)
|
|
|
|
|
|
|
|
// Assert service is now registered.
|
2021-04-14 23:02:42 +00:00
|
|
|
e2eutil.RequireConsulRegistered(r, tc.Consul(), consulNamespace, "nc-service", 1)
|
2019-01-26 05:06:50 +00:00
|
|
|
}
|
2019-01-28 02:15:35 +00:00
|
|
|
|
2020-08-18 22:37:02 +00:00
|
|
|
// TestCanaryInplaceUpgrades verifies setting and unsetting canary tags
|
2019-01-28 02:15:35 +00:00
|
|
|
func (tc *ConsulE2ETest) TestCanaryInplaceUpgrades(f *framework.F) {
|
2020-08-18 22:37:02 +00:00
|
|
|
t := f.T()
|
2021-01-04 22:06:47 +00:00
|
|
|
|
|
|
|
// TODO(shoenig) https://github.com/hashicorp/nomad/issues/9627
|
|
|
|
t.Skip("THIS TEST IS BROKEN (#9627)")
|
|
|
|
|
2019-01-28 02:15:35 +00:00
|
|
|
nomadClient := tc.Nomad()
|
2020-08-18 22:37:02 +00:00
|
|
|
consulClient := tc.Consul()
|
|
|
|
jobId := "consul" + uuid.Generate()[0:8]
|
2019-01-28 02:15:35 +00:00
|
|
|
tc.jobIds = append(tc.jobIds, jobId)
|
|
|
|
|
2020-08-18 22:37:02 +00:00
|
|
|
allocs := e2eutil.RegisterAndWaitForAllocs(f.T(), nomadClient, consulJobCanaryTags, jobId, "")
|
|
|
|
require.Equal(t, 2, len(allocs))
|
2019-01-28 02:15:35 +00:00
|
|
|
|
2020-08-18 22:37:02 +00:00
|
|
|
allocIDs := e2eutil.AllocIDsFromAllocationListStubs(allocs)
|
|
|
|
e2eutil.WaitForAllocsRunning(t, nomadClient, allocIDs)
|
|
|
|
|
|
|
|
// Start a deployment
|
|
|
|
job, _, err := nomadClient.Jobs().Info(jobId, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
job.Meta = map[string]string{"version": "2"}
|
|
|
|
resp, _, err := nomadClient.Jobs().Register(job, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, resp.EvalID)
|
|
|
|
|
|
|
|
// Eventually have a canary
|
|
|
|
var activeDeploy *api.Deployment
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
deploys, _, err := nomadClient.Jobs().Deployments(jobId, false, nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if expected := 2; len(deploys) != expected {
|
|
|
|
return false, fmt.Errorf("expected 2 deploys but found %v", deploys)
|
|
|
|
}
|
2019-01-28 02:15:35 +00:00
|
|
|
|
|
|
|
for _, d := range deploys {
|
2020-08-18 22:37:02 +00:00
|
|
|
if d.Status == structs.DeploymentStatusRunning {
|
|
|
|
activeDeploy = d
|
|
|
|
break
|
2019-01-28 02:15:35 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-18 22:37:02 +00:00
|
|
|
if activeDeploy == nil {
|
|
|
|
return false, fmt.Errorf("no running deployments: %v", deploys)
|
|
|
|
}
|
|
|
|
if expected := 1; len(activeDeploy.TaskGroups["consul_canary_test"].PlacedCanaries) != expected {
|
|
|
|
return false, fmt.Errorf("expected %d placed canaries but found %#v",
|
|
|
|
expected, activeDeploy.TaskGroups["consul_canary_test"])
|
|
|
|
}
|
2019-01-28 02:15:35 +00:00
|
|
|
|
2020-08-18 22:37:02 +00:00
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
2021-01-26 15:29:45 +00:00
|
|
|
f.NoError(err, "error while waiting for deploys")
|
2020-08-18 22:37:02 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
allocID := activeDeploy.TaskGroups["consul_canary_test"].PlacedCanaries[0]
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
alloc, _, err := nomadClient.Allocations().Info(allocID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if alloc.DeploymentStatus == nil {
|
|
|
|
return false, fmt.Errorf("canary alloc %s has no deployment status", allocID)
|
|
|
|
}
|
|
|
|
if alloc.DeploymentStatus.Healthy == nil {
|
|
|
|
return false, fmt.Errorf("canary alloc %s has no deployment health: %#v",
|
|
|
|
allocID, alloc.DeploymentStatus)
|
|
|
|
}
|
|
|
|
return *alloc.DeploymentStatus.Healthy, fmt.Errorf("expected healthy canary but found: %#v",
|
|
|
|
alloc.DeploymentStatus)
|
|
|
|
}, func(err error) {
|
2021-01-26 15:29:45 +00:00
|
|
|
f.NoError(err, "error waiting for canary to be healthy")
|
2020-08-18 22:37:02 +00:00
|
|
|
})
|
2019-01-28 02:15:35 +00:00
|
|
|
|
|
|
|
// Check Consul for canary tags
|
2020-08-18 22:37:02 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
consulServices, _, err := consulClient.Catalog().Service("canarytest", "", nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
for _, s := range consulServices {
|
|
|
|
if helper.CompareSliceSetString([]string{"canary", "foo"}, s.ServiceTags) {
|
|
|
|
return true, nil
|
2019-01-28 02:15:35 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-18 22:37:02 +00:00
|
|
|
return false, fmt.Errorf(`could not find service tags {"canary", "foo"}: %#v`, consulServices)
|
|
|
|
}, func(err error) {
|
2021-01-26 15:29:45 +00:00
|
|
|
f.NoError(err, "error waiting for canary tags")
|
2020-08-18 22:37:02 +00:00
|
|
|
})
|
2019-01-28 02:15:35 +00:00
|
|
|
|
2020-08-18 22:37:02 +00:00
|
|
|
// Promote canary
|
2019-01-28 02:15:35 +00:00
|
|
|
{
|
2020-08-18 22:37:02 +00:00
|
|
|
resp, _, err := nomadClient.Deployments().PromoteAll(activeDeploy.ID, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, resp.EvalID)
|
2019-01-28 02:15:35 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 22:37:02 +00:00
|
|
|
// Eventually canary is promoted
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
alloc, _, err := nomadClient.Allocations().Info(allocID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return !alloc.DeploymentStatus.Canary, fmt.Errorf("still a canary")
|
|
|
|
}, func(err error) {
|
2021-01-26 14:14:47 +00:00
|
|
|
require.NoError(t, err, "error waiting for canary to be promoted")
|
2020-08-18 22:37:02 +00:00
|
|
|
})
|
2019-01-28 02:15:35 +00:00
|
|
|
|
|
|
|
// Verify that no instances have canary tags
|
2020-08-18 22:37:02 +00:00
|
|
|
expected := []string{"foo", "bar"}
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
consulServices, _, err := consulClient.Catalog().Service("canarytest", "", nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
for _, s := range consulServices {
|
|
|
|
if !helper.CompareSliceSetString(expected, s.ServiceTags) {
|
|
|
|
return false, fmt.Errorf("expected %#v Consul tags but found %#v",
|
|
|
|
expected, s.ServiceTags)
|
2019-01-28 02:15:35 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-18 22:37:02 +00:00
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
2021-01-26 14:14:47 +00:00
|
|
|
require.NoError(t, err, "error waiting for non-canary tags")
|
2020-08-18 22:37:02 +00:00
|
|
|
})
|
2019-01-28 02:15:35 +00:00
|
|
|
|
|
|
|
}
|