blob: 2594b3f60951d3761ed86f59beb5c6151fb8f1c0 [file] [log] [blame]
Robin Thellend18205cf2014-10-21 13:53:59 -07001package main
Robin Thellend32c438b2014-05-22 17:28:00 -07002
3import (
4 "bytes"
5 "io/ioutil"
6 "os"
7 "strings"
8 "testing"
9
Jiri Simsa6ac95222015-02-23 16:11:49 -080010 "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 Simsa337af232015-02-27 14:36:46 -080017 "v.io/x/lib/vlog"
Cosmos Nicolaoud6c3c9c2014-09-30 15:42:53 -070018
Jiri Simsaffceefa2015-02-28 11:03:34 -080019 _ "v.io/x/ref/profiles"
20 "v.io/x/ref/services/mgmt/repository"
Cosmos Nicolaou1381f8a2015-03-13 09:40:34 -070021 "v.io/x/ref/test"
Robin Thellend32c438b2014-05-22 17:28:00 -070022)
23
24var (
25 envelope = application.Envelope{
Bogdan Caprita55c10122014-07-09 15:35:07 -070026 Title: "fifa world cup",
Robin Thellend32c438b2014-05-22 17:28:00 -070027 Args: []string{"arg1", "arg2", "arg3"},
Bogdan Capritac25a48c2015-02-12 13:45:51 -080028 Binary: application.SignedFile{File: "/path/to/binary"},
Robin Thellend32c438b2014-05-22 17:28:00 -070029 Env: []string{"env1", "env2", "env3"},
Bogdan Capritac25a48c2015-02-12 13:45:51 -080030 Packages: map[string]application.SignedFile{
31 "pkg1": application.SignedFile{
gauthamt3dbef0c2015-02-10 12:26:02 -080032 File: "/path/to/package1",
33 },
Robin Thellend64178ed2014-11-20 13:16:22 -080034 },
Robin Thellend32c438b2014-05-22 17:28:00 -070035 }
36 jsonEnv = `{
Bogdan Caprita55c10122014-07-09 15:35:07 -070037 "Title": "fifa world cup",
Robin Thellend32c438b2014-05-22 17:28:00 -070038 "Args": [
39 "arg1",
40 "arg2",
41 "arg3"
42 ],
Bogdan Capritac25a48c2015-02-12 13:45:51 -080043 "Binary": {
44 "File": "/path/to/binary",
45 "Signature": {
Jungho Ahnfa850e12015-02-12 19:00:24 -080046 "Purpose": null,
Bogdan Capritac25a48c2015-02-12 13:45:51 -080047 "Hash": "",
Jungho Ahnfa850e12015-02-12 19:00:24 -080048 "R": null,
49 "S": null
Bogdan Capritac25a48c2015-02-12 13:45:51 -080050 }
gauthamtcb03d132015-02-05 18:05:22 -080051 },
52 "Publisher": {
53 "CertificateChains": null
54 },
Robin Thellend32c438b2014-05-22 17:28:00 -070055 "Env": [
56 "env1",
57 "env2",
58 "env3"
Robin Thellend64178ed2014-11-20 13:16:22 -080059 ],
60 "Packages": {
gauthamt3dbef0c2015-02-10 12:26:02 -080061 "pkg1": {
62 "File": "/path/to/package1",
63 "Signature": {
Jungho Ahnfa850e12015-02-12 19:00:24 -080064 "Purpose": null,
gauthamt3dbef0c2015-02-10 12:26:02 -080065 "Hash": "",
Jungho Ahnfa850e12015-02-12 19:00:24 -080066 "R": null,
67 "S": null
gauthamt3dbef0c2015-02-10 12:26:02 -080068 }
69 }
Robin Thellend64178ed2014-11-20 13:16:22 -080070 }
Robin Thellend32c438b2014-05-22 17:28:00 -070071}`
72)
73
74type server struct {
75 suffix string
76}
77
Matt Rosencrantz5c7ed212015-02-27 22:42:35 -080078func (s *server) Match(_ ipc.ServerCall, profiles []string) (application.Envelope, error) {
Robin Thellend32c438b2014-05-22 17:28:00 -070079 vlog.VI(2).Infof("%v.Match(%v) was called", s.suffix, profiles)
80 return envelope, nil
81}
82
Matt Rosencrantz5c7ed212015-02-27 22:42:35 -080083func (s *server) Put(_ ipc.ServerCall, profiles []string, env application.Envelope) error {
Robin Thellend32c438b2014-05-22 17:28:00 -070084 vlog.VI(2).Infof("%v.Put(%v, %v) was called", s.suffix, profiles, env)
85 return nil
86}
87
Matt Rosencrantz5c7ed212015-02-27 22:42:35 -080088func (s *server) Remove(_ ipc.ServerCall, profile string) error {
Robin Thellend32c438b2014-05-22 17:28:00 -070089 vlog.VI(2).Infof("%v.Remove(%v) was called", s.suffix, profile)
90 return nil
91}
92
Benjamin Prosnitzb60efb92015-03-11 17:47:43 -070093func (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 Kroegerd6e1d1a2014-12-10 15:08:45 -080095 return nil
96}
97
Benjamin Prosnitzb60efb92015-03-11 17:47:43 -070098func (s *server) GetPermissions(ipc.ServerCall) (access.Permissions, string, error) {
99 vlog.VI(2).Infof("%v.GetPermissions() was called")
Robert Kroegerd6e1d1a2014-12-10 15:08:45 -0800100 return nil, "", nil
101}
102
Robin Thellend32c438b2014-05-22 17:28:00 -0700103type dispatcher struct {
104}
105
Bogdan Capritae96cd042015-02-03 17:32:57 -0800106func NewDispatcher() ipc.Dispatcher {
Robin Thellend32c438b2014-05-22 17:28:00 -0700107 return &dispatcher{}
108}
109
Robin Thellenda02fe8f2014-11-19 09:58:29 -0800110func (d *dispatcher) Lookup(suffix string) (interface{}, security.Authorizer, error) {
Cosmos Nicolaou710daa22014-11-11 19:39:18 -0800111 return repository.ApplicationServer(&server{suffix: suffix}), nil, nil
Robin Thellend32c438b2014-05-22 17:28:00 -0700112}
113
Matt Rosencrantzf541b772015-01-13 07:58:59 -0800114func startServer(t *testing.T, ctx *context.T) (ipc.Server, naming.Endpoint, error) {
Robin Thellend32c438b2014-05-22 17:28:00 -0700115 dispatcher := NewDispatcher()
Jiri Simsa6ac95222015-02-23 16:11:49 -0800116 server, err := v23.NewServer(ctx)
Robin Thellend32c438b2014-05-22 17:28:00 -0700117 if err != nil {
118 t.Errorf("NewServer failed: %v", err)
119 return nil, nil, err
120 }
Jiri Simsa6ac95222015-02-23 16:11:49 -0800121 endpoints, err := server.Listen(v23.GetListenSpec(ctx))
Robin Thellend32c438b2014-05-22 17:28:00 -0700122 if err != nil {
123 t.Errorf("Listen failed: %v", err)
124 return nil, nil, err
125 }
Cosmos Nicolaou92dba582014-11-05 17:24:10 -0800126 if err := server.ServeDispatcher("", dispatcher); err != nil {
Cosmos Nicolaoufdc838b2014-06-30 21:44:27 -0700127 t.Errorf("Serve failed: %v", err)
128 return nil, nil, err
129 }
Cosmos Nicolaou28dabfc2014-12-15 22:51:07 -0800130 return server, endpoints[0], nil
Robin Thellend32c438b2014-05-22 17:28:00 -0700131}
132
133func 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
139func TestApplicationClient(t *testing.T) {
Jiri Simsa6ac95222015-02-23 16:11:49 -0800140 var shutdown v23.Shutdown
Cosmos Nicolaou1381f8a2015-03-13 09:40:34 -0700141 gctx, shutdown = test.InitForTest()
Matt Rosencrantza5ad2722015-01-22 11:17:47 -0800142 defer shutdown()
Matt Rosencrantzc2ed03e2014-11-25 15:40:48 -0800143
Matt Rosencrantza5ad2722015-01-22 11:17:47 -0800144 server, endpoint, err := startServer(t, gctx)
Robin Thellend32c438b2014-05-22 17:28:00 -0700145 if err != nil {
146 return
147 }
148 defer stopServer(t, server)
149 // Setup the command-line.
Robin Thellend18205cf2014-10-21 13:53:59 -0700150 cmd := root()
Robin Thellend32c438b2014-05-22 17:28:00 -0700151 var stdout, stderr bytes.Buffer
152 cmd.Init(nil, &stdout, &stderr)
David Why Use Two When One Will Do Presottoadf0ca12014-11-13 10:49:01 -0800153 appName := naming.JoinAddressName(endpoint.String(), "myapp/1")
Robin Thellend32c438b2014-05-22 17:28:00 -0700154 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 Simsa1201b6c2014-08-19 13:26:31 -0700181 if expected, got := "Application envelope added successfully.", strings.TrimSpace(stdout.String()); got != expected {
Robin Thellend32c438b2014-05-22 17:28:00 -0700182 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 Simsafb3e24d2014-05-23 10:02:13 -0700206 os.Setenv("EDITOR", "perl -pi -e 's/arg1/arg111/'")
Robin Thellend32c438b2014-05-22 17:28:00 -0700207 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}