Example #1
0
func testStorageStoreRetrieveNodesForMetadata(storage cluster.Storage, t *testing.T) {
	node1 := cluster.Node{Address: "my-addr-1", Metadata: map[string]string{
		"region": "reg1",
		"foo":    "bar",
	}}
	err := storage.StoreNode(node1)
	assertIsNil(err, t)
	defer storage.RemoveNode("my-addr-1")
	node2 := cluster.Node{Address: "my-addr-2", Metadata: map[string]string{
		"region": "reg2",
		"foo":    "bar",
	}}
	err = storage.StoreNode(node2)
	assertIsNil(err, t)
	defer storage.RemoveNode("my-addr-2")
	nodes, err := storage.RetrieveNodesByMetadata(map[string]string{"region": "reg2"})
	assertIsNil(err, t)
	if len(nodes) != 1 {
		t.Fatalf("unexpected nodes len: %d", len(nodes))
	}
	if nodes[0].Address != node2.Address {
		t.Errorf("unexpected node: %s", nodes[0].Address)
	}
	if !reflect.DeepEqual(node2.Metadata, nodes[0].Metadata) {
		t.Errorf("unexpected node metadata. expected: %#v got: %#v", node2.Metadata, nodes[0].Metadata)
	}
	nodes, err = storage.RetrieveNodesByMetadata(map[string]string{"foo": "bar"})
	assertIsNil(err, t)
	if len(nodes) != 2 {
		t.Fatalf("unexpected nodes len: %d", len(nodes))
	}
	sort.Sort(cluster.NodeList(nodes))
	if nodes[0].Address != node1.Address || nodes[1].Address != node2.Address {
		t.Errorf("unexpected nodes: %#v", nodes)
	}
	if !reflect.DeepEqual(node1.Metadata, nodes[0].Metadata) {
		t.Errorf("unexpected node metadata. expected: %#v got: %#v", node1.Metadata, nodes[0].Metadata)
	}
	if !reflect.DeepEqual(node2.Metadata, nodes[1].Metadata) {
		t.Errorf("unexpected node metadata. expected: %#v got: %#v", node2.Metadata, nodes[1].Metadata)
	}
}