File: overlay_set_linux_test.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 (250 lines) | stat: -rw-r--r-- 7,891 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
// Copyright (c) 2023, 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 overlay

import (
	"os"
	"os/exec"
	"path/filepath"
	"testing"

	"github.com/sylabs/singularity/v4/internal/pkg/test/tool/require"
	"github.com/sylabs/singularity/v4/internal/pkg/util/fs"
)

func addROItemOrFatal(t *testing.T, s *Set, olStr string) *Item {
	i, err := NewItemFromString(olStr)
	if err != nil {
		t.Fatalf("could not initialize overlay item from string %q: %s", olStr, err)
	}
	s.ReadonlyOverlays = append(s.ReadonlyOverlays, i)

	return i
}

// wrapOverlayTest takes a testing function and wraps it in code that checks if
// the kernel has support for unprivileged overlays. If it does, the underlying
// function will be run twice, once with using kernel overlays and once using
// fuse-overlayfs (if present). Otherwise, only the latter option will be
// attempted.
func wrapOverlayTest(f func(t *testing.T)) func(t *testing.T) {
	unprivOls, unprivOlsErr := UnprivOverlaysSupported()
	return func(t *testing.T) {
		if unprivOlsErr != nil {
			t.Fatalf("while checking for unprivileged overlay support in kernel: %s", unprivOlsErr)
		}

		if unprivOls {
			kernelOverlayFunc := func(t *testing.T) {
				require.Command(t, "fusermount")
				f(t)
			}

			t.Run("kerneloverlay", kernelOverlayFunc)
			unprivOverlays.kernelSupport = false
		}

		fuseOverlayFunc := func(t *testing.T) {
			require.Command(t, "fuse-overlayfs")
			require.Command(t, "fusermount")
			f(t)
		}

		t.Run("fuseoverlayfs", fuseOverlayFunc)
		unprivOverlays.kernelSupport = unprivOls
	}
}

func TestAllTypesAtOnce(t *testing.T) {
	wrapOverlayTest(func(t *testing.T) {
		s := Set{}

		tmpRoOlDir := mkTempOlDirOrFatal(t)
		addROItemOrFatal(t, &s, tmpRoOlDir+":ro")

		squashfsSupported := false
		if _, err := exec.LookPath("squashfs"); err == nil {
			squashfsSupported = true
			addROItemOrFatal(t, &s, squashfsImgPath)
		}

		extfsSupported := false
		if _, err := exec.LookPath("fuse2fs"); err == nil {
			extfsSupported = true
			tmpDir := mkTempDirOrFatal(t)
			readonlyExtfsImgPath := filepath.Join(tmpDir, "readonly-extfs.img")
			if err := fs.CopyFile(extfsImgPath, readonlyExtfsImgPath, 0o444); err != nil {
				t.Fatalf("could not copy %q to %q: %s", extfsImgPath, readonlyExtfsImgPath, err)
			}
			addROItemOrFatal(t, &s, readonlyExtfsImgPath+":ro")
		}

		tmpRwOlDir := mkTempOlDirOrFatal(t)
		i, err := NewItemFromString(tmpRwOlDir)
		if err != nil {
			t.Fatalf("failed to create writable-dir overlay item (%q): %s", tmpRwOlDir, err)
		}
		s.WritableOverlay = i

		rootfsDir := mkTempDirOrFatal(t)
		if err := s.Mount(rootfsDir); err != nil {
			t.Fatalf("failed to mount overlay set: %s", err)
		}
		t.Cleanup(func() {
			if t.Failed() {
				s.Unmount(rootfsDir)
			}
		})

		var expectStr string
		if extfsSupported {
			expectStr = extfsTestString
		} else if squashfsSupported {
			expectStr = squashfsTestString
		}

		if squashfsSupported || extfsSupported {
			testFileMountedPath := filepath.Join(rootfsDir, testFilePath)
			data, err := os.ReadFile(testFileMountedPath)
			if err != nil {
				t.Fatalf("error while trying to read from file %q: %s", testFileMountedPath, err)
			}
			foundStr := string(data)
			if foundStr != expectStr {
				t.Errorf("incorrect file contents in mounted overlay set: expected %q, but found: %q", expectStr, foundStr)
			}
		}

		if err := s.Unmount(rootfsDir); err != nil {
			t.Errorf("while trying to unmount overlay set: %s", err)
		}
	})(t)
}

func TestPersistentWriteToDir(t *testing.T) {
	wrapOverlayTest(func(t *testing.T) {
		tmpRwOlDir := mkTempOlDirOrFatal(t)
		i, err := NewItemFromString(tmpRwOlDir)
		if err != nil {
			t.Fatalf("failed to create writable-dir overlay item (%q): %s", tmpRwOlDir, err)
		}
		s := Set{WritableOverlay: i}

		performPersistentWriteTest(t, s)
	})(t)
}

