1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
// Copyright (c) 2020-2022, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
//go:build linux
package gpu
import (
"os"
"path/filepath"
"reflect"
"strings"
"testing"
)
const testLibFile = "testdata/testliblist.conf"
var testLibList = []string{"libc.so", "echo"}
func TestElfMachine(t *testing.T) {
gotMachine, err := elfMachine()
if err != nil {
t.Errorf("elfMachine() error = %v", err)
return
}
if gotMachine <= 0 {
t.Errorf("elfMachine() gotMachine = %v is <=0", gotMachine)
}
}
func TestLdCache(t *testing.T) {
gotCache, err := ldCache()
if err != nil {
t.Errorf("ldCache() error = %v", err)
return
}
if len(gotCache) == 0 {
t.Error("ldCache() gave no results")
}
for name, path := range gotCache {
if strings.HasPrefix(name, "ld-linux") {
if strings.Contains(path, "ld-linux") {
return
}
}
}
t.Error("ldCache() result did not include expected ld-linux entry")
}
func Test_gpuliblist(t *testing.T) {
gotLibs, err := gpuliblist(testLibFile)
if err != nil {
t.Errorf("gpuliblist() error = %v", err)
return
}
if len(gotLibs) == 0 {
t.Error("gpuliblist() gave no results")
}
if !reflect.DeepEqual(gotLibs, testLibList) {
t.Errorf("gpuliblist() gave unexpected results, got: %v expected: %v", gotLibs, testLibList)
}
}
func TestSoLinks(t *testing.T) {
// Test link structure:
// a.so.1.2 -> a.so.1 -> a.so (file)
// - soLinks(a.so) should give both of these symlinks
// a.so.2 -> b.so
// - this should *not* get included, as it doesn't resolve back to a.so
tmpDir := t.TempDir()
aFile := filepath.Join(tmpDir, "a.so")
a1Link := filepath.Join(tmpDir, "a.so.1")
a12Link := filepath.Join(tmpDir, "a.so.1.2")
if err := os.WriteFile(aFile, nil, 0o644); err != nil {
t.Fatalf("Could not create file: %v", err)
}
if err := os.Symlink(aFile, a1Link); err != nil {
t.Fatalf("Could not symlink: %v", err)
}
if err := os.Symlink(aFile, a12Link); err != nil {
t.Fatalf("Could not symlink: %v", err)
}
bFile := filepath.Join(tmpDir, "b.so")
err := os.WriteFile(bFile, nil, 0o644)
if err != nil {
t.Fatalf("Could not create file: %v", err)
}
a2Link := filepath.Join(tmpDir, "a.so.2")
if err := os.Symlink(bFile, a2Link); err != nil {
t.Fatalf("Could not symlink: %v", err)
}
expectedLinks := []string{a1Link, a12Link}
gotLinks, err := soLinks(aFile)
if err != nil {
t.Errorf("soLinks() error = %v", err)
return
}
if len(gotLinks) == 0 {
t.Error("soLinks() gave no results")
}
if !reflect.DeepEqual(gotLinks, expectedLinks) {
t.Errorf("soList() gave unexpected results, got: %v expected: %v", gotLinks, expectedLinks)
}
}
func TestPaths(t *testing.T) {
// Very naive sanity test. Check we can find one lib and one binary without error
gotLibs, gotBin, err := paths(testLibList)
if err != nil {
t.Errorf("paths() error = %v", err)
return
}
if len(gotLibs) == 0 {
t.Error("paths() gave no libraries")
}
if len(gotBin) == 0 {
t.Error("paths() gave no binaries")
}
}
|