Robin Thellend | 18205cf | 2014-10-21 13:53:59 -0700 | [diff] [blame] | 1 | package main |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "io/ioutil" |
| 6 | "os" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 10 | "veyron.io/veyron/veyron2" |
| 11 | "veyron.io/veyron/veyron2/ipc" |
| 12 | "veyron.io/veyron/veyron2/naming" |
| 13 | "veyron.io/veyron/veyron2/rt" |
| 14 | "veyron.io/veyron/veyron2/security" |
| 15 | "veyron.io/veyron/veyron2/services/mgmt/application" |
| 16 | "veyron.io/veyron/veyron2/vlog" |
Cosmos Nicolaou | d6c3c9c | 2014-09-30 15:42:53 -0700 | [diff] [blame] | 17 | |
| 18 | "veyron.io/veyron/veyron/profiles" |
| 19 | "veyron.io/veyron/veyron/services/mgmt/repository" |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | var ( |
| 23 | envelope = application.Envelope{ |
Bogdan Caprita | 55c1012 | 2014-07-09 15:35:07 -0700 | [diff] [blame] | 24 | Title: "fifa world cup", |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 25 | Args: []string{"arg1", "arg2", "arg3"}, |
| 26 | Binary: "/path/to/binary", |
| 27 | Env: []string{"env1", "env2", "env3"}, |
| 28 | } |
| 29 | jsonEnv = `{ |
Bogdan Caprita | 55c1012 | 2014-07-09 15:35:07 -0700 | [diff] [blame] | 30 | "Title": "fifa world cup", |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 31 | "Args": [ |
| 32 | "arg1", |
| 33 | "arg2", |
| 34 | "arg3" |
| 35 | ], |
| 36 | "Binary": "/path/to/binary", |
| 37 | "Env": [ |
| 38 | "env1", |
| 39 | "env2", |
| 40 | "env3" |
| 41 | ] |
| 42 | }` |
| 43 | ) |
| 44 | |
| 45 | type server struct { |
| 46 | suffix string |
| 47 | } |
| 48 | |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 49 | func (s *server) Match(_ ipc.ServerContext, profiles []string) (application.Envelope, error) { |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 50 | vlog.VI(2).Infof("%v.Match(%v) was called", s.suffix, profiles) |
| 51 | return envelope, nil |
| 52 | } |
| 53 | |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 54 | func (s *server) Put(_ ipc.ServerContext, profiles []string, env application.Envelope) error { |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 55 | vlog.VI(2).Infof("%v.Put(%v, %v) was called", s.suffix, profiles, env) |
| 56 | return nil |
| 57 | } |
| 58 | |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 59 | func (s *server) Remove(_ ipc.ServerContext, profile string) error { |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 60 | vlog.VI(2).Infof("%v.Remove(%v) was called", s.suffix, profile) |
| 61 | return nil |
| 62 | } |
| 63 | |
| 64 | type dispatcher struct { |
| 65 | } |
| 66 | |
| 67 | func NewDispatcher() *dispatcher { |
| 68 | return &dispatcher{} |
| 69 | } |
| 70 | |
Cosmos Nicolaou | 1ee5e1a | 2014-11-02 10:20:30 -0800 | [diff] [blame] | 71 | func (d *dispatcher) Lookup(suffix, method string) (interface{}, security.Authorizer, error) { |
Jiri Simsa | ddbfebb | 2014-06-20 15:56:06 -0700 | [diff] [blame] | 72 | invoker := ipc.ReflectInvoker(repository.NewServerApplication(&server{suffix: suffix})) |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 73 | return invoker, nil, nil |
| 74 | } |
| 75 | |
| 76 | func startServer(t *testing.T, r veyron2.Runtime) (ipc.Server, naming.Endpoint, error) { |
| 77 | dispatcher := NewDispatcher() |
| 78 | server, err := r.NewServer() |
| 79 | if err != nil { |
| 80 | t.Errorf("NewServer failed: %v", err) |
| 81 | return nil, nil, err |
| 82 | } |
Cosmos Nicolaou | f8d4c2b | 2014-10-23 22:36:38 -0700 | [diff] [blame] | 83 | endpoint, err := server.Listen(profiles.LocalListenSpec) |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 84 | if err != nil { |
| 85 | t.Errorf("Listen failed: %v", err) |
| 86 | return nil, nil, err |
| 87 | } |
Cosmos Nicolaou | 92dba58 | 2014-11-05 17:24:10 -0800 | [diff] [blame] | 88 | if err := server.ServeDispatcher("", dispatcher); err != nil { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 89 | t.Errorf("Serve failed: %v", err) |
| 90 | return nil, nil, err |
| 91 | } |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 92 | return server, endpoint, nil |
| 93 | } |
| 94 | |
| 95 | func stopServer(t *testing.T, server ipc.Server) { |
| 96 | if err := server.Stop(); err != nil { |
| 97 | t.Errorf("server.Stop failed: %v", err) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | func TestApplicationClient(t *testing.T) { |
| 102 | runtime := rt.Init() |
| 103 | server, endpoint, err := startServer(t, runtime) |
| 104 | if err != nil { |
| 105 | return |
| 106 | } |
| 107 | defer stopServer(t, server) |
| 108 | // Setup the command-line. |
Robin Thellend | 18205cf | 2014-10-21 13:53:59 -0700 | [diff] [blame] | 109 | cmd := root() |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 110 | var stdout, stderr bytes.Buffer |
| 111 | cmd.Init(nil, &stdout, &stderr) |
Cosmos Nicolaou | ef43dc4 | 2014-06-13 14:38:51 -0700 | [diff] [blame] | 112 | appName := naming.JoinAddressName(endpoint.String(), "//myapp/1") |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 113 | profile := "myprofile" |
| 114 | |
| 115 | // Test the 'Match' command. |
| 116 | if err := cmd.Execute([]string{"match", appName, profile}); err != nil { |
| 117 | t.Fatalf("%v", err) |
| 118 | } |
| 119 | if expected, got := jsonEnv, strings.TrimSpace(stdout.String()); got != expected { |
| 120 | t.Errorf("Unexpected output from match. Got %q, expected %q", got, expected) |
| 121 | } |
| 122 | stdout.Reset() |
| 123 | |
| 124 | // Test the 'put' command. |
| 125 | f, err := ioutil.TempFile("", "test") |
| 126 | if err != nil { |
| 127 | t.Fatalf("%v", err) |
| 128 | } |
| 129 | fileName := f.Name() |
| 130 | defer os.Remove(fileName) |
| 131 | if _, err = f.Write([]byte(jsonEnv)); err != nil { |
| 132 | t.Fatalf("%v", err) |
| 133 | } |
| 134 | if err = f.Close(); err != nil { |
| 135 | t.Fatalf("%v", err) |
| 136 | } |
| 137 | if err := cmd.Execute([]string{"put", appName, profile, fileName}); err != nil { |
| 138 | t.Fatalf("%v", err) |
| 139 | } |
Jiri Simsa | 1201b6c | 2014-08-19 13:26:31 -0700 | [diff] [blame] | 140 | if expected, got := "Application envelope added successfully.", strings.TrimSpace(stdout.String()); got != expected { |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 141 | t.Errorf("Unexpected output from put. Got %q, expected %q", got, expected) |
| 142 | } |
| 143 | stdout.Reset() |
| 144 | |
| 145 | // Test the 'remove' command. |
| 146 | if err := cmd.Execute([]string{"remove", appName, profile}); err != nil { |
| 147 | t.Fatalf("%v", err) |
| 148 | } |
| 149 | if expected, got := "Application envelope removed successfully.", strings.TrimSpace(stdout.String()); got != expected { |
| 150 | t.Errorf("Unexpected output from remove. Got %q, expected %q", got, expected) |
| 151 | } |
| 152 | stdout.Reset() |
| 153 | |
| 154 | // Test the 'edit' command. (nothing changed) |
| 155 | os.Setenv("EDITOR", "true") |
| 156 | if err := cmd.Execute([]string{"edit", appName, profile}); err != nil { |
| 157 | t.Fatalf("%v", err) |
| 158 | } |
| 159 | if expected, got := "Nothing changed", strings.TrimSpace(stdout.String()); got != expected { |
| 160 | t.Errorf("Unexpected output from edit. Got %q, expected %q", got, expected) |
| 161 | } |
| 162 | stdout.Reset() |
| 163 | |
| 164 | // Test the 'edit' command. |
Jiri Simsa | fb3e24d | 2014-05-23 10:02:13 -0700 | [diff] [blame] | 165 | os.Setenv("EDITOR", "perl -pi -e 's/arg1/arg111/'") |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 166 | if err := cmd.Execute([]string{"edit", appName, profile}); err != nil { |
| 167 | t.Fatalf("%v", err) |
| 168 | } |
| 169 | if expected, got := "Application envelope updated successfully.", strings.TrimSpace(stdout.String()); got != expected { |
| 170 | t.Errorf("Unexpected output from edit. Got %q, expected %q", got, expected) |
| 171 | } |
| 172 | stdout.Reset() |
| 173 | } |