From 1c64ccbce25be92c6de5e5c19b09fb42e64feeed Mon Sep 17 00:00:00 2001 From: Dan Webb Date: Wed, 11 Dec 2024 09:41:29 +0000 Subject: [PATCH] Add tests Signed-off-by: Dan Webb --- .rubocop.yml | 2 ++ spec/docker_test/container_spec.rb | 13 ++++++++ spec/libraries/container_spec.rb | 33 ++++++++++++++++++- .../docker_test/recipes/container.rb | 12 +++++++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 .rubocop.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 000000000..61883002d --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,2 @@ +require: + - cookstyle diff --git a/spec/docker_test/container_spec.rb b/spec/docker_test/container_spec.rb index 6263ee42f..1e61adad3 100644 --- a/spec/docker_test/container_spec.rb +++ b/spec/docker_test/container_spec.rb @@ -945,4 +945,17 @@ ) end end + + context 'testing GPU support' do + let(:chef_run) { ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '18.04').converge(described_recipe) } + + it 'creates a container with GPU support' do + expect(chef_run).to run_docker_container('gpu_test').with( + repo: 'nvidia/cuda', + tag: 'latest', + gpus: 'all', + gpu_driver: 'nvidia' + ) + end + end end diff --git a/spec/libraries/container_spec.rb b/spec/libraries/container_spec.rb index da39b674e..00aa532d7 100644 --- a/spec/libraries/container_spec.rb +++ b/spec/libraries/container_spec.rb @@ -98,7 +98,7 @@ expect { chef_run }.to_not raise_error expect(chef_run).to create_docker_container('hello_world').with( tag: 'ubuntu:latest', - create_options: { 'name' => 'hello_world', 'Image' => 'hello_world:ubuntu:latest', 'Labels' => {}, 'Cmd' => nil, 'AttachStderr' => false, 'AttachStdin' => false, 'AttachStdout' => false, 'Domainname' => '', 'Entrypoint' => nil, 'Env' => [], 'ExposedPorts' => {}, 'Hostname' => nil, 'MacAddress' => nil, 'NetworkDisabled' => false, 'OpenStdin' => false, 'StdinOnce' => false, 'Tty' => false, 'User' => nil, 'Volumes' => {}, 'WorkingDir' => nil, 'HostConfig' => { 'Binds' => nil, 'CapAdd' => nil, 'CapDrop' => nil, 'CgroupParent' => '', 'CgroupnsMode' => 'private', 'CpuShares' => 0, 'CpusetCpus' => '', 'Devices' => [], 'Dns' => [], 'DnsSearch' => [], 'ExtraHosts' => nil, 'IpcMode' => 'shareable', 'Init' => nil, 'KernelMemory' => 0, 'Links' => nil, 'LogConfig' => nil, 'Memory' => 0, 'MemorySwap' => nil, 'MemoryReservation' => 0, 'NanoCpus' => 0, 'NetworkMode' => 'bridge', 'OomKillDisable' => false, 'OomScoreAdj' => -500, 'Privileged' => false, 'PidMode' => '', 'PortBindings' => {}, 'PublishAllPorts' => false, 'RestartPolicy' => { 'Name' => nil, 'MaximumRetryCount' => 0 }, 'ReadonlyRootfs' => false, 'Runtime' => 'runc', 'SecurityOpt' => nil, 'ShmSize' => 67108864, 'Sysctls' => {}, 'Tmpfs' => {}, 'Ulimits' => nil, 'UsernsMode' => '', 'UTSMode' => '', 'VolumesFrom' => nil, 'VolumeDriver' => nil }, 'NetworkingConfig' => { 'EndpointsConfig' => { 'bridge' => { 'IPAMConfig' => { 'IPv4Address' => nil }, 'Aliases' => [] } } }, 'Healthcheck' => { 'Test' => ['string'], 'Interval' => 0, 'Timeout' => 0, 'Retries' => 0, 'StartPeriod' => 0 } } + create_options: { 'name' => 'hello_world', 'Image' => 'hello_world:ubuntu:latest', 'Labels' => {}, 'Cmd' => nil, 'AttachStderr' => false, 'AttachStdin' => false, 'AttachStdout' => false, 'Domainname' => '', 'Entrypoint' => nil, 'Env' => [], 'ExposedPorts' => {}, 'Healthcheck' => nil, 'Hostname' => nil, 'MacAddress' => nil, 'NetworkDisabled' => false, 'OpenStdin' => false, 'StdinOnce' => false, 'Tty' => false, 'User' => nil, 'Volumes' => {}, 'WorkingDir' => nil, 'HostConfig' => { 'Binds' => nil, 'CapAdd' => nil, 'CapDrop' => nil, 'CgroupParent' => '', 'CgroupnsMode' => 'private', 'CpuShares' => 0, 'CpusetCpus' => '', 'Devices' => [], 'Dns' => [], 'DnsSearch' => [], 'ExtraHosts' => nil, 'IpcMode' => 'shareable', 'Init' => nil, 'KernelMemory' => 0, 'Links' => nil, 'LogConfig' => nil, 'Memory' => 0, 'MemorySwap' => nil, 'MemoryReservation' => 0, 'NanoCpus' => 0, 'NetworkMode' => 'bridge', 'OomKillDisable' => false, 'OomScoreAdj' => -500, 'Privileged' => false, 'PidMode' => '', 'PortBindings' => {}, 'PublishAllPorts' => false, 'RestartPolicy' => { 'Name' => nil, 'MaximumRetryCount' => 0 }, 'ReadonlyRootfs' => false, 'Runtime' => 'runc', 'SecurityOpt' => nil, 'ShmSize' => 67108864, 'Sysctls' => {}, 'Tmpfs' => {}, 'Ulimits' => nil, 'UsernsMode' => '', 'UTSMode' => '', 'VolumesFrom' => nil, 'VolumeDriver' => nil }, 'NetworkingConfig' => { 'EndpointsConfig' => { 'bridge' => { 'IPAMConfig' => { 'IPv4Address' => nil }, 'Aliases' => [] } } }, 'Healthcheck' => { 'Test' => ['string'], 'Interval' => 0, 'Timeout' => 0, 'Retries' => 0, 'StartPeriod' => 0 } } ) } end @@ -139,4 +139,35 @@ ) } end + + context 'when GPU support is configured' do + let(:resource) do + r = Chef::Resource::DockerContainer.new('gpu_test') + r.gpus 'all' + r.gpu_driver 'nvidia' + r + end + + it 'configures nvidia GPU support' do + expect(resource.create_options['HostConfig']['DeviceRequests']).to eq([{ + 'Driver' => 'nvidia', + 'Count' => -1, + 'Capabilities' => [['gpu']] + }]) + end + + it 'allows custom GPU driver' do + resource.gpu_driver 'custom_driver' + expect(resource.create_options['HostConfig']['DeviceRequests']).to eq([{ + 'Driver' => 'custom_driver', + 'Count' => -1, + 'Capabilities' => [['gpu']] + }]) + end + + it 'does not add DeviceRequests when gpus is not set' do + resource.gpus nil + expect(resource.create_options['HostConfig']['DeviceRequests']).to be_nil + end + end end diff --git a/test/cookbooks/docker_test/recipes/container.rb b/test/cookbooks/docker_test/recipes/container.rb index eb183acbf..a1dc1a91c 100644 --- a/test/cookbooks/docker_test/recipes/container.rb +++ b/test/cookbooks/docker_test/recipes/container.rb @@ -1156,6 +1156,18 @@ action :run_if_missing end +################### +# GPU test container +################### + +docker_container 'gpu_test' do + repo 'nvidia/cuda' + tag 'latest' + gpus 'all' + gpu_driver 'nvidia' + action :run_if_missing +end + ######################## # Dockerfile CMD changes ########################