提交 6d931a21 编写于 作者: B Benoit Gagnon 提交者: Simon Pasquier

Fix Windows support for custom-sd adapter (#6217)

* add test to custom-sd/adapter writeOutput() function
Signed-off-by: NBenoit Gagnon <benoit.gagnon@ubisoft.com>

* fix Adapter.writeOutput() function to work on Windows

On that platform, files cannot be moved while a process holds a handle
to them. Added an explicit Close() before that move. With this change,
the unit test succeeds.
Signed-off-by: NBenoit Gagnon <benoit.gagnon@ubisoft.com>

* add missing dot to comment
Signed-off-by: NBenoit Gagnon <benoit.gagnon@ubisoft.com>
上级 e0443e6f
......@@ -128,6 +128,9 @@ func (a *Adapter) writeOutput() error {
return err
}
// Close the file immediately for platforms (eg. Windows) that cannot move
// a file while a process is holding a file handle.
tmpfile.Close()
err = os.Rename(tmpfile.Name(), a.output)
if err != nil {
return err
......
......@@ -14,6 +14,10 @@
package adapter
import (
"context"
"github.com/prometheus/prometheus/util/testutil"
"io/ioutil"
"os"
"reflect"
"testing"
......@@ -222,3 +226,14 @@ func TestGenerateTargetGroups(t *testing.T) {
}
}
// TestWriteOutput checks the adapter can write a file to disk.
func TestWriteOutput(t *testing.T) {
ctx := context.Background()
tmpfile, err := ioutil.TempFile("", "sd_adapter_test")
testutil.Ok(t, err)
defer os.Remove(tmpfile.Name())
tmpfile.Close()
adapter := NewAdapter(ctx, tmpfile.Name(), "test_sd", nil, nil)
testutil.Ok(t, adapter.writeOutput())
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册