Robin Thellend | 18205cf | 2014-10-21 13:53:59 -0700 | [diff] [blame] | 1 | package main |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "io/ioutil" |
| 6 | "os" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 10 | "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 Nicolaou | d6c3c9c | 2014-09-30 15:42:53 -0700 | [diff] [blame] | 17 | |
| 18 | "veyron.io/veyron/veyron/profiles" |
| 19 | "veyron.io/veyron/veyron/services/mgmt/repository" |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | var ( |
| 23 | envelope = application.Envelope{ |
Bogdan Caprita | 55c1012 | 2014-07-09 15:35:07 -0700 | [diff] [blame] | 24 | Title: "fifa world cup", |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 25 | Args: []string{"arg1", "arg2", "arg3"}, |
| 26 | Binary: "/path/to/binary", |
| 27 | Env: []string{"env1", "env2", "env3"}, |
Robin Thellend | 64178ed | 2014-11-20 13:16:22 -0800 | [diff] [blame^] | 28 | Packages: map[string]string{ |
| 29 | "pkg1": "/path/to/package1", |
| 30 | }, |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 31 | } |
| 32 | jsonEnv = `{ |
Bogdan Caprita | 55c1012 | 2014-07-09 15:35:07 -0700 | [diff] [blame] | 33 | "Title": "fifa world cup", |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 34 | "Args": [ |
| 35 | "arg1", |
| 36 | "arg2", |
| 37 | "arg3" |
| 38 | ], |
| 39 | "Binary": "/path/to/binary", |
| 40 | "Env": [ |
| 41 | "env1", |
| 42 | "env2", |
| 43 | "env3" |
Robin Thellend | 64178ed | 2014-11-20 13:16:22 -0800 | [diff] [blame^] | 44 | ], |
| 45 | "Packages": { |
| 46 | "pkg1": "/path/to/package1" |
| 47 | } |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 48 | }` |
| 49 | ) |
| 50 | |
| 51 | type server struct { |
| 52 | suffix string |
| 53 | } |
| 54 | |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 55 | func (s *server) Match(_ ipc.ServerContext, profiles []string) (application.Envelope, error) { |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 56 | vlog.VI(2).Infof("%v.Match(%v) was called", s.suffix, profiles) |
| 57 | return envelope, nil |
| 58 | } |
| 59 | |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 60 | func (s *server) Put(_ ipc.ServerContext, profiles []string, env application.Envelope) error { |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 61 | vlog.VI(2).Infof("%v.Put(%v, %v) was called", s.suffix, profiles, env) |
| 62 | return nil |
| 63 | } |
| 64 | |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 65 | func (s *server) Remove(_ ipc.ServerContext, profile string) error { |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 66 | vlog.VI(2).Infof("%v.Remove(%v) was called", s.suffix, profile) |
| 67 | return nil |
| 68 | } |
| 69 | |
| 70 | type dispatcher struct { |
| 71 | } |
| 72 | |
| 73 | func NewDispatcher() *dispatcher { |
| 74 | return &dispatcher{} |
| 75 | } |
| 76 | |
Robin Thellend | a02fe8f | 2014-11-19 09:58:29 -0800 | [diff] [blame] | 77 | func (d *dispatcher) Lookup(suffix string) (interface{}, security.Authorizer, error) { |
Cosmos Nicolaou | 710daa2 | 2014-11-11 19:39:18 -0800 | [diff] [blame] | 78 | return repository.ApplicationServer(&server{suffix: suffix}), nil, nil |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | func 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 Nicolaou | f8d4c2b | 2014-10-23 22:36:38 -0700 | [diff] [blame] | 88 | endpoint, err := server.Listen(profiles.LocalListenSpec) |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 89 | if err != nil { |
| 90 | t.Errorf("Listen failed: %v", err) |
| 91 | return nil, nil, err |
| 92 | } |
Cosmos Nicolaou | 92dba58 | 2014-11-05 17:24:10 -0800 | [diff] [blame] | 93 | if err := server.ServeDispatcher("", dispatcher); err != nil { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 94 | t.Errorf("Serve failed: %v", err) |
| 95 | return nil, nil, err |
| 96 | } |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 97 | return server, endpoint, nil |
| 98 | } |
| 99 | |
| 100 | func 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 | |
| 106 | func TestApplicationClient(t *testing.T) { |
| 107 | runtime := rt.Init() |
| 108 | server, endpoint, err := startServer(t, runtime) |
| 109 | if err != nil { |
| 110 | return |
| 111 | } |
| 112 | defer stopServer(t, server) |
| 113 | // Setup the command-line. |
Robin Thellend | 18205cf | 2014-10-21 13:53:59 -0700 | [diff] [blame] | 114 | cmd := root() |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 115 | var stdout, stderr bytes.Buffer |
| 116 | cmd.Init(nil, &stdout, &stderr) |
David Why Use Two When One Will Do Presotto | adf0ca1 | 2014-11-13 10:49:01 -0800 | [diff] [blame] | 117 | appName := naming.JoinAddressName(endpoint.String(), "myapp/1") |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 118 | profile := "myprofile" |
| 119 | |
| 120 | // Test the 'Match' command. |
| 121 | if err := cmd.Execute([]string{"match", appName, profile}); err != nil { |
| 122 | t.Fatalf("%v", err) |
| 123 | } |
| 124 | if expected, got := jsonEnv, strings.TrimSpace(stdout.String()); got != expected { |
| 125 | t.Errorf("Unexpected output from match. Got %q, expected %q", got, expected) |
| 126 | } |
| 127 | stdout.Reset() |
| 128 | |
| 129 | // Test the 'put' command. |
| 130 | f, err := ioutil.TempFile("", "test") |
| 131 | if err != nil { |
| 132 | t.Fatalf("%v", err) |
| 133 | } |
| 134 | fileName := f.Name() |
| 135 | defer os.Remove(fileName) |
| 136 | if _, err = f.Write([]byte(jsonEnv)); err != nil { |
| 137 | t.Fatalf("%v", err) |
| 138 | } |
| 139 | if err = f.Close(); err != nil { |
| 140 | t.Fatalf("%v", err) |
| 141 | } |
| 142 | if err := cmd.Execute([]string{"put", appName, profile, fileName}); err != nil { |
| 143 | t.Fatalf("%v", err) |
| 144 | } |
Jiri Simsa | 1201b6c | 2014-08-19 13:26:31 -0700 | [diff] [blame] | 145 | if expected, got := "Application envelope added successfully.", strings.TrimSpace(stdout.String()); got != expected { |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 146 | t.Errorf("Unexpected output from put. Got %q, expected %q", got, expected) |
| 147 | } |
| 148 | stdout.Reset() |
| 149 | |
| 150 | // Test the 'remove' command. |
| 151 | if err := cmd.Execute([]string{"remove", appName, profile}); err != nil { |
| 152 | t.Fatalf("%v", err) |
| 153 | } |
| 154 | if expected, got := "Application envelope removed successfully.", strings.TrimSpace(stdout.String()); got != expected { |
| 155 | t.Errorf("Unexpected output from remove. Got %q, expected %q", got, expected) |
| 156 | } |
| 157 | stdout.Reset() |
| 158 | |
| 159 | // Test the 'edit' command. (nothing changed) |
| 160 | os.Setenv("EDITOR", "true") |
| 161 | if err := cmd.Execute([]string{"edit", appName, profile}); err != nil { |
| 162 | t.Fatalf("%v", err) |
| 163 | } |
| 164 | if expected, got := "Nothing changed", strings.TrimSpace(stdout.String()); got != expected { |
| 165 | t.Errorf("Unexpected output from edit. Got %q, expected %q", got, expected) |
| 166 | } |
| 167 | stdout.Reset() |
| 168 | |
| 169 | // Test the 'edit' command. |
Jiri Simsa | fb3e24d | 2014-05-23 10:02:13 -0700 | [diff] [blame] | 170 | os.Setenv("EDITOR", "perl -pi -e 's/arg1/arg111/'") |
Robin Thellend | 32c438b | 2014-05-22 17:28:00 -0700 | [diff] [blame] | 171 | if err := cmd.Execute([]string{"edit", appName, profile}); err != nil { |
| 172 | t.Fatalf("%v", err) |
| 173 | } |
| 174 | if expected, got := "Application envelope updated successfully.", strings.TrimSpace(stdout.String()); got != expected { |
| 175 | t.Errorf("Unexpected output from edit. Got %q, expected %q", got, expected) |
| 176 | } |
| 177 | stdout.Reset() |
| 178 | } |