github.com/apptainer/singularity@v3.1.1+incompatible/cmd/singularity/env_test.go (about) 1 // Copyright (c) 2018, Sylabs Inc. All rights reserved. 2 // This software is licensed under a 3-clause BSD license. Please consult the 3 // LICENSE.md file distributed with the sources of this project regarding your 4 // rights to use or distribute this software. 5 6 package main 7 8 import ( 9 "os" 10 "os/exec" 11 "strings" 12 "testing" 13 14 "github.com/sylabs/singularity/internal/pkg/test" 15 ) 16 17 func TestSingularityEnv(t *testing.T) { 18 // Singularity defines a path by default. See singularityware/singularity/etc/init. 19 var defaultImage = "docker://alpine:3.8" 20 var defaultPath = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" 21 22 // This image sets a custom path. 23 var customImage = "docker://godlovedc/lolcow" 24 var customPath = "/usr/games:" + defaultPath 25 26 // Append or prepend this path. 27 var partialPath = "/foo" 28 29 // Overwrite the path with this one. 30 var overwrittenPath = "/usr/bin:/bin" 31 32 var singularityEnvTests = []struct { 33 name string 34 image string 35 path string 36 env []string 37 }{ 38 {"DefaultPath", defaultImage, defaultPath, []string{}}, 39 {"CustomPath", customImage, customPath, []string{}}, 40 {"AppendToDefaultPath", defaultImage, defaultPath + ":" + partialPath, []string{"SINGULARITYENV_APPEND_PATH=/foo"}}, 41 {"AppendToCustomPath", customImage, customPath + ":" + partialPath, []string{"SINGULARITYENV_APPEND_PATH=/foo"}}, 42 {"PrependToDefaultPath", defaultImage, partialPath + ":" + defaultPath, []string{"SINGULARITYENV_PREPEND_PATH=/foo"}}, 43 {"PrependToCustomPath", customImage, partialPath + ":" + customPath, []string{"SINGULARITYENV_PREPEND_PATH=/foo"}}, 44 {"OverwriteDefaultPath", defaultImage, overwrittenPath, []string{"SINGULARITYENV_PATH=" + overwrittenPath}}, 45 {"OverwriteCustomPath", customImage, overwrittenPath, []string{"SINGULARITYENV_PATH=" + overwrittenPath}}, 46 } 47 48 for _, currentTest := range singularityEnvTests { 49 t.Run(currentTest.name, test.WithoutPrivilege(func(t *testing.T) { 50 args := []string{"exec", currentTest.image, "env"} 51 52 cmd := exec.Command(cmdPath, args...) 53 cmd.Env = append(os.Environ(), currentTest.env...) 54 b, err := cmd.CombinedOutput() 55 56 out := string(b) 57 t.Logf("args: '%v'", strings.Join(args, " ")) 58 t.Logf("env: '%v'", strings.Join(cmd.Env, " ")) 59 t.Log(out) 60 61 if err != nil { 62 t.Fatalf("Error running command: %v", err) 63 } 64 65 if !strings.Contains(out, currentTest.path) { 66 t.Fatalf("Command output did not contain the path '%s'", currentTest.path) 67 } 68 })) 69 } 70 }