Skip to content

Commit

Permalink
Fixing windows tests elastic#1
Browse files Browse the repository at this point in the history
  • Loading branch information
ph committed Mar 5, 2020
1 parent 05d37e0 commit b9a239c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
15 changes: 12 additions & 3 deletions x-pack/agent/pkg/agent/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ func (d *DiskStore) Save(in io.Reader) error {
errors.TypeFilesystem,
errors.M(errors.MetaKeyPath, tmpFile))
}
defer fd.Close()

// Always clean up the temporary file and ignore errors.
defer os.Remove(tmpFile)
Expand All @@ -170,6 +169,12 @@ func (d *DiskStore) Save(in io.Reader) error {
errors.M(errors.MetaKeyPath, tmpFile))
}

if err := fd.Close(); err != nil {
return errors.New(err, "could not close temporary file",
errors.TypeFilesystem,
errors.M(errors.MetaKeyPath, tmpFile))
}

if err := file.SafeFileRotate(d.target, tmpFile); err != nil {
return errors.New(err,
fmt.Sprintf("could not replace target file %s", d.target),
Expand Down Expand Up @@ -211,8 +216,6 @@ func (d *EncryptedDiskStore) Save(in io.Reader) error {
errors.TypeFilesystem,
errors.M(errors.MetaKeyPath, tmpFile))
}
defer fd.Close()

// Always clean up the temporary file and ignore errors.
defer os.Remove(tmpFile)

Expand All @@ -229,6 +232,12 @@ func (d *EncryptedDiskStore) Save(in io.Reader) error {
errors.M(errors.MetaKeyPath, tmpFile))
}

if err := fd.Close(); err != nil {
return errors.New(err, "could not close temporary file",
errors.TypeFilesystem,
errors.M(errors.MetaKeyPath, tmpFile))
}

if err := file.SafeFileRotate(d.target, tmpFile); err != nil {
return errors.New(err,
fmt.Sprintf("could not replace target file %s", d.target),
Expand Down
5 changes: 3 additions & 2 deletions x-pack/agent/pkg/agent/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func TestEncryptedDiskStore(t *testing.T) {
require.Equal(t, msg, content)
})

t.Run("when the target do no exist", func(t *testing.T) {
t.Run("when the target do not exist", func(t *testing.T) {
dir, err := ioutil.TempDir("", "configs")
require.NoError(t, err)
defer os.Remove(dir)
Expand Down Expand Up @@ -213,11 +213,12 @@ func genFile(b []byte) (string, error) {
return "", err
}
f.Write(b)
name := f.Name()
if err := f.Close(); err != nil {
return "", err
}

return f.Name(), nil
return name, nil
}

func requireFilesCount(t *testing.T, dir string, l int) {
Expand Down

0 comments on commit b9a239c

Please sign in to comment.