File: suite.go

package info (click to toggle)
singularity-container 4.0.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 21,672 kB
  • sloc: asm: 3,857; sh: 2,125; ansic: 1,677; awk: 414; makefile: 110; python: 99
file content (266 lines) | stat: -rw-r--r-- 9,064 bytes parent folder | download
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright (c) 2020, Control Command Inc. All rights reserved.
// Copyright (c) 2019-2022 Sylabs Inc. All rights reserved.
// Copyright (c) Contributors to the Apptainer project, established as
//   Apptainer a Series of LF Projects LLC.
// 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 e2e

import (
	"flag"
	"fmt"
	"log"
	"os"
	"os/exec"
	"path"
	"path/filepath"
	"strconv"
	"strings"
	"syscall"
	"testing"

	// Tests imports
	"github.com/sylabs/singularity/v4/e2e/actions"
	e2ebuildcfg "github.com/sylabs/singularity/v4/e2e/buildcfg"
	"github.com/sylabs/singularity/v4/e2e/cache"
	"github.com/sylabs/singularity/v4/e2e/cgroups"
	"github.com/sylabs/singularity/v4/e2e/cmdenvvars"
	"github.com/sylabs/singularity/v4/e2e/config"
	"github.com/sylabs/singularity/v4/e2e/delete"
	"github.com/sylabs/singularity/v4/e2e/docker"
	"github.com/sylabs/singularity/v4/e2e/ecl"
	singularityenv "github.com/sylabs/singularity/v4/e2e/env"
	"github.com/sylabs/singularity/v4/e2e/gpu"
	"github.com/sylabs/singularity/v4/e2e/help"
	"github.com/sylabs/singularity/v4/e2e/imgbuild"
	"github.com/sylabs/singularity/v4/e2e/inspect"
	"github.com/sylabs/singularity/v4/e2e/instance"
	"github.com/sylabs/singularity/v4/e2e/key"
	"github.com/sylabs/singularity/v4/e2e/keyserver"
	"github.com/sylabs/singularity/v4/e2e/oci"
	"github.com/sylabs/singularity/v4/e2e/overlay"
	"github.com/sylabs/singularity/v4/e2e/plugin"
	"github.com/sylabs/singularity/v4/e2e/pull"
	"github.com/sylabs/singularity/v4/e2e/push"
	"github.com/sylabs/singularity/v4/e2e/registry"
	"github.com/sylabs/singularity/v4/e2e/remote"
	"github.com/sylabs/singularity/v4/e2e/run"
	"github.com/sylabs/singularity/v4/e2e/runhelp"
	"github.com/sylabs/singularity/v4/e2e/security"
	"github.com/sylabs/singularity/v4/e2e/sign"
	"github.com/sylabs/singularity/v4/e2e/verify"
	"github.com/sylabs/singularity/v4/e2e/version"

	"github.com/sylabs/singularity/v4/e2e/internal/e2e"
	"github.com/sylabs/singularity/v4/e2e/internal/testhelper"
	"github.com/sylabs/singularity/v4/internal/pkg/buildcfg"
	"github.com/sylabs/singularity/v4/pkg/util/slice"
	useragent "github.com/sylabs/singularity/v4/pkg/util/user-agent"
)

var (
	runDisabled = flag.Bool("run_disabled", false, "run tests that have been temporarily disabled")
	runGroups   = flag.String("e2e_groups", "", "specify a comma separated list of e2e groups to run")
	runTests    = flag.String("e2e_tests", "", "specify a regex matching e2e tests to run")
)

