cert test flakiness: make write atomic, add log msgs

pull/7590/head
Thomas Stromberg 2020-04-10 08:32:41 -07:00
parent 4b3f1e5169
commit 7b8b6b027c
2 changed files with 19 additions and 6 deletions

View File

@ -93,15 +93,21 @@ func NewMemoryAssetTarget(d []byte, targetPath, permissions string) *MemoryAsset
// NewFileAsset creates a new FileAsset
func NewFileAsset(src, targetDir, targetName, permissions string) (*FileAsset, error) {
glog.V(4).Infof("NewFileAsset: %s -> %s", src, path.Join(targetDir, targetName))
f, err := os.Open(src)
if err != nil {
return nil, errors.Wrapf(err, "Error opening file asset: %s", src)
return nil, errors.Wrap(err, "open")
}
info, err := os.Stat(src)
if err != nil {
return nil, errors.Wrapf(err, "Error getting info for %s", src)
return nil, errors.Wrapf(err, "stat")
}
r := io.NewSectionReader(f, 0, info.Size())
if info.Size() == 0 {
glog.Warningf("NewFileAsset: %s is an empty file!", src)
}
return &FileAsset{
BaseAsset: BaseAsset{
SourcePath: src,
@ -109,7 +115,7 @@ func NewFileAsset(src, targetDir, targetName, permissions string) (*FileAsset, e
TargetName: targetName,
Permissions: permissions,
},
reader: r,
reader: io.NewSectionReader(f, 0, info.Size()),
}, nil
}
@ -117,6 +123,7 @@ func NewFileAsset(src, targetDir, targetName, permissions string) (*FileAsset, e
func (f *FileAsset) GetLength() (flen int) {
fi, err := os.Stat(f.SourcePath)
if err != nil {
glog.Errorf("stat(%q) failed: %v", f.SourcePath, err)
return 0
}
return int(fi.Size())
@ -126,6 +133,7 @@ func (f *FileAsset) GetLength() (flen int) {
func (f *FileAsset) GetModTime() (time.Time, error) {
fi, err := os.Stat(f.SourcePath)
if err != nil {
glog.Errorf("stat(%q) failed: %v", f.SourcePath, err)
return time.Time{}, err
}
return fi.ModTime(), nil

View File

@ -814,11 +814,16 @@ func setupFileSync(ctx context.Context, t *testing.T, profile string) {
testPem := "./testdata/minikube_test.pem"
err = copy.Copy(testPem, localTestCertPath())
if err != nil {
// Write to a temp file for an atomic write
tmpPem := localTestCertPath() + ".pem"
if err := copy.Copy(testPem, tmpPem); err != nil {
t.Fatalf("failed to copy %s: %v", testPem, err)
}
if err := os.Rename(tmpPem, localTestCertPath()); err != nil {
t.Fatalf("failed to rename %s: %v", tmpPem, err)
}
want, err := os.Stat(testPem)
if err != nil {
t.Fatalf("stat failed: %v", err)