Tweaks the WAN address translation unit tests.

pull/2275/head
James Phillips 2016-08-15 15:34:11 -07:00
parent c0ff412650
commit 55e83c9e1c
No known key found for this signature in database
GPG Key ID: 77183E682AC5FC11
4 changed files with 218 additions and 125 deletions

View File

@ -146,13 +146,41 @@ func TestCatalogNodes(t *testing.T) {
} }
func TestCatalogNodes_WanTranslation(t *testing.T) { func TestCatalogNodes_WanTranslation(t *testing.T) {
httpCtx1, httpCtx2 := setupWanHTTPServers(t) dir1, srv1 := makeHTTPServerWithConfig(t,
defer shutdownHTTPServer(httpCtx1) func(c *Config) {
defer shutdownHTTPServer(httpCtx2) c.Datacenter = "dc1"
srv1 := httpCtx1.srv c.TranslateWanAddrs = true
srv2 := httpCtx2.srv })
defer os.RemoveAll(dir1)
defer srv1.Shutdown()
defer srv1.agent.Shutdown()
testutil.WaitForLeader(t, srv1.agent.RPC, "dc1")
// Register a node with DC2 dir2, srv2 := makeHTTPServerWithConfig(t,
func(c *Config) {
c.Datacenter = "dc2"
c.TranslateWanAddrs = true
})
defer os.RemoveAll(dir2)
defer srv2.Shutdown()
defer srv2.agent.Shutdown()
testutil.WaitForLeader(t, srv2.agent.RPC, "dc2")
// Wait for the WAN join.
addr := fmt.Sprintf("127.0.0.1:%d",
srv1.agent.config.Ports.SerfWan)
if _, err := srv2.agent.JoinWAN([]string{addr}); err != nil {
t.Fatalf("err: %v", err)
}
testutil.WaitForResult(
func() (bool, error) {
return len(srv1.agent.WANMembers()) > 1, nil
},
func(err error) {
t.Fatalf("Failed waiting for WAN join: %v", err)
})
// Register a node with DC2.
{ {
args := &structs.RegisterRequest{ args := &structs.RegisterRequest{
Datacenter: "dc2", Datacenter: "dc2",
@ -172,65 +200,54 @@ func TestCatalogNodes_WanTranslation(t *testing.T) {
} }
} }
// Query nodes in DC2 from DC1.
req, err := http.NewRequest("GET", "/v1/catalog/nodes?dc=dc2", nil) req, err := http.NewRequest("GET", "/v1/catalog/nodes?dc=dc2", nil)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
// get nodes for DC2 from DC1
resp1 := httptest.NewRecorder() resp1 := httptest.NewRecorder()
obj1, err1 := srv1.CatalogNodes(resp1, req) obj1, err1 := srv1.CatalogNodes(resp1, req)
if err1 != nil { if err1 != nil {
t.Fatalf("err: %v", err1) t.Fatalf("err: %v", err1)
} }
// Verify an index is set
assertIndex(t, resp1) assertIndex(t, resp1)
// Expect that DC1 gives us a WAN address (since the node is in DC2).
nodes1 := obj1.(structs.Nodes) nodes1 := obj1.(structs.Nodes)
if len(nodes1) != 2 { if len(nodes1) != 2 {
t.Fatalf("bad: %v", obj1) t.Fatalf("bad: %v", obj1)
} }
var address string
var node1 *structs.Node
for _, node := range nodes1 { for _, node := range nodes1 {
if node.Node == "wan_translation_test" { if node.Node == "wan_translation_test" {
node1 = node address = node.Address
} }
} }
if address != "127.0.0.2" {
// Expect that DC1 gives us a public address (since the node is in DC2) t.Fatalf("bad: %s", address)
if node1.Address != "127.0.0.2" {
t.Fatalf("bad: %v", node1)
} }
// get nodes for DC2 from DC2 // Query DC2 from DC2.
resp2 := httptest.NewRecorder() resp2 := httptest.NewRecorder()
obj2, err2 := srv2.CatalogNodes(resp2, req) obj2, err2 := srv2.CatalogNodes(resp2, req)
if err2 != nil { if err2 != nil {
t.Fatalf("err: %v", err2) t.Fatalf("err: %v", err2)
} }
// Verify an index is set
assertIndex(t, resp2) assertIndex(t, resp2)
// Expect that DC2 gives us a private address (since the node is in DC2).
nodes2 := obj2.(structs.Nodes) nodes2 := obj2.(structs.Nodes)
if len(nodes2) != 2 { if len(nodes2) != 2 {
t.Fatalf("bad: %v", obj2) t.Fatalf("bad: %v", obj2)
} }
var node2 *structs.Node
for _, node := range nodes2 { for _, node := range nodes2 {
if node.Node == "wan_translation_test" { if node.Node == "wan_translation_test" {
node2 = node address = node.Address
} }
} }
if address != "127.0.0.1" {
// Expect that DC2 gives us a private address (since the node is in DC2) t.Fatalf("bad: %s", address)
if node2.Address != "127.0.0.1" {
t.Fatalf("bad: %v", node2)
} }
} }
@ -497,13 +514,41 @@ func TestCatalogServiceNodes(t *testing.T) {
} }
func TestCatalogServiceNodes_WanTranslation(t *testing.T) { func TestCatalogServiceNodes_WanTranslation(t *testing.T) {
httpCtx1, httpCtx2 := setupWanHTTPServers(t) dir1, srv1 := makeHTTPServerWithConfig(t,
defer shutdownHTTPServer(httpCtx1) func(c *Config) {
defer shutdownHTTPServer(httpCtx2) c.Datacenter = "dc1"
srv1 := httpCtx1.srv c.TranslateWanAddrs = true
srv2 := httpCtx2.srv })
defer os.RemoveAll(dir1)
defer srv1.Shutdown()
defer srv1.agent.Shutdown()
testutil.WaitForLeader(t, srv1.agent.RPC, "dc1")
// Register a node with DC2 dir2, srv2 := makeHTTPServerWithConfig(t,
func(c *Config) {
c.Datacenter = "dc2"
c.TranslateWanAddrs = true
})
defer os.RemoveAll(dir2)
defer srv2.Shutdown()
defer srv2.agent.Shutdown()
testutil.WaitForLeader(t, srv2.agent.RPC, "dc2")
// Wait for the WAN join.
addr := fmt.Sprintf("127.0.0.1:%d",
srv1.agent.config.Ports.SerfWan)
if _, err := srv2.agent.JoinWAN([]string{addr}); err != nil {
t.Fatalf("err: %v", err)
}
testutil.WaitForResult(
func() (bool, error) {
return len(srv1.agent.WANMembers()) > 1, nil
},
func(err error) {
t.Fatalf("Failed waiting for WAN join: %v", err)
})
// Register a node with DC2.
{ {
args := &structs.RegisterRequest{ args := &structs.RegisterRequest{
Datacenter: "dc2", Datacenter: "dc2",
@ -523,49 +568,43 @@ func TestCatalogServiceNodes_WanTranslation(t *testing.T) {
} }
} }
// Query for the node in DC2 from DC1.
req, err := http.NewRequest("GET", "/v1/catalog/service/http_wan_translation_test?dc=dc2", nil) req, err := http.NewRequest("GET", "/v1/catalog/service/http_wan_translation_test?dc=dc2", nil)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
// Ask HTTP server on DC1 for the node
resp1 := httptest.NewRecorder() resp1 := httptest.NewRecorder()
obj1, err1 := srv1.CatalogServiceNodes(resp1, req) obj1, err1 := srv1.CatalogServiceNodes(resp1, req)
if err1 != nil { if err1 != nil {
t.Fatalf("err: %v", err1) t.Fatalf("err: %v", err1)
} }
assertIndex(t, resp1) assertIndex(t, resp1)
// Expect that DC1 gives us a WAN address (since the node is in DC2).
nodes1 := obj1.(structs.ServiceNodes) nodes1 := obj1.(structs.ServiceNodes)
if len(nodes1) != 1 { if len(nodes1) != 1 {
t.Fatalf("bad: %v", obj1) t.Fatalf("bad: %v", obj1)
} }
node1 := nodes1[0] node1 := nodes1[0]
// Expect that DC1 gives us a public address (since the node is in DC2)
if node1.Address != "127.0.0.2" { if node1.Address != "127.0.0.2" {
t.Fatalf("bad: %v", node1) t.Fatalf("bad: %v", node1)
} }
// Ask HTTP server on DC2 for the node // Query DC2 from DC2.
resp2 := httptest.NewRecorder() resp2 := httptest.NewRecorder()
obj2, err2 := srv2.CatalogServiceNodes(resp2, req) obj2, err2 := srv2.CatalogServiceNodes(resp2, req)
if err2 != nil { if err2 != nil {
t.Fatalf("err: %v", err2) t.Fatalf("err: %v", err2)
} }
assertIndex(t, resp2) assertIndex(t, resp2)
// Expect that DC2 gives us a local address (since the node is in DC2).
nodes2 := obj2.(structs.ServiceNodes) nodes2 := obj2.(structs.ServiceNodes)
if len(nodes2) != 1 { if len(nodes2) != 1 {
t.Fatalf("bad: %v", obj2) t.Fatalf("bad: %v", obj2)
} }
node2 := nodes2[0] node2 := nodes2[0]
// Expect that DC2 gives us a local address (since the node is in DC2)
if node2.Address != "127.0.0.1" { if node2.Address != "127.0.0.1" {
t.Fatalf("bad: %v", node2) t.Fatalf("bad: %v", node2)
} }
@ -716,13 +755,41 @@ func TestCatalogNodeServices(t *testing.T) {
} }
func TestCatalogNodeServices_WanTranslation(t *testing.T) { func TestCatalogNodeServices_WanTranslation(t *testing.T) {
httpCtx1, httpCtx2 := setupWanHTTPServers(t) dir1, srv1 := makeHTTPServerWithConfig(t,
defer shutdownHTTPServer(httpCtx1) func(c *Config) {
defer shutdownHTTPServer(httpCtx2) c.Datacenter = "dc1"
srv1 := httpCtx1.srv c.TranslateWanAddrs = true
srv2 := httpCtx2.srv })
defer os.RemoveAll(dir1)
defer srv1.Shutdown()
defer srv1.agent.Shutdown()
testutil.WaitForLeader(t, srv1.agent.RPC, "dc1")
// Register a node with DC2 dir2, srv2 := makeHTTPServerWithConfig(t,
func(c *Config) {
c.Datacenter = "dc2"
c.TranslateWanAddrs = true
})
defer os.RemoveAll(dir2)
defer srv2.Shutdown()
defer srv2.agent.Shutdown()
testutil.WaitForLeader(t, srv2.agent.RPC, "dc2")
// Wait for the WAN join.
addr := fmt.Sprintf("127.0.0.1:%d",
srv1.agent.config.Ports.SerfWan)
if _, err := srv2.agent.JoinWAN([]string{addr}); err != nil {
t.Fatalf("err: %v", err)
}
testutil.WaitForResult(
func() (bool, error) {
return len(srv1.agent.WANMembers()) > 1, nil
},
func(err error) {
t.Fatalf("Failed waiting for WAN join: %v", err)
})
// Register a node with DC2.
{ {
args := &structs.RegisterRequest{ args := &structs.RegisterRequest{
Datacenter: "dc2", Datacenter: "dc2",
@ -742,12 +809,11 @@ func TestCatalogNodeServices_WanTranslation(t *testing.T) {
} }
} }
// Query for the node in DC2 from DC1.
req, err := http.NewRequest("GET", "/v1/catalog/node/foo?dc=dc2", nil) req, err := http.NewRequest("GET", "/v1/catalog/node/foo?dc=dc2", nil)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
// ask DC1 for node in DC2
resp1 := httptest.NewRecorder() resp1 := httptest.NewRecorder()
obj1, err1 := srv1.CatalogNodeServices(resp1, req) obj1, err1 := srv1.CatalogNodeServices(resp1, req)
if err1 != nil { if err1 != nil {
@ -755,19 +821,17 @@ func TestCatalogNodeServices_WanTranslation(t *testing.T) {
} }
assertIndex(t, resp1) assertIndex(t, resp1)
// Expect that DC1 gives us a WAN address (since the node is in DC2).
services1 := obj1.(*structs.NodeServices) services1 := obj1.(*structs.NodeServices)
if len(services1.Services) != 1 { if len(services1.Services) != 1 {
t.Fatalf("bad: %v", obj1) t.Fatalf("bad: %v", obj1)
} }
service1 := services1.Node service1 := services1.Node
// Expect that DC1 gives us a public address (since the node is in DC2)
if service1.Address != "127.0.0.2" { if service1.Address != "127.0.0.2" {
t.Fatalf("bad: %v", service1) t.Fatalf("bad: %v", service1)
} }
// ask DC2 for node in DC2 // Query DC2 from DC2.
resp2 := httptest.NewRecorder() resp2 := httptest.NewRecorder()
obj2, err2 := srv2.CatalogNodeServices(resp2, req) obj2, err2 := srv2.CatalogNodeServices(resp2, req)
if err2 != nil { if err2 != nil {
@ -775,14 +839,12 @@ func TestCatalogNodeServices_WanTranslation(t *testing.T) {
} }
assertIndex(t, resp2) assertIndex(t, resp2)
// Expect that DC2 gives us a private address (since the node is in DC2).
services2 := obj2.(*structs.NodeServices) services2 := obj2.(*structs.NodeServices)
if len(services2.Services) != 1 { if len(services2.Services) != 1 {
t.Fatalf("bad: %v", obj2) t.Fatalf("bad: %v", obj2)
} }
service2 := services2.Node service2 := services2.Node
// Expect that DC2 gives us a private address (since the node is in DC2)
if service2.Address != "127.0.0.1" { if service2.Address != "127.0.0.1" {
t.Fatalf("bad: %v", service2) t.Fatalf("bad: %v", service2)
} }

View File

@ -555,13 +555,41 @@ func TestHealthServiceNodes_PassingFilter(t *testing.T) {
} }
func TestHealthServiceNodes_WanTranslation(t *testing.T) { func TestHealthServiceNodes_WanTranslation(t *testing.T) {
httpCtx1, httpCtx2 := setupWanHTTPServers(t) dir1, srv1 := makeHTTPServerWithConfig(t,
defer shutdownHTTPServer(httpCtx1) func(c *Config) {
defer shutdownHTTPServer(httpCtx2) c.Datacenter = "dc1"
srv1 := httpCtx1.srv c.TranslateWanAddrs = true
srv2 := httpCtx2.srv })
defer os.RemoveAll(dir1)
defer srv1.Shutdown()
defer srv1.agent.Shutdown()
testutil.WaitForLeader(t, srv1.agent.RPC, "dc1")
// Register a node with DC2 dir2, srv2 := makeHTTPServerWithConfig(t,
func(c *Config) {
c.Datacenter = "dc2"
c.TranslateWanAddrs = true
})
defer os.RemoveAll(dir2)
defer srv2.Shutdown()
defer srv2.agent.Shutdown()
testutil.WaitForLeader(t, srv2.agent.RPC, "dc2")
// Wait for the WAN join.
addr := fmt.Sprintf("127.0.0.1:%d",
srv1.agent.config.Ports.SerfWan)
if _, err := srv2.agent.JoinWAN([]string{addr}); err != nil {
t.Fatalf("err: %v", err)
}
testutil.WaitForResult(
func() (bool, error) {
return len(srv1.agent.WANMembers()) > 1, nil
},
func(err error) {
t.Fatalf("Failed waiting for WAN join: %v", err)
})
// Register a node with DC2.
{ {
args := &structs.RegisterRequest{ args := &structs.RegisterRequest{
Datacenter: "dc2", Datacenter: "dc2",
@ -581,51 +609,42 @@ func TestHealthServiceNodes_WanTranslation(t *testing.T) {
} }
} }
// Query for a service in DC2 from DC1.
req, err := http.NewRequest("GET", "/v1/health/service/http_wan_translation_test?dc=dc2", nil) req, err := http.NewRequest("GET", "/v1/health/service/http_wan_translation_test?dc=dc2", nil)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
// ask DC1 for node in DC2
resp1 := httptest.NewRecorder() resp1 := httptest.NewRecorder()
obj1, err1 := srv1.HealthServiceNodes(resp1, req) obj1, err1 := srv1.HealthServiceNodes(resp1, req)
if err1 != nil { if err1 != nil {
t.Fatalf("err: %v", err1) t.Fatalf("err: %v", err1)
} }
assertIndex(t, resp1) assertIndex(t, resp1)
// Should be 1 health check for consul // Expect that DC1 gives us a WAN address (since the node is in DC2).
nodes1 := obj1.(structs.CheckServiceNodes) nodes1 := obj1.(structs.CheckServiceNodes)
if len(nodes1) != 1 { if len(nodes1) != 1 {
t.Fatalf("bad: %v", obj1) t.Fatalf("bad: %v", obj1)
} }
node1 := nodes1[0].Node node1 := nodes1[0].Node
// Expect that DC1 gives us a public address (since the node is in DC2)
if node1.Address != "127.0.0.2" { if node1.Address != "127.0.0.2" {
t.Fatalf("bad: %v", node1) t.Fatalf("bad: %v", node1)
} }
// ask DC2 for node in DC2 // Query DC2 from DC2.
resp2 := httptest.NewRecorder() resp2 := httptest.NewRecorder()
obj2, err2 := srv2.HealthServiceNodes(resp2, req) obj2, err2 := srv2.HealthServiceNodes(resp2, req)
if err2 != nil { if err2 != nil {
t.Fatalf("err: %v", err2) t.Fatalf("err: %v", err2)
} }
assertIndex(t, resp2) assertIndex(t, resp2)
// Should be 1 health check for consul // Expect that DC2 gives us a private address (since the node is in DC2).
nodes2 := obj2.(structs.CheckServiceNodes) nodes2 := obj2.(structs.CheckServiceNodes)
if len(nodes2) != 1 { if len(nodes2) != 1 {
t.Fatalf("bad: %v", obj2) t.Fatalf("bad: %v", obj2)
} }
node2 := nodes2[0].Node node2 := nodes2[0].Node
// Expect that DC2 gives us a private address (since the node is in DC2)
if node2.Address != "127.0.0.1" { if node2.Address != "127.0.0.1" {
t.Fatalf("bad: %v", node2) t.Fatalf("bad: %v", node2)
} }

View File

@ -23,50 +23,6 @@ import (
"github.com/hashicorp/go-cleanhttp" "github.com/hashicorp/go-cleanhttp"
) )
type HTTPServerCtx struct {
dir string
srv *HTTPServer
}
func setupWanHTTPServers(t *testing.T) (HTTPServerCtx, HTTPServerCtx) {
dir1, srv1 := makeHTTPServerWithConfig(t,
func(c *Config) {
c.Datacenter = "dc1"
c.TranslateWanAddrs = true
})
dir2, srv2 := makeHTTPServerWithConfig(t,
func(c *Config) {
c.Datacenter = "dc2"
c.TranslateWanAddrs = true
})
testutil.WaitForLeader(t, srv1.agent.RPC, "dc1")
testutil.WaitForLeader(t, srv2.agent.RPC, "dc2")
// Join WAN cluster
addr := fmt.Sprintf("127.0.0.1:%d",
srv1.agent.config.Ports.SerfWan)
if _, err := srv2.agent.JoinWAN([]string{addr}); err != nil {
t.Fatalf("err: %v", err)
}
testutil.WaitForResult(
func() (bool, error) {
return len(srv1.agent.WANMembers()) > 1, nil
},
func(err error) {
t.Fatalf("Failed waiting for WAN join: %v", err)
})
return HTTPServerCtx{dir1, srv1}, HTTPServerCtx{dir2, srv2}
}
func shutdownHTTPServer(httpCtx HTTPServerCtx) {
os.RemoveAll(httpCtx.dir)
httpCtx.srv.Shutdown()
httpCtx.srv.agent.Shutdown()
}
func makeHTTPServer(t *testing.T) (string, *HTTPServer) { func makeHTTPServer(t *testing.T) (string, *HTTPServer) {
return makeHTTPServerWithConfig(t, nil) return makeHTTPServerWithConfig(t, nil)
} }

View File

@ -359,7 +359,7 @@ func TestPreparedQuery_Execute(t *testing.T) {
} }
}) })
// testing WAN translation in the response // Ensure WAN translation occurs for a response outside of the local DC.
httpTestWithConfig(t, func(srv *HTTPServer) { httpTestWithConfig(t, func(srv *HTTPServer) {
m := MockPreparedQuery{} m := MockPreparedQuery{}
if err := srv.agent.InjectEndpoint("PreparedQuery", &m); err != nil { if err := srv.agent.InjectEndpoint("PreparedQuery", &m); err != nil {
@ -368,9 +368,14 @@ func TestPreparedQuery_Execute(t *testing.T) {
m.executeFn = func(args *structs.PreparedQueryExecuteRequest, reply *structs.PreparedQueryExecuteResponse) error { m.executeFn = func(args *structs.PreparedQueryExecuteRequest, reply *structs.PreparedQueryExecuteResponse) error {
nodesResponse := make(structs.CheckServiceNodes, 1) nodesResponse := make(structs.CheckServiceNodes, 1)
nodesResponse[0].Node = &structs.Node{Node: "foo", Address: "127.0.0.1", nodesResponse[0].Node = &structs.Node{
TaggedAddresses: map[string]string{"wan": "127.0.0.2"}} Node: "foo", Address: "127.0.0.1",
TaggedAddresses: map[string]string{
"wan": "127.0.0.2",
},
}
reply.Nodes = nodesResponse reply.Nodes = nodesResponse
reply.Datacenter = "dc2"
return nil return nil
} }
@ -405,6 +410,57 @@ func TestPreparedQuery_Execute(t *testing.T) {
c.TranslateWanAddrs = true c.TranslateWanAddrs = true
}) })
// Ensure WAN translation doesn't occur for the local DC.
httpTestWithConfig(t, func(srv *HTTPServer) {
m := MockPreparedQuery{}
if err := srv.agent.InjectEndpoint("PreparedQuery", &m); err != nil {
t.Fatalf("err: %v", err)
}
m.executeFn = func(args *structs.PreparedQueryExecuteRequest, reply *structs.PreparedQueryExecuteResponse) error {
nodesResponse := make(structs.CheckServiceNodes, 1)
nodesResponse[0].Node = &structs.Node{
Node: "foo", Address: "127.0.0.1",
TaggedAddresses: map[string]string{
"wan": "127.0.0.2",
},
}
reply.Nodes = nodesResponse
reply.Datacenter = "dc1"
return nil
}
body := bytes.NewBuffer(nil)
req, err := http.NewRequest("GET", "/v1/query/my-id/execute?dc=dc2", body)
if err != nil {
t.Fatalf("err: %v", err)
}
resp := httptest.NewRecorder()
obj, err := srv.PreparedQuerySpecific(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
if resp.Code != 200 {
t.Fatalf("bad code: %d", resp.Code)
}
r, ok := obj.(structs.PreparedQueryExecuteResponse)
if !ok {
t.Fatalf("unexpected: %T", obj)
}
if r.Nodes == nil || len(r.Nodes) != 1 {
t.Fatalf("bad: %v", r)
}
node := r.Nodes[0]
if node.Node.Address != "127.0.0.1" {
t.Fatalf("bad: %v", node.Node)
}
}, func(c *Config) {
c.Datacenter = "dc1"
c.TranslateWanAddrs = true
})
httpTest(t, func(srv *HTTPServer) { httpTest(t, func(srv *HTTPServer) {
body := bytes.NewBuffer(nil) body := bytes.NewBuffer(nil)
req, err := http.NewRequest("GET", "/v1/query/not-there/execute", body) req, err := http.NewRequest("GET", "/v1/query/not-there/execute", body)