var e2eGroups = map[string]testhelper.Group{
	"ACTIONS":    actions.E2ETests,
	"BUILDCFG":   e2ebuildcfg.E2ETests,
	"BUILD":      imgbuild.E2ETests,
	"CACHE":      cache.E2ETests,
	"CGROUPS":    cgroups.E2ETests,
	"CMDENVVARS": cmdenvvars.E2ETests,
	"CONFIG":     config.E2ETests,
	"DELETE":     delete.E2ETests,
	"DOCKER":     docker.E2ETests,
	"ECL":        ecl.E2ETests,
	"ENV":        singularityenv.E2ETests,
	"GPU":        gpu.E2ETests,
	"HELP":       help.E2ETests,
	"INSPECT":    inspect.E2ETests,
	"INSTANCE":   instance.E2ETests,
	"KEY":        key.E2ETests,
	"KEYSERVER":  keyserver.E2ETests,
	"OCI":        oci.E2ETests,
	"OVERLAY":    overlay.E2ETests,
	"PLUGIN":     plugin.E2ETests,
	"PULL":       pull.E2ETests,
	"PUSH":       push.E2ETests,
	"REGISTRY":   registry.E2ETests,
	"REMOTE":     remote.E2ETests,
	"RUN":        run.E2ETests,
	"RUNHELP":    runhelp.E2ETests,
	"SECURITY":   security.E2ETests,
	"SIGN":       sign.E2ETests,
	"VERIFY":     verify.E2ETests,
	"VERSION":    version.E2ETests,
}