func TestPersistentWriteToExtfsImg(t *testing.T) {
	require.Command(t, "fuse2fs")
	require.Command(t, "fuse-overlayfs")
	require.Command(t, "fusermount")
	tmpDir := mkTempDirOrFatal(t)

	// Create a copy of the extfs test image to be used for testing writable
	// extfs image overlays
	writableExtfsImgPath := filepath.Join(tmpDir, "writable-extfs.img")
	err := fs.CopyFile(extfsImgPath, writableExtfsImgPath, 0o755)
	if err != nil {
		t.Fatalf("could not copy %q to %q: %s", extfsImgPath, writableExtfsImgPath, err)
	}

	i, err := NewItemFromString(writableExtfsImgPath)
	if err != nil {
		t.Fatalf("failed to create writable-dir overlay item (%q): %s", writableExtfsImgPath, err)
	}
	s := Set{WritableOverlay: i}

	performPersistentWriteTest(t, s)
}

func performPersistentWriteTest(t *testing.T, s Set) {
	rootfsDir := mkTempDirOrFatal(t)

	// This cleanup will serve adequately for both iterations of the overlay-set
	// mounting, below. If it happens to get called while the set is not
	// mounted, it should fail silently.
	// Mount the overlay set, write a string to a file, and unmount.
	// Mount the same set again, and check that we see the file with the
	// expected contents.
	t.Cleanup(func() {
		if t.Failed() {
			s.Unmount(rootfsDir)
		}
	})

	if err := s.Mount(rootfsDir); err != nil {
		t.Fatalf("failed to mount overlay set: %s", err)
	}
	expectStr := "my_test_string"
	bytes := []byte(expectStr)
	testFilePath := "my_test_file"
	testFileMountedPath := filepath.Join(rootfsDir, testFilePath)
	if err := os.WriteFile(testFileMountedPath, bytes, 0o644); err != nil {
		t.Fatalf("while trying to write file inside mounted overlay-set: %s", err)
	}

	if err := s.Unmount(rootfsDir); err != nil {
		t.Fatalf("while trying to unmount overlay set: %s", err)
	}

	if err := s.Mount(rootfsDir); err != nil {
		t.Fatalf("failed to mount overlay set: %s", err)
	}
	data, err := os.ReadFile(testFileMountedPath)
	if err != nil {
		t.Fatalf("error while trying to read from file %q: %s", testFileMountedPath, err)
	}
	foundStr := string(data)
	if foundStr != expectStr {
		t.Errorf("incorrect file contents in mounted overlay set: expected %q, but found: %q", expectStr, foundStr)
	}
	if err := s.Unmount(rootfsDir); err != nil {
		t.Errorf("while trying to unmount overlay set: %s", err)
	}
}

func TestDuplicateItemsInSet(t *testing.T) {
	wrapOverlayTest(func(t *testing.T) {
		var rootfsDir string
		var rwI *Item
		var err error

		s := Set{}

		// First, test mounting of an overlay set with only readonly items, one of
		// which is a duplicate of another.
		addROItemOrFatal(t, &s, mkTempOlDirOrFatal(t)+":ro")
		roI2 := addROItemOrFatal(t, &s, mkTempOlDirOrFatal(t)+":ro")
		addROItemOrFatal(t, &s, mkTempOlDirOrFatal(t)+":ro")
		addROItemOrFatal(t, &s, roI2.SourcePath+":ro")
		addROItemOrFatal(t, &s, mkTempOlDirOrFatal(t)+":ro")

		rootfsDir = mkTempDirOrFatal(t)
		if err := s.Mount(rootfsDir); err == nil {
			t.Errorf("unexpected success: Mounting overlay.Set with duplicate (%q) should have failed", roI2.SourcePath)
			if err := s.Unmount(rootfsDir); err != nil {
				t.Fatalf("could not unmount erroneous successful mount of overlay set: %s", err)
			}
		}

		// Next, test mounting of an overlay set with a writable item as well as
		// several readonly items, one of which is a duplicate of another.
		tmpRwOlDir := mkTempOlDirOrFatal(t)
		rwI, err = NewItemFromString(tmpRwOlDir)
		if err != nil {
			t.Fatalf("failed to create writable-dir overlay item (%q): %s", tmpRwOlDir, err)
		}
		s.WritableOverlay = rwI

		rootfsDir = mkTempDirOrFatal(t)
		if err := s.Mount(rootfsDir); err == nil {
			t.Errorf("unexpected success: Mounting overlay.Set with duplicate (%q) should have failed", roI2.SourcePath)
			if err := s.Unmount(rootfsDir); err != nil {
				t.Fatalf("could not unmount erroneous successful mount of overlay set: %s", err)
			}
		}
	})(t)
}