Repository: google/gvisor
File: runsc/container/container_test.go (Go)

Code before:
}

// erofsImageInfo contains information about a created EROFS image.
type erofsImageInfo struct {
	name    string
	options string
	path    string
}

// erofsTestEnv holds the environment details for EROFS tests.
type erofsTestEnv struct {
	scriptFile string
	checksums  string
	images     []erofsImageInfo
}

// setupEROFS creates a temporary directory with some random files and EROFS images.
// It returns a test environment struct containing the directory paths and images,
// a cleanup function that must be called to remove the temporary files, and an error.
func setupEROFS() (*erofsTestEnv, func(), error) {
	// Create a temporary directory to save the test files.
	testDir, err := os.MkdirTemp(testutil.TmpDir(), "erofs_mount_test_")
	if err != nil {
		return nil, nil, fmt.Errorf("os.MkdirTemp() failed: %v", err)
	}
	cleanup := func() { os.RemoveAll(testDir) }

	// Create a temporary directory with some random files in it, which will
	// be used as the source directory to create the EROFS images.
	sourceDir := filepath.Join(testDir, "source")
	if err := os.Mkdir(sourceDir, 0755); err != nil {
		cleanup()
		return nil, nil, fmt.Errorf("os.Mkdir() failed: %v", err)
	}
	// Create some files with leading non-alphanumeric characters in name. It's helpful
	// to verify the on-disk directory entries order.
	for _, c := range []byte("!#$%&()*+,-:;<=>?@[]^_`{|}~") {
		name := fmt.Sprintf("%s/%c_file", sourceDir, c)
		// Create the file with random data.
		if err := os.WriteFile(name, []byte(fmt.Sprintf("%v", rand.Uint64())), 0644); err != nil {
			cleanup()
			return nil, nil, fmt.Errorf("error creating %q: %v", name, err)
		}
	}
	testApp, err := testutil.FindFile("test/cmd/test_app/test_app")
	if err != nil {
		cleanup()
		return nil, nil, fmt.Errorf("error finding test_app: %v", err)
	}
	// Source directory is a small directory. Let's create a big directory in it.
	// So we can cover both cases.

Review comment (suggestion):
Please use `cleanup.Make()` here:
```
	cu := cleanup.Make(func() {
		os.RemoveAll(testDir)
	})
	defer cu.Clean()
```

When you want to return the cleanup function at the end of this function, you can do `cu.Release()`. And the deferred `cu.Clean()` will be a no-op after that. Consequently, remove all the manual `cleanup()` invocations, those can be error-prone.

Code after:
}

// erofsImageInfo contains information about a created EROFS image.
type erofsImageInfo struct {
	name    string
	options string
	path    string
}

// erofsTestEnv holds the environment details for EROFS tests.
type erofsTestEnv struct {
	scriptFile string
	checksums  string
	images     []erofsImageInfo
}

// setupEROFS creates a temporary directory with some random files and EROFS images.
// It returns a test environment struct containing the directory paths and images,
// a cleanup function that must be called to remove the temporary files, and an error.
func setupEROFS() (*erofsTestEnv, func(), error) {
	// Create a temporary directory to save the test files.
	testDir, err := os.MkdirTemp(testutil.TmpDir(), "erofs_mount_test_")
	if err != nil {
		return nil, nil, fmt.Errorf("os.MkdirTemp() failed: %v", err)
	}
	cu := cleanup.Make(func() {
		os.RemoveAll(testDir)
	})
	defer cu.Clean()

	// Create a temporary directory with some random files in it, which will
	// be used as the source directory to create the EROFS images.
	sourceDir := filepath.Join(testDir, "source")
	if err := os.Mkdir(sourceDir, 0755); err != nil {
		return nil, nil, fmt.Errorf("os.Mkdir() failed: %v", err)
	}
	// Create some files with leading non-alphanumeric characters in name. It's helpful
	// to verify the on-disk directory entries order.
	for _, c := range []byte("!#$%&()*+,-:;<=>?@[]^_`{|}~") {
		name := fmt.Sprintf("%s/%c_file", sourceDir, c)
		// Create the file with random data.
		if err := os.WriteFile(name, []byte(fmt.Sprintf("%v", rand.Uint64())), 0644); err != nil {
			return nil, nil, fmt.Errorf("error creating %q: %v", name, err)
		}
	}
	testApp, err := testutil.FindFile("test/cmd/test_app/test_app")
	if err != nil {
		return nil, nil, fmt.Errorf("error finding test_app: %v", err)
	}
	// Source directory is a small directory. Let's create a big directory in it.
	// So we can cover both cases.