store_linux_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package libnetwork
  2. import (
  3. "errors"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "github.com/docker/docker/libnetwork/config"
  8. "github.com/docker/docker/libnetwork/datastore"
  9. store "github.com/docker/docker/libnetwork/internal/kvstore"
  10. )
  11. func TestBoltdbBackend(t *testing.T) {
  12. defer os.Remove(datastore.DefaultScope("").Client.Address)
  13. testLocalBackend(t, "", "", nil)
  14. tmpPath := filepath.Join(t.TempDir(), "boltdb.db")
  15. testLocalBackend(t, "boltdb", tmpPath, &store.Config{
  16. Bucket: "testBackend",
  17. })
  18. }
  19. func TestNoPersist(t *testing.T) {
  20. dbFile := filepath.Join(t.TempDir(), "bolt.db")
  21. configOption := func(c *config.Config) {
  22. c.Scope.Client.Provider = "boltdb"
  23. c.Scope.Client.Address = dbFile
  24. c.Scope.Client.Config = &store.Config{Bucket: "testBackend"}
  25. }
  26. testController, err := New(configOption)
  27. if err != nil {
  28. t.Fatalf("Error creating new controller: %v", err)
  29. }
  30. defer testController.Stop()
  31. nw, err := testController.NewNetwork("host", "host", "", NetworkOptionPersist(false))
  32. if err != nil {
  33. t.Fatalf(`Error creating default "host" network: %v`, err)
  34. }
  35. ep, err := nw.CreateEndpoint("newendpoint", []EndpointOption{}...)
  36. if err != nil {
  37. t.Fatalf("Error creating endpoint: %v", err)
  38. }
  39. testController.Stop()
  40. // Create a new controller using the same database-file. The network
  41. // should not have persisted.
  42. testController, err = New(configOption)
  43. if err != nil {
  44. t.Fatalf("Error creating new controller: %v", err)
  45. }
  46. defer testController.Stop()
  47. // 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....
  48. nwKVObject := &Network{id: nw.ID()}
  49. err = testController.getStore().GetObject(datastore.Key(datastore.NetworkKeyPrefix, nw.ID()), nwKVObject)
  50. if !errors.Is(err, store.ErrKeyNotFound) {
  51. t.Errorf("Expected %q error when retrieving network from store, got: %q", store.ErrKeyNotFound, err)
  52. }
  53. if nwKVObject.Exists() {
  54. t.Errorf("Network with persist=false should not be stored in KV Store")
  55. }
  56. epKVObject := &Endpoint{network: nw, id: ep.ID()}
  57. err = testController.getStore().GetObject(datastore.Key(datastore.EndpointKeyPrefix, nw.ID(), ep.ID()), epKVObject)
  58. if !errors.Is(err, store.ErrKeyNotFound) {
  59. t.Errorf("Expected %v error when retrieving endpoint from store, got: %v", store.ErrKeyNotFound, err)
  60. }
  61. if epKVObject.Exists() {
  62. t.Errorf("Endpoint in Network with persist=false should not be stored in KV Store")
  63. }
  64. }