Robin Thellend | 18205cf | 2014-10-21 13:53:59 -0700 | [diff] [blame] | 1 | package main |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 9 | "v.io/core/veyron2" |
Matt Rosencrantz | f541b77 | 2015-01-13 07:58:59 -0800 | [diff] [blame] | 10 | "v.io/core/veyron2/context" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 11 | "v.io/core/veyron2/ipc" |
| 12 | "v.io/core/veyron2/naming" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 13 | "v.io/core/veyron2/security" |
| 14 | "v.io/core/veyron2/services/mgmt/build" |
| 15 | "v.io/core/veyron2/vlog" |
Cosmos Nicolaou | d6c3c9c | 2014-09-30 15:42:53 -0700 | [diff] [blame] | 16 | |
Ankur | 0003fdc | 2015-01-22 10:59:41 -0800 | [diff] [blame] | 17 | tsecurity "v.io/core/veyron/lib/testutil/security" |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 18 | _ "v.io/core/veyron/profiles" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 19 | "v.io/core/veyron/services/mgmt/profile" |
| 20 | "v.io/core/veyron/services/mgmt/repository" |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 21 | ) |
| 22 | |
| 23 | var ( |
| 24 | // spec is an example profile specification used throughout the test. |
| 25 | spec = profile.Specification{ |
Jiri Simsa | 2f8bc27 | 2014-07-16 12:29:15 -0700 | [diff] [blame] | 26 | Arch: build.AMD64, |
| 27 | Description: "Example profile to test the profile repository implementation.", |
| 28 | Format: build.ELF, |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 29 | Libraries: map[profile.Library]struct{}{profile.Library{Name: "foo", MajorVersion: "1", MinorVersion: "0"}: struct{}{}}, |
| 30 | Label: "example", |
Jiri Simsa | 2f8bc27 | 2014-07-16 12:29:15 -0700 | [diff] [blame] | 31 | OS: build.Linux, |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 32 | } |
| 33 | ) |
| 34 | |
| 35 | type server struct { |
| 36 | suffix string |
| 37 | } |
| 38 | |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 39 | func (s *server) Label(ipc.ServerContext) (string, error) { |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 40 | vlog.VI(2).Infof("%v.Label() was called", s.suffix) |
| 41 | if s.suffix != "exists" { |
| 42 | return "", fmt.Errorf("profile doesn't exist: %v", s.suffix) |
| 43 | } |
| 44 | return spec.Label, nil |
| 45 | } |
| 46 | |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 47 | func (s *server) Description(ipc.ServerContext) (string, error) { |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 48 | vlog.VI(2).Infof("%v.Description() was called", s.suffix) |
| 49 | if s.suffix != "exists" { |
| 50 | return "", fmt.Errorf("profile doesn't exist: %v", s.suffix) |
| 51 | } |
| 52 | return spec.Description, nil |
| 53 | } |
| 54 | |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 55 | func (s *server) Specification(ipc.ServerContext) (profile.Specification, error) { |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 56 | vlog.VI(2).Infof("%v.Specification() was called", s.suffix) |
| 57 | if s.suffix != "exists" { |
| 58 | return profile.Specification{}, fmt.Errorf("profile doesn't exist: %v", s.suffix) |
| 59 | } |
| 60 | return spec, nil |
| 61 | } |
| 62 | |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 63 | func (s *server) Put(_ ipc.ServerContext, _ profile.Specification) error { |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 64 | vlog.VI(2).Infof("%v.Put() was called", s.suffix) |
| 65 | return nil |
| 66 | } |
| 67 | |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 68 | func (s *server) Remove(ipc.ServerContext) error { |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 69 | vlog.VI(2).Infof("%v.Remove() was called", s.suffix) |
| 70 | if s.suffix != "exists" { |
| 71 | return fmt.Errorf("profile doesn't exist: %v", s.suffix) |
| 72 | } |
| 73 | return nil |
| 74 | } |
| 75 | |
| 76 | type dispatcher struct { |
| 77 | } |
| 78 | |
| 79 | func NewDispatcher() *dispatcher { |
| 80 | return &dispatcher{} |
| 81 | } |
| 82 | |
Robin Thellend | a02fe8f | 2014-11-19 09:58:29 -0800 | [diff] [blame] | 83 | func (d *dispatcher) Lookup(suffix string) (interface{}, security.Authorizer, error) { |
Cosmos Nicolaou | 710daa2 | 2014-11-11 19:39:18 -0800 | [diff] [blame] | 84 | return repository.ProfileServer(&server{suffix: suffix}), nil, nil |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Matt Rosencrantz | f541b77 | 2015-01-13 07:58:59 -0800 | [diff] [blame] | 87 | func startServer(t *testing.T, ctx *context.T) (ipc.Server, naming.Endpoint, error) { |
| 88 | server, err := veyron2.NewServer(ctx) |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 89 | if err != nil { |
| 90 | t.Errorf("NewServer failed: %v", err) |
| 91 | return nil, nil, err |
| 92 | } |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 93 | endpoints, err := server.Listen(veyron2.GetListenSpec(ctx)) |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 94 | if err != nil { |
| 95 | t.Errorf("Listen failed: %v", err) |
| 96 | return nil, nil, err |
| 97 | } |
Cosmos Nicolaou | 92dba58 | 2014-11-05 17:24:10 -0800 | [diff] [blame] | 98 | if err := server.ServeDispatcher("", NewDispatcher()); err != nil { |
| 99 | t.Errorf("ServeDispatcher failed: %v", err) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 100 | return nil, nil, err |
| 101 | } |
Cosmos Nicolaou | 28dabfc | 2014-12-15 22:51:07 -0800 | [diff] [blame] | 102 | return server, endpoints[0], nil |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | func stopServer(t *testing.T, server ipc.Server) { |
| 106 | if err := server.Stop(); err != nil { |
| 107 | t.Errorf("server.Stop failed: %v", err) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | func TestProfileClient(t *testing.T) { |
Matt Rosencrantz | a5ad272 | 2015-01-22 11:17:47 -0800 | [diff] [blame] | 112 | var shutdown veyron2.Shutdown |
| 113 | gctx, shutdown = veyron2.Init() |
| 114 | defer shutdown() |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 115 | var err error |
Matt Rosencrantz | a5ad272 | 2015-01-22 11:17:47 -0800 | [diff] [blame] | 116 | if gctx, err = veyron2.SetPrincipal(gctx, tsecurity.NewPrincipal("test-blessing")); err != nil { |
| 117 | panic(err) |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 118 | } |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 119 | |
Matt Rosencrantz | a5ad272 | 2015-01-22 11:17:47 -0800 | [diff] [blame] | 120 | server, endpoint, err := startServer(t, gctx) |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 121 | if err != nil { |
| 122 | return |
| 123 | } |
| 124 | defer stopServer(t, server) |
| 125 | // Setup the command-line. |
Robin Thellend | 18205cf | 2014-10-21 13:53:59 -0700 | [diff] [blame] | 126 | cmd := root() |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 127 | var stdout, stderr bytes.Buffer |
| 128 | cmd.Init(nil, &stdout, &stderr) |
David Why Use Two When One Will Do Presotto | adf0ca1 | 2014-11-13 10:49:01 -0800 | [diff] [blame] | 129 | exists := naming.JoinAddressName(endpoint.String(), "exists") |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 130 | |
| 131 | // Test the 'label' command. |
| 132 | if err := cmd.Execute([]string{"label", exists}); err != nil { |
| 133 | t.Fatalf("%v", err) |
| 134 | } |
| 135 | if expected, got := spec.Label, strings.TrimSpace(stdout.String()); got != expected { |
| 136 | t.Errorf("Got %q, expected %q", got, expected) |
| 137 | } |
| 138 | stdout.Reset() |
| 139 | |
| 140 | // Test the 'description' command. |
| 141 | if err := cmd.Execute([]string{"description", exists}); err != nil { |
| 142 | t.Fatalf("%v", err) |
| 143 | } |
| 144 | if expected, got := spec.Description, strings.TrimSpace(stdout.String()); got != expected { |
| 145 | t.Errorf("Got %q, expected %q", got, expected) |
| 146 | } |
| 147 | stdout.Reset() |
| 148 | |
| 149 | // Test the 'spec' command. |
Jiri Simsa | 75dda81 | 2014-12-08 15:47:19 -0800 | [diff] [blame] | 150 | if err := cmd.Execute([]string{"specification", exists}); err != nil { |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 151 | t.Fatalf("%v", err) |
| 152 | } |
| 153 | if expected, got := fmt.Sprintf("%#v", spec), strings.TrimSpace(stdout.String()); got != expected { |
| 154 | t.Errorf("Got %q, expected %q", got, expected) |
| 155 | } |
| 156 | stdout.Reset() |
| 157 | |
| 158 | // Test the 'put' command. |
| 159 | if err := cmd.Execute([]string{"put", exists}); err != nil { |
| 160 | t.Fatalf("%v", err) |
| 161 | } |
Jiri Simsa | 1023a34 | 2014-08-20 18:01:22 -0700 | [diff] [blame] | 162 | if expected, got := "Profile added successfully.", strings.TrimSpace(stdout.String()); got != expected { |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 163 | t.Errorf("Got %q, expected %q", got, expected) |
| 164 | } |
| 165 | stdout.Reset() |
| 166 | |
| 167 | // Test the 'remove' command. |
| 168 | if err := cmd.Execute([]string{"remove", exists}); err != nil { |
| 169 | t.Fatalf("%v", err) |
| 170 | } |
| 171 | if expected, got := "Profile removed successfully.", strings.TrimSpace(stdout.String()); got != expected { |
| 172 | t.Errorf("Got %q, expected %q", got, expected) |
| 173 | } |
| 174 | stdout.Reset() |
| 175 | } |