|
| 1 | +#! /bin/bash |
| 2 | +# Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +readonly containerd_dind_ctr="container-config-containerd-dind-ctr-name" |
| 17 | +readonly containerd_test_ctr="container-config-containerd-test-ctr-name" |
| 18 | +readonly containerd_dind_socket="/run/nvidia/docker.sock" |
| 19 | +readonly containerd_dind_containerd_dir="/run/docker/containerd" |
| 20 | + |
| 21 | +testing::containerd::dind::setup() { |
| 22 | + # Docker creates /etc/docker when starting |
| 23 | + # by default there isn't any config in this directory (even after the daemon starts) |
| 24 | + docker run -d --rm --privileged \ |
| 25 | + -v "${shared_dir}/etc/docker:/etc/docker" \ |
| 26 | + -v "${shared_dir}/run/nvidia:/run/nvidia" \ |
| 27 | + -v "${shared_dir}/usr/local/nvidia:/usr/local/nvidia" \ |
| 28 | + -v "${shared_dir}/run/docker/containerd:/run/docker/containerd" \ |
| 29 | + --name "${containerd_dind_ctr}" \ |
| 30 | + docker:dind -H unix://${containerd_dind_socket} |
| 31 | +} |
| 32 | + |
| 33 | +testing::containerd::dind::exec() { |
| 34 | + docker exec "${containerd_dind_ctr}" sh -c "$*" |
| 35 | +} |
| 36 | + |
| 37 | +testing::containerd::toolkit::run() { |
| 38 | + local version=${1} |
| 39 | + |
| 40 | + # We run ctr image list to ensure that containerd has successfully started in the docker-in-docker container |
| 41 | + with_retry 5 5s testing::containerd::dind::exec " \ |
| 42 | + ctr --address=${containerd_dind_containerd_dir}/containerd.sock image list -q" |
| 43 | + |
| 44 | + # Ensure that we can run some non GPU containers from within dind |
| 45 | + with_retry 3 5s testing::containerd::dind::exec " \ |
| 46 | + ctr --address=${containerd_dind_containerd_dir}/containerd.sock image pull nvcr.io/nvidia/cuda:11.1.1-base-ubuntu20.04; \ |
| 47 | + ctr --address=${containerd_dind_containerd_dir}/containerd.sock run --rm --runtime=io.containerd.runtime.v1.linux nvcr.io/nvidia/cuda:11.1.1-base-ubuntu20.04 cuda echo foo" |
| 48 | + |
| 49 | + # Share the volumes so that we can edit the config file and point to the new runtime |
| 50 | + # Share the pid so that we can ask docker to reload its config |
| 51 | + docker run --rm --privileged \ |
| 52 | + --volumes-from "${containerd_dind_ctr}" \ |
| 53 | + -v "${shared_dir}/etc/containerd/config_${version}.toml:${containerd_dind_containerd_dir}/containerd.toml" \ |
| 54 | + --pid "container:${containerd_dind_ctr}" \ |
| 55 | + -e RUNTIME="containerd" \ |
| 56 | + -e RUNTIME_ARGS="--config=${containerd_dind_containerd_dir}/containerd.toml --socket=${containerd_dind_containerd_dir}/containerd.sock" \ |
| 57 | + --name "${containerd_test_ctr}" \ |
| 58 | + "${toolkit_container_image}" "/usr/local/nvidia" "--no-daemon" |
| 59 | + |
| 60 | + # We run ctr image list to ensure that containerd has successfully started in the docker-in-docker container |
| 61 | + with_retry 5 5s testing::containerd::dind::exec " \ |
| 62 | + ctr --address=${containerd_dind_containerd_dir}/containerd.sock image list -q" |
| 63 | + |
| 64 | + # Ensure that we haven't broken non GPU containers |
| 65 | + with_retry 3 5s testing::containerd::dind::exec " \ |
| 66 | + ctr --address=${containerd_dind_containerd_dir}/containerd.sock image pull nvcr.io/nvidia/cuda:11.1.1-base-ubuntu20.04; \ |
| 67 | + ctr --address=${containerd_dind_containerd_dir}/containerd.sock run --rm --runtime=io.containerd.runtime.v1.linux nvcr.io/nvidia/cuda:11.1.1-base-ubuntu20.04 cuda echo foo" |
| 68 | +} |
| 69 | + |
| 70 | +# This test runs containerd setup and containerd cleanup in succession to ensure that the |
| 71 | +# config is restored correctly. |
| 72 | +testing::containerd::toolkit::test_config() { |
| 73 | + local version=${1} |
| 74 | + |
| 75 | + # We run ctr image list to ensure that containerd has successfully started in the docker-in-docker container |
| 76 | + with_retry 5 5s testing::containerd::dind::exec " \ |
| 77 | + ctr --address=${containerd_dind_containerd_dir}/containerd.sock image list -q" |
| 78 | + |
| 79 | + local input_config="${shared_dir}/etc/containerd/config_${version}.toml" |
| 80 | + local output_config="${shared_dir}/output/config_${version}.toml" |
| 81 | + local output_dir=$(dirname ${output_config}) |
| 82 | + |
| 83 | + mkdir -p ${output_dir} |
| 84 | + cp -p "${input_config}" "${output_config}" |
| 85 | + |
| 86 | + docker run --rm --privileged \ |
| 87 | + --volumes-from "${containerd_dind_ctr}" \ |
| 88 | + -v "${output_dir}:${output_dir}" \ |
| 89 | + --name "${containerd_test_ctr}" \ |
| 90 | + --entrypoint sh \ |
| 91 | + "${toolkit_container_image}" -c "containerd setup \ |
| 92 | + --config=${output_config} \ |
| 93 | + --socket=${containerd_dind_containerd_dir}/containerd.sock \ |
| 94 | + --restart-mode=none \ |
| 95 | + /usr/local/nvidia/toolkit" |
| 96 | + |
| 97 | + # As a basic test we check that the config has changed |
| 98 | + diff "${input_config}" "${output_config}" || test ${?} -ne 0 |
| 99 | + grep -q -E "^version = \d" "${output_config}" |
| 100 | + grep -q -E "default_runtime_name = \"nvidia\"" "${output_config}" |
| 101 | + |
| 102 | + docker run --rm --privileged \ |
| 103 | + --volumes-from "${containerd_dind_ctr}" \ |
| 104 | + -v "${output_dir}:${output_dir}" \ |
| 105 | + --name "${containerd_test_ctr}" \ |
| 106 | + --entrypoint sh \ |
| 107 | + "${toolkit_container_image}" -c "containerd cleanup \ |
| 108 | + --config=${output_config} \ |
| 109 | + --socket=${containerd_dind_containerd_dir}/containerd.sock \ |
| 110 | + --restart-mode=none \ |
| 111 | + /usr/local/nvidia/toolkit" |
| 112 | + |
| 113 | + if [[ -s "${input_config}" ]]; then |
| 114 | + # Compare the input and output config. These should be the same. |
| 115 | + diff "${input_config}" "${output_config}" || true |
| 116 | + else |
| 117 | + # If the input config is empty, the output should not exist. |
| 118 | + test ! -e "${output_config}" |
| 119 | + fi |
| 120 | +} |
| 121 | + |
| 122 | +testing::containerd::main() { |
| 123 | + testing::containerd::dind::setup |
| 124 | + |
| 125 | + testing::containerd::toolkit::test_config empty |
| 126 | + testing::containerd::toolkit::test_config v1 |
| 127 | + testing::containerd::toolkit::test_config v2 |
| 128 | + |
| 129 | + testing::containerd::cleanup |
| 130 | + |
| 131 | + testing::containerd::dind::setup |
| 132 | + testing::containerd::toolkit::run empty |
| 133 | + testing::containerd::cleanup |
| 134 | + |
| 135 | + testing::containerd::dind::setup |
| 136 | + testing::containerd::toolkit::run v1 |
| 137 | + testing::containerd::cleanup |
| 138 | + |
| 139 | + testing::containerd::dind::setup |
| 140 | + testing::containerd::toolkit::run v2 |
| 141 | + testing::containerd::cleanup |
| 142 | +} |
| 143 | + |
| 144 | +testing::containerd::cleanup() { |
| 145 | + docker kill "${containerd_dind_ctr}" &> /dev/null || true |
| 146 | + docker kill "${containerd_test_ctr}" &> /dev/null || true |
| 147 | +} |
0 commit comments