// Run is the main func for the test framework, initializes the required vars
// and sets the environment for the RunE2ETests framework
func Run(t *testing.T) {
	flag.Parse()

	var testenv e2e.TestEnv

	if *runDisabled {
		testenv.RunDisabled = true
	}
	// init buildcfg values
	useragent.InitValue(buildcfg.PACKAGE_NAME, buildcfg.PACKAGE_VERSION)

	// Ensure binary is in $PATH
	cmdPath := filepath.Join(buildcfg.BINDIR, "singularity")
	if _, err := exec.LookPath(cmdPath); err != nil {
		log.Fatalf("singularity is not installed on this system: %v", err)
	}

	testenv.CmdPath = cmdPath

	sysconfdir := func(fn string) string {
		return filepath.Join(buildcfg.SYSCONFDIR, "singularity", fn)
	}

	// Make temp dir for tests
	name, err := os.MkdirTemp("", "stest.")
	if err != nil {
		log.Fatalf("failed to create temporary directory: %v", err)
	}
	defer e2e.Privileged(func(t *testing.T) {
		if t.Failed() {
			t.Logf("Test failed, not removing %s", name)
			return
		}

		os.RemoveAll(name)
	})(t)

	if err := os.Chmod(name, 0o755); err != nil {
		log.Fatalf("failed to chmod temporary directory: %v", err)
	}
	testenv.TestDir = name

	// Make shared cache dirs for privileged and unpriviliged E2E tests.
	// Individual tests that depend on specific ordered cache behavior, or
	// directly test the cache, should override the TestEnv values within the
	// specific test.
	privCacheDir, cleanPrivCache := e2e.MakeCacheDir(t, testenv.TestDir)
	testenv.PrivCacheDir = privCacheDir
	defer e2e.Privileged(func(t *testing.T) {
		cleanPrivCache(t)
	})

	unprivCacheDir, cleanUnprivCache := e2e.MakeCacheDir(t, testenv.TestDir)
	testenv.UnprivCacheDir = unprivCacheDir
	defer cleanUnprivCache(t)

	// e2e tests need to run in a somehow agnostic environment, so we
	// don't use environment of user executing tests in order to not
	// wrongly interfering with cache stuff, sylabs library tokens,
	// PGP keys
	e2e.SetupHomeDirectories(t)

	// generate singularity.conf with default values
	e2e.SetupDefaultConfig(t, filepath.Join(testenv.TestDir, "singularity.conf"))

	// create an empty plugin directory
	e2e.SetupPluginDir(t, testenv.TestDir)

	// duplicate system remote.yaml and create a temporary one on top of original
	e2e.SetupSystemRemoteFile(t, testenv.TestDir)

	// create an empty ECL configuration and empty global keyring
	e2e.SetupSystemECLAndGlobalKeyRing(t, testenv.TestDir)

	// Creates '$HOME/.singularity/docker-config.json' with credentials
	e2e.SetupDockerHubCredentials(t)

	// Ensure config files are installed
	configFiles := []string{
		sysconfdir("singularity.conf"),
		sysconfdir("ecl.toml"),
		sysconfdir("capability.json"),
		sysconfdir("nvliblist.conf"),
	}

	for _, cf := range configFiles {
		//nolint:forcetypeassert
		if fi, err := os.Stat(cf); err != nil {
			t.Fatalf("%s is not installed on this system: %v", cf, err)
		} else if !fi.Mode().IsRegular() {
			t.Fatalf("%s is not a regular file", cf)
		} else if fi.Sys().(*syscall.Stat_t).Uid != 0 {
			t.Fatalf("%s must be owned by root", cf)
		}
	}

	// Provision local registry
	testenv.TestRegistry = e2e.StartRegistry(t, testenv)
	testenv.TestRegistryImage = fmt.Sprintf("docker://%s/my-alpine:latest", testenv.TestRegistry)

	// Copy small test image (alpine:latest) into local registry from DockerHub
	insecureSource := false
	insecureValue := os.Getenv("E2E_DOCKER_MIRROR_INSECURE")
	if insecureValue != "" {
		insecureSource, err = strconv.ParseBool(insecureValue)
		if err != nil {
			t.Fatalf("could not convert E2E_DOCKER_MIRROR_INSECURE=%s: %s", insecureValue, err)
		}
	}
	e2e.CopyOCIImage(t, "docker://alpine:latest", testenv.TestRegistryImage, insecureSource, true)

	// SIF base test path, built on demand by e2e.EnsureImage
	imagePath := path.Join(name, "test.sif")
	t.Log("Path to test image:", imagePath)
	testenv.ImagePath = imagePath

	// OCI Archive test image path, built on demand by e2e.EnsureOCIArchive
	ociArchivePath := path.Join(name, "oci.tar")
	t.Log("Path to test OCI archive:", ociArchivePath)
	testenv.OCIArchivePath = ociArchivePath

	// OCI-SIF Image, retrieved on demand by e2e.EnsureOCISIF
	ociSifPath := path.Join(name, "oci-sif.sif")
	t.Log("Path to test OCI-SIF image:", ociArchivePath)
	testenv.OCISIFPath = ociSifPath

	// Docker Archive test image path, built on demand by e2e.EnsureDockerArchive
	dockerArchivePath := path.Join(name, "docker.tar")
	t.Log("Path to test Docker archive:", dockerArchivePath)
	testenv.DockerArchivePath = dockerArchivePath

	// Local registry ORAS SIF image, built on demand by e2e.EnsureORASImage
	testenv.OrasTestImage = fmt.Sprintf("oras://%s/oras_test_sif:latest", testenv.TestRegistry)

	// Local registry ORAS OCI-SIF image, built on demand by e2e.EnsureORASOCISIF
	testenv.OrasTestOCISIF = fmt.Sprintf("oras://%s/oras_test_oci-sif:latest", testenv.TestRegistry)

	// OCI-SIF image pushed as OCI image to local registry
	testenv.TestRegistryOCISIF = fmt.Sprintf("docker://%s/registry_test_oci-sif:latest", testenv.TestRegistry)

	t.Cleanup(func() {
		if !t.Failed() {
			os.Remove(imagePath)
			os.Remove(ociArchivePath)
			os.Remove(dockerArchivePath)
		}
	})

	suite := testhelper.NewSuite(t, testenv)

	groups := []string{}
	if runGroups != nil && *runGroups != "" {
		groups = strings.Split(*runGroups, ",")
	}

	for key, val := range e2eGroups {
		if len(groups) == 0 || slice.ContainsString(groups, key) {
			suite.AddGroup(key, val)
		}
	}

	suite.Run(runTests)
}