-
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.
Merge pull request #14219 from hashicorp/e2e-nsd-checks
e2e: add e2e tests for nomad service disco checks
- Loading branch information
Showing
6 changed files
with
171 additions
and
2 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,37 @@ | ||
job "checks_happy" { | ||
datacenters = ["dc1"] | ||
type = "service" | ||
|
||
constraint { | ||
attribute = "${attr.kernel.name}" | ||
value = "linux" | ||
} | ||
|
||
group "group" { | ||
network { | ||
mode = "host" | ||
port "http" {} | ||
} | ||
|
||
service { | ||
provider = "nomad" | ||
name = "http-server" | ||
port = "http" | ||
check { | ||
name = "http-server-check" | ||
type = "http" | ||
path = "/" | ||
interval = "2s" | ||
timeout = "1s" | ||
} | ||
} | ||
|
||
task "python-http" { | ||
driver = "raw_exec" | ||
config { | ||
command = "python3" | ||
args = ["-m", "http.server", "${NOMAD_PORT_http}"] | ||
} | ||
} | ||
} | ||
} |
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,38 @@ | ||
job "checks_sad" { | ||
datacenters = ["dc1"] | ||
type = "service" | ||
|
||
constraint { | ||
attribute = "${attr.kernel.name}" | ||
value = "linux" | ||
} | ||
|
||
group "group" { | ||
network { | ||
mode = "host" | ||
port "http" {} | ||
} | ||
|
||
service { | ||
provider = "nomad" | ||
name = "http-server" | ||
port = "http" | ||
check { | ||
name = "http-server-check" | ||
type = "http" | ||
path = "/" | ||
method = "POST" # not allowed by http.server | ||
interval = "2s" | ||
timeout = "1s" | ||
} | ||
} | ||
|
||
task "python-http" { | ||
driver = "raw_exec" | ||
config { | ||
command = "python3" | ||
args = ["-m", "http.server", "${NOMAD_PORT_http}"] | ||
} | ||
} | ||
} | ||
} |
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,83 @@ | ||
package servicediscovery | ||
|
||
import ( | ||
"context" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/nomad/e2e/e2eutil" | ||
"github.com/hashicorp/nomad/helper/uuid" | ||
"github.com/shoenig/test/must" | ||
) | ||
|
||
func testChecksHappy(t *testing.T) { | ||
nomadClient := e2eutil.NomadClient(t) | ||
|
||
// Generate our unique job ID which will be used for this test. | ||
jobID := "nsd-check-happy-" + 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) | ||
|
||
// Register the happy checks job. | ||
allocStubs := e2eutil.RegisterAndWaitForAllocs(t, nomadClient, jobChecksHappy, jobID, "") | ||
must.Len(t, 1, allocStubs) | ||
|
||
// wait for the alloc to be running | ||
e2eutil.WaitForAllocRunning(t, nomadClient, allocStubs[0].ID) | ||
|
||
// get the output of 'nomad alloc checks' | ||
output, err := e2eutil.AllocChecks(allocStubs[0].ID) | ||
must.NoError(t, err) | ||
|
||
// assert the output contains success | ||
statusRe := regexp.MustCompile(`Status\s+=\s+success`) | ||
must.RegexMatch(t, statusRe, output) | ||
|
||
// assert the output contains 200 status code | ||
statusCodeRe := regexp.MustCompile(`StatusCode\s+=\s+200`) | ||
must.RegexMatch(t, statusCodeRe, output) | ||
|
||
// assert output contains nomad's success string | ||
must.StrContains(t, output, `nomad: http ok`) | ||
} | ||
|
||
func testChecksSad(t *testing.T) { | ||
nomadClient := e2eutil.NomadClient(t) | ||
|
||
// Generate our unique job ID which will be used for this test. | ||
jobID := "nsd-check-sad-" + 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) | ||
|
||
// Register the sad checks job. | ||
allocStubs := e2eutil.RegisterAndWaitForAllocs(t, nomadClient, jobChecksSad, jobID, "") | ||
must.Len(t, 1, allocStubs) | ||
|
||
// wait for the alloc to be running | ||
e2eutil.WaitForAllocRunning(t, nomadClient, allocStubs[0].ID) | ||
|
||
// get the output of 'nomad alloc checks' | ||
output, err := e2eutil.AllocChecks(allocStubs[0].ID) | ||
must.NoError(t, err) | ||
|
||
// assert the output contains failure | ||
statusRe := regexp.MustCompile(`Status\s+=\s+failure`) | ||
must.RegexMatch(t, statusRe, output) | ||
|
||
// assert the output contains 501 status code | ||
statusCodeRe := regexp.MustCompile(`StatusCode\s+=\s+501`) | ||
must.RegexMatch(t, statusCodeRe, output) | ||
|
||
// assert output contains error output from python http.server | ||
must.StrContains(t, output, `<p>Error code explanation: HTTPStatus.NOT_IMPLEMENTED - Server does not support this operation.</p>`) | ||
} |
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