blob: 31eb088dcd25eca550599f9e6ca9af06eef65d93 [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 Simsa764efb72014-12-25 20:57:03 -080010 "v.io/core/veyron2"
Matt Rosencrantzf541b772015-01-13 07:58:59 -080011 "v.io/core/veyron2/context"
Jiri Simsa764efb72014-12-25 20:57:03 -080012 "v.io/core/veyron2/ipc"
13 "v.io/core/veyron2/naming"
Jiri Simsa764efb72014-12-25 20:57:03 -080014 "v.io/core/veyron2/security"
15 "v.io/core/veyron2/services/mgmt/application"
Robert Kroegerd6e1d1a2014-12-10 15:08:45 -080016 "v.io/core/veyron2/services/security/access"
Jiri Simsa764efb72014-12-25 20:57:03 -080017 "v.io/core/veyron2/vlog"
Cosmos Nicolaoud6c3c9c2014-09-30 15:42:53 -070018
Ankurf416ac52015-01-29 13:58:24 -080019 "v.io/core/veyron/lib/testutil"
Suharsh Sivakumard68949c2015-01-26 10:32:23 -080020 _ "v.io/core/veyron/profiles"
Jiri Simsa764efb72014-12-25 20:57:03 -080021 "v.io/core/veyron/services/mgmt/repository"
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"},
28 Binary: "/path/to/binary",
29 Env: []string{"env1", "env2", "env3"},
Robin Thellend64178ed2014-11-20 13:16:22 -080030 Packages: map[string]string{
31 "pkg1": "/path/to/package1",
32 },
Robin Thellend32c438b2014-05-22 17:28:00 -070033 }
34 jsonEnv = `{
Bogdan Caprita55c10122014-07-09 15:35:07 -070035 "Title": "fifa world cup",
Robin Thellend32c438b2014-05-22 17:28:00 -070036 "Args": [
37 "arg1",
38 "arg2",
39 "arg3"
40 ],
41 "Binary": "/path/to/binary",
gauthamtcb03d132015-02-05 18:05:22 -080042 "Signature": {
43 "Purpose": "",
44 "Hash": "",
45 "R": "",
46 "S": ""
47 },
48 "Publisher": {
49 "CertificateChains": null
50 },
Robin Thellend32c438b2014-05-22 17:28:00 -070051 "Env": [
52 "env1",
53 "env2",
54 "env3"
Robin Thellend64178ed2014-11-20 13:16:22 -080055 ],
56 "Packages": {
57 "pkg1": "/path/to/package1"
58 }
Robin Thellend32c438b2014-05-22 17:28:00 -070059}`
60)
61
62type server struct {
63 suffix string
64}
65
Matt Rosencrantzf5afcaf2014-06-02 11:31:22 -070066func (s *server) Match(_ ipc.ServerContext, profiles []string) (application.Envelope, error) {
Robin Thellend32c438b2014-05-22 17:28:00 -070067 vlog.VI(2).Infof("%v.Match(%v) was called", s.suffix, profiles)
68 return envelope, nil
69}
70
Matt Rosencrantzf5afcaf2014-06-02 11:31:22 -070071func (s *server) Put(_ ipc.ServerContext, profiles []string, env application.Envelope) error {
Robin Thellend32c438b2014-05-22 17:28:00 -070072 vlog.VI(2).Infof("%v.Put(%v, %v) was called", s.suffix, profiles, env)
73 return nil
74}
75
Matt Rosencrantzf5afcaf2014-06-02 11:31:22 -070076func (s *server) Remove(_ ipc.ServerContext, profile string) error {
Robin Thellend32c438b2014-05-22 17:28:00 -070077 vlog.VI(2).Infof("%v.Remove(%v) was called", s.suffix, profile)
78 return nil
79}
80
Robert Kroegerd6e1d1a2014-12-10 15:08:45 -080081func (s *server) SetACL(_ ipc.ServerContext, acl access.TaggedACLMap, etag string) error {
82 vlog.VI(2).Infof("%v.SetACL(%v, %v) was called", acl, etag)
83 return nil
84}
85
86func (s *server) GetACL(ipc.ServerContext) (access.TaggedACLMap, string, error) {
87 vlog.VI(2).Infof("%v.GetACL() was called")
88 return nil, "", nil
89}
90
Robin Thellend32c438b2014-05-22 17:28:00 -070091type dispatcher struct {
92}
93
Bogdan Capritae96cd042015-02-03 17:32:57 -080094func NewDispatcher() ipc.Dispatcher {
Robin Thellend32c438b2014-05-22 17:28:00 -070095 return &dispatcher{}
96}
97
Robin Thellenda02fe8f2014-11-19 09:58:29 -080098func (d *dispatcher) Lookup(suffix string) (interface{}, security.Authorizer, error) {
Cosmos Nicolaou710daa22014-11-11 19:39:18 -080099 return repository.ApplicationServer(&server{suffix: suffix}), nil, nil
Robin Thellend32c438b2014-05-22 17:28:00 -0700100}
101
Matt Rosencrantzf541b772015-01-13 07:58:59 -0800102func startServer(t *testing.T, ctx *context.T) (ipc.Server, naming.Endpoint, error) {
Robin Thellend32c438b2014-05-22 17:28:00 -0700103 dispatcher := NewDispatcher()
Matt Rosencrantzf541b772015-01-13 07:58:59 -0800104 server, err := veyron2.NewServer(ctx)
Robin Thellend32c438b2014-05-22 17:28:00 -0700105 if err != nil {
106 t.Errorf("NewServer failed: %v", err)
107 return nil, nil, err
108 }
Suharsh Sivakumard68949c2015-01-26 10:32:23 -0800109 endpoints, err := server.Listen(veyron2.GetListenSpec(ctx))
Robin Thellend32c438b2014-05-22 17:28:00 -0700110 if err != nil {
111 t.Errorf("Listen failed: %v", err)
112 return nil, nil, err
113 }
Cosmos Nicolaou92dba582014-11-05 17:24:10 -0800114 if err := server.ServeDispatcher("", dispatcher); err != nil {
Cosmos Nicolaoufdc838b2014-06-30 21:44:27 -0700115 t.Errorf("Serve failed: %v", err)
116 return nil, nil, err
117 }
Cosmos Nicolaou28dabfc2014-12-15 22:51:07 -0800118 return server, endpoints[0], nil
Robin Thellend32c438b2014-05-22 17:28:00 -0700119}
120
121func stopServer(t *testing.T, server ipc.Server) {
122 if err := server.Stop(); err != nil {
123 t.Errorf("server.Stop failed: %v", err)
124 }
125}
126
127func TestApplicationClient(t *testing.T) {
Matt Rosencrantza5ad2722015-01-22 11:17:47 -0800128 var shutdown veyron2.Shutdown
Ankurf416ac52015-01-29 13:58:24 -0800129 gctx, shutdown = testutil.InitForTest()
Matt Rosencrantza5ad2722015-01-22 11:17:47 -0800130 defer shutdown()
Matt Rosencrantzc2ed03e2014-11-25 15:40:48 -0800131
Matt Rosencrantza5ad2722015-01-22 11:17:47 -0800132 server, endpoint, err := startServer(t, gctx)
Robin Thellend32c438b2014-05-22 17:28:00 -0700133 if err != nil {
134 return
135 }
136 defer stopServer(t, server)
137 // Setup the command-line.
Robin Thellend18205cf2014-10-21 13:53:59 -0700138 cmd := root()
Robin Thellend32c438b2014-05-22 17:28:00 -0700139 var stdout, stderr bytes.Buffer
140 cmd.Init(nil, &stdout, &stderr)
David Why Use Two When One Will Do Presottoadf0ca12014-11-13 10:49:01 -0800141 appName := naming.JoinAddressName(endpoint.String(), "myapp/1")
Robin Thellend32c438b2014-05-22 17:28:00 -0700142 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 Simsa1201b6c2014-08-19 13:26:31 -0700169 if expected, got := "Application envelope added successfully.", strings.TrimSpace(stdout.String()); got != expected {
Robin Thellend32c438b2014-05-22 17:28:00 -0700170 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 Simsafb3e24d2014-05-23 10:02:13 -0700194 os.Setenv("EDITOR", "perl -pi -e 's/arg1/arg111/'")
Robin Thellend32c438b2014-05-22 17:28:00 -0700195 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}