api: improve reliability of catalog endpoint tests

pull/586/head
Ryan Uber 2015-01-07 18:29:42 -08:00
parent cd4f17a410
commit cb1b722813
1 changed files with 146 additions and 90 deletions

View File

@ -1,7 +1,10 @@
package api package api
import ( import (
"fmt"
"testing" "testing"
"github.com/hashicorp/consul/testutil"
) )
func TestCatalog_Datacenters(t *testing.T) { func TestCatalog_Datacenters(t *testing.T) {
@ -10,14 +13,20 @@ func TestCatalog_Datacenters(t *testing.T) {
catalog := c.Catalog() catalog := c.Catalog()
datacenters, err := catalog.Datacenters() testutil.WaitForResult(func() (bool, error) {
if err != nil { datacenters, err := catalog.Datacenters()
t.Fatalf("err: %v", err) if err != nil {
} return false, err
}
if len(datacenters) == 0 { if len(datacenters) == 0 {
t.Fatalf("Bad: %v", datacenters) return false, fmt.Errorf("Bad: %v", datacenters)
} }
return true, nil
}, func(err error) {
t.Fatalf("err: %s", err)
})
} }
func TestCatalog_Nodes(t *testing.T) { func TestCatalog_Nodes(t *testing.T) {
@ -26,18 +35,24 @@ func TestCatalog_Nodes(t *testing.T) {
catalog := c.Catalog() catalog := c.Catalog()
nodes, meta, err := catalog.Nodes(nil) testutil.WaitForResult(func() (bool, error) {
if err != nil { nodes, meta, err := catalog.Nodes(nil)
t.Fatalf("err: %v", err) if err != nil {
} return false, err
}
if meta.LastIndex == 0 { if meta.LastIndex == 0 {
t.Fatalf("Bad: %v", meta) return false, fmt.Errorf("Bad: %v", meta)
} }
if len(nodes) == 0 { if len(nodes) == 0 {
t.Fatalf("Bad: %v", nodes) return false, fmt.Errorf("Bad: %v", nodes)
} }
return true, nil
}, func(err error) {
t.Fatalf("err: %s", err)
})
} }
func TestCatalog_Services(t *testing.T) { func TestCatalog_Services(t *testing.T) {
@ -46,18 +61,24 @@ func TestCatalog_Services(t *testing.T) {
catalog := c.Catalog() catalog := c.Catalog()
services, meta, err := catalog.Services(nil) testutil.WaitForResult(func() (bool, error) {
if err != nil { services, meta, err := catalog.Services(nil)
t.Fatalf("err: %v", err) if err != nil {
} return false, err
}
if meta.LastIndex == 0 { if meta.LastIndex == 0 {
t.Fatalf("Bad: %v", meta) return false, fmt.Errorf("Bad: %v", meta)
} }
if len(services) == 0 { if len(services) == 0 {
t.Fatalf("Bad: %v", services) return false, fmt.Errorf("Bad: %v", services)
} }
return true, nil
}, func(err error) {
t.Fatalf("err: %s", err)
})
} }
func TestCatalog_Service(t *testing.T) { func TestCatalog_Service(t *testing.T) {
@ -66,18 +87,24 @@ func TestCatalog_Service(t *testing.T) {
catalog := c.Catalog() catalog := c.Catalog()
services, meta, err := catalog.Service("consul", "", nil) testutil.WaitForResult(func() (bool, error) {
if err != nil { services, meta, err := catalog.Service("consul", "", nil)
t.Fatalf("err: %v", err) if err != nil {
} return false, err
}
if meta.LastIndex == 0 { if meta.LastIndex == 0 {
t.Fatalf("Bad: %v", meta) return false, fmt.Errorf("Bad: %v", meta)
} }
if len(services) == 0 { if len(services) == 0 {
t.Fatalf("Bad: %v", services) return false, fmt.Errorf("Bad: %v", services)
} }
return true, nil
}, func(err error) {
t.Fatalf("err: %s", err)
})
} }
func TestCatalog_Node(t *testing.T) { func TestCatalog_Node(t *testing.T) {
@ -85,19 +112,25 @@ func TestCatalog_Node(t *testing.T) {
defer s.stop() defer s.stop()
catalog := c.Catalog() catalog := c.Catalog()
name, _ := c.Agent().NodeName() name, _ := c.Agent().NodeName()
info, meta, err := catalog.Node(name, nil)
if err != nil {
t.Fatalf("err: %v", err)
}
if meta.LastIndex == 0 { testutil.WaitForResult(func() (bool, error) {
t.Fatalf("Bad: %v", meta) info, meta, err := catalog.Node(name, nil)
} if err != nil {
if len(info.Services) == 0 { return false, err
t.Fatalf("Bad: %v", info) }
}
if meta.LastIndex == 0 {
return false, fmt.Errorf("Bad: %v", meta)
}
if len(info.Services) == 0 {
return false, fmt.Errorf("Bad: %v", info)
}
return true, nil
}, func(err error) {
t.Fatalf("err: %s", err)
})
} }
func TestCatalog_Registration(t *testing.T) { func TestCatalog_Registration(t *testing.T) {
@ -130,32 +163,35 @@ func TestCatalog_Registration(t *testing.T) {
Check: check, Check: check,
} }
_, err := catalog.Register(reg, nil) testutil.WaitForResult(func() (bool, error) {
if _, err := catalog.Register(reg, nil); err != nil {
return false, err
}
if err != nil { node, _, err := catalog.Node("foobar", nil)
t.Fatalf("err: %v", err) if err != nil {
} return false, err
}
node, _, err := catalog.Node("foobar", nil) if _, ok := node.Services["redis1"]; !ok {
return false, fmt.Errorf("missing service: redis1")
}
if err != nil { health, _, err := c.Health().Node("foobar", nil)
t.Fatalf("err: %v", err) if err != nil {
} return false, err
}
if _, ok := node.Services["redis1"]; !ok { if health[0].CheckID != "service:redis1" {
t.Fatalf("missing service: redis1") return false, fmt.Errorf("missing checkid service:redis1")
} }
health, _, err := c.Health().Node("foobar", nil) return true, nil
}, func(err error) {
if err != nil { t.Fatalf("err: %s", err)
t.Fatalf("err: %v", err) })
}
if health[0].CheckID != "service:redis1" {
t.Fatalf("missing checkid service:redis1")
}
// Test catalog deregistration of the previously registered service
dereg := &CatalogDeregistration{ dereg := &CatalogDeregistration{
Datacenter: "dc1", Datacenter: "dc1",
Node: "foobar", Node: "foobar",
@ -167,15 +203,22 @@ func TestCatalog_Registration(t *testing.T) {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
node, _, err = catalog.Node("foobar", nil) testutil.WaitForResult(func() (bool, error) {
if err != nil { node, _, err := catalog.Node("foobar", nil)
t.Fatalf("err: %v", err) if err != nil {
} return false, err
}
if _, ok := node.Services["redis1"]; ok { if _, ok := node.Services["redis1"]; ok {
t.Fatalf("ServiceID:redis1 is not deregistered") return false, fmt.Errorf("ServiceID:redis1 is not deregistered")
} }
return true, nil
}, func(err error) {
t.Fatalf("err: %s", err)
})
// Test deregistration of the previously registered check
dereg = &CatalogDeregistration{ dereg = &CatalogDeregistration{
Datacenter: "dc1", Datacenter: "dc1",
Node: "foobar", Node: "foobar",
@ -187,31 +230,44 @@ func TestCatalog_Registration(t *testing.T) {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
health, _, err = c.Health().Node("foobar", nil) testutil.WaitForResult(func() (bool, error) {
if err != nil { health, _, err := c.Health().Node("foobar", nil)
t.Fatalf("err: %v", err) if err != nil {
} return false, err
}
if len(health) != 0 { if len(health) != 0 {
t.Fatalf("CheckID:service:redis1 is not deregistered") return false, fmt.Errorf("CheckID:service:redis1 is not deregistered")
} }
return true, nil
}, func(err error) {
t.Fatalf("err: %s", err)
})
// Test node deregistration of the previously registered node
dereg = &CatalogDeregistration{ dereg = &CatalogDeregistration{
Datacenter: "dc1", Datacenter: "dc1",
Node: "foobar", Node: "foobar",
Address: "192.168.10.10", Address: "192.168.10.10",
} }
if _, err = catalog.Deregister(dereg, nil); err != nil { if _, err := catalog.Deregister(dereg, nil); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
node, _, err = catalog.Node("foobar", nil) testutil.WaitForResult(func() (bool, error) {
if err != nil { node, _, err := catalog.Node("foobar", nil)
t.Fatalf("err: %v", err) if err != nil {
} return false, err
}
if node != nil { if node != nil {
t.Fatalf("node is not deregistered: %v", node) return false, fmt.Errorf("node is not deregistered: %v", node)
} }
return true, nil
}, func(err error) {
t.Fatalf("err: %s", err)
})
} }