blob: 25de7716e3fba2162e199cc12e1fe3498ffdc401 [file] [log] [blame]
Robin Thellend18205cf2014-10-21 13:53:59 -07001package main
Robin Thellend9259f3b2014-05-21 10:07:24 -07002
3import (
4 "bytes"
5 "fmt"
6 "strings"
7 "testing"
8
Jiri Simsa764efb72014-12-25 20:57:03 -08009 "v.io/core/veyron2"
Matt Rosencrantzf541b772015-01-13 07:58:59 -080010 "v.io/core/veyron2/context"
Jiri Simsa764efb72014-12-25 20:57:03 -080011 "v.io/core/veyron2/ipc"
12 "v.io/core/veyron2/naming"
Jiri Simsa764efb72014-12-25 20:57:03 -080013 "v.io/core/veyron2/security"
14 "v.io/core/veyron2/services/mgmt/build"
15 "v.io/core/veyron2/vlog"
Cosmos Nicolaoud6c3c9c2014-09-30 15:42:53 -070016
Ankur0003fdc2015-01-22 10:59:41 -080017 tsecurity "v.io/core/veyron/lib/testutil/security"
Suharsh Sivakumar19fbf992015-01-23 11:02:27 -080018 _ "v.io/core/veyron/profiles"
Jiri Simsa764efb72014-12-25 20:57:03 -080019 "v.io/core/veyron/services/mgmt/profile"
20 "v.io/core/veyron/services/mgmt/repository"
Robin Thellend9259f3b2014-05-21 10:07:24 -070021)
22
23var (
24 // spec is an example profile specification used throughout the test.
25 spec = profile.Specification{
Jiri Simsa2f8bc272014-07-16 12:29:15 -070026 Arch: build.AMD64,
27 Description: "Example profile to test the profile repository implementation.",
28 Format: build.ELF,
Robin Thellend9259f3b2014-05-21 10:07:24 -070029 Libraries: map[profile.Library]struct{}{profile.Library{Name: "foo", MajorVersion: "1", MinorVersion: "0"}: struct{}{}},
30 Label: "example",
Jiri Simsa2f8bc272014-07-16 12:29:15 -070031 OS: build.Linux,
Robin Thellend9259f3b2014-05-21 10:07:24 -070032 }
33)
34
35type server struct {
36 suffix string
37}
38
Matt Rosencrantzf5afcaf2014-06-02 11:31:22 -070039func (s *server) Label(ipc.ServerContext) (string, error) {
Robin Thellend9259f3b2014-05-21 10:07:24 -070040 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 Rosencrantzf5afcaf2014-06-02 11:31:22 -070047func (s *server) Description(ipc.ServerContext) (string, error) {
Robin Thellend9259f3b2014-05-21 10:07:24 -070048 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 Rosencrantzf5afcaf2014-06-02 11:31:22 -070055func (s *server) Specification(ipc.ServerContext) (profile.Specification, error) {
Robin Thellend9259f3b2014-05-21 10:07:24 -070056 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 Rosencrantzf5afcaf2014-06-02 11:31:22 -070063func (s *server) Put(_ ipc.ServerContext, _ profile.Specification) error {
Robin Thellend9259f3b2014-05-21 10:07:24 -070064 vlog.VI(2).Infof("%v.Put() was called", s.suffix)
65 return nil
66}
67
Matt Rosencrantzf5afcaf2014-06-02 11:31:22 -070068func (s *server) Remove(ipc.ServerContext) error {
Robin Thellend9259f3b2014-05-21 10:07:24 -070069 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
76type dispatcher struct {
77}
78
79func NewDispatcher() *dispatcher {
80 return &dispatcher{}
81}
82
Robin Thellenda02fe8f2014-11-19 09:58:29 -080083func (d *dispatcher) Lookup(suffix string) (interface{}, security.Authorizer, error) {
Cosmos Nicolaou710daa22014-11-11 19:39:18 -080084 return repository.ProfileServer(&server{suffix: suffix}), nil, nil
Robin Thellend9259f3b2014-05-21 10:07:24 -070085}
86
Matt Rosencrantzf541b772015-01-13 07:58:59 -080087func startServer(t *testing.T, ctx *context.T) (ipc.Server, naming.Endpoint, error) {
88 server, err := veyron2.NewServer(ctx)
Robin Thellend9259f3b2014-05-21 10:07:24 -070089 if err != nil {
90 t.Errorf("NewServer failed: %v", err)
91 return nil, nil, err
92 }
Suharsh Sivakumar19fbf992015-01-23 11:02:27 -080093 endpoints, err := server.Listen(veyron2.GetListenSpec(ctx))
Robin Thellend9259f3b2014-05-21 10:07:24 -070094 if err != nil {
95 t.Errorf("Listen failed: %v", err)
96 return nil, nil, err
97 }
Cosmos Nicolaou92dba582014-11-05 17:24:10 -080098 if err := server.ServeDispatcher("", NewDispatcher()); err != nil {
99 t.Errorf("ServeDispatcher failed: %v", err)
Cosmos Nicolaoufdc838b2014-06-30 21:44:27 -0700100 return nil, nil, err
101 }
Cosmos Nicolaou28dabfc2014-12-15 22:51:07 -0800102 return server, endpoints[0], nil
Robin Thellend9259f3b2014-05-21 10:07:24 -0700103}
104
105func 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
111func TestProfileClient(t *testing.T) {
Matt Rosencrantza5ad2722015-01-22 11:17:47 -0800112 var shutdown veyron2.Shutdown
113 gctx, shutdown = veyron2.Init()
114 defer shutdown()
Matt Rosencrantzc2ed03e2014-11-25 15:40:48 -0800115 var err error
Matt Rosencrantza5ad2722015-01-22 11:17:47 -0800116 if gctx, err = veyron2.SetPrincipal(gctx, tsecurity.NewPrincipal("test-blessing")); err != nil {
117 panic(err)
Matt Rosencrantzc2ed03e2014-11-25 15:40:48 -0800118 }
Matt Rosencrantzc2ed03e2014-11-25 15:40:48 -0800119
Matt Rosencrantza5ad2722015-01-22 11:17:47 -0800120 server, endpoint, err := startServer(t, gctx)
Robin Thellend9259f3b2014-05-21 10:07:24 -0700121 if err != nil {
122 return
123 }
124 defer stopServer(t, server)
125 // Setup the command-line.
Robin Thellend18205cf2014-10-21 13:53:59 -0700126 cmd := root()
Robin Thellend9259f3b2014-05-21 10:07:24 -0700127 var stdout, stderr bytes.Buffer
128 cmd.Init(nil, &stdout, &stderr)
David Why Use Two When One Will Do Presottoadf0ca12014-11-13 10:49:01 -0800129 exists := naming.JoinAddressName(endpoint.String(), "exists")
Robin Thellend9259f3b2014-05-21 10:07:24 -0700130
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 Simsa75dda812014-12-08 15:47:19 -0800150 if err := cmd.Execute([]string{"specification", exists}); err != nil {
Robin Thellend9259f3b2014-05-21 10:07:24 -0700151 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 Simsa1023a342014-08-20 18:01:22 -0700162 if expected, got := "Profile added successfully.", strings.TrimSpace(stdout.String()); got != expected {
Robin Thellend9259f3b2014-05-21 10:07:24 -0700163 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}