store_test.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package libnetwork
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "github.com/docker/docker/libnetwork/config"
  7. store "github.com/docker/docker/libnetwork/internal/kvstore"
  8. "github.com/docker/docker/libnetwork/netlabel"
  9. "github.com/docker/docker/libnetwork/options"
  10. )
  11. func testLocalBackend(t *testing.T, provider, url string, storeConfig *store.Config) {
  12. cfgOptions := []config.Option{func(c *config.Config) {
  13. c.Scope.Client.Provider = provider
  14. c.Scope.Client.Address = url
  15. c.Scope.Client.Config = storeConfig
  16. }}
  17. cfgOptions = append(cfgOptions, config.OptionDriverConfig("host", map[string]interface{}{
  18. netlabel.GenericData: options.Generic{},
  19. }))
  20. testController, err := New(cfgOptions...)
  21. if err != nil {
  22. t.Fatalf("Error new controller: %v", err)
  23. }
  24. defer testController.Stop()
  25. nw, err := testController.NewNetwork("host", "host", "")
  26. if err != nil {
  27. t.Fatalf(`Error creating default "host" network: %v`, err)
  28. }
  29. ep, err := nw.CreateEndpoint("newendpoint", []EndpointOption{}...)
  30. if err != nil {
  31. t.Fatalf("Error creating endpoint: %v", err)
  32. }
  33. nwKVObject := &Network{id: nw.ID()}
  34. err = testController.getStore().GetObject(nwKVObject)
  35. if err != nil {
  36. t.Errorf("Error when retrieving network key from store: %v", err)
  37. }
  38. if !nwKVObject.Exists() {
  39. t.Errorf("Network key should have been created.")
  40. }
  41. epKVObject := &Endpoint{network: nw, id: ep.ID()}
  42. err = testController.getStore().GetObject(epKVObject)
  43. if err != nil {
  44. t.Errorf("Error when retrieving Endpoint key from store: %v", err)
  45. }
  46. if !epKVObject.Exists() {
  47. t.Errorf("Endpoint key should have been created.")
  48. }
  49. testController.Stop()
  50. // test restore of local store
  51. testController, err = New(cfgOptions...)
  52. if err != nil {
  53. t.Fatalf("Error creating controller: %v", err)
  54. }
  55. defer testController.Stop()
  56. if _, err = testController.NetworkByID(nw.ID()); err != nil {
  57. t.Errorf("Error getting network %v", err)
  58. }
  59. }
  60. // OptionBoltdbWithRandomDBFile function returns a random dir for local store backend
  61. func OptionBoltdbWithRandomDBFile(t *testing.T) config.Option {
  62. t.Helper()
  63. tmp := filepath.Join(t.TempDir(), "bolt.db")
  64. if err := os.WriteFile(tmp, nil, 0o600); err != nil {
  65. t.Fatal(err)
  66. }
  67. return func(c *config.Config) {
  68. c.Scope.Client.Provider = "boltdb"
  69. c.Scope.Client.Address = tmp
  70. c.Scope.Client.Config = &store.Config{Bucket: "testBackend"}
  71. }
  72. }