store_linux_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. nwKVObject := &Network{id: nw.ID()}
  48. err = testController.getStore().GetObject(nwKVObject)
  49. if !errors.Is(err, store.ErrKeyNotFound) {
  50. t.Errorf("Expected %q error when retrieving network from store, got: %q", store.ErrKeyNotFound, err)
  51. }
  52. if nwKVObject.Exists() {
  53. t.Errorf("Network with persist=false should not be stored in KV Store")
  54. }
  55. epKVObject := &Endpoint{network: nw, id: ep.ID()}
  56. err = testController.getStore().GetObject(epKVObject)
  57. if !errors.Is(err, store.ErrKeyNotFound) {
  58. t.Errorf("Expected %v error when retrieving endpoint from store, got: %v", store.ErrKeyNotFound, err)
  59. }
  60. if epKVObject.Exists() {
  61. t.Errorf("Endpoint in Network with persist=false should not be stored in KV Store")
  62. }
  63. }