Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1 | package impl_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "io/ioutil" |
| 7 | "os" |
| 8 | "path" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
Robin Thellend | 308aae0 | 2014-05-29 12:55:56 -0700 | [diff] [blame] | 12 | "veyron/tools/content/impl" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 13 | |
| 14 | "veyron2" |
| 15 | "veyron2/ipc" |
| 16 | "veyron2/naming" |
| 17 | "veyron2/rt" |
| 18 | "veyron2/security" |
Jiri Simsa | ddbfebb | 2014-06-20 15:56:06 -0700 | [diff] [blame^] | 19 | "veyron2/services/mgmt/repository" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 20 | "veyron2/vlog" |
| 21 | ) |
| 22 | |
| 23 | type server struct { |
| 24 | suffix string |
| 25 | } |
| 26 | |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 27 | func (s *server) Delete(ipc.ServerContext) error { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 28 | vlog.VI(2).Infof("Delete() was called. suffix=%v", s.suffix) |
| 29 | if s.suffix != "exists" { |
| 30 | return fmt.Errorf("content doesn't exist: %v", s.suffix) |
| 31 | } |
| 32 | return nil |
| 33 | } |
| 34 | |
Jiri Simsa | ddbfebb | 2014-06-20 15:56:06 -0700 | [diff] [blame^] | 35 | func (s *server) Download(_ ipc.ServerContext, stream repository.ContentServiceDownloadStream) error { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 36 | vlog.VI(2).Infof("Download() was called. suffix=%v", s.suffix) |
| 37 | stream.Send([]byte("Hello")) |
| 38 | stream.Send([]byte("World")) |
| 39 | return nil |
| 40 | } |
| 41 | |
Jiri Simsa | ddbfebb | 2014-06-20 15:56:06 -0700 | [diff] [blame^] | 42 | func (s *server) Upload(_ ipc.ServerContext, stream repository.ContentServiceUploadStream) (string, error) { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 43 | vlog.VI(2).Infof("Upload() was called. suffix=%v", s.suffix) |
| 44 | for { |
| 45 | if _, err := stream.Recv(); err != nil { |
| 46 | break |
| 47 | } |
| 48 | } |
| 49 | return "newcontentid", nil |
| 50 | } |
| 51 | |
| 52 | type dispatcher struct { |
| 53 | } |
| 54 | |
| 55 | func NewDispatcher() *dispatcher { |
| 56 | return &dispatcher{} |
| 57 | } |
| 58 | |
| 59 | func (d *dispatcher) Lookup(suffix string) (ipc.Invoker, security.Authorizer, error) { |
Jiri Simsa | ddbfebb | 2014-06-20 15:56:06 -0700 | [diff] [blame^] | 60 | invoker := ipc.ReflectInvoker(repository.NewServerContent(&server{suffix: suffix})) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 61 | return invoker, nil, nil |
| 62 | } |
| 63 | |
| 64 | func startServer(t *testing.T, r veyron2.Runtime) (ipc.Server, naming.Endpoint, error) { |
| 65 | dispatcher := NewDispatcher() |
| 66 | server, err := r.NewServer() |
| 67 | if err != nil { |
| 68 | t.Errorf("NewServer failed: %v", err) |
| 69 | return nil, nil, err |
| 70 | } |
| 71 | if err := server.Register("", dispatcher); err != nil { |
| 72 | t.Errorf("Register failed: %v", err) |
| 73 | return nil, nil, err |
| 74 | } |
| 75 | endpoint, err := server.Listen("tcp", "localhost:0") |
| 76 | if err != nil { |
| 77 | t.Errorf("Listen failed: %v", err) |
| 78 | return nil, nil, err |
| 79 | } |
| 80 | return server, endpoint, nil |
| 81 | } |
| 82 | |
| 83 | func stopServer(t *testing.T, server ipc.Server) { |
| 84 | if err := server.Stop(); err != nil { |
| 85 | t.Errorf("server.Stop failed: %v", err) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func TestContentClient(t *testing.T) { |
| 90 | runtime := rt.Init() |
| 91 | server, endpoint, err := startServer(t, runtime) |
| 92 | if err != nil { |
| 93 | return |
| 94 | } |
| 95 | defer stopServer(t, server) |
| 96 | // Setup the command-line. |
| 97 | cmd := impl.Root() |
| 98 | var stdout, stderr bytes.Buffer |
| 99 | cmd.Init(nil, &stdout, &stderr) |
| 100 | |
| 101 | // Test the 'delete' command. |
Cosmos Nicolaou | 6933540 | 2014-05-20 14:41:58 -0700 | [diff] [blame] | 102 | if err := cmd.Execute([]string{"delete", naming.JoinAddressName(endpoint.String(), "//exists")}); err != nil { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 103 | t.Fatalf("%v", err) |
| 104 | } |
| 105 | if expected, got := "Content deleted successfully", strings.TrimSpace(stdout.String()); got != expected { |
| 106 | t.Errorf("Got %q, expected %q", got, expected) |
| 107 | } |
| 108 | stdout.Reset() |
| 109 | |
| 110 | // Test the 'download' command. |
| 111 | dir, err := ioutil.TempDir("", "contentimpltest") |
| 112 | if err != nil { |
| 113 | t.Fatalf("%v", err) |
| 114 | } |
| 115 | defer os.Remove(dir) |
| 116 | file := path.Join(dir, "testfile") |
| 117 | defer os.Remove(file) |
Cosmos Nicolaou | 6933540 | 2014-05-20 14:41:58 -0700 | [diff] [blame] | 118 | if err := cmd.Execute([]string{"download", naming.JoinAddressName(endpoint.String(), "//exists"), file}); err != nil { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 119 | t.Fatalf("%v", err) |
| 120 | } |
| 121 | if expected, got := "Content downloaded to file "+file, strings.TrimSpace(stdout.String()); got != expected { |
| 122 | t.Errorf("Got %q, expected %q", got, expected) |
| 123 | } |
| 124 | buf, err := ioutil.ReadFile(file) |
| 125 | if err != nil { |
| 126 | t.Fatalf("%v", err) |
| 127 | } |
| 128 | if expected := "HelloWorld"; string(buf) != expected { |
| 129 | t.Errorf("Got %q, expected %q", string(buf), expected) |
| 130 | } |
| 131 | stdout.Reset() |
| 132 | |
| 133 | // Test the 'upload' command. |
| 134 | if err := cmd.Execute([]string{"upload", naming.JoinAddressName(endpoint.String(), ""), file}); err != nil { |
| 135 | t.Fatalf("%v", err) |
| 136 | } |
| 137 | if expected, got := "newcontentid", strings.TrimSpace(stdout.String()); got != expected { |
| 138 | t.Errorf("Got %q, expected %q", got, expected) |
| 139 | } |
| 140 | } |