diff --git a/e2e/cni/cni_test.go b/e2e/cni/cni_test.go new file mode 100644 index 000000000..6525503a1 --- /dev/null +++ b/e2e/cni/cni_test.go @@ -0,0 +1,30 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: BUSL-1.1 + +package cni + +import ( + "testing" + + "github.com/hashicorp/nomad/e2e/v3/cluster3" + "github.com/hashicorp/nomad/e2e/v3/jobs3" + "github.com/shoenig/test/must" +) + +func TestCNIIntegration(t *testing.T) { + cluster3.Establish(t, + cluster3.Leader(), + cluster3.LinuxClients(1), + ) + + t.Run("testArgs", testCNIArgs) +} + +func testCNIArgs(t *testing.T) { + job, _ := jobs3.Submit(t, "./input/cni_args.nomad.hcl") + logs := job.Exec("group", "task", []string{"cat", "local/victory"}) + t.Logf("FancyMessage: %s", logs.Stdout) + // "global" is the Nomad node's region, interpolated in the jobspec, + // passed through the CNI plugin, and cat-ed by the task. + must.Eq(t, "global\n", logs.Stdout) +} diff --git a/e2e/cni/input/cni_args.nomad.hcl b/e2e/cni/input/cni_args.nomad.hcl new file mode 100644 index 000000000..1d58b774a --- /dev/null +++ b/e2e/cni/input/cni_args.nomad.hcl @@ -0,0 +1,40 @@ +# Copyright (c) HashiCorp, Inc. +# SPDX-License-Identifier: BUSL-1.1 + +job "cni_args" { + group "group" { + network { + mode = "cni/cni_args" + cni { + # feature under test + args = { + # the message gets placed as a file called "victory" + # in the task dir specified here by the cni_args.sh plugin + FancyMessage = "${node.region}" + FancyTaskDir = "${NOMAD_ALLOC_DIR}/task/local" + } + } + } + task "task" { + driver = "docker" + config { + image = "busybox:1" + command = "sh" + args = ["-c", "cat local/victory; sleep 60"] + } + } + # go faster + update { + min_healthy_time = "0s" + } + # fail faster (if it does fail) + reschedule { + attempts = 0 + unlimited = false + } + restart { + attempts = 0 + mode = "fail" + } + } +} diff --git a/e2e/terraform/packer/ubuntu-jammy-amd64/cni_args.conflist b/e2e/terraform/packer/ubuntu-jammy-amd64/cni_args.conflist new file mode 100644 index 000000000..ae119111d --- /dev/null +++ b/e2e/terraform/packer/ubuntu-jammy-amd64/cni_args.conflist @@ -0,0 +1,9 @@ +{ + "cniVersion": "1.0.0", + "name": "cni_args", + "plugins": [ + { + "type": "cni_args.sh" + } + ] +} diff --git a/e2e/terraform/packer/ubuntu-jammy-amd64/cni_args.sh b/e2e/terraform/packer/ubuntu-jammy-amd64/cni_args.sh new file mode 100755 index 000000000..7735d35d5 --- /dev/null +++ b/e2e/terraform/packer/ubuntu-jammy-amd64/cni_args.sh @@ -0,0 +1,70 @@ +#!/usr/bin/env bash + +# Copyright (c) HashiCorp, Inc. +# SPDX-License-Identifier: BUSL-1.1 + +set -euo pipefail + +# things are prefixed with "Fancy*" because this is a fancy plugin. +# CNI_ARGS='IgnoreUnknown=true;FancyTaskDir=/tmp/cni_args;FancyMessage=hiiii;Another=whatever' +# what we need to do: +# 1. read CNI_ARGS environment variable +# * write to a file named $FancyTaskDir/victory +# 2. write CNI-spec json to stdout for Nomad to read + +# https://github.com/containernetworking/cni/blob/main/SPEC.md#version-success +function version() { + cat <&2 echo "got task dir: $task_dir; message: $message" + + mkdir -p "$task_dir" + # and write something to a file we can check in the test. + echo "$message" > "$task_dir/victory" +} + +# run the appropriate CNI command +case "$CNI_COMMAND" in + VERSION) version ; exit ;; + ADD) add ;; +esac + +# bogus reply so nomad doesn't error +cat <