-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
e2e: add a test for checking default WI Consul workflow for services …
…and tasks (#19500)
- Loading branch information
1 parent
76ba3e1
commit bb3d222
Showing
3 changed files
with
126 additions
and
0 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
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,63 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
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" | ||
) | ||
|
||
// testConsulWI_Service_and_Task asserts we can | ||
// - consigure Consul correctly with setup consul -y command | ||
// - run a job with a service and a task | ||
// - make sure the expected service is registered in Consul | ||
// - get Consul token written to a task secret directory | ||
func testConsulWI_Service_and_Task(t *testing.T) { | ||
const jobFile = "./input/consul_wi_example.nomad" | ||
jobID := "consul-wi-" + 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) | ||
|
||
// Run the setup helper that should configure Consul ACL with default | ||
// policies, roles, auth method and binding rules. | ||
_, err := e2eutil.Command("nomad", "setup", "consul", "-y") | ||
must.NoError(t, err) | ||
|
||
// register a job | ||
err = e2eutil.Register(jobID, jobFile) | ||
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) | ||
|
||
// check if the service is registered with Consul | ||
_, _, consulErr := consulClient.Catalog().Service("consul-example", "", nil) | ||
must.NoError(t, consulErr) | ||
|
||
allocID := e2eutil.SingleAllocID(t, jobID, "", 0) | ||
|
||
// secret dir should contain a Consul token file | ||
_, err = e2eutil.AllocExec(allocID, "example", "ls example/secrets/consul_token", "default", nil) | ||
must.NoError(t, err) | ||
|
||
// rendered template should contain a Consul token string | ||
err = e2eutil.WaitForAllocFile(allocID, "example/local/config.txt", func(content string) bool { | ||
return len(content) > 12 // CONSUL_TOKEN=actual_consul_token and not a blank string | ||
}, nil) | ||
must.NoError(t, err) | ||
} |
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,62 @@ | ||
# Copyright (c) HashiCorp, Inc. | ||
# SPDX-License-Identifier: BUSL-1.1 | ||
|
||
job "example" { | ||
|
||
constraint { | ||
attribute = "${attr.kernel.name}" | ||
value = "linux" | ||
} | ||
|
||
group "example" { | ||
network { | ||
port "db" { | ||
to = 5678 | ||
} | ||
} | ||
|
||
task "example" { | ||
driver = "docker" | ||
|
||
config { | ||
image = "busybox:1" | ||
command = "nc" | ||
args = ["-ll", "-p", "5678", "-e", "/bin/cat"] | ||
|
||
ports = ["db"] | ||
} | ||
|
||
identity { | ||
name = "consul_default" | ||
aud = ["consul.io"] | ||
} | ||
|
||
consul {} | ||
|
||
template { | ||
data = <<-EOT | ||
CONSUL_TOKEN={{ env "CONSUL_TOKEN" }} | ||
EOT | ||
destination = "local/config.txt" | ||
} | ||
|
||
resources { | ||
cpu = 100 | ||
memory = 100 | ||
} | ||
|
||
service { | ||
name = "consul-example" | ||
tags = ["global", "cache"] | ||
port = "db" | ||
|
||
check { | ||
name = "alive" | ||
type = "tcp" | ||
interval = "10s" | ||
timeout = "2s" | ||
} | ||
} | ||
} | ||
} | ||
} |