blob: 76e1a13a76caad0e3bf7897924f83b6e44eef5d1 [file] [log] [blame]
Robin Thellend18205cf2014-10-21 13:53:59 -07001package main
Jiri Simsa9d22b7d2014-08-05 14:10:22 -07002
3import (
4 "bytes"
5 "strings"
6 "testing"
7
Jiri Simsa764efb72014-12-25 20:57:03 -08008 "v.io/core/veyron2"
Matt Rosencrantzf541b772015-01-13 07:58:59 -08009 "v.io/core/veyron2/context"
Jiri Simsa764efb72014-12-25 20:57:03 -080010 "v.io/core/veyron2/ipc"
11 "v.io/core/veyron2/naming"
Jiri Simsa764efb72014-12-25 20:57:03 -080012 "v.io/core/veyron2/services/mgmt/binary"
13 "v.io/core/veyron2/services/mgmt/build"
14 verror "v.io/core/veyron2/verror2"
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"
Jiri Simsa764efb72014-12-25 20:57:03 -080018 "v.io/core/veyron/profiles"
Jiri Simsa9d22b7d2014-08-05 14:10:22 -070019)
20
21type mock struct{}
22
Todd Wang702385a2014-11-07 01:54:08 -080023func (mock) Build(ctx build.BuilderBuildContext, arch build.Architecture, opsys build.OperatingSystem) ([]byte, error) {
Jiri Simsa9d22b7d2014-08-05 14:10:22 -070024 vlog.VI(2).Infof("Build(%v, %v) was called", arch, opsys)
Todd Wang702385a2014-11-07 01:54:08 -080025 iterator := ctx.RecvStream()
Jiri Simsa1bf5bd22014-08-16 13:18:45 -070026 for iterator.Advance() {
27 }
28 if err := iterator.Err(); err != nil {
29 vlog.Errorf("Advance() failed: %v", err)
Matt Rosencrantz8f9fca12014-12-19 14:02:31 -080030 return nil, verror.Make(verror.Internal, ctx.Context())
Jiri Simsa1bf5bd22014-08-16 13:18:45 -070031 }
Jiri Simsa9d22b7d2014-08-05 14:10:22 -070032 return nil, nil
33}
34
35func (mock) Describe(_ ipc.ServerContext, name string) (binary.Description, error) {
36 vlog.VI(2).Infof("Describe(%v) was called", name)
37 return binary.Description{}, nil
38}
39
40type dispatcher struct{}
41
Matt Rosencrantzf541b772015-01-13 07:58:59 -080042func startServer(ctx *context.T, t *testing.T) (ipc.Server, naming.Endpoint) {
43 server, err := veyron2.NewServer(ctx)
Jiri Simsa9d22b7d2014-08-05 14:10:22 -070044 if err != nil {
45 t.Fatalf("NewServer failed: %v", err)
46 }
Cosmos Nicolaou28dabfc2014-12-15 22:51:07 -080047 endpoints, err := server.Listen(profiles.LocalListenSpec)
Jiri Simsa9d22b7d2014-08-05 14:10:22 -070048 if err != nil {
Cosmos Nicolaoud6c3c9c2014-09-30 15:42:53 -070049 t.Fatalf("Listen(%s) failed: %v", profiles.LocalListenSpec, err)
Jiri Simsa9d22b7d2014-08-05 14:10:22 -070050 }
51 unpublished := ""
Todd Wang702385a2014-11-07 01:54:08 -080052 if err := server.Serve(unpublished, build.BuilderServer(&mock{}), nil); err != nil {
Jiri Simsa9d22b7d2014-08-05 14:10:22 -070053 t.Fatalf("Serve(%v) failed: %v", unpublished, err)
54 }
Cosmos Nicolaou28dabfc2014-12-15 22:51:07 -080055 return server, endpoints[0]
Jiri Simsa9d22b7d2014-08-05 14:10:22 -070056}
57
58func stopServer(t *testing.T, server ipc.Server) {
59 if err := server.Stop(); err != nil {
60 t.Errorf("Stop() failed: %v", err)
61 }
62}
63
64func TestBuildClient(t *testing.T) {
Matt Rosencrantza5ad2722015-01-22 11:17:47 -080065 var shutdown veyron2.Shutdown
66 gctx, shutdown = veyron2.Init()
67 defer shutdown()
Matt Rosencrantzc2ed03e2014-11-25 15:40:48 -080068 var err error
Matt Rosencrantza5ad2722015-01-22 11:17:47 -080069 if gctx, err = veyron2.SetPrincipal(gctx, tsecurity.NewPrincipal("test-blessing")); err != nil {
70 panic(err)
Matt Rosencrantzc2ed03e2014-11-25 15:40:48 -080071 }
Matt Rosencrantza5ad2722015-01-22 11:17:47 -080072
73 server, endpoint := startServer(gctx, t)
Jiri Simsa9d22b7d2014-08-05 14:10:22 -070074 defer stopServer(t, server)
75
Robin Thellend18205cf2014-10-21 13:53:59 -070076 cmd := root()
Jiri Simsa9d22b7d2014-08-05 14:10:22 -070077 var stdout, stderr bytes.Buffer
78 cmd.Init(nil, &stdout, &stderr)
79
80 // Test the 'Build' command.
Jiri Simsa764efb72014-12-25 20:57:03 -080081 if err := cmd.Execute([]string{"build", naming.JoinAddressName(endpoint.String(), ""), "v.io/core/veyron/tools/build"}); err != nil {
Jiri Simsa9d22b7d2014-08-05 14:10:22 -070082 t.Fatalf("%v", err)
83 }
84 if expected, got := "", strings.TrimSpace(stdout.String()); got != expected {
85 t.Errorf("Unexpected output from build: got %q, expected %q", got, expected)
86 }
87}