blob: 4917d7dcee8666029e484dc98752ef8da97a27e7 [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"
Ankur0003fdc2015-01-22 10:59:41 -080014 "v.io/core/veyron2/options"
Jiri Simsa764efb72014-12-25 20:57:03 -080015 "v.io/core/veyron2/rt"
16 "v.io/core/veyron2/security"
17 "v.io/core/veyron2/services/mgmt/application"
Robert Kroegerd6e1d1a2014-12-10 15:08:45 -080018 "v.io/core/veyron2/services/security/access"
Jiri Simsa764efb72014-12-25 20:57:03 -080019 "v.io/core/veyron2/vlog"
Cosmos Nicolaoud6c3c9c2014-09-30 15:42:53 -070020
Ankur0003fdc2015-01-22 10:59:41 -080021 tsecurity "v.io/core/veyron/lib/testutil/security"
Jiri Simsa764efb72014-12-25 20:57:03 -080022 "v.io/core/veyron/profiles"
23 "v.io/core/veyron/services/mgmt/repository"
Robin Thellend32c438b2014-05-22 17:28:00 -070024)
25
26var (
27 envelope = application.Envelope{
Bogdan Caprita55c10122014-07-09 15:35:07 -070028 Title: "fifa world cup",
Robin Thellend32c438b2014-05-22 17:28:00 -070029 Args: []string{"arg1", "arg2", "arg3"},
30 Binary: "/path/to/binary",
31 Env: []string{"env1", "env2", "env3"},
Robin Thellend64178ed2014-11-20 13:16:22 -080032 Packages: map[string]string{
33 "pkg1": "/path/to/package1",
34 },
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 ],
43 "Binary": "/path/to/binary",
44 "Env": [
45 "env1",
46 "env2",
47 "env3"
Robin Thellend64178ed2014-11-20 13:16:22 -080048 ],
49 "Packages": {
50 "pkg1": "/path/to/package1"
51 }
Robin Thellend32c438b2014-05-22 17:28:00 -070052}`
53)
54
55type server struct {
56 suffix string
57}
58
Matt Rosencrantzf5afcaf2014-06-02 11:31:22 -070059func (s *server) Match(_ ipc.ServerContext, profiles []string) (application.Envelope, error) {
Robin Thellend32c438b2014-05-22 17:28:00 -070060 vlog.VI(2).Infof("%v.Match(%v) was called", s.suffix, profiles)
61 return envelope, nil
62}
63
Matt Rosencrantzf5afcaf2014-06-02 11:31:22 -070064func (s *server) Put(_ ipc.ServerContext, profiles []string, env application.Envelope) error {
Robin Thellend32c438b2014-05-22 17:28:00 -070065 vlog.VI(2).Infof("%v.Put(%v, %v) was called", s.suffix, profiles, env)
66 return nil
67}
68
Matt Rosencrantzf5afcaf2014-06-02 11:31:22 -070069func (s *server) Remove(_ ipc.ServerContext, profile string) error {
Robin Thellend32c438b2014-05-22 17:28:00 -070070 vlog.VI(2).Infof("%v.Remove(%v) was called", s.suffix, profile)
71 return nil
72}
73
Robert Kroegerd6e1d1a2014-12-10 15:08:45 -080074func (s *server) SetACL(_ ipc.ServerContext, acl access.TaggedACLMap, etag string) error {
75 vlog.VI(2).Infof("%v.SetACL(%v, %v) was called", acl, etag)
76 return nil
77}
78
79func (s *server) GetACL(ipc.ServerContext) (access.TaggedACLMap, string, error) {
80 vlog.VI(2).Infof("%v.GetACL() was called")
81 return nil, "", nil
82}
83
Robin Thellend32c438b2014-05-22 17:28:00 -070084type dispatcher struct {
85}
86
87func NewDispatcher() *dispatcher {
88 return &dispatcher{}
89}
90
Robin Thellenda02fe8f2014-11-19 09:58:29 -080091func (d *dispatcher) Lookup(suffix string) (interface{}, security.Authorizer, error) {
Cosmos Nicolaou710daa22014-11-11 19:39:18 -080092 return repository.ApplicationServer(&server{suffix: suffix}), nil, nil
Robin Thellend32c438b2014-05-22 17:28:00 -070093}
94
Matt Rosencrantzf541b772015-01-13 07:58:59 -080095func startServer(t *testing.T, ctx *context.T) (ipc.Server, naming.Endpoint, error) {
Robin Thellend32c438b2014-05-22 17:28:00 -070096 dispatcher := NewDispatcher()
Matt Rosencrantzf541b772015-01-13 07:58:59 -080097 server, err := veyron2.NewServer(ctx)
Robin Thellend32c438b2014-05-22 17:28:00 -070098 if err != nil {
99 t.Errorf("NewServer failed: %v", err)
100 return nil, nil, err
101 }
Cosmos Nicolaou28dabfc2014-12-15 22:51:07 -0800102 endpoints, err := server.Listen(profiles.LocalListenSpec)
Robin Thellend32c438b2014-05-22 17:28:00 -0700103 if err != nil {
104 t.Errorf("Listen failed: %v", err)
105 return nil, nil, err
106 }
Cosmos Nicolaou92dba582014-11-05 17:24:10 -0800107 if err := server.ServeDispatcher("", dispatcher); err != nil {
Cosmos Nicolaoufdc838b2014-06-30 21:44:27 -0700108 t.Errorf("Serve failed: %v", err)
109 return nil, nil, err
110 }
Cosmos Nicolaou28dabfc2014-12-15 22:51:07 -0800111 return server, endpoints[0], nil
Robin Thellend32c438b2014-05-22 17:28:00 -0700112}
113
114func stopServer(t *testing.T, server ipc.Server) {
115 if err := server.Stop(); err != nil {
116 t.Errorf("server.Stop failed: %v", err)
117 }
118}
119
120func TestApplicationClient(t *testing.T) {
Matt Rosencrantzc2ed03e2014-11-25 15:40:48 -0800121 var err error
Ankur0003fdc2015-01-22 10:59:41 -0800122 // TODO(ataly, mattr, suharshs): This is a HACK to ensure that the server and the
123 // client have the same freshly created principal. One way to avoid the RuntimePrincipal
124 // option is to have a global client context.T (in main.go) instead of a veyron2.Runtime.
125 runtime, err = rt.New(options.RuntimePrincipal{tsecurity.NewPrincipal("test-blessing")})
Matt Rosencrantzc2ed03e2014-11-25 15:40:48 -0800126 if err != nil {
127 t.Fatalf("Unexpected error initializing runtime: %s", err)
128 }
129 defer runtime.Cleanup()
Matt Rosencrantzf541b772015-01-13 07:58:59 -0800130 ctx := runtime.NewContext()
Matt Rosencrantzc2ed03e2014-11-25 15:40:48 -0800131
Matt Rosencrantzf541b772015-01-13 07:58:59 -0800132 server, endpoint, err := startServer(t, ctx)
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}