feat: apply sealed installation plans without interactive setup
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package safeio
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPublishNewFileExposesOnlyCompleteWinner(t *testing.T) {
|
||||
root, err := filepath.EvalSymlinks(t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Chmod(root, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
path := filepath.Join(root, "asset")
|
||||
a, b := bytes.Repeat([]byte("a"), 2<<20), bytes.Repeat([]byte("b"), 2<<20)
|
||||
start := make(chan struct{})
|
||||
results := make(chan error, 2)
|
||||
var writers sync.WaitGroup
|
||||
for _, value := range [][]byte{a, b} {
|
||||
writers.Add(1)
|
||||
go func(value []byte) {
|
||||
defer writers.Done()
|
||||
<-start
|
||||
results <- PublishCanonicalNewFile(path, value, 0o600)
|
||||
}(value)
|
||||
}
|
||||
done := make(chan struct{})
|
||||
go func() { writers.Wait(); close(done) }()
|
||||
close(start)
|
||||
for {
|
||||
contents, err := os.ReadFile(path)
|
||||
if err == nil && !bytes.Equal(contents, a) && !bytes.Equal(contents, b) {
|
||||
t.Fatal("partial publication observed")
|
||||
}
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
case <-done:
|
||||
goto finished
|
||||
default:
|
||||
}
|
||||
}
|
||||
finished:
|
||||
first, second := <-results, <-results
|
||||
if (first == nil) == (second == nil) {
|
||||
t.Fatal("exactly one writer must publish")
|
||||
}
|
||||
before, _ := os.ReadFile(path)
|
||||
if PublishCanonicalNewFile(path, []byte("replacement"), 0o600) == nil {
|
||||
t.Fatal("existing publication overwritten")
|
||||
}
|
||||
after, _ := os.ReadFile(path)
|
||||
if !bytes.Equal(before, after) {
|
||||
t.Fatal("failed publication changed target")
|
||||
}
|
||||
entries, _ := os.ReadDir(root)
|
||||
if len(entries) != 1 {
|
||||
t.Fatal("temporary files leaked")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user