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