forked from apptainer/singularity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.go
More file actions
189 lines (169 loc) · 7.96 KB
/
example.go
File metadata and controls
189 lines (169 loc) · 7.96 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) 2019, 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.
package example
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/sylabs/singularity/e2e/internal/e2e"
"github.com/sylabs/singularity/internal/pkg/test"
)
type testingEnv struct {
// base env for running tests
CmdPath string `split_words:"true"`
TestDir string `split_words:"true"`
RunDisabled bool `default:"false"`
// base image for tests
ImagePath string `split_words:"true"`
}
var testenv testingEnv
// exec tests min fuctionality for singularity exec
func testSingularityGeneric(t *testing.T, name string, privileged bool, action string, options e2e.ExecOpts, image string) {
testFn := test.WithoutPrivilege
if privileged {
testFn = test.WithPrivilege
}
const (
testfile = "testSingularityExec.tmp"
testdir = "testSingularityExec.dir"
)
t.Run(name, testFn(func(t *testing.T) {
if options.Userns {
// check if user namespace is supported and skip test if not
}
workdir, err := e2e.MakeTmpDir(testenv.TestDir, "d-", 0755)
defer os.RemoveAll(workdir)
if err := os.MkdirAll(filepath.Join(workdir, "tmp", testdir), 0755); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(workdir, testfile), []byte{}, 0755); err != nil {
t.Fatal(err)
}
// running parallel test could have side effects while
// setting current working directory because it would affect
// other Go threads too, it's safer to set cmd.Dir instead
pwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer os.Chdir(pwd)
// required when running as root with user namespace enabled
// because generally current working directory is located in
// user home directory, so even if running as root and user
// namespace enabled, root will get a permission denied while
// mounting current working directory. Change this to temporary
// workdir
if err := os.Chdir(workdir); err != nil {
t.Fatal(err)
}
tests := []struct {
name string
argv []string
e2e.ExecOpts
exitCode int
searchOutput string
}{
{"true", []string{"true"}, e2e.ExecOpts{}, 0, ""},
{"trueAbsPAth", []string{"/bin/true"}, e2e.ExecOpts{}, 0, ""},
{"false", []string{"false"}, e2e.ExecOpts{}, 1, ""},
{"falseAbsPath", []string{"/bin/false"}, e2e.ExecOpts{}, 1, ""},
// Scif apps tests
{"ScifTestAppGood", []string{"testapp.sh"}, e2e.ExecOpts{App: "testapp"}, 0, ""},
{"ScifTestAppBad", []string{"testapp.sh"}, e2e.ExecOpts{App: "fakeapp"}, 1, ""},
{"ScifTestfolderOrg", []string{"test", "-d", "/scif"}, e2e.ExecOpts{}, 0, ""},
{"ScifTestfolderOrg", []string{"test", "-d", "/scif/apps"}, e2e.ExecOpts{}, 0, ""},
{"ScifTestfolderOrg", []string{"test", "-d", "/scif/data"}, e2e.ExecOpts{}, 0, ""},
{"ScifTestfolderOrg", []string{"test", "-d", "/scif/apps/foo"}, e2e.ExecOpts{}, 0, ""},
{"ScifTestfolderOrg", []string{"test", "-d", "/scif/apps/bar"}, e2e.ExecOpts{}, 0, ""},
// blocked by issue [scif-apps] Files created at install step fall into an unexpected path #2404
{"ScifTestfolderOrg", []string{"test", "-f", "/scif/apps/foo/filefoo.exec"}, e2e.ExecOpts{}, 0, ""},
{"ScifTestfolderOrg", []string{"test", "-f", "/scif/apps/bar/filebar.exec"}, e2e.ExecOpts{}, 0, ""},
{"ScifTestfolderOrg", []string{"test", "-d", "/scif/data/foo/output"}, e2e.ExecOpts{}, 0, ""},
{"ScifTestfolderOrg", []string{"test", "-d", "/scif/data/foo/input"}, e2e.ExecOpts{}, 0, ""},
{"WorkdirContain", []string{"test", "-d", "/tmp/" + testdir}, e2e.ExecOpts{Workdir: workdir, Contain: true}, 0, ""},
{"Workdir", []string{"test", "-d", "/tmp/" + testdir}, e2e.ExecOpts{Workdir: workdir}, 1, ""},
{"pwdGood", []string{"true"}, e2e.ExecOpts{Pwd: "/etc"}, 0, ""},
{"home", []string{"test", "-f", filepath.Join(workdir, testfile)}, e2e.ExecOpts{Home: workdir}, 0, ""},
{"noHome", []string{"test", "-f", "/home/" + testfile}, e2e.ExecOpts{Home: workdir + ":/home", NoHome: true}, 1, ""},
{"homePath", []string{"test", "-f", "/home/" + testfile}, e2e.ExecOpts{Home: workdir + ":/home"}, 0, ""},
{"homeTmp", []string{"true"}, e2e.ExecOpts{Home: "/tmp"}, 0, ""},
{"homeTmpExplicit", []string{"true"}, e2e.ExecOpts{Home: "/tmp:/home"}, 0, ""},
{"ScifTestAppGood", []string{"testapp.sh"}, e2e.ExecOpts{App: "testapp"}, 0, ""},
{"ScifTestAppBad", []string{"testapp.sh"}, e2e.ExecOpts{App: "fakeapp"}, 1, ""},
{"userBind", []string{"test", "-f", "/mnt/" + testfile}, e2e.ExecOpts{Binds: []string{workdir + ":/mnt"}}, 0, ""},
{"PwdGood", []string{"pwd"}, e2e.ExecOpts{Pwd: "/etc"}, 0, "/etc"},
}
for _, tt := range tests {
// note that we need to run tests with the same
// privileges wrapper function otherwise tests
// will be always executed as root
t.Run(tt.name, testFn(func(t *testing.T) {
if options.Userns {
tt.ExecOpts.Userns = true
}
stdout, stderr, exitCode, err := e2e.ImageExec(t, testenv.CmdPath, action, tt.ExecOpts, image, tt.argv)
if stdout != "" && !strings.Contains(stdout, tt.searchOutput) {
t.Log(stdout)
t.Fatalf("unexpected output returned running '%v': %v", strings.Join(tt.argv, " "), err)
} else if tt.exitCode >= 0 && exitCode == tt.exitCode {
// PASS
return
} else if tt.exitCode == 0 && exitCode != 0 {
// FAIL
t.Log(stderr)
t.Fatalf("unexpected failure running '%v': %v", strings.Join(tt.argv, " "), err)
} else if tt.exitCode != 0 && exitCode == 0 {
// FAIL
t.Log(stderr)
t.Fatalf("unexpected success running '%v'", strings.Join(tt.argv, " "))
} else if err != nil {
// FAIL
t.Log(stderr)
t.Fatalf("unexpected error running '%v': %s", strings.Join(tt.argv, " "), err)
}
}))
}
}))
}
// RunE2ETests is the main func to trigger the test suite
func RunE2ETests(t *testing.T) {
e2e.LoadEnv(t, &testenv)
e2e.EnsureImage(t)
// world writable to allow unprivileged build to write
// sandbox image
sandboxUnpriv, err := e2e.MakeTmpDir(testenv.TestDir, "d-", 0777)
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(sandboxUnpriv)
// need to change how SINGULARITY_CACHEDIR is set for parallel tests
test.DropPrivilege(t)
if output, err := e2e.ImageBuild(testenv.CmdPath, e2e.BuildOpts{Force: true, Sandbox: true}, sandboxUnpriv, testenv.ImagePath); err != nil {
t.Fatalf("%s: %s", err, string(output))
}
test.ResetPrivilege(t)
sandboxPriv, err := e2e.MakeTmpDir(testenv.TestDir, "d-", 0755)
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(sandboxPriv)
if output, err := e2e.ImageBuild(testenv.CmdPath, e2e.BuildOpts{Force: true, Sandbox: true}, sandboxPriv, testenv.ImagePath); err != nil {
t.Fatalf("%s: %s", err, string(output))
}
testSingularityGeneric(t, "Exec", false, "exec", e2e.ExecOpts{}, testenv.ImagePath)
testSingularityGeneric(t, "ExecPriv", true, "exec", e2e.ExecOpts{}, testenv.ImagePath)
testSingularityGeneric(t, "Run", false, "run", e2e.ExecOpts{}, testenv.ImagePath)
testSingularityGeneric(t, "RunPriv", true, "run", e2e.ExecOpts{}, testenv.ImagePath)
testSingularityGeneric(t, "ExecUsernsWithSIF", false, "exec", e2e.ExecOpts{Userns: true}, testenv.ImagePath)
testSingularityGeneric(t, "ExecUsernsPrivWithSIF", true, "exec", e2e.ExecOpts{Userns: true}, testenv.ImagePath)
testSingularityGeneric(t, "RunUsernsWithSIF", false, "run", e2e.ExecOpts{Userns: true}, testenv.ImagePath)
testSingularityGeneric(t, "RunUsernsPrivWithSIF", true, "run", e2e.ExecOpts{Userns: true}, testenv.ImagePath)
testSingularityGeneric(t, "ExecUsernsWithSandbox", false, "exec", e2e.ExecOpts{Userns: true}, sandboxUnpriv)
testSingularityGeneric(t, "ExecUsernsPrivWithSandbox", true, "exec", e2e.ExecOpts{Userns: true}, sandboxPriv)
testSingularityGeneric(t, "RunUsernsWithSandbox", false, "run", e2e.ExecOpts{Userns: true}, sandboxUnpriv)
testSingularityGeneric(t, "RunUsernsPrivWithSandbox", true, "run", e2e.ExecOpts{Userns: true}, sandboxPriv)
}