File: instance.go

package info (click to toggle)
apptainer 1.4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,780 kB
  • sloc: sh: 3,329; ansic: 1,706; awk: 414; python: 103; makefile: 54
file content (622 lines) | stat: -rw-r--r-- 16,076 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
// Copyright (c) Contributors to the Apptainer project, established as
//   Apptainer a Series of LF Projects LLC.
//   For website terms of use, trademark policy, privacy policy and other
//   project policies see https://lfprojects.org/policies
// Copyright (c) 2019-2024, 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 instance

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
	"os/user"
	"path/filepath"
	"strconv"
	"strings"
	"syscall"
	"testing"
	"time"

	"github.com/apptainer/apptainer/e2e/internal/e2e"
	"github.com/apptainer/apptainer/e2e/internal/testhelper"
	"github.com/apptainer/apptainer/pkg/util/fs/proc"
	"github.com/google/uuid"
	"github.com/pkg/errors"
)

// randomName generates a random name based on a UUID.
func randomName(t *testing.T) string {
	t.Helper()

	id, err := uuid.NewRandom()
	if err != nil {
		t.Fatal(err)
	}
	return id.String()
}

type ctx struct {
	env     e2e.TestEnv
	profile e2e.Profile
	withEnv []string
}

// Test that a basic echo server instance can be started, communicated with,
// and stopped.
func (c *ctx) testBasicEchoServer(t *testing.T) {
	const instanceName = "echo1"

	args := []string{c.env.ImagePath, instanceName, strconv.Itoa(instanceStartPort)}

	// Start the instance.
	c.env.RunApptainer(
		t,
		e2e.WithProfile(c.profile),
		e2e.WithCommand("instance start"),
		e2e.WithArgs(args...),
		e2e.PostRun(func(t *testing.T) {
			if t.Failed() {
				return
			}
			// Try to contact the instance.
			echo(t, instanceStartPort, false)
			c.stopInstance(t, instanceName)
		}),
		e2e.ExpectExit(0),
	)
}

// Test that a basic reverse-echo server defined in an appstart script can be started,
// communicated with, and stopped.
func (c *ctx) testAppEchoServer(t *testing.T) {
	const instanceName = "echoApp"

	args := []string{
		"--app",
		"foo",
		c.env.ImagePath,
		instanceName,
		strconv.Itoa(instanceStartPort),
	}

	// Start the instance.
	c.env.RunApptainer(
		t,
		e2e.WithProfile(c.profile),
		e2e.WithCommand("instance start"),
		e2e.WithArgs(args...),
		e2e.PostRun(func(t *testing.T) {
			if t.Failed() {
				return
			}
			// Try to contact the instance.
			echo(t, instanceStartPort, true)
			c.stopInstance(t, instanceName)
		}),
		e2e.ExpectExit(0),
	)
}

// Test that instance run command executes the runscript
func (c *ctx) testInstanceRun(t *testing.T) {
	const instanceName = "testtrue"

	args := []string{c.env.ImagePath, instanceName, "true"}

	// Start the instance.
	c.env.RunApptainer(
		t,
		e2e.WithProfile(c.profile),
		e2e.WithCommand("instance run"),
		e2e.WithArgs(args...),
		e2e.PostRun(func(t *testing.T) {
			if t.Failed() {
				return
			}
			// read the log file to see if runscript was used. (It should record
			// the text "Running command: true")
			d, err := os.UserHomeDir()
			if err != nil {
				t.Fatal(err)
			}
			h, err := os.Hostname()
			if err != nil {
				t.Fatal(err)
			}
			u, err := user.Current()
			if err != nil {
				t.Fatal(err)
			}
			ilog := filepath.Join(d, ".apptainer", "instances", "logs", h, u.Username, instanceName+".out")
			b, err := os.ReadFile(ilog)
			if err != nil {
				t.Fatal(err)
			}
			s := string(b)
			echo(t, instanceStartPort, false)
			c.stopInstance(t, instanceName)
			if !strings.Contains(s, "Running command: true") {
				t.Fatal()
			}
		}),
		e2e.ExpectExit(0),
	)
}

