Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

e2e: add some e2e tests for pledge task driver #17909

Merged
merged 2 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions e2e/pledge/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

// Package pledge tests the community pledge task driver.
// https://github.com/shoenig/nomad-pledge-driver
package pledge
62 changes: 62 additions & 0 deletions e2e/pledge/input/bridge.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

job "bridge" {

constraint {
attribute = "${attr.kernel.name}"
value = "linux"
}

group "group" {
network {
mode = "bridge"
port "http" { to = 8181 }
}

service {
provider = "nomad"
name = "pybridge"
port = "http"
tags = ["public=${attr.unique.platform.aws.public-ipv4}"]
check {
name = "up"
type = "http"
path = "/index.html"
interval = "6s"
timeout = "1s"
}
}

task "python" {
driver = "pledge"
config {
command = "python3"
args = ["-m", "http.server", "8181", "--directory", "${NOMAD_TASK_DIR}"]
promises = "stdio rpath inet"
unveil = ["r:/etc/mime.types", "r:${NOMAD_TASK_DIR}"]
}

template {
destination = "local/index.html"
data = <<EOH
<!doctype html>
<html>
<title>bridge mode</title>
<body><p>Hello, pal!</p></body>
</html>
EOH
}
}

restart {
attempts = 0
mode = "fail"
}

update {
min_healthy_time = "4s"
}
}
}

41 changes: 41 additions & 0 deletions e2e/pledge/input/curl.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

variable "address" {
type = string
description = "The address to cURL"
}

job "curl" {
type = "batch"

constraint {
attribute = "${attr.kernel.name}"
value = "linux"
}

group "group" {
network {
mode = "host"
}

reschedule {
attempts = 0
unlimited = false
}

restart {
attempts = 0
mode = "fail"
}

task "curl" {
driver = "pledge"
config {
command = "curl"
args = ["${var.address}"]
promises = "stdio rpath inet dns sendfd"
}
}
}
}
39 changes: 39 additions & 0 deletions e2e/pledge/input/sleep.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

job "sleep" {
constraint {
attribute = "${attr.kernel.name}"
value = "linux"
}

group "group" {
update {
min_healthy_time = "4s"
}

reschedule {
attempts = 0
unlimited = false
}

restart {
attempts = 0
mode = "fail"
}

task "task" {
driver = "pledge"

config {
command = "sleep"
args = ["infinity"]
}

resources {
cpu = 10
memory = 32
}
}
}
}
34 changes: 34 additions & 0 deletions e2e/pledge/input/unveil.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

job "unveil" {
type = "batch"

constraint {
attribute = "${attr.kernel.name}"
value = "linux"
}

group "group" {

reschedule {
attempts = 0
unlimited = false
}

restart {
attempts = 0
mode = "fail"
}

task "cat" {
driver = "pledge"
config {
command = "cat"
args = ["/etc/passwd"]
promises = "stdio rpath"
unveil = ["r:/etc/passwd"]
}
}
}
}
64 changes: 64 additions & 0 deletions e2e/pledge/pledge_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package pledge

import (
"fmt"
"testing"
"time"

"github.com/hashicorp/nomad/e2e/e2eutil"
"github.com/hashicorp/nomad/e2e/v3/cluster3"
"github.com/hashicorp/nomad/e2e/v3/jobs3"
"github.com/shoenig/test/must"
)

func TestPledge(t *testing.T) {
cluster3.Establish(t,
cluster3.Leader(),
cluster3.LinuxClients(1),
cluster3.Timeout(10*time.Second),
)

t.Run("testSleep", testSleep)
t.Run("testBridgeNetwork", testBridgeNetwork)
t.Run("testUnveil", testUnveil)
}

func testSleep(t *testing.T) {
_, cleanup := jobs3.Submit(t, "./input/sleep.hcl")
t.Cleanup(cleanup)
}

func testBridgeNetwork(t *testing.T) {
_, cleanup := jobs3.Submit(t, "./input/bridge.hcl")
t.Cleanup(cleanup)

ip, port := findService(t, "pybridge")
address := fmt.Sprintf("http://%s:%d", ip, port)

curlJob, curlCleanup := jobs3.Submit(t, "./input/curl.hcl",
jobs3.Var("address", address),
jobs3.WaitComplete("curl"),
)
t.Cleanup(curlCleanup)

logs := curlJob.TaskLogs("group", "curl")
must.StrContains(t, logs.Stdout, "<title>bridge mode</title>")
}

func testUnveil(t *testing.T) {
job, cleanup := jobs3.Submit(t, "./input/unveil.hcl")
t.Cleanup(cleanup)

logs := job.TaskLogs("group", "cat")
must.StrContains(t, logs.Stdout, "root:x:0:0")
}

// findService returns the service address and port
func findService(t *testing.T, name string) (string, int) {
services, _, err := e2eutil.NomadClient(t).Services().Get(name, nil)
must.NoError(t, err)
return services[0].Address, services[0].Port
}
6 changes: 6 additions & 0 deletions e2e/terraform/etc/nomad.d/client-linux.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,9 @@ plugin "docker" {
}
}
}

plugin "nomad-pledge-driver" {
config {
pledge_executable = "/usr/local/bin/pledge"
}
}
1 change: 1 addition & 0 deletions e2e/terraform/packer/build
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Build an AMI for the target configuration

Examples
build ubuntu-jammy-amd64
build windows-2016-amd64

EOF

Expand Down
9 changes: 9 additions & 0 deletions e2e/terraform/packer/ubuntu-jammy-amd64/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ sudo apt-get -y install podman catatonit
echo "Installing Podman Driver"
sudo hc-install install --path ${NOMAD_PLUGIN_DIR} --version 0.4.2 nomad-driver-podman

# Pledge
echo "Installing Pledge Driver"
curl -fsSL -o /tmp/pledge-driver.tar.gz https://github.com/shoenig/nomad-pledge-driver/releases/download/v0.2.3/nomad-pledge-driver_0.2.3_linux_amd64.tar.gz
curl -fsSL -o /tmp/pledge https://github.com/shoenig/nomad-pledge-driver/releases/download/pledge-1.8.com/pledge-1.8.com
tar -C /tmp -xf /tmp/pledge-driver.tar.gz
sudo mv /tmp/nomad-pledge-driver ${NOMAD_PLUGIN_DIR}
sudo mv /tmp/pledge /usr/local/bin
sudo chmod +x /usr/local/bin/pledge

# ECS
if [ -a "/tmp/linux/nomad-driver-ecs" ]; then
echo "Installing nomad-driver-ecs"
Expand Down