|
| 1 | +// Copyright 2023 Nutanix. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package containerdapplypatchesandrestart |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + . "github.com/onsi/ginkgo/v2" |
| 10 | + "github.com/onsi/gomega" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1" |
| 13 | + runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" |
| 14 | + |
| 15 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation" |
| 16 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest" |
| 17 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest/request" |
| 18 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/test/helpers" |
| 19 | +) |
| 20 | + |
| 21 | +func TestContainerdApplyPatchesAndRestartPatch(t *testing.T) { |
| 22 | + gomega.RegisterFailHandler(Fail) |
| 23 | + RunSpecs(t, "Containerd apply patches and restart mutator suite") |
| 24 | +} |
| 25 | + |
| 26 | +var _ = Describe("Generate Containerd apply patches and restart patches", func() { |
| 27 | + // only add aws region patch |
| 28 | + patchGenerator := func() mutation.GeneratePatches { |
| 29 | + return mutation.NewMetaGeneratePatchesHandler("", helpers.TestEnv.Client, NewPatch()).(mutation.GeneratePatches) |
| 30 | + } |
| 31 | + |
| 32 | + testDefs := []capitest.PatchTestDef{ |
| 33 | + { |
| 34 | + Name: "restart script and command added to control plane kubeadm config spec", |
| 35 | + RequestItem: request.NewKubeadmControlPlaneTemplateRequestItem(""), |
| 36 | + ExpectedPatchMatchers: []capitest.JSONPatchMatcher{ |
| 37 | + { |
| 38 | + Operation: "add", |
| 39 | + Path: "/spec/template/spec/kubeadmConfigSpec/files", |
| 40 | + ValueMatcher: gomega.HaveExactElements( |
| 41 | + gomega.HaveKeyWithValue( |
| 42 | + "path", containerdApplyPatchesScriptOnRemote, |
| 43 | + ), |
| 44 | + gomega.HaveKeyWithValue( |
| 45 | + "path", ContainerdRestartScriptOnRemote, |
| 46 | + ), |
| 47 | + ), |
| 48 | + }, |
| 49 | + { |
| 50 | + Operation: "add", |
| 51 | + Path: "/spec/template/spec/kubeadmConfigSpec/preKubeadmCommands", |
| 52 | + ValueMatcher: gomega.HaveExactElements( |
| 53 | + containerdApplyPatchesScriptOnRemoteCommand, |
| 54 | + ContainerdRestartScriptOnRemoteCommand, |
| 55 | + ), |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + Name: "restart script and command added to worker node kubeadm config template", |
| 61 | + Vars: []runtimehooksv1.Variable{ |
| 62 | + capitest.VariableWithValue( |
| 63 | + "builtin", |
| 64 | + map[string]any{ |
| 65 | + "machineDeployment": map[string]any{ |
| 66 | + "class": "*", |
| 67 | + }, |
| 68 | + }, |
| 69 | + ), |
| 70 | + }, |
| 71 | + RequestItem: request.NewKubeadmConfigTemplateRequestItem(""), |
| 72 | + ExpectedPatchMatchers: []capitest.JSONPatchMatcher{ |
| 73 | + { |
| 74 | + Operation: "add", |
| 75 | + Path: "/spec/template/spec/files", |
| 76 | + ValueMatcher: gomega.HaveExactElements( |
| 77 | + gomega.HaveKeyWithValue( |
| 78 | + "path", containerdApplyPatchesScriptOnRemote, |
| 79 | + ), |
| 80 | + gomega.HaveKeyWithValue( |
| 81 | + "path", ContainerdRestartScriptOnRemote, |
| 82 | + ), |
| 83 | + ), |
| 84 | + }, |
| 85 | + { |
| 86 | + Operation: "add", |
| 87 | + Path: "/spec/template/spec/preKubeadmCommands", |
| 88 | + ValueMatcher: gomega.HaveExactElements( |
| 89 | + containerdApplyPatchesScriptOnRemoteCommand, |
| 90 | + ContainerdRestartScriptOnRemoteCommand, |
| 91 | + ), |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + } |
| 96 | + |
| 97 | + // create test node for each case |
| 98 | + for testIdx := range testDefs { |
| 99 | + tt := testDefs[testIdx] |
| 100 | + It(tt.Name, func() { |
| 101 | + capitest.AssertGeneratePatches( |
| 102 | + GinkgoT(), |
| 103 | + patchGenerator, |
| 104 | + &tt, |
| 105 | + ) |
| 106 | + }) |
| 107 | + } |
| 108 | +}) |
| 109 | + |
| 110 | +func Test_generateContainerdApplyPatchesScript(t *testing.T) { |
| 111 | + wantFile := bootstrapv1.File{ |
| 112 | + Path: "/etc/containerd/apply-patches.sh", |
| 113 | + Owner: "", |
| 114 | + Permissions: "0700", |
| 115 | + Encoding: "", |
| 116 | + Append: false, |
| 117 | + //nolint:lll // just a long string |
| 118 | + Content: `#!/bin/bash |
| 119 | +set -euo pipefail |
| 120 | +IFS=$'\n\t' |
| 121 | +
|
| 122 | +# This script is used to merge the TOML files in the patch directory into the containerd configuration file. |
| 123 | +
|
| 124 | +# Check if there are any TOML files in the patch directory, exiting if none are found. |
| 125 | +# Use a for loop that will only run a maximum of once to check if there are any files in the patch directory because |
| 126 | +# using -e does not work with globs. |
| 127 | +# See https://github.com/koalaman/shellcheck/wiki/SC2144 for an explanation of the following loop. |
| 128 | +patches_exist=false |
| 129 | +for file in "/etc/containerd/cre.d"/*.toml; do |
| 130 | + if [ -e "${file}" ]; then |
| 131 | + patches_exist=true |
| 132 | + fi |
| 133 | + # Always break after the first iteration. |
| 134 | + break |
| 135 | +done |
| 136 | +
|
| 137 | +if [ "${patches_exist}" = false ]; then |
| 138 | + echo "No TOML files found in the patch directory: /etc/containerd/cre.d - nothing to do" |
| 139 | + exit 0 |
| 140 | +fi |
| 141 | +
|
| 142 | +# Use go template variable to avoid hard-coding the toml-merge image name in this script. |
| 143 | +declare -r TOML_MERGE_IMAGE="ghcr.io/mesosphere/toml-merge:v0.2.0" |
| 144 | +
|
| 145 | +# Check if the toml-merge image is already present in ctr images list, if not pull it. |
| 146 | +if ! ctr --namespace k8s.io images check "name==${TOML_MERGE_IMAGE}" | grep "${TOML_MERGE_IMAGE}" >/dev/null; then |
| 147 | + ctr --namespace k8s.io images pull "${TOML_MERGE_IMAGE}" |
| 148 | +fi |
| 149 | +
|
| 150 | +# Cleanup the temporary directory on exit. |
| 151 | +cleanup() { |
| 152 | + ctr images unmount "${tmp_ctr_mount_dir}" || true |
| 153 | +} |
| 154 | +trap 'cleanup' EXIT |
| 155 | +
|
| 156 | +# Create a temporary directory to mount the toml-merge image filesystem. |
| 157 | +readonly tmp_ctr_mount_dir="$(mktemp -d)" |
| 158 | +
|
| 159 | +# Mount the toml-merge image filesystem and run the toml-merge binary to merge the TOML files. |
| 160 | +ctr --namespace k8s.io images mount "${TOML_MERGE_IMAGE}" "${tmp_ctr_mount_dir}" |
| 161 | +"${tmp_ctr_mount_dir}/usr/local/bin/toml-merge" -i --patch-file "/etc/containerd/cre.d/*.toml" /etc/containerd/config.toml |
| 162 | +`, |
| 163 | + } |
| 164 | + wantCmd := "/bin/bash /etc/containerd/apply-patches.sh" |
| 165 | + file, cmd, _ := generateContainerdApplyPatchesScript() |
| 166 | + assert.Equal(t, wantFile, file) |
| 167 | + assert.Equal(t, wantCmd, cmd) |
| 168 | +} |
0 commit comments