diff --git a/libnetwork/libnetwork_internal_test.go b/libnetwork/libnetwork_internal_test.go index e65024d581..72e45e5c63 100644 --- a/libnetwork/libnetwork_internal_test.go +++ b/libnetwork/libnetwork_internal_test.go @@ -570,11 +570,7 @@ func TestIpamReleaseOnNetDriverFailures(t *testing.T) { defer testutils.SetupTestOSContext(t)() - cfgOptions, err := OptionBoltdbWithRandomDBFile() - if err != nil { - t.Fatal(err) - } - c, err := New(cfgOptions...) + c, err := New(OptionBoltdbWithRandomDBFile(t)) if err != nil { t.Fatal(err) } diff --git a/libnetwork/libnetwork_test.go b/libnetwork/libnetwork_test.go index 7a8778de62..b62afdd964 100644 --- a/libnetwork/libnetwork_test.go +++ b/libnetwork/libnetwork_test.go @@ -51,11 +51,10 @@ func newController(t *testing.T) libnetwork.NetworkController { }, } - cfgOptions, err := libnetwork.OptionBoltdbWithRandomDBFile() - if err != nil { - t.Fatal(err) - } - c, err := libnetwork.New(append(cfgOptions, config.OptionDriverConfig(bridgeNetType, genericOption))...) + c, err := libnetwork.New( + libnetwork.OptionBoltdbWithRandomDBFile(t), + config.OptionDriverConfig(bridgeNetType, genericOption), + ) if err != nil { t.Fatal(err) } diff --git a/libnetwork/sandbox_test.go b/libnetwork/sandbox_test.go index 4370b05dd5..091d244db7 100644 --- a/libnetwork/sandbox_test.go +++ b/libnetwork/sandbox_test.go @@ -25,11 +25,10 @@ func getTestEnv(t *testing.T, opts ...[]NetworkOption) (NetworkController, []Net genericOption := make(map[string]interface{}) genericOption[netlabel.GenericData] = option - cfgOptions, err := OptionBoltdbWithRandomDBFile() - if err != nil { - t.Fatal(err) - } - c, err := New(append(cfgOptions, config.OptionDriverConfig(netType, genericOption))...) + c, err := New( + OptionBoltdbWithRandomDBFile(t), + config.OptionDriverConfig(netType, genericOption), + ) if err != nil { t.Fatal(err) } diff --git a/libnetwork/store_linux_test.go b/libnetwork/store_linux_test.go index 09ced57887..5a8bc1f8ab 100644 --- a/libnetwork/store_linux_test.go +++ b/libnetwork/store_linux_test.go @@ -17,11 +17,7 @@ func TestBoltdbBackend(t *testing.T) { } func TestNoPersist(t *testing.T) { - cfgOptions, err := OptionBoltdbWithRandomDBFile() - if err != nil { - t.Fatalf("Error creating random boltdb file : %v", err) - } - ctrl, err := New(cfgOptions...) + ctrl, err := New(OptionBoltdbWithRandomDBFile(t)) if err != nil { t.Fatalf("Error new controller: %v", err) } diff --git a/libnetwork/store_test.go b/libnetwork/store_test.go index 68192fcaa8..95e6b301b8 100644 --- a/libnetwork/store_test.go +++ b/libnetwork/store_test.go @@ -1,8 +1,8 @@ package libnetwork import ( - "fmt" "os" + "path/filepath" "testing" "github.com/docker/docker/libnetwork/config" @@ -57,34 +57,29 @@ func testLocalBackend(t *testing.T, provider, url string, storeConfig *store.Con } // OptionBoltdbWithRandomDBFile function returns a random dir for local store backend -func OptionBoltdbWithRandomDBFile() ([]config.Option, error) { - tmp, err := os.CreateTemp("", "libnetwork-") - if err != nil { - return nil, fmt.Errorf("Error creating temp file: %v", err) +func OptionBoltdbWithRandomDBFile(t *testing.T) config.Option { + t.Helper() + tmp := filepath.Join(t.TempDir(), "bolt.db") + if err := os.WriteFile(tmp, nil, 0o600); err != nil { + t.Fatal(err) } - if err := tmp.Close(); err != nil { - return nil, fmt.Errorf("Error closing temp file: %v", err) + + return func(c *config.Config) { + config.OptionLocalKVProvider("boltdb")(c) + config.OptionLocalKVProviderURL(tmp)(c) + config.OptionLocalKVProviderConfig(&store.Config{Bucket: "testBackend"})(c) } - cfgOptions := []config.Option{} - cfgOptions = append(cfgOptions, config.OptionLocalKVProvider("boltdb")) - cfgOptions = append(cfgOptions, config.OptionLocalKVProviderURL(tmp.Name())) - sCfg := &store.Config{Bucket: "testBackend"} - cfgOptions = append(cfgOptions, config.OptionLocalKVProviderConfig(sCfg)) - return cfgOptions, nil } func TestMultipleControllersWithSameStore(t *testing.T) { - cfgOptions, err := OptionBoltdbWithRandomDBFile() - if err != nil { - t.Fatalf("Error getting random boltdb configs %v", err) - } - ctrl1, err := New(cfgOptions...) + cfgOptions := OptionBoltdbWithRandomDBFile(t) + ctrl1, err := New(cfgOptions) if err != nil { t.Fatalf("Error new controller: %v", err) } defer ctrl1.Stop() // Use the same boltdb file without closing the previous controller - ctrl2, err := New(cfgOptions...) + ctrl2, err := New(cfgOptions) if err != nil { t.Fatalf("Local store must support concurrent controllers") }