Skip to content

Commit

Permalink
e2e: add a test for checking default WI Consul workflow for services …
Browse files Browse the repository at this point in the history
…and tasks (#19500)
  • Loading branch information
pkazmierczak authored Jan 2, 2024
1 parent 76ba3e1 commit bb3d222
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions e2e/consul/consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ func TestConsul(t *testing.T) {

t.Run("testServiceReversion", testServiceReversion)
t.Run("testAllocRestart", testAllocRestart)
t.Run("testConsulWI", testConsulWI)
}
63 changes: 63 additions & 0 deletions e2e/consul/consul_wi_test.go
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)
}
62 changes: 62 additions & 0 deletions e2e/consul/input/consul_wi.nomad.hcl
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"
}
}
}
}
}

0 comments on commit bb3d222

Please sign in to comment.