// Test creating many instances, but don't stop them.
func (c *ctx) testCreateManyInstances(t *testing.T) {
	const n = 10

	// Start n instances.
	for i := 0; i < n; i++ {
		port := instanceStartPort + i
		instanceName := "echo" + strconv.Itoa(i+1)

		c.env.RunApptainer(
			t,
			e2e.WithProfile(c.profile),
			e2e.WithCommand("instance start"),
			e2e.WithArgs(c.env.ImagePath, instanceName, strconv.Itoa(port)),
			e2e.PostRun(func(t *testing.T) {
				echo(t, port, false)
			}),
			e2e.ExpectExit(0),
		)

		c.expectInstance(t, instanceName, 1, false)
	}
}

// Test stopping all running instances.
func (c *ctx) testStopAll(t *testing.T) {
	c.stopInstance(t, "", "--all")
}

// Test basic options like mounting a custom home directory, changing the
// hostname, etc.
func (c *ctx) testBasicOptions(t *testing.T) {
	const fileName = "hello"
	const instanceName = "testbasic"
	const testHostname = "echoserver99"
	cmdList := [2]string{"instance start", "instance run"}
	fileContents := []byte("world")

	// Create a temporary directory to serve as a home directory.
	dir, err := os.MkdirTemp(c.env.TestDir, "TestInstance")
	if err != nil {
		t.Fatalf("Failed to create temporary directory: %v", err)
	}
	defer os.RemoveAll(dir)

	// Create and populate a temporary file.
	tempFile := filepath.Join(dir, fileName)
	err = os.WriteFile(tempFile, fileContents, 0o644)
	err = errors.Wrapf(err, "creating temporary test file %s", tempFile)
	if err != nil {
		t.Fatalf("Failed to create file: %+v", err)
	}

	// Start and Run an instance with the temporary directory as the home
	// directory.
	for _, cmd := range cmdList {
		c.env.RunApptainer(
			t,
			e2e.WithProfile(c.profile),
			e2e.WithCommand(cmd),
			e2e.WithArgs(
				"-H", dir+":/home/temp",
				"--hostname", testHostname,
				"-e",
				c.env.ImagePath,
				instanceName,
				strconv.Itoa(instanceStartPort),
			),
			e2e.PostRun(func(t *testing.T) {
				if t.Failed() {
					return
				}

				// Verify we can see the file's contents from within the container.
				stdout, _, success := c.execInstance(t, instanceName, "cat", "/home/temp/"+fileName)
				if success && !bytes.Equal(fileContents, []byte(stdout)) {
					t.Errorf("File contents were %s, but expected %s", stdout, string(fileContents))
				}

				// Verify that the hostname has been set correctly.
				stdout, _, success = c.execInstance(t, instanceName, "hostname")
				if success && !bytes.Equal([]byte(testHostname+"\n"), []byte(stdout)) {
					t.Errorf("Hostname is %s, but expected %s", stdout, testHostname)
				}

				// Verify that the APPTAINER_INSTANCE has been set correctly.
				stdout, _, success = c.execInstance(t, instanceName, "sh", "-c", "echo $APPTAINER_INSTANCE")
				if success && !bytes.Equal([]byte(instanceName+"\n"), []byte(stdout)) {
					t.Errorf("APPTAINER_INSTANCE is %s, but expected %s", stdout, instanceName)
				}

				// Stop the instance.
				c.stopInstance(t, instanceName)
			}),
			e2e.ExpectExit(0),
		)
	}
}

