store_test.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package libnetwork
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "github.com/docker/docker/libnetwork/config"
  7. "github.com/docker/docker/libnetwork/datastore"
  8. store "github.com/docker/docker/libnetwork/internal/kvstore"
  9. "github.com/docker/docker/libnetwork/netlabel"
  10. "github.com/docker/docker/libnetwork/options"
  11. )
  12. func testLocalBackend(t *testing.T, provider, url string, storeConfig *store.Config) {
  13. cfgOptions := []config.Option{func(c *config.Config) {
  14. c.Scope.Client.Provider = provider
  15. c.Scope.Client.Address = url
  16. c.Scope.Client.Config = storeConfig
  17. }}
  18. cfgOptions = append(cfgOptions, config.OptionDriverConfig("host", map[string]interface{}{
  19. netlabel.GenericData: options.Generic{},
  20. }))
  21. testController, err := New(cfgOptions...)
  22. if err != nil {
  23. t.Fatalf("Error new controller: %v", err)
  24. }
  25. defer testController.Stop()
  26. nw, err := testController.NewNetwork("host", "host", "")
  27. if err != nil {
  28. t.Fatalf(`Error creating default "host" network: %v`, err)
  29. }
  30. ep, err := nw.CreateEndpoint("newendpoint", []EndpointOption{}...)
  31. if err != nil {
  32. t.Fatalf("Error creating endpoint: %v", err)
  33. }
  34. // FIXME(thaJeztah): GetObject uses the given key for lookups if no cache-store is present, but the KvObject's Key() to look up in cache....
  35. nwKVObject := &Network{id: nw.ID()}
  36. err = testController.getStore().GetObject(datastore.Key(datastore.NetworkKeyPrefix, nw.ID()), nwKVObject)
  37. if err != nil {
  38. t.Errorf("Error when retrieving network key from store: %v", err)
  39. }
  40. if !nwKVObject.Exists() {
  41. t.Errorf("Network key should have been created.")
  42. }
  43. epKVObject := &Endpoint{network: nw, id: ep.ID()}
  44. err = testController.getStore().GetObject(datastore.Key(datastore.EndpointKeyPrefix, nw.ID(), ep.ID()), epKVObject)
  45. if err != nil {
  46. t.Errorf("Error when retrieving Endpoint key from store: %v", err)
  47. }
  48. if !epKVObject.Exists() {
  49. t.Errorf("Endpoint key should have been created.")
  50. }
  51. testController.Stop()
  52. // test restore of local store
  53. testController, err = New(cfgOptions...)
  54. if err != nil {
  55. t.Fatalf("Error creating controller: %v", err)
  56. }
  57. defer testController.Stop()
  58. if _, err = testController.NetworkByID(nw.ID()); err != nil {
  59. t.Errorf("Error getting network %v", err)
  60. }
  61. }
  62. // OptionBoltdbWithRandomDBFile function returns a random dir for local store backend
  63. func OptionBoltdbWithRandomDBFile(t *testing.T) config.Option {
  64. t.Helper()
  65. tmp := filepath.Join(t.TempDir(), "bolt.db")
  66. if err := os.WriteFile(tmp, nil, 0o600); err != nil {
  67. t.Fatal(err)
  68. }
  69. return func(c *config.Config) {
  70. c.Scope.Client.Provider = "boltdb"
  71. c.Scope.Client.Address = tmp
  72. c.Scope.Client.Config = &store.Config{Bucket: "testBackend"}
  73. }
  74. }
  75. func TestMultipleControllersWithSameStore(t *testing.T) {
  76. cfgOptions := OptionBoltdbWithRandomDBFile(t)
  77. ctrl1, err := New(cfgOptions)
  78. if err != nil {
  79. t.Fatalf("Error new controller: %v", err)
  80. }
  81. defer ctrl1.Stop()
  82. // Use the same boltdb file without closing the previous controller
  83. ctrl2, err := New(cfgOptions)
  84. if err != nil {
  85. t.Fatalf("Local store must support concurrent controllers")
  86. }
  87. ctrl2.Stop()
  88. }