Ver código fonte

libnetwork: don't access KVStore directly in tests

Test the datastore, not the KVStore backing it.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 1 ano atrás
pai
commit
a5ee0d6af8
2 arquivos alterados com 36 adições e 13 exclusões
  1. 18 6
      libnetwork/store_linux_test.go
  2. 18 7
      libnetwork/store_test.go

+ 18 - 6
libnetwork/store_linux_test.go

@@ -1,6 +1,7 @@
 package libnetwork
 
 import (
+	"errors"
 	"os"
 	"path/filepath"
 	"testing"
@@ -32,12 +33,23 @@ func TestNoPersist(t *testing.T) {
 	if err != nil {
 		t.Fatalf("Error creating endpoint: %v", err)
 	}
-	kvStore := testController.getStore().KVStore()
-	if exists, _ := kvStore.Exists(datastore.Key(datastore.NetworkKeyPrefix, nw.ID())); exists {
-		t.Fatalf("Network with persist=false should not be stored in KV Store")
+
+	// 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....
+	nwKVObject := &Network{id: nw.ID()}
+	err = testController.getStore().GetObject(datastore.Key(datastore.NetworkKeyPrefix, nw.ID()), nwKVObject)
+	if !errors.Is(err, store.ErrKeyNotFound) {
+		t.Errorf("Expected %v error when retrieving network from store, got: %v", store.ErrKeyNotFound, err)
+	}
+	if nwKVObject.Exists() {
+		t.Errorf("Network with persist=false should not be stored in KV Store")
+	}
+
+	epKVObject := &Endpoint{network: nw, id: ep.ID()}
+	err = testController.getStore().GetObject(datastore.Key(datastore.EndpointKeyPrefix, nw.ID(), ep.ID()), epKVObject)
+	if !errors.Is(err, store.ErrKeyNotFound) {
+		t.Errorf("Expected %v error when retrieving endpoint from store, got: %v", store.ErrKeyNotFound, err)
 	}
-	if exists, _ := kvStore.Exists(datastore.Key([]string{datastore.EndpointKeyPrefix, nw.ID(), ep.ID()}...)); exists {
-		t.Fatalf("Endpoint in Network with persist=false should not be stored in KV Store")
+	if epKVObject.Exists() {
+		t.Errorf("Endpoint in Network with persist=false should not be stored in KV Store")
 	}
-	kvStore.Close()
 }

+ 18 - 7
libnetwork/store_test.go

@@ -36,14 +36,25 @@ func testLocalBackend(t *testing.T, provider, url string, storeConfig *store.Con
 	if err != nil {
 		t.Fatalf("Error creating endpoint: %v", err)
 	}
-	kvStore := testController.getStore().KVStore()
-	if exists, err := kvStore.Exists(datastore.Key(datastore.NetworkKeyPrefix, nw.ID())); !exists || err != nil {
-		t.Fatalf("Network key should have been created.")
+	// 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....
+	nwKVObject := &Network{id: nw.ID()}
+	err = testController.getStore().GetObject(datastore.Key(datastore.NetworkKeyPrefix, nw.ID()), nwKVObject)
+	if err != nil {
+		t.Errorf("Error when retrieving network key from store: %v", err)
+	}
+	if !nwKVObject.Exists() {
+		t.Errorf("Network key should have been created.")
+	}
+
+	epKVObject := &Endpoint{network: nw, id: ep.ID()}
+	err = testController.getStore().GetObject(datastore.Key(datastore.EndpointKeyPrefix, nw.ID(), ep.ID()), epKVObject)
+	if err != nil {
+		t.Errorf("Error when retrieving Endpoint key from store: %v", err)
 	}
-	if exists, err := kvStore.Exists(datastore.Key([]string{datastore.EndpointKeyPrefix, nw.ID(), ep.ID()}...)); !exists || err != nil {
-		t.Fatalf("Endpoint key should have been created.")
+	if !epKVObject.Exists() {
+		t.Errorf("Endpoint key should have been created.")
 	}
-	kvStore.Close()
+	testController.Stop()
 
 	// test restore of local store
 	testController, err = New(cfgOptions...)
@@ -52,7 +63,7 @@ func testLocalBackend(t *testing.T, provider, url string, storeConfig *store.Con
 	}
 	defer testController.Stop()
 	if _, err = testController.NetworkByID(nw.ID()); err != nil {
-		t.Fatalf("Error getting network %v", err)
+		t.Errorf("Error getting network %v", err)
 	}
 }