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 | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 10 | "v.io/core/veyron2" |
Matt Rosencrantz | f541b77 | 2015-01-13 07:58:59 -0800 | [diff] [blame] | 11 | "v.io/core/veyron2/context" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 12 | "v.io/core/veyron2/ipc" |
| 13 | "v.io/core/veyron2/naming" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 14 | "v.io/core/veyron2/security" |
| 15 | "v.io/core/veyron2/services/mgmt/application" |
Robert Kroeger | d6e1d1a | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 16 | "v.io/core/veyron2/services/security/access" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 17 | "v.io/core/veyron2/vlog" |
Cosmos Nicolaou | d6c3c9c | 2014-09-30 15:42:53 -0700 | [diff] [blame] | 18 | |
Ankur | 0003fdc | 2015-01-22 10:59:41 -0800 | [diff] [blame] | 19 | tsecurity "v.io/core/veyron/lib/testutil/security" |
Suharsh Sivakumar | d68949c | 2015-01-26 10:32:23 -0800 | [diff] [blame] | 20 | _ "v.io/core/veyron/profiles" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 21 | "v.io/core/veyron/services/mgmt/repository" |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | var ( |
| 25 | envelope = application.Envelope{ |
Bogdan Caprita | 55c1012 | 2014-07-09 15:35:07 -0700 | [diff] [blame] | 26 | Title: "fifa world cup", |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 27 | Args: []string{"arg1", "arg2", "arg3"}, |
| 28 | Binary: "/path/to/binary", |
| 29 | Env: []string{"env1", "env2", "env3"}, |
Robin Thellend | 64178ed | 2014-11-20 13:16:22 -0800 | [diff] [blame] | 30 | Packages: map[string]string{ |
| 31 | "pkg1": "/path/to/package1", |
| 32 | }, |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 33 | } |
| 34 | jsonEnv = `{ |
Bogdan Caprita | 55c1012 | 2014-07-09 15:35:07 -0700 | [diff] [blame] | 35 | "Title": "fifa world cup", |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 36 | "Args": [ |
| 37 | "arg1", |
| 38 | "arg2", |
| 39 | "arg3" |
| 40 | ], |
| 41 | "Binary": "/path/to/binary", |
| 42 | "Env": [ |
| 43 | "env1", |
| 44 | "env2", |
| 45 | "env3" |
Robin Thellend | 64178ed | 2014-11-20 13:16:22 -0800 | [diff] [blame] | 46 | ], |
| 47 | "Packages": { |
| 48 | "pkg1": "/path/to/package1" |
| 49 | } |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 50 | }` |
| 51 | ) |
| 52 | |
| 53 | type server struct { |
| 54 | suffix string |
| 55 | } |
| 56 | |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 57 | func (s *server) Match(_ ipc.ServerContext, profiles []string) (application.Envelope, error) { |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 58 | vlog.VI(2).Infof("%v.Match(%v) was called", s.suffix, profiles) |
| 59 | return envelope, nil |
| 60 | } |
| 61 | |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 62 | func (s *server) Put(_ ipc.ServerContext, profiles []string, env application.Envelope) error { |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 63 | vlog.VI(2).Infof("%v.Put(%v, %v) was called", s.suffix, profiles, env) |
| 64 | return nil |
| 65 | } |
| 66 | |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 67 | func (s *server) Remove(_ ipc.ServerContext, profile string) error { |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 68 | vlog.VI(2).Infof("%v.Remove(%v) was called", s.suffix, profile) |
| 69 | return nil |
| 70 | } |
| 71 | |
Robert Kroeger | d6e1d1a | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 72 | func (s *server) SetACL(_ ipc.ServerContext, acl access.TaggedACLMap, etag string) error { |
| 73 | vlog.VI(2).Infof("%v.SetACL(%v, %v) was called", acl, etag) |
| 74 | return nil |
| 75 | } |
| 76 | |
| 77 | func (s *server) GetACL(ipc.ServerContext) (access.TaggedACLMap, string, error) { |
| 78 | vlog.VI(2).Infof("%v.GetACL() was called") |
| 79 | return nil, "", nil |
| 80 | } |
| 81 | |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 82 | type dispatcher struct { |
| 83 | } |
| 84 | |
| 85 | func NewDispatcher() *dispatcher { |
| 86 | return &dispatcher{} |
| 87 | } |
| 88 | |
Robin Thellend | a02fe8f | 2014-11-19 09:58:29 -0800 | [diff] [blame] | 89 | func (d *dispatcher) Lookup(suffix string) (interface{}, security.Authorizer, error) { |
Cosmos Nicolaou | 710daa2 | 2014-11-11 19:39:18 -0800 | [diff] [blame] | 90 | return repository.ApplicationServer(&server{suffix: suffix}), nil, nil |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Matt Rosencrantz | f541b77 | 2015-01-13 07:58:59 -0800 | [diff] [blame] | 93 | func startServer(t *testing.T, ctx *context.T) (ipc.Server, naming.Endpoint, error) { |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 94 | dispatcher := NewDispatcher() |
Matt Rosencrantz | f541b77 | 2015-01-13 07:58:59 -0800 | [diff] [blame] | 95 | server, err := veyron2.NewServer(ctx) |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 96 | if err != nil { |
| 97 | t.Errorf("NewServer failed: %v", err) |
| 98 | return nil, nil, err |
| 99 | } |
Suharsh Sivakumar | d68949c | 2015-01-26 10:32:23 -0800 | [diff] [blame] | 100 | endpoints, err := server.Listen(veyron2.GetListenSpec(ctx)) |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 101 | if err != nil { |
| 102 | t.Errorf("Listen failed: %v", err) |
| 103 | return nil, nil, err |
| 104 | } |
Cosmos Nicolaou | 92dba58 | 2014-11-05 17:24:10 -0800 | [diff] [blame] | 105 | if err := server.ServeDispatcher("", dispatcher); err != nil { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 106 | t.Errorf("Serve failed: %v", err) |
| 107 | return nil, nil, err |
| 108 | } |
Cosmos Nicolaou | 28dabfc | 2014-12-15 22:51:07 -0800 | [diff] [blame] | 109 | return server, endpoints[0], nil |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | func stopServer(t *testing.T, server ipc.Server) { |
| 113 | if err := server.Stop(); err != nil { |
| 114 | t.Errorf("server.Stop failed: %v", err) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func TestApplicationClient(t *testing.T) { |
Matt Rosencrantz | a5ad272 | 2015-01-22 11:17:47 -0800 | [diff] [blame] | 119 | var shutdown veyron2.Shutdown |
| 120 | gctx, shutdown = veyron2.Init() |
| 121 | defer shutdown() |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 122 | var err error |
Matt Rosencrantz | a5ad272 | 2015-01-22 11:17:47 -0800 | [diff] [blame] | 123 | if gctx, err = veyron2.SetPrincipal(gctx, tsecurity.NewPrincipal("test-blessing")); err != nil { |
| 124 | panic(err) |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 125 | } |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 126 | |
Matt Rosencrantz | a5ad272 | 2015-01-22 11:17:47 -0800 | [diff] [blame] | 127 | server, endpoint, err := startServer(t, gctx) |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 128 | if err != nil { |
| 129 | return |
| 130 | } |
| 131 | defer stopServer(t, server) |
| 132 | // Setup the command-line. |
Robin Thellend | 18205cf | 2014-10-21 13:53:59 -0700 | [diff] [blame] | 133 | cmd := root() |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 134 | var stdout, stderr bytes.Buffer |
| 135 | cmd.Init(nil, &stdout, &stderr) |
David Why Use Two When One Will Do Presotto | adf0ca1 | 2014-11-13 10:49:01 -0800 | [diff] [blame] | 136 | appName := naming.JoinAddressName(endpoint.String(), "myapp/1") |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 137 | profile := "myprofile" |
| 138 | |
| 139 | // Test the 'Match' command. |
| 140 | if err := cmd.Execute([]string{"match", appName, profile}); err != nil { |
| 141 | t.Fatalf("%v", err) |
| 142 | } |
| 143 | if expected, got := jsonEnv, strings.TrimSpace(stdout.String()); got != expected { |
| 144 | t.Errorf("Unexpected output from match. Got %q, expected %q", got, expected) |
| 145 | } |
| 146 | stdout.Reset() |
| 147 | |
| 148 | // Test the 'put' command. |
| 149 | f, err := ioutil.TempFile("", "test") |
| 150 | if err != nil { |
| 151 | t.Fatalf("%v", err) |
| 152 | } |
| 153 | fileName := f.Name() |
| 154 | defer os.Remove(fileName) |
| 155 | if _, err = f.Write([]byte(jsonEnv)); err != nil { |
| 156 | t.Fatalf("%v", err) |
| 157 | } |
| 158 | if err = f.Close(); err != nil { |
| 159 | t.Fatalf("%v", err) |
| 160 | } |
| 161 | if err := cmd.Execute([]string{"put", appName, profile, fileName}); err != nil { |
| 162 | t.Fatalf("%v", err) |
| 163 | } |
Jiri Simsa | 1201b6c | 2014-08-19 13:26:31 -0700 | [diff] [blame] | 164 | 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] | 165 | t.Errorf("Unexpected output from put. Got %q, expected %q", got, expected) |
| 166 | } |
| 167 | stdout.Reset() |
| 168 | |
| 169 | // Test the 'remove' command. |
| 170 | if err := cmd.Execute([]string{"remove", appName, profile}); err != nil { |
| 171 | t.Fatalf("%v", err) |
| 172 | } |
| 173 | if expected, got := "Application envelope removed successfully.", strings.TrimSpace(stdout.String()); got != expected { |
| 174 | t.Errorf("Unexpected output from remove. Got %q, expected %q", got, expected) |
| 175 | } |
| 176 | stdout.Reset() |
| 177 | |
| 178 | // Test the 'edit' command. (nothing changed) |
| 179 | os.Setenv("EDITOR", "true") |
| 180 | if err := cmd.Execute([]string{"edit", appName, profile}); err != nil { |
| 181 | t.Fatalf("%v", err) |
| 182 | } |
| 183 | if expected, got := "Nothing changed", strings.TrimSpace(stdout.String()); got != expected { |
| 184 | t.Errorf("Unexpected output from edit. Got %q, expected %q", got, expected) |
| 185 | } |
| 186 | stdout.Reset() |
| 187 | |
| 188 | // Test the 'edit' command. |
Jiri Simsa | fb3e24d | 2014-05-23 10:02:13 -0700 | [diff] [blame] | 189 | os.Setenv("EDITOR", "perl -pi -e 's/arg1/arg111/'") |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 190 | if err := cmd.Execute([]string{"edit", appName, profile}); err != nil { |
| 191 | t.Fatalf("%v", err) |
| 192 | } |
| 193 | if expected, got := "Application envelope updated successfully.", strings.TrimSpace(stdout.String()); got != expected { |
| 194 | t.Errorf("Unexpected output from edit. Got %q, expected %q", got, expected) |
| 195 | } |
| 196 | stdout.Reset() |
| 197 | } |