-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
consul: restore consul token when reverting a job (#15996)
* consul: reset consul token on job during registration of a reversion * e2e: add test for reverting a job with a consul service * cl: fixup cl entry
- Loading branch information
Showing
5 changed files
with
142 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:bug | ||
consul: Fixed a bug where consul token was not respected when reverting a job | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
variable "service" { | ||
type = string | ||
} | ||
|
||
job "service-reversion" { | ||
datacenters = ["dc1"] | ||
type = "service" | ||
|
||
constraint { | ||
attribute = "${attr.kernel.name}" | ||
value = "linux" | ||
} | ||
|
||
group "sleep" { | ||
|
||
service { | ||
name = "${var.service}" | ||
} | ||
|
||
task "busybox" { | ||
driver = "docker" | ||
|
||
config { | ||
image = "busybox:1" | ||
command = "sleep" | ||
args = ["infinity"] | ||
} | ||
|
||
resources { | ||
cpu = 16 | ||
memory = 32 | ||
disk = 64 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package consul | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/nomad/e2e/e2eutil" | ||
"github.com/hashicorp/nomad/helper/uuid" | ||
"github.com/hashicorp/nomad/nomad/structs" | ||
"github.com/shoenig/test/must" | ||
) | ||
|
||
func TestConsul(t *testing.T) { | ||
// todo: migrate the remaining consul tests | ||
|
||
nomad := e2eutil.NomadClient(t) | ||
|
||
e2eutil.WaitForLeader(t, nomad) | ||
e2eutil.WaitForNodesReady(t, nomad, 1) | ||
|
||
t.Run("testServiceReversion", testServiceReversion) | ||
} | ||
|
||
// testServiceReversion asserts we can | ||
// - submit a job with a service | ||
// - update that job and modify service | ||
// - revert the job, restoring the original service | ||
func testServiceReversion(t *testing.T) { | ||
const jobFile = "./input/service_reversion.nomad" | ||
jobID := "service-reversion-" + uuid.Short() | ||
jobIDs := []string{jobID} | ||
|
||
// Defer a cleanup function to remove the job. This will trigger if the | ||
// test fails, unless the cancel function is called. | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
defer e2eutil.CleanupJobsAndGCWithContext(t, ctx, &jobIDs) | ||
|
||
// initial register of job, with service="one" | ||
vars := []string{"-var", "service=one"} | ||
err := e2eutil.RegisterWithArgs(jobID, jobFile, vars...) | ||
must.NoError(t, err) | ||
|
||
// wait for job to be running | ||
err = e2eutil.WaitForAllocStatusExpected(jobID, "", []string{structs.AllocClientStatusRunning}) | ||
must.NoError(t, err) | ||
|
||
// get our consul client | ||
consulClient := e2eutil.ConsulClient(t) | ||
|
||
assertService := func(name string, count int) { | ||
services, _, consulErr := consulClient.Catalog().Service(name, "", nil) | ||
must.NoError(t, consulErr) | ||
must.Len(t, count, services, must.Sprintf("expected %d instances of %s, got %d", count, name, len(services))) | ||
} | ||
|
||
// query services, assert 1 instance of "one" | ||
assertService("one", 1) | ||
assertService("two", 0) | ||
|
||
// second register of job, with service="two" | ||
vars = []string{"-var", "service=two"} | ||
err = e2eutil.RegisterWithArgs(jobID, jobFile, vars...) | ||
must.NoError(t, err) | ||
|
||
// wait for job to be running | ||
err = e2eutil.WaitForAllocStatusExpected(jobID, "", []string{structs.AllocClientStatusRunning}) | ||
must.NoError(t, err) | ||
|
||
// query services, assert 0 instance of "one" (replaced), 1 of "two" | ||
assertService("one", 0) | ||
assertService("two", 1) | ||
|
||
// now revert our job back to version 0 | ||
err = e2eutil.Revert(jobID, jobFile, 0) | ||
must.NoError(t, err) | ||
|
||
// wait for job to be running | ||
err = e2eutil.WaitForAllocStatusExpected(jobID, "", []string{structs.AllocClientStatusRunning}) | ||
must.NoError(t, err) | ||
|
||
// query services, assert 1 instance of "one" (reverted), 1 of "two" (removed) | ||
assertService("one", 1) | ||
assertService("two", 0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters