blob: 696dea1441dc4ecf6a8423f19f82f6f10d7db615 [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"},
Robin Thellend64178ed2014-11-20 13:16:22 -080028 Packages: map[string]string{
29 "pkg1": "/path/to/package1",
30 },
Robin Thellend32c438b2014-05-22 17:28:00 -070031 }
32 jsonEnv = `{
Bogdan Caprita55c10122014-07-09 15:35:07 -070033 "Title": "fifa world cup",
Robin Thellend32c438b2014-05-22 17:28:00 -070034 "Args": [
35 "arg1",
36 "arg2",
37 "arg3"
38 ],
39 "Binary": "/path/to/binary",
40 "Env": [
41 "env1",
42 "env2",
43 "env3"
Robin Thellend64178ed2014-11-20 13:16:22 -080044 ],
45 "Packages": {
46 "pkg1": "/path/to/package1"
47 }
Robin Thellend32c438b2014-05-22 17:28:00 -070048}`
49)
50
51type server struct {
52 suffix string
53}
54
Matt Rosencrantzf5afcaf2014-06-02 11:31:22 -070055func (s *server) Match(_ ipc.ServerContext, profiles []string) (application.Envelope, error) {
Robin Thellend32c438b2014-05-22 17:28:00 -070056 vlog.VI(2).Infof("%v.Match(%v) was called", s.suffix, profiles)
57 return envelope, nil
58}
59
Matt Rosencrantzf5afcaf2014-06-02 11:31:22 -070060func (s *server) Put(_ ipc.ServerContext, profiles []string, env application.Envelope) error {
Robin Thellend32c438b2014-05-22 17:28:00 -070061 vlog.VI(2).Infof("%v.Put(%v, %v) was called", s.suffix, profiles, env)
62 return nil
63}
64
Matt Rosencrantzf5afcaf2014-06-02 11:31:22 -070065func (s *server) Remove(_ ipc.ServerContext, profile string) error {
Robin Thellend32c438b2014-05-22 17:28:00 -070066 vlog.VI(2).Infof("%v.Remove(%v) was called", s.suffix, profile)
67 return nil
68}
69
70type dispatcher struct {
71}
72
73func NewDispatcher() *dispatcher {
74 return &dispatcher{}
75}
76
Robin Thellenda02fe8f2014-11-19 09:58:29 -080077func (d *dispatcher) Lookup(suffix string) (interface{}, security.Authorizer, error) {
Cosmos Nicolaou710daa22014-11-11 19:39:18 -080078 return repository.ApplicationServer(&server{suffix: suffix}), nil, nil
Robin Thellend32c438b2014-05-22 17:28:00 -070079}
80
81func startServer(t *testing.T, r veyron2.Runtime) (ipc.Server, naming.Endpoint, error) {
82 dispatcher := NewDispatcher()
83 server, err := r.NewServer()
84 if err != nil {
85 t.Errorf("NewServer failed: %v", err)
86 return nil, nil, err
87 }
Cosmos Nicolaou28dabfc2014-12-15 22:51:07 -080088 endpoints, err := server.Listen(profiles.LocalListenSpec)
Robin Thellend32c438b2014-05-22 17:28:00 -070089 if err != nil {
90 t.Errorf("Listen failed: %v", err)
91 return nil, nil, err
92 }
Cosmos Nicolaou92dba582014-11-05 17:24:10 -080093 if err := server.ServeDispatcher("", dispatcher); err != nil {
Cosmos Nicolaoufdc838b2014-06-30 21:44:27 -070094 t.Errorf("Serve failed: %v", err)
95 return nil, nil, err
96 }
Cosmos Nicolaou28dabfc2014-12-15 22:51:07 -080097 return server, endpoints[0], nil
Robin Thellend32c438b2014-05-22 17:28:00 -070098}
99
100func stopServer(t *testing.T, server ipc.Server) {
101 if err := server.Stop(); err != nil {
102 t.Errorf("server.Stop failed: %v", err)
103 }
104}
105
106func TestApplicationClient(t *testing.T) {
Matt Rosencrantzc2ed03e2014-11-25 15:40:48 -0800107 var err error
108 runtime, err = rt.New()
109 if err != nil {
110 t.Fatalf("Unexpected error initializing runtime: %s", err)
111 }
112 defer runtime.Cleanup()
113
Robin Thellend32c438b2014-05-22 17:28:00 -0700114 server, endpoint, err := startServer(t, runtime)
115 if err != nil {
116 return
117 }
118 defer stopServer(t, server)
119 // Setup the command-line.
Robin Thellend18205cf2014-10-21 13:53:59 -0700120 cmd := root()
Robin Thellend32c438b2014-05-22 17:28:00 -0700121 var stdout, stderr bytes.Buffer
122 cmd.Init(nil, &stdout, &stderr)
David Why Use Two When One Will Do Presottoadf0ca12014-11-13 10:49:01 -0800123 appName := naming.JoinAddressName(endpoint.String(), "myapp/1")
Robin Thellend32c438b2014-05-22 17:28:00 -0700124 profile := "myprofile"
125
126 // Test the 'Match' command.
127 if err := cmd.Execute([]string{"match", appName, profile}); err != nil {
128 t.Fatalf("%v", err)
129 }
130 if expected, got := jsonEnv, strings.TrimSpace(stdout.String()); got != expected {
131 t.Errorf("Unexpected output from match. Got %q, expected %q", got, expected)
132 }
133 stdout.Reset()
134
135 // Test the 'put' command.
136 f, err := ioutil.TempFile("", "test")
137 if err != nil {
138 t.Fatalf("%v", err)
139 }
140 fileName := f.Name()
141 defer os.Remove(fileName)
142 if _, err = f.Write([]byte(jsonEnv)); err != nil {
143 t.Fatalf("%v", err)
144 }
145 if err = f.Close(); err != nil {
146 t.Fatalf("%v", err)
147 }
148 if err := cmd.Execute([]string{"put", appName, profile, fileName}); err != nil {
149 t.Fatalf("%v", err)
150 }
Jiri Simsa1201b6c2014-08-19 13:26:31 -0700151 if expected, got := "Application envelope added successfully.", strings.TrimSpace(stdout.String()); got != expected {
Robin Thellend32c438b2014-05-22 17:28:00 -0700152 t.Errorf("Unexpected output from put. Got %q, expected %q", got, expected)
153 }
154 stdout.Reset()
155
156 // Test the 'remove' command.
157 if err := cmd.Execute([]string{"remove", appName, profile}); err != nil {
158 t.Fatalf("%v", err)
159 }
160 if expected, got := "Application envelope removed successfully.", strings.TrimSpace(stdout.String()); got != expected {
161 t.Errorf("Unexpected output from remove. Got %q, expected %q", got, expected)
162 }
163 stdout.Reset()
164
165 // Test the 'edit' command. (nothing changed)
166 os.Setenv("EDITOR", "true")
167 if err := cmd.Execute([]string{"edit", appName, profile}); err != nil {
168 t.Fatalf("%v", err)
169 }
170 if expected, got := "Nothing changed", strings.TrimSpace(stdout.String()); got != expected {
171 t.Errorf("Unexpected output from edit. Got %q, expected %q", got, expected)
172 }
173 stdout.Reset()
174
175 // Test the 'edit' command.
Jiri Simsafb3e24d2014-05-23 10:02:13 -0700176 os.Setenv("EDITOR", "perl -pi -e 's/arg1/arg111/'")
Robin Thellend32c438b2014-05-22 17:28:00 -0700177 if err := cmd.Execute([]string{"edit", appName, profile}); err != nil {
178 t.Fatalf("%v", err)
179 }
180 if expected, got := "Application envelope updated successfully.", strings.TrimSpace(stdout.String()); got != expected {
181 t.Errorf("Unexpected output from edit. Got %q, expected %q", got, expected)
182 }
183 stdout.Reset()
184}