containerd/pkg/atomicfile/file_test.go
Samuel Karp f3ba7c8a35
atomicfile: new package for atomic file writes
Certain files may need to be written atomically so that partial writes
are not visible to other processes. On Unix-like platforms such as
Linux, FreeBSD, and Darwin, this is accomplished by writing a temporary
file, syncing, and renaming over the destination file name. On Windows,
the same operations are performed, but Windows does not guarantee that a
rename operation is atomic.

Partial/inconsistent reads can occur due to:
1. A process attempting to read the file while containerd is writing it
   (both in the case of a new file with a short/incomplete write or in
   the case of an existing, updated file where new bytes may be written
   at the beginning but old bytes may still be present after).
2. Concurrent goroutines in containerd leading to multiple active
   writers of the same file.

The above mechanism explicitly protects against (1) as all writes are to
a file with a temporary name.

There is no explicit protection against multiple, concurrent goroutines
attempting to write the same file. However, atomically writing the file
should mean only one writer will "win" and a consistent file will be
visible.

Signed-off-by: Samuel Karp <samuelkarp@google.com>
2023-06-02 16:56:33 -07:00

78 lines
2.4 KiB
Go

/*
Copyright The containerd 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 atomicfile
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFile(t *testing.T) {
const content = "this is some test content for a file"
dir := t.TempDir()
path := filepath.Join(dir, "test-file")
f, err := New(path, 0o644)
require.NoError(t, err, "failed to create file")
n, err := fmt.Fprint(f, content)
assert.NoError(t, err, "failed to write content")
assert.Equal(t, len(content), n, "written bytes should be equal")
err = f.Close()
require.NoError(t, err, "failed to close file")
actual, err := os.ReadFile(path)
assert.NoError(t, err, "failed to read file")
assert.Equal(t, content, string(actual))
}
func TestConcurrentWrites(t *testing.T) {
const content1 = "this is the first content of the file. there should be none other."
const content2 = "the second content of the file should win!"
dir := t.TempDir()
path := filepath.Join(dir, "test-file")
file1, err := New(path, 0o600)
require.NoError(t, err, "failed to create file1")
file2, err := New(path, 0o644)
require.NoError(t, err, "failed to create file2")
n, err := fmt.Fprint(file1, content1)
assert.NoError(t, err, "failed to write content1")
assert.Equal(t, len(content1), n, "written bytes should be equal")
n, err = fmt.Fprint(file2, content2)
assert.NoError(t, err, "failed to write content2")
assert.Equal(t, len(content2), n, "written bytes should be equal")
err = file1.Close()
require.NoError(t, err, "failed to close file1")
actual, err := os.ReadFile(path)
assert.NoError(t, err, "failed to read file")
assert.Equal(t, content1, string(actual))
err = file2.Close()
require.NoError(t, err, "failed to close file2")
actual, err = os.ReadFile(path)
assert.NoError(t, err, "failed to read file")
assert.Equal(t, content2, string(actual))
}