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