blob: bd323c9bdeeccd463d0e518a170c4531871789a7 [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 Simsa519c5072014-09-17 21:37:57 -070010 "veyron.io/veyron/veyron2"
11 "veyron.io/veyron/veyron2/ipc"
12 "veyron.io/veyron/veyron2/naming"
13 "veyron.io/veyron/veyron2/rt"
14 "veyron.io/veyron/veyron2/security"
15 "veyron.io/veyron/veyron2/services/mgmt/application"
16 "veyron.io/veyron/veyron2/vlog"
Cosmos Nicolaoud6c3c9c2014-09-30 15:42:53 -070017
18 "veyron.io/veyron/veyron/profiles"
19 "veyron.io/veyron/veyron/services/mgmt/repository"
Robin Thellend32c438b2014-05-22 17:28:00 -070020)
21
22var (
23 envelope = application.Envelope{
Bogdan Caprita55c10122014-07-09 15:35:07 -070024 Title: "fifa world cup",
Robin Thellend32c438b2014-05-22 17:28:00 -070025 Args: []string{"arg1", "arg2", "arg3"},
26 Binary: "/path/to/binary",
27 Env: []string{"env1", "env2", "env3"},
28 }
29 jsonEnv = `{
Bogdan Caprita55c10122014-07-09 15:35:07 -070030 "Title": "fifa world cup",
Robin Thellend32c438b2014-05-22 17:28:00 -070031 "Args": [
32 "arg1",
33 "arg2",
34 "arg3"
35 ],
36 "Binary": "/path/to/binary",
37 "Env": [
38 "env1",
39 "env2",
40 "env3"
41 ]
42}`
43)
44
45type server struct {
46 suffix string
47}
48
Matt Rosencrantzf5afcaf2014-06-02 11:31:22 -070049func (s *server) Match(_ ipc.ServerContext, profiles []string) (application.Envelope, error) {
Robin Thellend32c438b2014-05-22 17:28:00 -070050 vlog.VI(2).Infof("%v.Match(%v) was called", s.suffix, profiles)
51 return envelope, nil
52}
53
Matt Rosencrantzf5afcaf2014-06-02 11:31:22 -070054func (s *server) Put(_ ipc.ServerContext, profiles []string, env application.Envelope) error {
Robin Thellend32c438b2014-05-22 17:28:00 -070055 vlog.VI(2).Infof("%v.Put(%v, %v) was called", s.suffix, profiles, env)
56 return nil
57}
58
Matt Rosencrantzf5afcaf2014-06-02 11:31:22 -070059func (s *server) Remove(_ ipc.ServerContext, profile string) error {
Robin Thellend32c438b2014-05-22 17:28:00 -070060 vlog.VI(2).Infof("%v.Remove(%v) was called", s.suffix, profile)
61 return nil
62}
63
64type dispatcher struct {
65}
66
67func NewDispatcher() *dispatcher {
68 return &dispatcher{}
69}
70
Cosmos Nicolaou1ee5e1a2014-11-02 10:20:30 -080071func (d *dispatcher) Lookup(suffix, method string) (interface{}, security.Authorizer, error) {
Jiri Simsaddbfebb2014-06-20 15:56:06 -070072 invoker := ipc.ReflectInvoker(repository.NewServerApplication(&server{suffix: suffix}))
Robin Thellend32c438b2014-05-22 17:28:00 -070073 return invoker, nil, nil
74}
75
76func startServer(t *testing.T, r veyron2.Runtime) (ipc.Server, naming.Endpoint, error) {
77 dispatcher := NewDispatcher()
78 server, err := r.NewServer()
79 if err != nil {
80 t.Errorf("NewServer failed: %v", err)
81 return nil, nil, err
82 }
Cosmos Nicolaouf8d4c2b2014-10-23 22:36:38 -070083 endpoint, err := server.Listen(profiles.LocalListenSpec)
Robin Thellend32c438b2014-05-22 17:28:00 -070084 if err != nil {
85 t.Errorf("Listen failed: %v", err)
86 return nil, nil, err
87 }
Cosmos Nicolaou92dba582014-11-05 17:24:10 -080088 if err := server.ServeDispatcher("", dispatcher); err != nil {
Cosmos Nicolaoufdc838b2014-06-30 21:44:27 -070089 t.Errorf("Serve failed: %v", err)
90 return nil, nil, err
91 }
Robin Thellend32c438b2014-05-22 17:28:00 -070092 return server, endpoint, nil
93}
94
95func stopServer(t *testing.T, server ipc.Server) {
96 if err := server.Stop(); err != nil {
97 t.Errorf("server.Stop failed: %v", err)
98 }
99}
100
101func TestApplicationClient(t *testing.T) {
102 runtime := rt.Init()
103 server, endpoint, err := startServer(t, runtime)
104 if err != nil {
105 return
106 }
107 defer stopServer(t, server)
108 // Setup the command-line.
Robin Thellend18205cf2014-10-21 13:53:59 -0700109 cmd := root()
Robin Thellend32c438b2014-05-22 17:28:00 -0700110 var stdout, stderr bytes.Buffer
111 cmd.Init(nil, &stdout, &stderr)
Cosmos Nicolaouef43dc42014-06-13 14:38:51 -0700112 appName := naming.JoinAddressName(endpoint.String(), "//myapp/1")
Robin Thellend32c438b2014-05-22 17:28:00 -0700113 profile := "myprofile"
114
115 // Test the 'Match' command.
116 if err := cmd.Execute([]string{"match", appName, profile}); err != nil {
117 t.Fatalf("%v", err)
118 }
119 if expected, got := jsonEnv, strings.TrimSpace(stdout.String()); got != expected {
120 t.Errorf("Unexpected output from match. Got %q, expected %q", got, expected)
121 }
122 stdout.Reset()
123
124 // Test the 'put' command.
125 f, err := ioutil.TempFile("", "test")
126 if err != nil {
127 t.Fatalf("%v", err)
128 }
129 fileName := f.Name()
130 defer os.Remove(fileName)
131 if _, err = f.Write([]byte(jsonEnv)); err != nil {
132 t.Fatalf("%v", err)
133 }
134 if err = f.Close(); err != nil {
135 t.Fatalf("%v", err)
136 }
137 if err := cmd.Execute([]string{"put", appName, profile, fileName}); err != nil {
138 t.Fatalf("%v", err)
139 }
Jiri Simsa1201b6c2014-08-19 13:26:31 -0700140 if expected, got := "Application envelope added successfully.", strings.TrimSpace(stdout.String()); got != expected {
Robin Thellend32c438b2014-05-22 17:28:00 -0700141 t.Errorf("Unexpected output from put. Got %q, expected %q", got, expected)
142 }
143 stdout.Reset()
144
145 // Test the 'remove' command.
146 if err := cmd.Execute([]string{"remove", appName, profile}); err != nil {
147 t.Fatalf("%v", err)
148 }
149 if expected, got := "Application envelope removed successfully.", strings.TrimSpace(stdout.String()); got != expected {
150 t.Errorf("Unexpected output from remove. Got %q, expected %q", got, expected)
151 }
152 stdout.Reset()
153
154 // Test the 'edit' command. (nothing changed)
155 os.Setenv("EDITOR", "true")
156 if err := cmd.Execute([]string{"edit", appName, profile}); err != nil {
157 t.Fatalf("%v", err)
158 }
159 if expected, got := "Nothing changed", strings.TrimSpace(stdout.String()); got != expected {
160 t.Errorf("Unexpected output from edit. Got %q, expected %q", got, expected)
161 }
162 stdout.Reset()
163
164 // Test the 'edit' command.
Jiri Simsafb3e24d2014-05-23 10:02:13 -0700165 os.Setenv("EDITOR", "perl -pi -e 's/arg1/arg111/'")
Robin Thellend32c438b2014-05-22 17:28:00 -0700166 if err := cmd.Execute([]string{"edit", appName, profile}); err != nil {
167 t.Fatalf("%v", err)
168 }
169 if expected, got := "Application envelope updated successfully.", strings.TrimSpace(stdout.String()); got != expected {
170 t.Errorf("Unexpected output from edit. Got %q, expected %q", got, expected)
171 }
172 stdout.Reset()
173}