// Test that contain works.
func (c *ctx) testContain(t *testing.T) {
	const instanceName = "testcontain"
	const fileName = "thegreattestfile"
	cmdList := [2]string{"instance start", "instance run"}

	// Create a temporary directory to serve as a contain directory.
	dir, err := os.MkdirTemp("", "TestInstance")
	if err != nil {
		t.Fatalf("Failed to create temporary directory: %v", err)
	}
	defer os.RemoveAll(dir)

	// Start and Run the instance.
	for _, cmd := range cmdList {
		c.env.RunApptainer(
			t,
			e2e.WithProfile(c.profile),
			e2e.WithCommand(cmd),
			e2e.WithArgs(
				"-c",
				"-W", dir,
				c.env.ImagePath,
				instanceName,
				strconv.Itoa(instanceStartPort),
			),
			e2e.PostRun(func(t *testing.T) {
				if t.Failed() {
					return
				}

				// Touch a file within /tmp.
				_, _, success := c.execInstance(t, instanceName, "touch", "/tmp/"+fileName)
				if success {
					// Verify that the touched file exists outside the container.
					if _, err = os.Stat(filepath.Join(dir, "tmp", fileName)); os.IsNotExist(err) {
						t.Errorf("The temp file doesn't exist.")
					}
				}

				// Stop the container.
				c.stopInstance(t, instanceName)
			}),
			e2e.ExpectExit(0),
		)
	}
}

// Test by running directly from URI
func (c *ctx) testInstanceFromURI(t *testing.T) {
	e2e.EnsureORASImage(t, c.env)
	name := "test_from_uri"
	args := []string{c.env.OrasTestImage, name}
	cmdList := [2]string{"instance start", "instance run"}
	for _, cmd := range cmdList {
		c.env.RunApptainer(
			t,
			e2e.WithProfile(c.profile),
			e2e.WithCommand(cmd),
			e2e.WithArgs(args...),
			e2e.PostRun(func(t *testing.T) {
				if t.Failed() {
					return
				}
				c.execInstance(t, name, "id")
				c.stopInstance(t, name)
			}),
			e2e.ExpectExit(0),
		)
	}
}

// Execute an instance process, kill master process
// and try to start another instance with same name
func (c *ctx) testGhostInstance(t *testing.T) {
	cmdList := [2]string{"instance start", "instance run"}

	for _, cmd := range cmdList {
		// pick up a random name
		instanceName := randomName(t)
		pidfile := filepath.Join(c.env.TestDir, instanceName)

		postFn := func(t *testing.T) {
			defer os.Remove(pidfile)

			if t.Failed() {
				t.Fatalf("instance %s failed to start correctly", instanceName)
			}

			d, err := os.ReadFile(pidfile)
			if err != nil {
				t.Fatalf("failed to read pid file: %s", err)
			}
			trimmed := strings.TrimSuffix(string(d), "\n")
			pid, err := strconv.ParseInt(trimmed, 10, 32)
			if err != nil {
				t.Fatalf("failed to convert PID %s in %s: %s", trimmed, pidfile, err)
			}
			ppid, err := proc.Getppid(int(pid))
			if err != nil {
				t.Fatalf("failed to get parent process ID for process %d: %s", pid, err)
			}

			// starting same instance twice must return an error
			c.env.RunApptainer(
				t,
				e2e.WithProfile(c.profile),
				e2e.WithCommand(cmd),
				e2e.WithArgs(c.env.ImagePath, instanceName),
				e2e.ExpectExit(
					255,
					e2e.ExpectErrorf(e2e.ContainMatch, "instance %s already exists", instanceName),
				),
			)

			// kill master process
			if err := syscall.Kill(int(ppid), syscall.SIGKILL); err != nil {
				t.Fatalf("failed to send KILL signal to %d: %s", ppid, err)
			}

			// now check we are deleting ghost instance files correctly
			c.env.RunApptainer(
				t,
				e2e.WithProfile(c.profile),
				e2e.WithCommand(cmd),
				e2e.WithArgs(c.env.ImagePath, instanceName),
				e2e.PostRun(func(t *testing.T) {
					if t.Failed() {
						return
					}
					c.stopInstance(t, instanceName)
				}),
				e2e.ExpectExit(0),
			)
		}

		c.env.RunApptainer(
			t,
			e2e.WithProfile(c.profile),
			e2e.WithCommand(cmd),
			e2e.WithArgs("--pid-file", pidfile, c.env.ImagePath, instanceName),
			e2e.PostRun(postFn),
			e2e.ExpectExit(0),
		)
	}
}

