| package installer | |
| import ( | |
| "os" | |
| "path/filepath" | |
| "strings" | |
| "testing" | |
| ) | |
| func TestSetupDaemonsNoGridd(t *testing.T) { | |
| tempRoot := t.TempDir() | |
| installDir := "/var/lib/nvidia" | |
| hostInstallDir := filepath.Join(tempRoot, installDir, "bin") | |
| if err := os.MkdirAll(hostInstallDir, 0755); err != nil { | |
| t.Fatalf("failed to create install dir: %v", err) | |
| } | |
| // Only nvidia-persistenced exists, not nvidia-gridd (non-Grid GPU such as a4x-max). | |
| persistencedBin := filepath.Join(hostInstallDir, "nvidia-persistenced") | |
| if err := os.WriteFile(persistencedBin, []byte("fake"), 0755); err != nil { | |
| t.Fatalf("failed to create fake nvidia-persistenced: %v", err) | |
| } | |
| if err := SetupDaemons(installDir, tempRoot); err != nil { | |
| t.Fatalf("SetupDaemons returned unexpected error: %v", err) | |
| } | |
| // Verify no systemd units were created when nvidia-gridd binary is not present. | |
| sysdDir := filepath.Join(tempRoot, "etc/systemd/system") | |
| if _, err := os.Stat(sysdDir); !os.IsNotExist(err) { | |
| t.Errorf("expected systemd dir not to be created when nvidia-gridd is missing, but stat returned: %v", err) | |
| } | |
| } | |
| func TestSetupDaemonsWithGridd(t *testing.T) { | |
| tempRoot := t.TempDir() | |
| installDir := "/var/lib/nvidia" | |
| hostInstallDir := filepath.Join(tempRoot, installDir, "bin") | |
| if err := os.MkdirAll(hostInstallDir, 0755); err != nil { | |
| t.Fatalf("failed to create bin dir: %v", err) | |
| } | |
| // Create dummy nvidia-gridd and nvidia-persistenced binaries. | |
| for _, bin := range []string{"nvidia-gridd", "nvidia-persistenced"} { | |
| binPath := filepath.Join(hostInstallDir, bin) | |
| if err := os.WriteFile(binPath, []byte("fake"), 0755); err != nil { | |
| t.Fatalf("failed to create fake %s: %v", bin, err) | |
| } | |
| } | |
| if err := SetupDaemons(installDir, tempRoot); err != nil { | |
| t.Fatalf("SetupDaemons returned unexpected error: %v", err) | |
| } | |
| sysdDir := filepath.Join(tempRoot, "etc/systemd/system") | |
| expectedUnits := map[string][]string{ | |
| "nvidia-persistenced.service": { | |
| "ConditionPathExists=/var/lib/nvidia/bin/nvidia-persistenced", | |
| "Before=nvidia-gridd.service", | |
| "Wants=nvidia-gridd.service", | |
| "ExecStart=/var/lib/nvidia/bin/nvidia-persistenced", | |
| "Restart=always", | |
| }, | |
| "nvidia-gridd.service": { | |
| "ConditionPathExists=/var/lib/nvidia/bin/nvidia-gridd", | |
| "After=nvidia-persistenced.service", | |
| "BindsTo=nvidia-persistenced.service", | |
| "PartOf=nvidia-persistenced.service", | |
| "Environment=\"LD_LIBRARY_PATH=/var/lib/nvidia/gridd-libs\"", | |
| "ExecStart=/var/lib/nvidia/bin/nvidia-gridd", | |
| "Restart=always", | |
| }, | |
| } | |
| for unitName, substrings := range expectedUnits { | |
| unitPath := filepath.Join(sysdDir, unitName) | |
| contentBytes, err := os.ReadFile(unitPath) | |
| if err != nil { | |
| t.Errorf("expected %s to be created, got err: %v", unitPath, err) | |
| continue | |
| } | |
| content := string(contentBytes) | |
| for _, want := range substrings { | |
| if !strings.Contains(content, want) { | |
| t.Errorf("unit %s missing expected content %q; got:\n%s", unitName, want, content) | |
| } | |
| } | |
| } | |
| for _, unwantedUnit := range []string{"nvidia-persistenced.path", "nvidia-gridd.path"} { | |
| unitPath := filepath.Join(sysdDir, unwantedUnit) | |
| if _, err := os.Stat(unitPath); !os.IsNotExist(err) { | |
| t.Errorf("expected %s not to be created, got err: %v", unitPath, err) | |
| } | |
| } | |
| } |