mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2025-02-04 03:56:23 +00:00
[no-relnote] Add unit test for installer command
This change adds a basic unit test for the nvidia-ckt-installer command. Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
parent
a7786d4d41
commit
5ed25bb375
@ -38,6 +38,7 @@ type options struct {
|
|||||||
runtimeArgs string
|
runtimeArgs string
|
||||||
root string
|
root string
|
||||||
pidFile string
|
pidFile string
|
||||||
|
sourceRoot string
|
||||||
|
|
||||||
toolkitOptions toolkit.Options
|
toolkitOptions toolkit.Options
|
||||||
runtimeOptions runtime.Options
|
runtimeOptions runtime.Options
|
||||||
@ -141,6 +142,13 @@ func (a app) build() *cli.App {
|
|||||||
Destination: &options.root,
|
Destination: &options.root,
|
||||||
EnvVars: []string{"ROOT"},
|
EnvVars: []string{"ROOT"},
|
||||||
},
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "source-root",
|
||||||
|
Value: "/",
|
||||||
|
Usage: "The folder where the required toolkit artifacts can be found",
|
||||||
|
Destination: &options.sourceRoot,
|
||||||
|
EnvVars: []string{"SOURCE_ROOT"},
|
||||||
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "pid-file",
|
Name: "pid-file",
|
||||||
Value: defaultPidFile,
|
Value: defaultPidFile,
|
||||||
@ -159,6 +167,7 @@ func (a app) build() *cli.App {
|
|||||||
func (a *app) Before(c *cli.Context, o *options) error {
|
func (a *app) Before(c *cli.Context, o *options) error {
|
||||||
a.toolkit = toolkit.NewInstaller(
|
a.toolkit = toolkit.NewInstaller(
|
||||||
toolkit.WithLogger(a.logger),
|
toolkit.WithLogger(a.logger),
|
||||||
|
toolkit.WithSourceRoot(o.sourceRoot),
|
||||||
toolkit.WithToolkitRoot(o.toolkitRoot()),
|
toolkit.WithToolkitRoot(o.toolkitRoot()),
|
||||||
)
|
)
|
||||||
return a.validateFlags(c, o)
|
return a.validateFlags(c, o)
|
||||||
|
@ -18,10 +18,15 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
testlog "github.com/sirupsen/logrus/hooks/test"
|
testlog "github.com/sirupsen/logrus/hooks/test"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/test"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParseArgs(t *testing.T) {
|
func TestParseArgs(t *testing.T) {
|
||||||
@ -84,3 +89,118 @@ func TestParseArgs(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestApp(t *testing.T) {
|
||||||
|
t.Setenv("__NVCT_TESTING_DEVICES_ARE_FILES", "true")
|
||||||
|
logger, _ := testlog.NewNullLogger()
|
||||||
|
|
||||||
|
moduleRoot, err := test.GetModuleRoot()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
artifactRoot := filepath.Join(moduleRoot, "testdata", "installer", "artifacts")
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
description string
|
||||||
|
args []string
|
||||||
|
expectedToolkitConfig string
|
||||||
|
expectedRuntimeConfig string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
description: "no args",
|
||||||
|
expectedToolkitConfig: `accept-nvidia-visible-devices-as-volume-mounts = false
|
||||||
|
accept-nvidia-visible-devices-envvar-when-unprivileged = true
|
||||||
|
disable-require = false
|
||||||
|
supported-driver-capabilities = "compat32,compute,display,graphics,ngx,utility,video"
|
||||||
|
swarm-resource = ""
|
||||||
|
|
||||||
|
[nvidia-container-cli]
|
||||||
|
debug = ""
|
||||||
|
environment = []
|
||||||
|
ldcache = ""
|
||||||
|
ldconfig = "@/run/nvidia/driver/sbin/ldconfig"
|
||||||
|
load-kmods = true
|
||||||
|
no-cgroups = false
|
||||||
|
path = "{{ .toolkitRoot }}/toolkit/nvidia-container-cli"
|
||||||
|
root = "/run/nvidia/driver"
|
||||||
|
user = ""
|
||||||
|
|
||||||
|
[nvidia-container-runtime]
|
||||||
|
debug = "/dev/null"
|
||||||
|
log-level = "info"
|
||||||
|
mode = "auto"
|
||||||
|
runtimes = ["docker-runc", "runc", "crun"]
|
||||||
|
|
||||||
|
[nvidia-container-runtime.modes]
|
||||||
|
|
||||||
|
[nvidia-container-runtime.modes.cdi]
|
||||||
|
annotation-prefixes = ["cdi.k8s.io/"]
|
||||||
|
default-kind = "nvidia.com/gpu"
|
||||||
|
spec-dirs = ["/etc/cdi", "/var/run/cdi"]
|
||||||
|
|
||||||
|
[nvidia-container-runtime.modes.csv]
|
||||||
|
mount-spec-path = "/etc/nvidia-container-runtime/host-files-for-container.d"
|
||||||
|
|
||||||
|
[nvidia-container-runtime-hook]
|
||||||
|
path = "{{ .toolkitRoot }}/toolkit/nvidia-container-runtime-hook"
|
||||||
|
skip-mode-detection = true
|
||||||
|
|
||||||
|
[nvidia-ctk]
|
||||||
|
path = "{{ .toolkitRoot }}/toolkit/nvidia-ctk"
|
||||||
|
`,
|
||||||
|
expectedRuntimeConfig: `{
|
||||||
|
"default-runtime": "nvidia",
|
||||||
|
"runtimes": {
|
||||||
|
"nvidia": {
|
||||||
|
"args": [],
|
||||||
|
"path": "{{ .toolkitRoot }}/toolkit/nvidia-container-runtime"
|
||||||
|
},
|
||||||
|
"nvidia-cdi": {
|
||||||
|
"args": [],
|
||||||
|
"path": "{{ .toolkitRoot }}/toolkit/nvidia-container-runtime.cdi"
|
||||||
|
},
|
||||||
|
"nvidia-legacy": {
|
||||||
|
"args": [],
|
||||||
|
"path": "{{ .toolkitRoot }}/toolkit/nvidia-container-runtime.legacy"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.description, func(t *testing.T) {
|
||||||
|
testRoot := t.TempDir()
|
||||||
|
|
||||||
|
runtimeConfigFile := filepath.Join(testRoot, "config.file")
|
||||||
|
|
||||||
|
toolkitRoot := filepath.Join(testRoot, "toolkit-test")
|
||||||
|
toolkitConfigFile := filepath.Join(toolkitRoot, "toolkit/.config/nvidia-container-runtime/config.toml")
|
||||||
|
|
||||||
|
app := NewApp(logger, toolkitRoot)
|
||||||
|
|
||||||
|
testArgs := []string{
|
||||||
|
"nvidia-ctk-installer",
|
||||||
|
"--no-daemon",
|
||||||
|
"--pid-file=" + filepath.Join(testRoot, "toolkit.pid"),
|
||||||
|
"--source-root=" + filepath.Join(artifactRoot, "deb"),
|
||||||
|
"--config=" + runtimeConfigFile,
|
||||||
|
"--restart-mode=none",
|
||||||
|
}
|
||||||
|
|
||||||
|
err := app.Run(append(testArgs, tc.args...))
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
require.FileExists(t, toolkitConfigFile)
|
||||||
|
toolkitConfigFileContents, err := os.ReadFile(toolkitConfigFile)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.EqualValues(t, strings.ReplaceAll(tc.expectedToolkitConfig, "{{ .toolkitRoot }}", toolkitRoot), string(toolkitConfigFileContents))
|
||||||
|
|
||||||
|
require.FileExists(t, runtimeConfigFile)
|
||||||
|
runtimeConfigFileContents, err := os.ReadFile(runtimeConfigFile)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.EqualValues(t, strings.ReplaceAll(tc.expectedRuntimeConfig, "{{ .toolkitRoot }}", toolkitRoot), string(runtimeConfigFileContents))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user