From 4a1faf435c1e0795b95a842017afb3101041ddb5 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Thu, 3 Apr 2014 12:03:10 -0700 Subject: [PATCH] consul: Updating all the tests --- consul/catalog_endpoint_test.go | 24 ++++----- consul/fsm_test.go | 16 +++--- consul/health_endpoint_test.go | 8 +-- consul/state_store_test.go | 96 ++++++++++++++++----------------- 4 files changed, 72 insertions(+), 72 deletions(-) diff --git a/consul/catalog_endpoint_test.go b/consul/catalog_endpoint_test.go index 9ebd079d1f..1184d6ddb5 100644 --- a/consul/catalog_endpoint_test.go +++ b/consul/catalog_endpoint_test.go @@ -23,7 +23,7 @@ func TestCatalogRegister(t *testing.T) { Address: "127.0.0.1", Service: &structs.NodeService{ Service: "db", - Tag: "master", + Tags: []string{"master"}, Port: 8000, }, } @@ -79,7 +79,7 @@ func TestCatalogRegister_ForwardLeader(t *testing.T) { Address: "127.0.0.1", Service: &structs.NodeService{ Service: "db", - Tag: "master", + Tags: []string{"master"}, Port: 8000, }, } @@ -116,7 +116,7 @@ func TestCatalogRegister_ForwardDC(t *testing.T) { Address: "127.0.0.1", Service: &structs.NodeService{ Service: "db", - Tag: "master", + Tags: []string{"master"}, Port: 8000, }, } @@ -277,7 +277,7 @@ func TestCatalogListServices(t *testing.T) { // Just add a node s1.fsm.State().EnsureNode(1, structs.Node{"foo", "127.0.0.1"}) - s1.fsm.State().EnsureService(2, "foo", &structs.NodeService{"db", "db", "primary", 5000}) + s1.fsm.State().EnsureService(2, "foo", &structs.NodeService{"db", "db", []string{"primary"}, 5000}) if err := client.Call("Catalog.ListServices", &args, &out); err != nil { t.Fatalf("err: %v", err) @@ -287,7 +287,7 @@ func TestCatalogListServices(t *testing.T) { t.Fatalf("bad: %v", out) } // Consul service should auto-register - if len(out.Services["consul"]) != 1 { + if _, ok := out.Services["consul"]; !ok { t.Fatalf("bad: %v", out) } if len(out.Services["db"]) != 1 { @@ -327,7 +327,7 @@ func TestCatalogListServices_Blocking(t *testing.T) { go func() { time.Sleep(100 * time.Millisecond) s1.fsm.State().EnsureNode(1, structs.Node{"foo", "127.0.0.1"}) - s1.fsm.State().EnsureService(2, "foo", &structs.NodeService{"db", "db", "primary", 5000}) + s1.fsm.State().EnsureService(2, "foo", &structs.NodeService{"db", "db", []string{"primary"}, 5000}) }() // Re-run the query @@ -418,7 +418,7 @@ func TestCatalogListServiceNodes(t *testing.T) { // Just add a node s1.fsm.State().EnsureNode(1, structs.Node{"foo", "127.0.0.1"}) - s1.fsm.State().EnsureService(2, "foo", &structs.NodeService{"db", "db", "primary", 5000}) + s1.fsm.State().EnsureService(2, "foo", &structs.NodeService{"db", "db", []string{"primary"}, 5000}) if err := client.Call("Catalog.ServiceNodes", &args, &out); err != nil { t.Fatalf("err: %v", err) @@ -462,8 +462,8 @@ func TestCatalogNodeServices(t *testing.T) { // Just add a node s1.fsm.State().EnsureNode(1, structs.Node{"foo", "127.0.0.1"}) - s1.fsm.State().EnsureService(2, "foo", &structs.NodeService{"db", "db", "primary", 5000}) - s1.fsm.State().EnsureService(3, "foo", &structs.NodeService{"web", "web", "", 80}) + s1.fsm.State().EnsureService(2, "foo", &structs.NodeService{"db", "db", []string{"primary"}, 5000}) + s1.fsm.State().EnsureService(3, "foo", &structs.NodeService{"web", "web", nil, 80}) if err := client.Call("Catalog.NodeServices", &args, &out); err != nil { t.Fatalf("err: %v", err) @@ -476,10 +476,10 @@ func TestCatalogNodeServices(t *testing.T) { t.Fatalf("bad: %v", out) } services := out.NodeServices.Services - if services["db"].Tag != "primary" || services["db"].Port != 5000 { + if !strContains(services["db"].Tags, "primary") || services["db"].Port != 5000 { t.Fatalf("bad: %v", out) } - if services["web"].Tag != "" || services["web"].Port != 80 { + if services["web"].Tags != nil || services["web"].Port != 80 { t.Fatalf("bad: %v", out) } } @@ -498,7 +498,7 @@ func TestCatalogRegister_FailedCase1(t *testing.T) { Address: "127.0.0.2", Service: &structs.NodeService{ Service: "web", - Tag: "", + Tags: nil, Port: 8000, }, } diff --git a/consul/fsm_test.go b/consul/fsm_test.go index 24f7c7cbbf..ac8453bec2 100644 --- a/consul/fsm_test.go +++ b/consul/fsm_test.go @@ -85,7 +85,7 @@ func TestFSM_RegisterNode_Service(t *testing.T) { Service: &structs.NodeService{ ID: "db", Service: "db", - Tag: "master", + Tags: []string{"master"}, Port: 8000, }, Check: &structs.HealthCheck{ @@ -138,7 +138,7 @@ func TestFSM_DeregisterService(t *testing.T) { Service: &structs.NodeService{ ID: "db", Service: "db", - Tag: "master", + Tags: []string{"master"}, Port: 8000, }, } @@ -248,7 +248,7 @@ func TestFSM_DeregisterNode(t *testing.T) { Service: &structs.NodeService{ ID: "db", Service: "db", - Tag: "master", + Tags: []string{"master"}, Port: 8000, }, Check: &structs.HealthCheck{ @@ -311,10 +311,10 @@ func TestFSM_SnapshotRestore(t *testing.T) { // Add some state fsm.state.EnsureNode(1, structs.Node{"foo", "127.0.0.1"}) fsm.state.EnsureNode(2, structs.Node{"baz", "127.0.0.2"}) - fsm.state.EnsureService(3, "foo", &structs.NodeService{"web", "web", "", 80}) - fsm.state.EnsureService(4, "foo", &structs.NodeService{"db", "db", "primary", 5000}) - fsm.state.EnsureService(5, "baz", &structs.NodeService{"web", "web", "", 80}) - fsm.state.EnsureService(6, "baz", &structs.NodeService{"db", "db", "secondary", 5000}) + fsm.state.EnsureService(3, "foo", &structs.NodeService{"web", "web", nil, 80}) + fsm.state.EnsureService(4, "foo", &structs.NodeService{"db", "db", []string{"primary"}, 5000}) + fsm.state.EnsureService(5, "baz", &structs.NodeService{"web", "web", nil, 80}) + fsm.state.EnsureService(6, "baz", &structs.NodeService{"db", "db", []string{"secondary"}, 5000}) fsm.state.EnsureCheck(7, &structs.HealthCheck{ Node: "foo", CheckID: "web", @@ -363,7 +363,7 @@ func TestFSM_SnapshotRestore(t *testing.T) { if len(fooSrv.Services) != 2 { t.Fatalf("Bad: %v", fooSrv) } - if fooSrv.Services["db"].Tag != "primary" { + if !strContains(fooSrv.Services["db"].Tags, "primary") { t.Fatalf("Bad: %v", fooSrv) } if fooSrv.Services["db"].Port != 5000 { diff --git a/consul/health_endpoint_test.go b/consul/health_endpoint_test.go index 000b57ee47..465f358467 100644 --- a/consul/health_endpoint_test.go +++ b/consul/health_endpoint_test.go @@ -160,7 +160,7 @@ func TestHealth_ServiceNodes(t *testing.T) { Service: &structs.NodeService{ ID: "db", Service: "db", - Tag: "master", + Tags: []string{"master"}, }, Check: &structs.HealthCheck{ Name: "db connect", @@ -180,7 +180,7 @@ func TestHealth_ServiceNodes(t *testing.T) { Service: &structs.NodeService{ ID: "db", Service: "db", - Tag: "slave", + Tags: []string{"slave"}, }, Check: &structs.HealthCheck{ Name: "db connect", @@ -213,10 +213,10 @@ func TestHealth_ServiceNodes(t *testing.T) { if nodes[1].Node.Node != "bar" { t.Fatalf("Bad: %v", nodes[1]) } - if nodes[0].Service.Tag != "master" { + if !strContains(nodes[0].Service.Tags, "master") { t.Fatalf("Bad: %v", nodes[0]) } - if nodes[1].Service.Tag != "slave" { + if !strContains(nodes[1].Service.Tags, "slave") { t.Fatalf("Bad: %v", nodes[1]) } if nodes[0].Checks[0].Status != structs.HealthPassing { diff --git a/consul/state_store_test.go b/consul/state_store_test.go index 5e5bf3e8e1..dbdb2bcc23 100644 --- a/consul/state_store_test.go +++ b/consul/state_store_test.go @@ -96,15 +96,15 @@ func TestEnsureService(t *testing.T) { t.Fatalf("err: %v", err) } - if err := store.EnsureService(11, "foo", &structs.NodeService{"api", "api", "", 5000}); err != nil { + if err := store.EnsureService(11, "foo", &structs.NodeService{"api", "api", nil, 5000}); err != nil { t.Fatalf("err: %v", err) } - if err := store.EnsureService(12, "foo", &structs.NodeService{"api", "api", "", 5001}); err != nil { + if err := store.EnsureService(12, "foo", &structs.NodeService{"api", "api", nil, 5001}); err != nil { t.Fatalf("err: %v", err) } - if err := store.EnsureService(13, "foo", &structs.NodeService{"db", "db", "master", 8000}); err != nil { + if err := store.EnsureService(13, "foo", &structs.NodeService{"db", "db", []string{"master"}, 8000}); err != nil { t.Fatalf("err: %v", err) } @@ -117,7 +117,7 @@ func TestEnsureService(t *testing.T) { if !ok { t.Fatalf("missing api: %#v", services) } - if entry.Tag != "" || entry.Port != 5001 { + if entry.Tags != nil || entry.Port != 5001 { t.Fatalf("Bad entry: %#v", entry) } @@ -125,7 +125,7 @@ func TestEnsureService(t *testing.T) { if !ok { t.Fatalf("missing db: %#v", services) } - if entry.Tag != "master" || entry.Port != 8000 { + if !strContains(entry.Tags, "master") || entry.Port != 8000 { t.Fatalf("Bad entry: %#v", entry) } } @@ -141,15 +141,15 @@ func TestEnsureService_DuplicateNode(t *testing.T) { t.Fatalf("err: %v", err) } - if err := store.EnsureService(11, "foo", &structs.NodeService{"api1", "api", "", 5000}); err != nil { + if err := store.EnsureService(11, "foo", &structs.NodeService{"api1", "api", nil, 5000}); err != nil { t.Fatalf("err: %v", err) } - if err := store.EnsureService(12, "foo", &structs.NodeService{"api2", "api", "", 5001}); err != nil { + if err := store.EnsureService(12, "foo", &structs.NodeService{"api2", "api", nil, 5001}); err != nil { t.Fatalf("err: %v", err) } - if err := store.EnsureService(13, "foo", &structs.NodeService{"api3", "api", "", 5002}); err != nil { + if err := store.EnsureService(13, "foo", &structs.NodeService{"api3", "api", nil, 5002}); err != nil { t.Fatalf("err: %v", err) } @@ -162,7 +162,7 @@ func TestEnsureService_DuplicateNode(t *testing.T) { if !ok { t.Fatalf("missing api: %#v", services) } - if entry.Tag != "" || entry.Port != 5000 { + if entry.Tags != nil || entry.Port != 5000 { t.Fatalf("Bad entry: %#v", entry) } @@ -170,7 +170,7 @@ func TestEnsureService_DuplicateNode(t *testing.T) { if !ok { t.Fatalf("missing api: %#v", services) } - if entry.Tag != "" || entry.Port != 5001 { + if entry.Tags != nil || entry.Port != 5001 { t.Fatalf("Bad entry: %#v", entry) } @@ -178,7 +178,7 @@ func TestEnsureService_DuplicateNode(t *testing.T) { if !ok { t.Fatalf("missing api: %#v", services) } - if entry.Tag != "" || entry.Port != 5002 { + if entry.Tags != nil || entry.Port != 5002 { t.Fatalf("Bad entry: %#v", entry) } } @@ -194,7 +194,7 @@ func TestDeleteNodeService(t *testing.T) { t.Fatalf("err: %v", err) } - if err := store.EnsureService(12, "foo", &structs.NodeService{"api", "api", "", 5000}); err != nil { + if err := store.EnsureService(12, "foo", &structs.NodeService{"api", "api", nil, 5000}); err != nil { t.Fatalf("err: %v", err) } @@ -242,11 +242,11 @@ func TestDeleteNodeService_One(t *testing.T) { t.Fatalf("err: %v", err) } - if err := store.EnsureService(12, "foo", &structs.NodeService{"api", "api", "", 5000}); err != nil { + if err := store.EnsureService(12, "foo", &structs.NodeService{"api", "api", nil, 5000}); err != nil { t.Fatalf("err: %v", err) } - if err := store.EnsureService(13, "foo", &structs.NodeService{"api2", "api", "", 5001}); err != nil { + if err := store.EnsureService(13, "foo", &structs.NodeService{"api2", "api", nil, 5001}); err != nil { t.Fatalf("err: %v", err) } @@ -279,7 +279,7 @@ func TestDeleteNode(t *testing.T) { t.Fatalf("err: %v") } - if err := store.EnsureService(21, "foo", &structs.NodeService{"api", "api", "", 5000}); err != nil { + if err := store.EnsureService(21, "foo", &structs.NodeService{"api", "api", nil, 5000}); err != nil { t.Fatalf("err: %v") } @@ -338,15 +338,15 @@ func TestGetServices(t *testing.T) { t.Fatalf("err: %v") } - if err := store.EnsureService(32, "foo", &structs.NodeService{"api", "api", "", 5000}); err != nil { + if err := store.EnsureService(32, "foo", &structs.NodeService{"api", "api", nil, 5000}); err != nil { t.Fatalf("err: %v") } - if err := store.EnsureService(33, "foo", &structs.NodeService{"db", "db", "master", 8000}); err != nil { + if err := store.EnsureService(33, "foo", &structs.NodeService{"db", "db", []string{"master"}, 8000}); err != nil { t.Fatalf("err: %v") } - if err := store.EnsureService(34, "bar", &structs.NodeService{"db", "db", "slave", 8000}); err != nil { + if err := store.EnsureService(34, "bar", &structs.NodeService{"db", "db", []string{"slave"}, 8000}); err != nil { t.Fatalf("err: %v") } @@ -359,7 +359,7 @@ func TestGetServices(t *testing.T) { if !ok { t.Fatalf("missing api: %#v", services) } - if len(tags) != 1 || tags[0] != "" { + if len(tags) != 0 { t.Fatalf("Bad entry: %#v", tags) } @@ -388,23 +388,23 @@ func TestServiceNodes(t *testing.T) { t.Fatalf("err: %v") } - if err := store.EnsureService(12, "foo", &structs.NodeService{"api", "api", "", 5000}); err != nil { + if err := store.EnsureService(12, "foo", &structs.NodeService{"api", "api", nil, 5000}); err != nil { t.Fatalf("err: %v") } - if err := store.EnsureService(13, "bar", &structs.NodeService{"api", "api", "", 5000}); err != nil { + if err := store.EnsureService(13, "bar", &structs.NodeService{"api", "api", nil, 5000}); err != nil { t.Fatalf("err: %v") } - if err := store.EnsureService(14, "foo", &structs.NodeService{"db", "db", "master", 8000}); err != nil { + if err := store.EnsureService(14, "foo", &structs.NodeService{"db", "db", []string{"master"}, 8000}); err != nil { t.Fatalf("err: %v") } - if err := store.EnsureService(15, "bar", &structs.NodeService{"db", "db", "slave", 8000}); err != nil { + if err := store.EnsureService(15, "bar", &structs.NodeService{"db", "db", []string{"slave"}, 8000}); err != nil { t.Fatalf("err: %v") } - if err := store.EnsureService(16, "bar", &structs.NodeService{"db2", "db", "slave", 8001}); err != nil { + if err := store.EnsureService(16, "bar", &structs.NodeService{"db2", "db", []string{"slave"}, 8001}); err != nil { t.Fatalf("err: %v") } @@ -424,7 +424,7 @@ func TestServiceNodes(t *testing.T) { if nodes[0].ServiceID != "db" { t.Fatalf("bad: %v", nodes) } - if nodes[0].ServiceTag != "master" { + if !strContains(nodes[0].ServiceTags, "master") { t.Fatalf("bad: %v", nodes) } if nodes[0].ServicePort != 8000 { @@ -440,7 +440,7 @@ func TestServiceNodes(t *testing.T) { if nodes[1].ServiceID != "db" { t.Fatalf("bad: %v", nodes) } - if nodes[1].ServiceTag != "slave" { + if !strContains(nodes[1].ServiceTags, "slave") { t.Fatalf("bad: %v", nodes) } if nodes[1].ServicePort != 8000 { @@ -456,7 +456,7 @@ func TestServiceNodes(t *testing.T) { if nodes[2].ServiceID != "db2" { t.Fatalf("bad: %v", nodes) } - if nodes[2].ServiceTag != "slave" { + if !strContains(nodes[2].ServiceTags, "slave") { t.Fatalf("bad: %v", nodes) } if nodes[2].ServicePort != 8001 { @@ -479,15 +479,15 @@ func TestServiceTagNodes(t *testing.T) { t.Fatalf("err: %v") } - if err := store.EnsureService(17, "foo", &structs.NodeService{"db", "db", "master", 8000}); err != nil { + if err := store.EnsureService(17, "foo", &structs.NodeService{"db", "db", []string{"master"}, 8000}); err != nil { t.Fatalf("err: %v") } - if err := store.EnsureService(18, "foo", &structs.NodeService{"db2", "db", "slave", 8001}); err != nil { + if err := store.EnsureService(18, "foo", &structs.NodeService{"db2", "db", []string{"slave"}, 8001}); err != nil { t.Fatalf("err: %v") } - if err := store.EnsureService(19, "bar", &structs.NodeService{"db", "db", "slave", 8000}); err != nil { + if err := store.EnsureService(19, "bar", &structs.NodeService{"db", "db", []string{"slave"}, 8000}); err != nil { t.Fatalf("err: %v") } @@ -504,7 +504,7 @@ func TestServiceTagNodes(t *testing.T) { if nodes[0].Address != "127.0.0.1" { t.Fatalf("bad: %v", nodes) } - if nodes[0].ServiceTag != "master" { + if !strContains(nodes[0].ServiceTags, "master") { t.Fatalf("bad: %v", nodes) } if nodes[0].ServicePort != 8000 { @@ -527,15 +527,15 @@ func TestStoreSnapshot(t *testing.T) { t.Fatalf("err: %v") } - if err := store.EnsureService(10, "foo", &structs.NodeService{"db", "db", "master", 8000}); err != nil { + if err := store.EnsureService(10, "foo", &structs.NodeService{"db", "db", []string{"master"}, 8000}); err != nil { t.Fatalf("err: %v") } - if err := store.EnsureService(11, "foo", &structs.NodeService{"db2", "db", "slave", 8001}); err != nil { + if err := store.EnsureService(11, "foo", &structs.NodeService{"db2", "db", []string{"slave"}, 8001}); err != nil { t.Fatalf("err: %v") } - if err := store.EnsureService(12, "bar", &structs.NodeService{"db", "db", "slave", 8000}); err != nil { + if err := store.EnsureService(12, "bar", &structs.NodeService{"db", "db", []string{"slave"}, 8000}); err != nil { t.Fatalf("err: %v") } @@ -580,15 +580,15 @@ func TestStoreSnapshot(t *testing.T) { // Ensure we get the service entries services := snap.NodeServices("foo") - if services.Services["db"].Tag != "master" { + if !strContains(services.Services["db"].Tags, "master") { t.Fatalf("bad: %v", services) } - if services.Services["db2"].Tag != "slave" { + if !strContains(services.Services["db2"].Tags, "slave") { t.Fatalf("bad: %v", services) } services = snap.NodeServices("bar") - if services.Services["db"].Tag != "slave" { + if !strContains(services.Services["db"].Tags, "slave") { t.Fatalf("bad: %v", services) } @@ -624,10 +624,10 @@ func TestStoreSnapshot(t *testing.T) { } // Make some changes! - if err := store.EnsureService(14, "foo", &structs.NodeService{"db", "db", "slave", 8000}); err != nil { + if err := store.EnsureService(14, "foo", &structs.NodeService{"db", "db", []string{"slave"}, 8000}); err != nil { t.Fatalf("err: %v", err) } - if err := store.EnsureService(15, "bar", &structs.NodeService{"db", "db", "master", 8000}); err != nil { + if err := store.EnsureService(15, "bar", &structs.NodeService{"db", "db", []string{"master"}, 8000}); err != nil { t.Fatalf("err: %v", err) } if err := store.EnsureNode(16, structs.Node{"baz", "127.0.0.3"}); err != nil { @@ -656,15 +656,15 @@ func TestStoreSnapshot(t *testing.T) { // Ensure old service entries services = snap.NodeServices("foo") - if services.Services["db"].Tag != "master" { + if !strContains(services.Services["db"].Tags, "master") { t.Fatalf("bad: %v", services) } - if services.Services["db2"].Tag != "slave" { + if !strContains(services.Services["db2"].Tags, "slave") { t.Fatalf("bad: %v", services) } services = snap.NodeServices("bar") - if services.Services["db"].Tag != "slave" { + if !strContains(services.Services["db"].Tags, "slave") { t.Fatalf("bad: %v", services) } @@ -709,7 +709,7 @@ func TestEnsureCheck(t *testing.T) { if err := store.EnsureNode(1, structs.Node{"foo", "127.0.0.1"}); err != nil { t.Fatalf("err: %v", err) } - if err := store.EnsureService(2, "foo", &structs.NodeService{"db1", "db", "master", 8000}); err != nil { + if err := store.EnsureService(2, "foo", &structs.NodeService{"db1", "db", []string{"master"}, 8000}); err != nil { t.Fatalf("err: %v") } check := &structs.HealthCheck{ @@ -791,7 +791,7 @@ func TestDeleteNodeCheck(t *testing.T) { if err := store.EnsureNode(1, structs.Node{"foo", "127.0.0.1"}); err != nil { t.Fatalf("err: %v", err) } - if err := store.EnsureService(2, "foo", &structs.NodeService{"db1", "db", "master", 8000}); err != nil { + if err := store.EnsureService(2, "foo", &structs.NodeService{"db1", "db", []string{"master"}, 8000}); err != nil { t.Fatalf("err: %v") } check := &structs.HealthCheck{ @@ -841,7 +841,7 @@ func TestCheckServiceNodes(t *testing.T) { if err := store.EnsureNode(1, structs.Node{"foo", "127.0.0.1"}); err != nil { t.Fatalf("err: %v", err) } - if err := store.EnsureService(2, "foo", &structs.NodeService{"db1", "db", "master", 8000}); err != nil { + if err := store.EnsureService(2, "foo", &structs.NodeService{"db1", "db", []string{"master"}, 8000}); err != nil { t.Fatalf("err: %v") } check := &structs.HealthCheck{ @@ -922,7 +922,7 @@ func BenchmarkCheckServiceNodes(t *testing.B) { if err := store.EnsureNode(1, structs.Node{"foo", "127.0.0.1"}); err != nil { t.Fatalf("err: %v", err) } - if err := store.EnsureService(2, "foo", &structs.NodeService{"db1", "db", "master", 8000}); err != nil { + if err := store.EnsureService(2, "foo", &structs.NodeService{"db1", "db", []string{"master"}, 8000}); err != nil { t.Fatalf("err: %v") } check := &structs.HealthCheck{ @@ -964,7 +964,7 @@ func TestSS_Register_Deregister_Query(t *testing.T) { srv := &structs.NodeService{ "statsite-box-stats", "statsite-box-stats", - "", + nil, 0} if err := store.EnsureService(2, "foo", srv); err != nil { t.Fatalf("err: %v") @@ -973,7 +973,7 @@ func TestSS_Register_Deregister_Query(t *testing.T) { srv = &structs.NodeService{ "statsite-share-stats", "statsite-share-stats", - "", + nil, 0} if err := store.EnsureService(3, "foo", srv); err != nil { t.Fatalf("err: %v")