Robin Thellend | 18205cf | 2014-10-21 13:53:59 -0700 | [diff] [blame] | 1 | package main |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 8 | "v.io/core/veyron2" |
| 9 | "v.io/core/veyron2/ipc" |
| 10 | "v.io/core/veyron2/naming" |
| 11 | "v.io/core/veyron2/rt" |
| 12 | "v.io/core/veyron2/security" |
| 13 | "v.io/core/veyron2/services/mounttable" |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 14 | "v.io/core/veyron2/services/security/access" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 15 | "v.io/core/veyron2/vlog" |
Cosmos Nicolaou | d6c3c9c | 2014-09-30 15:42:53 -0700 | [diff] [blame] | 16 | |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 17 | "v.io/core/veyron/profiles" |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 18 | ) |
| 19 | |
| 20 | type server struct { |
| 21 | suffix string |
| 22 | } |
| 23 | |
Robin Thellend | 39ac323 | 2014-12-02 09:50:41 -0800 | [diff] [blame] | 24 | func (s *server) Glob__(ctx ipc.ServerContext, pattern string) (<-chan naming.VDLMountEntry, error) { |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 25 | vlog.VI(2).Infof("Glob() was called. suffix=%v pattern=%q", s.suffix, pattern) |
Robin Thellend | 39ac323 | 2014-12-02 09:50:41 -0800 | [diff] [blame] | 26 | ch := make(chan naming.VDLMountEntry, 2) |
| 27 | ch <- naming.VDLMountEntry{"name1", []naming.VDLMountedServer{{"server1", 123}}, false} |
| 28 | ch <- naming.VDLMountEntry{"name2", []naming.VDLMountedServer{{"server2", 456}, {"server3", 789}}, false} |
| 29 | close(ch) |
| 30 | return ch, nil |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Todd Wang | 1aa5769 | 2014-11-11 13:53:29 -0800 | [diff] [blame] | 33 | func (s *server) Mount(_ ipc.ServerContext, server string, ttl uint32, flags naming.MountFlag) error { |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 34 | vlog.VI(2).Infof("Mount() was called. suffix=%v server=%q ttl=%d", s.suffix, server, ttl) |
| 35 | return nil |
| 36 | } |
| 37 | |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 38 | func (s *server) Unmount(_ ipc.ServerContext, server string) error { |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 39 | vlog.VI(2).Infof("Unmount() was called. suffix=%v server=%q", s.suffix, server) |
| 40 | return nil |
| 41 | } |
| 42 | |
Todd Wang | 1aa5769 | 2014-11-11 13:53:29 -0800 | [diff] [blame] | 43 | func (s *server) ResolveStep(ipc.ServerContext) (servers []naming.VDLMountedServer, suffix string, err error) { |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 44 | vlog.VI(2).Infof("ResolveStep() was called. suffix=%v", s.suffix) |
Todd Wang | 1aa5769 | 2014-11-11 13:53:29 -0800 | [diff] [blame] | 45 | servers = []naming.VDLMountedServer{{"server1", 123}} |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 46 | suffix = s.suffix |
| 47 | return |
| 48 | } |
| 49 | |
Todd Wang | 1aa5769 | 2014-11-11 13:53:29 -0800 | [diff] [blame] | 50 | func (s *server) ResolveStepX(ipc.ServerContext) (entry naming.VDLMountEntry, err error) { |
David Why Use Two When One Will Do Presotto | 6f9f574 | 2014-10-20 16:27:05 -0700 | [diff] [blame] | 51 | vlog.VI(2).Infof("ResolveStepX() was called. suffix=%v", s.suffix) |
Todd Wang | 1aa5769 | 2014-11-11 13:53:29 -0800 | [diff] [blame] | 52 | entry.Servers = []naming.VDLMountedServer{{"server1", 123}} |
David Why Use Two When One Will Do Presotto | 6f9f574 | 2014-10-20 16:27:05 -0700 | [diff] [blame] | 53 | entry.Name = s.suffix |
| 54 | return |
| 55 | } |
| 56 | |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 57 | func (s *server) Delete(ipc.ServerContext, bool) error { |
| 58 | vlog.VI(2).Infof("Delete() was called. suffix=%v", s.suffix) |
| 59 | return nil |
| 60 | } |
| 61 | func (s *server) SetACL(ipc.ServerContext, access.TaggedACLMap, string) error { |
| 62 | vlog.VI(2).Infof("SetACL() was called. suffix=%v", s.suffix) |
| 63 | return nil |
| 64 | } |
| 65 | |
| 66 | func (s *server) GetACL(ipc.ServerContext) (access.TaggedACLMap, string, error) { |
| 67 | vlog.VI(2).Infof("GetACL() was called. suffix=%v", s.suffix) |
| 68 | return nil, "", nil |
| 69 | } |
| 70 | |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 71 | type dispatcher struct { |
| 72 | } |
| 73 | |
Robin Thellend | a02fe8f | 2014-11-19 09:58:29 -0800 | [diff] [blame] | 74 | func (d *dispatcher) Lookup(suffix string) (interface{}, security.Authorizer, error) { |
Cosmos Nicolaou | 710daa2 | 2014-11-11 19:39:18 -0800 | [diff] [blame] | 75 | return mounttable.MountTableServer(&server{suffix: suffix}), nil, nil |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | func startServer(t *testing.T, r veyron2.Runtime) (ipc.Server, naming.Endpoint, error) { |
| 79 | dispatcher := new(dispatcher) |
| 80 | server, err := r.NewServer() |
| 81 | if err != nil { |
| 82 | t.Errorf("NewServer failed: %v", err) |
| 83 | return nil, nil, err |
| 84 | } |
Cosmos Nicolaou | 28dabfc | 2014-12-15 22:51:07 -0800 | [diff] [blame] | 85 | endpoints, err := server.Listen(profiles.LocalListenSpec) |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 86 | if err != nil { |
| 87 | t.Errorf("Listen failed: %v", err) |
| 88 | return nil, nil, err |
| 89 | } |
Cosmos Nicolaou | 92dba58 | 2014-11-05 17:24:10 -0800 | [diff] [blame] | 90 | if err := server.ServeDispatcher("", dispatcher); err != nil { |
| 91 | t.Errorf("ServeDispatcher failed: %v", err) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 92 | return nil, nil, err |
| 93 | } |
Cosmos Nicolaou | 28dabfc | 2014-12-15 22:51:07 -0800 | [diff] [blame] | 94 | return server, endpoints[0], nil |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | func stopServer(t *testing.T, server ipc.Server) { |
| 98 | if err := server.Stop(); err != nil { |
| 99 | t.Errorf("server.Stop failed: %v", err) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func TestMountTableClient(t *testing.T) { |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 104 | var err error |
| 105 | runtime, err = rt.New() |
| 106 | if err != nil { |
| 107 | t.Fatalf("Unexpected error initializing runtime: %s", err) |
| 108 | } |
| 109 | defer runtime.Cleanup() |
| 110 | |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 111 | server, endpoint, err := startServer(t, runtime) |
| 112 | if err != nil { |
| 113 | return |
| 114 | } |
| 115 | defer stopServer(t, server) |
| 116 | // Setup the command-line. |
Robin Thellend | 18205cf | 2014-10-21 13:53:59 -0700 | [diff] [blame] | 117 | cmd := root() |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 118 | var stdout, stderr bytes.Buffer |
| 119 | cmd.Init(nil, &stdout, &stderr) |
| 120 | |
| 121 | // Test the 'glob' command. |
Cosmos Nicolaou | 6933540 | 2014-05-20 14:41:58 -0700 | [diff] [blame] | 122 | if err := cmd.Execute([]string{"glob", naming.JoinAddressName(endpoint.String(), ""), "*"}); err != nil { |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 123 | t.Fatalf("%v", err) |
| 124 | } |
| 125 | if expected, got := "name1 server1 (TTL 2m3s)\nname2 server2 (TTL 7m36s) server3 (TTL 13m9s)", strings.TrimSpace(stdout.String()); got != expected { |
| 126 | t.Errorf("Got %q, expected %q", got, expected) |
| 127 | } |
| 128 | stdout.Reset() |
| 129 | |
| 130 | // Test the 'mount' command. |
Cosmos Nicolaou | 6933540 | 2014-05-20 14:41:58 -0700 | [diff] [blame] | 131 | if err := cmd.Execute([]string{"mount", naming.JoinAddressName(endpoint.String(), ""), "server", "123s"}); err != nil { |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 132 | t.Fatalf("%v", err) |
| 133 | } |
| 134 | if expected, got := "Name mounted successfully.", strings.TrimSpace(stdout.String()); got != expected { |
| 135 | t.Errorf("Got %q, expected %q", got, expected) |
| 136 | } |
| 137 | stdout.Reset() |
| 138 | |
| 139 | // Test the 'unmount' command. |
Cosmos Nicolaou | 6933540 | 2014-05-20 14:41:58 -0700 | [diff] [blame] | 140 | if err := cmd.Execute([]string{"unmount", naming.JoinAddressName(endpoint.String(), ""), "server"}); err != nil { |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 141 | t.Fatalf("%v", err) |
| 142 | } |
| 143 | if expected, got := "Name unmounted successfully.", strings.TrimSpace(stdout.String()); got != expected { |
| 144 | t.Errorf("Got %q, expected %q", got, expected) |
| 145 | } |
| 146 | stdout.Reset() |
| 147 | |
| 148 | // Test the 'resolvestep' command. |
David Why Use Two When One Will Do Presotto | 8b4dbbf | 2014-11-06 10:50:14 -0800 | [diff] [blame] | 149 | vlog.Infof("resovestep %s", naming.JoinAddressName(endpoint.String(), "name")) |
| 150 | if err := cmd.Execute([]string{"resolvestep", naming.JoinAddressName(endpoint.String(), "name")}); err != nil { |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 151 | t.Fatalf("%v", err) |
| 152 | } |
David Why Use Two When One Will Do Presotto | 6f9f574 | 2014-10-20 16:27:05 -0700 | [diff] [blame] | 153 | if expected, got := `Servers: [{server1 123}] Suffix: "name" MT: false`, strings.TrimSpace(stdout.String()); got != expected { |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 154 | t.Errorf("Got %q, expected %q", got, expected) |
| 155 | } |
| 156 | stdout.Reset() |
| 157 | } |