// Test instances when using an alternate configdir
func (c *ctx) testInstanceWithConfigDir(t *testing.T) {
	dir, err := os.MkdirTemp(c.env.TestDir, "InstanceWithConfigDir")
	if err != nil {
		t.Fatalf("Failed to create temporary directory: %v", err)
	}
	defer os.RemoveAll(dir)

	c.withEnv = append(os.Environ(), "APPTAINER_CONFIGDIR="+dir)
	defer func() {
		c.withEnv = []string{}
	}()

	name := "movedConfig"
	c.env.RunApptainer(
		t,
		e2e.WithProfile(c.profile),
		e2e.WithCommand("instance start"),
		e2e.WithArgs(c.env.ImagePath, name),
		e2e.WithEnv(c.withEnv),
		e2e.ExpectExit(0),
	)

	c.expectInstance(t, name, 1, false)
	c.execInstance(t, name, "id")
	c.stopInstance(t, name)

	e2e.Privileged(func(t *testing.T) {
		if _, err := os.Stat(dir + "/instances/app"); err != nil {
			t.Fatalf("failed %v", err)
		}
	})(t)
}

// testShareNSMopde will test --sharens flag
func (c *ctx) testShareNSMode(t *testing.T) {
	dir, err := os.MkdirTemp(c.env.TestDir, "InstanceShareNS")
	if err != nil {
		t.Fatalf("Failed to create temporary directory: %v", err)
	}
	defer os.RemoveAll(dir)

	file := fmt.Sprintf("%s/file", dir)
	f, err := os.Create(file)
	if err != nil {
		t.Fatalf("failed to create file, this is unexpected, err: %v", err)
	}
	f.Close()

	insNumber := 2
	for i := 0; i < insNumber; i++ {
		go c.env.RunApptainer(
			t,
			e2e.WithProfile(c.profile),
			e2e.WithCommand("exec"),
			e2e.WithArgs("--bind", fmt.Sprintf("%s:/canary/file", file), "--sharens", c.env.ImagePath, "sh", "-c", "echo 0 >> /canary/file; sleep 1"),
			e2e.ExpectExit(0),
		)
	}

	// waiting enough time for the file written
	time.Sleep(2 * time.Second)

	f, err = os.Open(file)
	if err != nil {
		t.Fatalf("failed to open file: %v, this is unexpected", err)
	}
	defer f.Close()

	scanner := bufio.NewScanner(f)
	scanner.Split(bufio.ScanLines)

	var count int
	for scanner.Scan() {
		count++
	}

	if err := scanner.Err(); err != nil {
		t.Fatalf("having issue while scanning file: %s, err: %v", file, err)
	}

	if count != insNumber {
		t.Fatalf("should have %d lines, but actually is %d", insNumber, count)
	}
}

