Skip to content

Commit

Permalink
e2e: cni args tests (hashicorp#23597)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Bennett <dbennett@hashicorp.com>
  • Loading branch information
martisah and gulducat authored Jul 15, 2024
1 parent d688b71 commit bc81c85
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 1 deletion.
30 changes: 30 additions & 0 deletions e2e/cni/cni_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
40 changes: 40 additions & 0 deletions e2e/cni/input/cni_args.nomad.hcl
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
9 changes: 9 additions & 0 deletions e2e/terraform/packer/ubuntu-jammy-amd64/cni_args.conflist
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"cniVersion": "1.0.0",
"name": "cni_args",
"plugins": [
{
"type": "cni_args.sh"
}
]
}
70 changes: 70 additions & 0 deletions e2e/terraform/packer/ubuntu-jammy-amd64/cni_args.sh
Original file line number Diff line number Diff line change
@@ -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 <<EOF
{
"cniVersion": "1.0.0",
"supportedVersions": [ "0.1.0", "0.2.0", "0.3.0", "0.3.1", "0.4.0", "1.0.0" ]
}
EOF
}

# https://github.com/containernetworking/cni/blob/main/SPEC.md#add-success
function add() {
# get our task dir out of the env var
task_dir="$(echo "$CNI_ARGS" | tr ';' '\n' | awk -F= '/^FancyTaskDir=/ {print$2}')"
message="$(echo "$CNI_ARGS" | tr ';' '\n' | awk -F= '/^FancyMessage=/ {print$2}')"
1>&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 <<EOF
{
"cniVersion" : "1.0.0",
"ips": [
{
"address": "10.1.0.5/16",
"gateway": "10.1.0.1",
"interface": 1
}
],
"routes": [
{
"dst": "0.0.0.0/0"
}
],
"interfaces": [
{
"name": "cni0",
"mac": "00:11:22:33:44:55"
}
],
"dns": {
"nameservers": [ "10.1.0.1" ]
}
}
EOF

7 changes: 6 additions & 1 deletion e2e/terraform/packer/ubuntu-jammy-amd64/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# setup script for Ubuntu Linux 22.04. Assumes that Packer has placed
# build-time config files at /tmp/linux

set -euo pipefail
set -xeuo pipefail

NOMAD_PLUGIN_DIR=/opt/nomad/plugins/

Expand All @@ -22,6 +22,7 @@ mkdir_for_root /opt
mkdir_for_root /opt/bin # for envoy
mkdir_for_root /srv/data # for host volumes
mkdir_for_root /opt/cni/bin
mkdir_for_root /opt/cni/config

# Dependencies
sudo apt-get update
Expand Down Expand Up @@ -117,6 +118,10 @@ wget -q -O - \
https://github.com/containernetworking/plugins/releases/download/v1.0.0/cni-plugins-linux-amd64-v1.0.0.tgz \
| sudo tar -C /opt/cni/bin -xz

# Copy cni_args plugin and network configuration files into opt/cni/bin and opt/cni/config
sudo mv /tmp/linux/cni_args.conflist /opt/cni/config
sudo mv /tmp/linux/cni_args.sh /opt/cni/bin

# Podman
echo "Installing Podman"
sudo apt-get -y install podman catatonit
Expand Down

0 comments on commit bc81c85

Please sign in to comment.