Skip to content

Commit be3666c

Browse files
Rename test folder to tests
Signed-off-by: Carlos Eduardo Arango Gutierrez <[email protected]>
1 parent d75b1ad commit be3666c

File tree

704 files changed

+428185
-10
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

704 files changed

+428185
-10
lines changed

.github/dependabot.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ updates:
99
directories:
1010
- "/"
1111
- "deployments/devel"
12+
- "tests"
1213
schedule:
1314
interval: "daily"
1415
labels:

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*.swp
44
*.swo
55
/coverage.out*
6-
/test/output/
6+
/tests/output/
77
/nvidia-container-runtime
88
/nvidia-container-runtime.*
99
/nvidia-container-runtime-hook

DEVELOPMENT.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ environment variables.
3434

3535
## Testing packages locally
3636

37-
The [test/release](./test/release/) folder contains documentation on how the installation of local or staged packages can be tested.
37+
The [tests/release](./tests/release/) folder contains documentation on how the installation of local or staged packages can be tested.
3838

3939

4040
## Releasing

cmd/nvidia-container-runtime/main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import (
2222
const (
2323
nvidiaRuntime = "nvidia-container-runtime"
2424
nvidiaHook = "nvidia-container-runtime-hook"
25-
bundlePathSuffix = "test/output/bundle/"
25+
bundlePathSuffix = "tests/output/bundle/"
2626
specFile = "config.json"
27-
unmodifiedSpecFileSuffix = "test/input/test_spec.json"
27+
unmodifiedSpecFileSuffix = "tests/input/test_spec.json"
2828
)
2929

3030
const (

internal/oci/spec_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ func TestMaintainSpec(t *testing.T) {
1919
}
2020

2121
for _, f := range files {
22-
inputSpecPath := filepath.Join(moduleRoot, "test/input", f)
22+
inputSpecPath := filepath.Join(moduleRoot, "tests/input", f)
2323

2424
spec := NewFileSpec(inputSpecPath).(*fileSpec)
2525

2626
_, err := spec.Load()
2727
require.NoError(t, err)
2828

29-
outputSpecPath := filepath.Join(moduleRoot, "test/output", f)
29+
outputSpecPath := filepath.Join(moduleRoot, "tests/output", f)
3030
spec.path = outputSpecPath
3131
spec.Flush()
3232

internal/platform-support/tegra/csv/csv_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestGetFileList(t *testing.T) {
3636
}{
3737
{
3838
description: "returns list of CSV files",
39-
root: "test/input/csv_samples/",
39+
root: "tests/input/csv_samples/",
4040
files: []string{
4141
"jetson.csv",
4242
"simple_wrong.csv",
@@ -46,15 +46,15 @@ func TestGetFileList(t *testing.T) {
4646
},
4747
{
4848
description: "handles empty folder",
49-
root: "test/input/csv_samples/empty",
49+
root: "tests/input/csv_samples/empty",
5050
},
5151
{
5252
description: "handles non-existent folder",
53-
root: "test/input/csv_samples/NONEXISTENT",
53+
root: "tests/input/csv_samples/NONEXISTENT",
5454
},
5555
{
5656
description: "handles non-existent folder root",
57-
root: "/NONEXISTENT/test/input/csv_samples/",
57+
root: "/NONEXISTENT/tests/input/csv_samples/",
5858
},
5959
}
6060

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
echo mock hook

tests/bin/runc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
echo mock runc

tests/container/common.sh

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#! /bin/bash
2+
# Copyright (c) 2019-2021, 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 CRIO_HOOKS_DIR="/usr/share/containers/oci/hooks.d"
17+
readonly CRIO_HOOK_FILENAME="oci-nvidia-hook.json"
18+
19+
# shellcheck disable=SC2015
20+
[ -t 2 ] && readonly LOG_TTY=1 || readonly LOG_NO_TTY=1
21+
22+
if [ "${LOG_TTY-0}" -eq 1 ] && [ "$(tput colors)" -ge 15 ]; then
23+
readonly FMT_BOLD=$(tput bold)
24+
readonly FMT_RED=$(tput setaf 1)
25+
readonly FMT_YELLOW=$(tput setaf 3)
26+
readonly FMT_BLUE=$(tput setaf 12)
27+
readonly FMT_CLEAR=$(tput sgr0)
28+
fi
29+
30+
log() {
31+
local -r level="$1"; shift
32+
local -r message="$*"
33+
34+
local fmt_on="${FMT_CLEAR-}"
35+
local -r fmt_off="${FMT_CLEAR-}"
36+
37+
case "${level}" in
38+
INFO) fmt_on="${FMT_BLUE-}" ;;
39+
WARN) fmt_on="${FMT_YELLOW-}" ;;
40+
ERROR) fmt_on="${FMT_RED-}" ;;
41+
esac
42+
printf "%s[%s]%s %b\n" "${fmt_on}" "${level}" "${fmt_off}" "${message}" >&2
43+
}
44+
45+
with_retry() {
46+
local max_attempts="$1"
47+
local delay="$2"
48+
local count=0
49+
local rc
50+
shift 2
51+
52+
while true; do
53+
set +e
54+
"$@"; rc="$?"
55+
set -e
56+
57+
count="$((count+1))"
58+
59+
if [[ "${rc}" -eq 0 ]]; then
60+
return 0
61+
fi
62+
63+
if [[ "${max_attempts}" -le 0 ]] || [[ "${count}" -lt "${max_attempts}" ]]; then
64+
sleep "${delay}"
65+
else
66+
break
67+
fi
68+
done
69+
70+
return 1
71+
}
72+
73+
testing::setup() {
74+
cp -Rp ${basedir}/shared ${shared_dir}
75+
mkdir -p "${shared_dir}/etc/containerd"
76+
mkdir -p "${shared_dir}/etc/docker"
77+
mkdir -p "${shared_dir}/run/docker/containerd"
78+
mkdir -p "${shared_dir}/run/nvidia"
79+
mkdir -p "${shared_dir}/usr/local/nvidia"
80+
mkdir -p "${shared_dir}${CRIO_HOOKS_DIR}"
81+
}
82+
83+
testing::cleanup() {
84+
if [[ "${CLEANUP}" == "false" ]]; then
85+
echo "Skipping cleanup: CLEANUP=${CLEANUP}"
86+
return 0
87+
fi
88+
if [[ -e "${shared_dir}" ]]; then
89+
docker run --rm \
90+
-v "${shared_dir}:/work" \
91+
alpine sh -c 'rm -rf /work/*'
92+
rmdir "${shared_dir}"
93+
fi
94+
95+
if [[ "${test_cases:-""}" == "" ]]; then
96+
echo "No test cases defined. Skipping test case cleanup"
97+
return 0
98+
fi
99+
100+
for tc in ${test_cases}; do
101+
testing::${tc}::cleanup
102+
done
103+
}
104+
105+
testing::docker_run::toolkit::shell() {
106+
docker run --rm --privileged \
107+
--entrypoint sh \
108+
-v "${shared_dir}/etc/containerd:/etc/containerd" \
109+
-v "${shared_dir}/etc/docker:/etc/docker" \
110+
-v "${shared_dir}/run/docker/containerd:/run/docker/containerd" \
111+
-v "${shared_dir}/run/nvidia:/run/nvidia" \
112+
-v "${shared_dir}/usr/local/nvidia:/usr/local/nvidia" \
113+
-v "${shared_dir}${CRIO_HOOKS_DIR}:${CRIO_HOOKS_DIR}" \
114+
"${toolkit_container_image}" "-c" "$*"
115+
}
116+
117+

tests/container/containerd_test.sh

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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

Comments
 (0)