소스 검색

libnetwork_test: improve OptionBoltdbWithRandomDBFile

Now that this function is only ever called from contexts where a
*testing.T value is available, several improvements can be made to it.
Refactor it to be a test helper function so that callers do not need to
check and fail the test themselves. Leverage t.TempDir() so that the
temporary file is cleaned up automatically. Change it to return a single
config.Option to get better ergonomics at call sites.

Signed-off-by: Cory Snider <csnider@mirantis.com>
Cory Snider 2 년 전
부모
커밋
6a15f40803
5개의 변경된 파일24개의 추가작업 그리고 39개의 파일을 삭제
  1. 1 5
      libnetwork/libnetwork_internal_test.go
  2. 4 5
      libnetwork/libnetwork_test.go
  3. 4 5
      libnetwork/sandbox_test.go
  4. 1 5
      libnetwork/store_linux_test.go
  5. 14 19
      libnetwork/store_test.go

+ 1 - 5
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)
 	}

+ 4 - 5
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)
 	}

+ 4 - 5
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)
 	}

+ 1 - 5
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)
 	}

+ 14 - 19
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")
 	}