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