@@ -18,10 +18,15 @@ package main
1818
1919import (
2020 "fmt"
21+ "os"
22+ "path/filepath"
23+ "strings"
2124 "testing"
2225
2326 testlog "github.com/sirupsen/logrus/hooks/test"
2427 "github.com/stretchr/testify/require"
28+
29+ "github.com/NVIDIA/nvidia-container-toolkit/internal/test"
2530)
2631
2732func TestParseArgs (t * testing.T ) {
@@ -84,3 +89,118 @@ func TestParseArgs(t *testing.T) {
8489 })
8590 }
8691}
92+
93+ func TestApp (t * testing.T ) {
94+ t .Setenv ("__NVCT_TESTING_DEVICES_ARE_FILES" , "true" )
95+ logger , _ := testlog .NewNullLogger ()
96+
97+ moduleRoot , err := test .GetModuleRoot ()
98+ require .NoError (t , err )
99+
100+ artifactRoot := filepath .Join (moduleRoot , "testdata" , "installer" , "artifacts" )
101+
102+ testCases := []struct {
103+ description string
104+ args []string
105+ expectedToolkitConfig string
106+ expectedRuntimeConfig string
107+ }{
108+ {
109+ description : "no args" ,
110+ expectedToolkitConfig : `accept-nvidia-visible-devices-as-volume-mounts = false
111+ accept-nvidia-visible-devices-envvar-when-unprivileged = true
112+ disable-require = false
113+ supported-driver-capabilities = "compat32,compute,display,graphics,ngx,utility,video"
114+ swarm-resource = ""
115+
116+ [nvidia-container-cli]
117+ debug = ""
118+ environment = []
119+ ldcache = ""
120+ ldconfig = "@/run/nvidia/driver/sbin/ldconfig"
121+ load-kmods = true
122+ no-cgroups = false
123+ path = "{{ .toolkitRoot }}/toolkit/nvidia-container-cli"
124+ root = "/run/nvidia/driver"
125+ user = ""
126+
127+ [nvidia-container-runtime]
128+ debug = "/dev/null"
129+ log-level = "info"
130+ mode = "auto"
131+ runtimes = ["docker-runc", "runc", "crun"]
132+
133+ [nvidia-container-runtime.modes]
134+
135+ [nvidia-container-runtime.modes.cdi]
136+ annotation-prefixes = ["cdi.k8s.io/"]
137+ default-kind = "nvidia.com/gpu"
138+ spec-dirs = ["/etc/cdi", "/var/run/cdi"]
139+
140+ [nvidia-container-runtime.modes.csv]
141+ mount-spec-path = "/etc/nvidia-container-runtime/host-files-for-container.d"
142+
143+ [nvidia-container-runtime-hook]
144+ path = "{{ .toolkitRoot }}/toolkit/nvidia-container-runtime-hook"
145+ skip-mode-detection = true
146+
147+ [nvidia-ctk]
148+ path = "{{ .toolkitRoot }}/toolkit/nvidia-ctk"
149+ ` ,
150+ expectedRuntimeConfig : `{
151+ "default-runtime": "nvidia",
152+ "runtimes": {
153+ "nvidia": {
154+ "args": [],
155+ "path": "{{ .toolkitRoot }}/toolkit/nvidia-container-runtime"
156+ },
157+ "nvidia-cdi": {
158+ "args": [],
159+ "path": "{{ .toolkitRoot }}/toolkit/nvidia-container-runtime.cdi"
160+ },
161+ "nvidia-legacy": {
162+ "args": [],
163+ "path": "{{ .toolkitRoot }}/toolkit/nvidia-container-runtime.legacy"
164+ }
165+ }
166+ }` ,
167+ },
168+ }
169+
170+ for _ , tc := range testCases {
171+ t .Run (tc .description , func (t * testing.T ) {
172+ testRoot := t .TempDir ()
173+
174+ runtimeConfigFile := filepath .Join (testRoot , "config.file" )
175+
176+ toolkitRoot := filepath .Join (testRoot , "toolkit-test" )
177+ toolkitConfigFile := filepath .Join (toolkitRoot , "toolkit/.config/nvidia-container-runtime/config.toml" )
178+
179+ app := NewApp (logger , toolkitRoot )
180+
181+ testArgs := []string {
182+ "nvidia-ctk-installer" ,
183+ "--no-daemon" ,
184+ "--pid-file=" + filepath .Join (testRoot , "toolkit.pid" ),
185+ "--source-root=" + filepath .Join (artifactRoot , "deb" ),
186+ "--config=" + runtimeConfigFile ,
187+ "--restart-mode=none" ,
188+ }
189+
190+ err := app .Run (append (testArgs , tc .args ... ))
191+
192+ require .NoError (t , err )
193+
194+ require .FileExists (t , toolkitConfigFile )
195+ toolkitConfigFileContents , err := os .ReadFile (toolkitConfigFile )
196+ require .NoError (t , err )
197+ require .EqualValues (t , strings .ReplaceAll (tc .expectedToolkitConfig , "{{ .toolkitRoot }}" , toolkitRoot ), string (toolkitConfigFileContents ))
198+
199+ require .FileExists (t , runtimeConfigFile )
200+ runtimeConfigFileContents , err := os .ReadFile (runtimeConfigFile )
201+ require .NoError (t , err )
202+ require .EqualValues (t , strings .ReplaceAll (tc .expectedRuntimeConfig , "{{ .toolkitRoot }}" , toolkitRoot ), string (runtimeConfigFileContents ))
203+ })
204+ }
205+
206+ }
0 commit comments