summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/fs/copy_up_test.go
blob: 1d80bf15a8bb017aac1a1e0afb24175f9c8ad232 (plain)
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
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fs_test

import (
	"bytes"
	"crypto/rand"
	"fmt"
	"io"
	"sync"
	"testing"

	"gvisor.dev/gvisor/pkg/sentry/fs"
	_ "gvisor.dev/gvisor/pkg/sentry/fs/tmpfs"
	"gvisor.dev/gvisor/pkg/sentry/kernel/contexttest"
	"gvisor.dev/gvisor/pkg/sentry/usermem"
)

const (
	// origFileSize is the original file size. This many bytes should be
	// copied up before the test file is modified.
	origFileSize = 4096

	// truncatedFileSize is the size to truncate all test files.
	truncateFileSize = 10
)

// TestConcurrentCopyUp is a copy up stress test for an overlay.
//
// It creates a 64-level deep directory tree in the lower filesystem and
// populates the last subdirectory with 64 files containing random content:
//
//    /lower
//      /sudir0/.../subdir63/
//                     /file0
//                     ...
//                     /file63
//
// The files are truncated concurrently by 4 goroutines per file.
// These goroutines contend with copying up all parent 64 subdirectories
// as well as the final file content.
//
// At the end of the test, we assert that the files respect the new truncated
// size and contain the content we expect.
func TestConcurrentCopyUp(t *testing.T) {
	ctx := contexttest.Context(t)
	files := makeOverlayTestFiles(t)

	var wg sync.WaitGroup
	for _, file := range files {
		for i := 0; i < 4; i++ {
			wg.Add(1)
			go func(o *overlayTestFile) {
				if err := o.File.Dirent.Inode.Truncate(ctx, o.File.Dirent, truncateFileSize); err != nil {
					t.Fatalf("failed to copy up: %v", err)
				}
				wg.Done()
			}(file)
		}
	}
	wg.Wait()

	for _, file := range files {
		got := make([]byte, origFileSize)
		n, err := file.File.Readv(ctx, usermem.BytesIOSequence(got))
		if int(n) != truncateFileSize {
			t.Fatalf("read %d bytes from file, want %d", n, truncateFileSize)
		}
		if err != nil && err != io.EOF {
			t.Fatalf("read got error %v, want nil", err)
		}
		if !bytes.Equal(got[:n], file.content[:truncateFileSize]) {
			t.Fatalf("file content is %v, want %v", got[:n], file.content[:truncateFileSize])
		}
	}
}

type overlayTestFile struct {
	File    *fs.File
	name    string
	content []byte
}

func makeOverlayTestFiles(t *testing.T) []*overlayTestFile {
	ctx := contexttest.Context(t)

	// Create a lower tmpfs mount.
	fsys, _ := fs.FindFilesystem("tmpfs")
	lower, err := fsys.Mount(contexttest.Context(t), "", fs.MountSourceFlags{}, "", nil)
	if err != nil {
		t.Fatalf("failed to mount tmpfs: %v", err)
	}
	lowerRoot := fs.NewDirent(ctx, lower, "")

	// Make a deep set of subdirectories that everyone shares.
	next := lowerRoot
	for i := 0; i < 64; i++ {
		name := fmt.Sprintf("subdir%d", i)
		err := next.CreateDirectory(ctx, lowerRoot, name, fs.FilePermsFromMode(0777))
		if err != nil {
			t.Fatalf("failed to create dir %q: %v", name, err)
		}
		next, err = next.Walk(ctx, lowerRoot, name)
		if err != nil {
			t.Fatalf("failed to walk to %q: %v", name, err)
		}
	}

	// Make a bunch of files in the last directory.
	var files []*overlayTestFile
	for i := 0; i < 64; i++ {
		name := fmt.Sprintf("file%d", i)
		f, err := next.Create(ctx, next, name, fs.FileFlags{Read: true, Write: true}, fs.FilePermsFromMode(0666))
		if err != nil {
			t.Fatalf("failed to create file %q: %v", name, err)
		}
		defer f.DecRef()

		relname, _ := f.Dirent.FullName(lowerRoot)

		o := &overlayTestFile{
			name:    relname,
			content: make([]byte, origFileSize),
		}

		if _, err := rand.Read(o.content); err != nil {
			t.Fatalf("failed to read from /dev/urandom: %v", err)
		}

		if _, err := f.Writev(ctx, usermem.BytesIOSequence(o.content)); err != nil {
			t.Fatalf("failed to write content to file %q: %v", name, err)
		}

		files = append(files, o)
	}

	// Create an empty upper tmpfs mount which we will copy up into.
	upper, err := fsys.Mount(ctx, "", fs.MountSourceFlags{}, "", nil)
	if err != nil {
		t.Fatalf("failed to mount tmpfs: %v", err)
	}

	// Construct an overlay root.
	overlay, err := fs.NewOverlayRoot(ctx, upper, lower, fs.MountSourceFlags{})
	if err != nil {
		t.Fatalf("failed to construct overlay root: %v", err)
	}

	// Create a MountNamespace to traverse the file system.
	mns, err := fs.NewMountNamespace(ctx, overlay)
	if err != nil {
		t.Fatalf("failed to construct mount manager: %v", err)
	}

	// Walk to all of the files in the overlay, open them readable.
	for _, f := range files {
		maxTraversals := uint(0)
		d, err := mns.FindInode(ctx, mns.Root(), mns.Root(), f.name, &maxTraversals)
		if err != nil {
			t.Fatalf("failed to find %q: %v", f.name, err)
		}
		defer d.DecRef()

		f.File, err = d.Inode.GetFile(ctx, d, fs.FileFlags{Read: true})
		if err != nil {
			t.Fatalf("failed to open file %q readable: %v", f.name, err)
		}
	}

	return files
}