blob: de607ae7ac70a4b48e0ef6ab89fcef7a853dd19a [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
Ankur0003fdc2015-01-22 10:59:41 -080019 tsecurity "v.io/core/veyron/lib/testutil/security"
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",
42 "Env": [
43 "env1",
44 "env2",
45 "env3"
Robin Thellend64178ed2014-11-20 13:16:22 -080046 ],
47 "Packages": {
48 "pkg1": "/path/to/package1"
49 }
Robin Thellend32c438b2014-05-22 17:28:00 -070050}`
51)
52
53type server struct {
54 suffix string
55}
56
Matt Rosencrantzf5afcaf2014-06-02 11:31:22 -070057func (s *server) Match(_ ipc.ServerContext, profiles []string) (application.Envelope, error) {
Robin Thellend32c438b2014-05-22 17:28:00 -070058 vlog.VI(2).Infof("%v.Match(%v) was called", s.suffix, profiles)
59 return envelope, nil
60}
61
Matt Rosencrantzf5afcaf2014-06-02 11:31:22 -070062func (s *server) Put(_ ipc.ServerContext, profiles []string, env application.Envelope) error {
Robin Thellend32c438b2014-05-22 17:28:00 -070063 vlog.VI(2).Infof("%v.Put(%v, %v) was called", s.suffix, profiles, env)
64 return nil
65}
66
Matt Rosencrantzf5afcaf2014-06-02 11:31:22 -070067func (s *server) Remove(_ ipc.ServerContext, profile string) error {
Robin Thellend32c438b2014-05-22 17:28:00 -070068 vlog.VI(2).Infof("%v.Remove(%v) was called", s.suffix, profile)
69 return nil
70}
71
Robert Kroegerd6e1d1a2014-12-10 15:08:45 -080072func (s *server) SetACL(_ ipc.ServerContext, acl access.TaggedACLMap, etag string) error {
73 vlog.VI(2).Infof("%v.SetACL(%v, %v) was called", acl, etag)
74 return nil
75}
76
77func (s *server) GetACL(ipc.ServerContext) (access.TaggedACLMap, string, error) {
78 vlog.VI(2).Infof("%v.GetACL() was called")
79 return nil, "", nil
80}
81
Robin Thellend32c438b2014-05-22 17:28:00 -070082type dispatcher struct {
83}
84
85func NewDispatcher() *dispatcher {
86 return &dispatcher{}
87}
88
Robin Thellenda02fe8f2014-11-19 09:58:29 -080089func (d *dispatcher) Lookup(suffix string) (interface{}, security.Authorizer, error) {
Cosmos Nicolaou710daa22014-11-11 19:39:18 -080090 return repository.ApplicationServer(&server{suffix: suffix}), nil, nil
Robin Thellend32c438b2014-05-22 17:28:00 -070091}
92
Matt Rosencrantzf541b772015-01-13 07:58:59 -080093func startServer(t *testing.T, ctx *context.T) (ipc.Server, naming.Endpoint, error) {
Robin Thellend32c438b2014-05-22 17:28:00 -070094 dispatcher := NewDispatcher()
Matt Rosencrantzf541b772015-01-13 07:58:59 -080095 server, err := veyron2.NewServer(ctx)
Robin Thellend32c438b2014-05-22 17:28:00 -070096 if err != nil {
97 t.Errorf("NewServer failed: %v", err)
98 return nil, nil, err
99 }
Suharsh Sivakumard68949c2015-01-26 10:32:23 -0800100 endpoints, err := server.Listen(veyron2.GetListenSpec(ctx))
Robin Thellend32c438b2014-05-22 17:28:00 -0700101 if err != nil {
102 t.Errorf("Listen failed: %v", err)
103 return nil, nil, err
104 }
Cosmos Nicolaou92dba582014-11-05 17:24:10 -0800105 if err := server.ServeDispatcher("", dispatcher); err != nil {
Cosmos Nicolaoufdc838b2014-06-30 21:44:27 -0700106 t.Errorf("Serve failed: %v", err)
107 return nil, nil, err
108 }
Cosmos Nicolaou28dabfc2014-12-15 22:51:07 -0800109 return server, endpoints[0], nil
Robin Thellend32c438b2014-05-22 17:28:00 -0700110}
111
112func stopServer(t *testing.T, server ipc.Server) {
113 if err := server.Stop(); err != nil {
114 t.Errorf("server.Stop failed: %v", err)
115 }
116}
117
118func TestApplicationClient(t *testing.T) {
Matt Rosencrantza5ad2722015-01-22 11:17:47 -0800119 var shutdown veyron2.Shutdown
120 gctx, shutdown = veyron2.Init()
121 defer shutdown()
Matt Rosencrantzc2ed03e2014-11-25 15:40:48 -0800122 var err error
Matt Rosencrantza5ad2722015-01-22 11:17:47 -0800123 if gctx, err = veyron2.SetPrincipal(gctx, tsecurity.NewPrincipal("test-blessing")); err != nil {
124 panic(err)
Matt Rosencrantzc2ed03e2014-11-25 15:40:48 -0800125 }
Matt Rosencrantzc2ed03e2014-11-25 15:40:48 -0800126
Matt Rosencrantza5ad2722015-01-22 11:17:47 -0800127 server, endpoint, err := startServer(t, gctx)
Robin Thellend32c438b2014-05-22 17:28:00 -0700128 if err != nil {
129 return
130 }
131 defer stopServer(t, server)
132 // Setup the command-line.
Robin Thellend18205cf2014-10-21 13:53:59 -0700133 cmd := root()
Robin Thellend32c438b2014-05-22 17:28:00 -0700134 var stdout, stderr bytes.Buffer
135 cmd.Init(nil, &stdout, &stderr)
David Why Use Two When One Will Do Presottoadf0ca12014-11-13 10:49:01 -0800136 appName := naming.JoinAddressName(endpoint.String(), "myapp/1")
Robin Thellend32c438b2014-05-22 17:28:00 -0700137 profile := "myprofile"
138
139 // Test the 'Match' command.
140 if err := cmd.Execute([]string{"match", appName, profile}); err != nil {
141 t.Fatalf("%v", err)
142 }
143 if expected, got := jsonEnv, strings.TrimSpace(stdout.String()); got != expected {
144 t.Errorf("Unexpected output from match. Got %q, expected %q", got, expected)
145 }
146 stdout.Reset()
147
148 // Test the 'put' command.
149 f, err := ioutil.TempFile("", "test")
150 if err != nil {
151 t.Fatalf("%v", err)
152 }
153 fileName := f.Name()
154 defer os.Remove(fileName)
155 if _, err = f.Write([]byte(jsonEnv)); err != nil {
156 t.Fatalf("%v", err)
157 }
158 if err = f.Close(); err != nil {
159 t.Fatalf("%v", err)
160 }
161 if err := cmd.Execute([]string{"put", appName, profile, fileName}); err != nil {
162 t.Fatalf("%v", err)
163 }
Jiri Simsa1201b6c2014-08-19 13:26:31 -0700164 if expected, got := "Application envelope added successfully.", strings.TrimSpace(stdout.String()); got != expected {
Robin Thellend32c438b2014-05-22 17:28:00 -0700165 t.Errorf("Unexpected output from put. Got %q, expected %q", got, expected)
166 }
167 stdout.Reset()
168
169 // Test the 'remove' command.
170 if err := cmd.Execute([]string{"remove", appName, profile}); err != nil {
171 t.Fatalf("%v", err)
172 }
173 if expected, got := "Application envelope removed successfully.", strings.TrimSpace(stdout.String()); got != expected {
174 t.Errorf("Unexpected output from remove. Got %q, expected %q", got, expected)
175 }
176 stdout.Reset()
177
178 // Test the 'edit' command. (nothing changed)
179 os.Setenv("EDITOR", "true")
180 if err := cmd.Execute([]string{"edit", appName, profile}); err != nil {
181 t.Fatalf("%v", err)
182 }
183 if expected, got := "Nothing changed", strings.TrimSpace(stdout.String()); got != expected {
184 t.Errorf("Unexpected output from edit. Got %q, expected %q", got, expected)
185 }
186 stdout.Reset()
187
188 // Test the 'edit' command.
Jiri Simsafb3e24d2014-05-23 10:02:13 -0700189 os.Setenv("EDITOR", "perl -pi -e 's/arg1/arg111/'")
Robin Thellend32c438b2014-05-22 17:28:00 -0700190 if err := cmd.Execute([]string{"edit", appName, profile}); err != nil {
191 t.Fatalf("%v", err)
192 }
193 if expected, got := "Application envelope updated successfully.", strings.TrimSpace(stdout.String()); got != expected {
194 t.Errorf("Unexpected output from edit. Got %q, expected %q", got, expected)
195 }
196 stdout.Reset()
197}