// Test that custom auth file authentication works with instance start
func (c *ctx) testInstanceAuthFile(t *testing.T) {
	e2e.EnsureORASImage(t, c.env)
	instanceName := "actionAuthTesterInstance"
	localAuthFileName := "./my_local_authfile"
	authFileArgs := []string{"--authfile", localAuthFileName}

	tmpdir, tmpdirCleanup := e2e.MakeTempDir(t, c.env.TestDir, "action-auth", "")
	t.Cleanup(func() {
		if !t.Failed() {
			tmpdirCleanup(t)
		}
	})

	prevCwd, err := os.Getwd()
	if err != nil {
		t.Fatalf("could not get current working directory: %s", err)
	}
	defer os.Chdir(prevCwd)
	if err = os.Chdir(tmpdir); err != nil {
		t.Fatalf("could not change cwd to %q: %s", tmpdir, err)
	}

	tests := []struct {
		name          string
		subCmd        string
		args          []string
		whileLoggedIn bool
		expectExit    int
	}{
		{
			name:          "start before auth",
			subCmd:        "start",
			args:          append(authFileArgs, "--disable-cache", "--no-https", c.env.TestRegistryPrivImage, instanceName),
			whileLoggedIn: false,
			expectExit:    255,
		},
		{
			name:          "start",
			subCmd:        "start",
			args:          append(authFileArgs, "--disable-cache", "--no-https", c.env.TestRegistryPrivImage, instanceName),
			whileLoggedIn: true,
			expectExit:    0,
		},
		{
			name:          "stop",
			subCmd:        "stop",
			args:          []string{instanceName},
			whileLoggedIn: true,
			expectExit:    0,
		},
		{
			name:          "start noauth",
			subCmd:        "start",
			args:          append(authFileArgs, "--disable-cache", "--no-https", c.env.TestRegistryPrivImage, instanceName),
			whileLoggedIn: false,
			expectExit:    255,
		},
	}

	profiles := []e2e.Profile{
		e2e.UserProfile,
		e2e.RootProfile,
	}

	for _, p := range profiles {
		t.Run(p.String(), func(t *testing.T) {
			for _, tt := range tests {
				if tt.whileLoggedIn {
					e2e.PrivateRepoLogin(t, c.env, p, localAuthFileName)
				} else {
					e2e.PrivateRepoLogout(t, c.env, p, localAuthFileName)
				}
				c.env.RunApptainer(
					t,
					e2e.AsSubtest(tt.name),
					e2e.WithProfile(p),
					e2e.WithCommand("instance "+tt.subCmd),
					e2e.WithArgs(tt.args...),
					e2e.ExpectExit(tt.expectExit),
				)
			}
		})
	}
}

// E2ETests is the main func to trigger the test suite
func E2ETests(env e2e.TestEnv) testhelper.Tests {
	c := &ctx{
		env: env,
	}
	np := testhelper.NoParallel

	return testhelper.Tests{
		"ordered": func(t *testing.T) {
			c := &ctx{
				env:     env,
				profile: e2e.UserProfile,
			}

			e2e.EnsureImage(t, c.env)

			// Define and loop through tests.
			tests := []struct {
				name     string
				function func(*testing.T)
			}{
				{"BasicEchoServer", c.testBasicEchoServer},
				{"AppEchoServer", c.testAppEchoServer},
				{"BasicOptions", c.testBasicOptions},
				{"Contain", c.testContain},
				{"InstanceFromURI", c.testInstanceFromURI},
				{"CreateManyInstances", c.testCreateManyInstances},
				{"InstanceRun", c.testInstanceRun},
				{"StopAll", c.testStopAll},
				{"GhostInstance", c.testGhostInstance},
				{"CheckpointInstance", c.testCheckpointInstance},
				{"InstanceWithConfigDir", c.testInstanceWithConfigDir},
				{"ShareNSMode", c.testShareNSMode},
				{"issue 2189", c.issue2189},
			}

			profiles := []e2e.Profile{
				e2e.UserProfile,
				e2e.RootProfile,
			}

			for _, profile := range profiles {
				t.Run(profile.String(), func(t *testing.T) {
					c.profile = profile
					for _, tt := range tests {
						t.Run(tt.name, tt.function)
					}
				})
			}
		},
		"issue 5033": c.issue5033,                // https://github.com/apptainer/singularity/issues/4836
		"auth":       np(c.testInstanceAuthFile), // custom --authfile with instance start command
	}
}