blob: 5d37e21bb856c8ed1349e338a852ca347c1d68d4 [file] [log] [blame]
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001package mounttable
2
3import (
4 "fmt"
David Why Use Two When One Will Do Presotto55ed4302014-07-30 14:50:45 -07005 "os"
Jiri Simsa5293dcb2014-05-10 09:56:38 -07006 "reflect"
7 "testing"
8 "time"
9
Jiri Simsa519c5072014-09-17 21:37:57 -070010 "veyron.io/veyron/veyron2/naming"
David Why Use Two When One Will Do Presotto9c374c22014-10-21 16:16:30 -070011 "veyron.io/veyron/veyron2/options"
Jiri Simsa519c5072014-09-17 21:37:57 -070012 "veyron.io/veyron/veyron2/vlog"
Cosmos Nicolaoud6c3c9c2014-09-30 15:42:53 -070013
Asim Shankarc920db32014-10-16 19:18:21 -070014 "veyron.io/veyron/veyron/lib/testutil"
Cosmos Nicolaoud6c3c9c2014-09-30 15:42:53 -070015 "veyron.io/veyron/veyron/profiles"
Jiri Simsa5293dcb2014-05-10 09:56:38 -070016)
17
Asim Shankarc920db32014-10-16 19:18:21 -070018func init() { testutil.Init() }
19
Jiri Simsa5293dcb2014-05-10 09:56:38 -070020func protocolAndAddress(e naming.Endpoint) (string, string, error) {
21 addr := e.Addr()
22 if addr == nil {
23 return "", "", fmt.Errorf("failed to get address")
24 }
25 return addr.Network(), addr.String(), nil
26}
27
28func TestNeighborhood(t *testing.T) {
Jiri Simsa5293dcb2014-05-10 09:56:38 -070029 vlog.Infof("TestNeighborhood")
Asim Shankar88292912014-10-09 19:41:07 -070030 server, err := rootRT.NewServer()
Jiri Simsa5293dcb2014-05-10 09:56:38 -070031 if err != nil {
32 boom(t, "r.NewServer: %s", err)
33 }
Cosmos Nicolaoufdc838b2014-06-30 21:44:27 -070034 defer server.Stop()
Jiri Simsa5293dcb2014-05-10 09:56:38 -070035
36 // Start serving on a loopback address.
Cosmos Nicolaou28dabfc2014-12-15 22:51:07 -080037 eps, err := server.Listen(profiles.LocalListenSpec)
Jiri Simsa5293dcb2014-05-10 09:56:38 -070038 if err != nil {
39 boom(t, "Failed to Listen mount table: %s", err)
40 }
Cosmos Nicolaou28dabfc2014-12-15 22:51:07 -080041 estr := eps[0].String()
Robin Thellend06a99de2014-06-25 14:31:21 -070042 addresses := []string{
43 naming.JoinAddressName(estr, ""),
44 naming.JoinAddressName(estr, "suffix1"),
45 naming.JoinAddressName(estr, "suffix2"),
46 }
David Why Use Two When One Will Do Presotto55ed4302014-07-30 14:50:45 -070047
48 // Create a name for the server.
49 serverName := fmt.Sprintf("nhtest%d", os.Getpid())
50
Jiri Simsa5293dcb2014-05-10 09:56:38 -070051 // Add neighborhood server.
Matt Rosencrantzac32b6c2014-12-01 15:49:18 -080052 nhd, err := NewLoopbackNeighborhoodServer(rootRT, serverName, addresses...)
Jiri Simsa5293dcb2014-05-10 09:56:38 -070053 if err != nil {
Robin Thellend06a99de2014-06-25 14:31:21 -070054 boom(t, "Failed to create neighborhood server: %s\n", err)
Jiri Simsa5293dcb2014-05-10 09:56:38 -070055 }
56 defer nhd.Stop()
Cosmos Nicolaou92dba582014-11-05 17:24:10 -080057 if err := server.ServeDispatcher("", nhd); err != nil {
Jiri Simsa5293dcb2014-05-10 09:56:38 -070058 boom(t, "Failed to register neighborhood server: %s", err)
59 }
60
61 // Wait for the mounttable to appear in mdns
62L:
63 for tries := 1; tries < 2; tries++ {
David Why Use Two When One Will Do Presotto9c374c22014-10-21 16:16:30 -070064 names := doGlob(t, estr, "", "*", rootRT)
Jiri Simsa5293dcb2014-05-10 09:56:38 -070065 t.Logf("names %v", names)
66 for _, n := range names {
David Why Use Two When One Will Do Presotto55ed4302014-07-30 14:50:45 -070067 if n == serverName {
Jiri Simsa5293dcb2014-05-10 09:56:38 -070068 break L
69 }
70 }
71 time.Sleep(1 * time.Second)
72 }
73
David Why Use Two When One Will Do Presotto55ed4302014-07-30 14:50:45 -070074 // Make sure we get back a root for the server.
David Why Use Two When One Will Do Presotto9c374c22014-10-21 16:16:30 -070075 want, got := []string{""}, doGlob(t, estr, serverName, "", rootRT)
Jiri Simsa5293dcb2014-05-10 09:56:38 -070076 if !reflect.DeepEqual(want, got) {
77 t.Errorf("Unexpected Glob result want: %q, got: %q", want, got)
78 }
79
80 // Make sure we can resolve through the neighborhood.
81 expectedSuffix := "a/b"
David Why Use Two When One Will Do Presotto9c374c22014-10-21 16:16:30 -070082 ctx := rootRT.NewContext()
83 client := rootRT.Client()
84 name := naming.JoinAddressName(estr, serverName+"/"+expectedSuffix)
85 call, cerr := client.StartCall(ctx, name, "ResolveStepX", nil, options.NoResolve(true))
86 if cerr != nil {
87 boom(t, "ResolveStepX.StartCall: %s", cerr)
Jiri Simsa5293dcb2014-05-10 09:56:38 -070088 }
Todd Wang1aa57692014-11-11 13:53:29 -080089 var entry naming.VDLMountEntry
David Why Use Two When One Will Do Presotto9c374c22014-10-21 16:16:30 -070090 if cerr = call.Finish(&entry, &err); cerr != nil {
91 boom(t, "ResolveStepX: %s", cerr)
Jiri Simsa5293dcb2014-05-10 09:56:38 -070092 }
93
94 // Resolution returned something. Make sure its correct.
David Why Use Two When One Will Do Presotto6f9f5742014-10-20 16:27:05 -070095 if entry.Name != expectedSuffix {
96 boom(t, "resolveStep suffix: expected %s, got %s", expectedSuffix, entry.Name)
Jiri Simsa5293dcb2014-05-10 09:56:38 -070097 }
David Why Use Two When One Will Do Presotto6f9f5742014-10-20 16:27:05 -070098 if len(entry.Servers) == 0 {
Jiri Simsa5293dcb2014-05-10 09:56:38 -070099 boom(t, "resolveStep returns no severs")
100 }
Robin Thellend06a99de2014-06-25 14:31:21 -0700101L2:
David Why Use Two When One Will Do Presotto6f9f5742014-10-20 16:27:05 -0700102 for _, s := range entry.Servers {
Robin Thellend06a99de2014-06-25 14:31:21 -0700103 for _, a := range addresses {
104 if a == s.Server {
105 continue L2
106 }
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700107 }
Robin Thellend06a99de2014-06-25 14:31:21 -0700108 boom(t, "Unexpected address from resolveStep result: %v", s.Server)
109 }
110L3:
111 for _, a := range addresses {
David Why Use Two When One Will Do Presotto6f9f5742014-10-20 16:27:05 -0700112 for _, s := range entry.Servers {
Robin Thellend06a99de2014-06-25 14:31:21 -0700113 if a == s.Server {
114 continue L3
115 }
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700116 }
Robin Thellend06a99de2014-06-25 14:31:21 -0700117 boom(t, "Missing address from resolveStep result: %v", a)
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700118 }
119}