Robin Thellend | 18205cf | 2014-10-21 13:53:59 -0700 | [diff] [blame] | 1 | package main |
Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 8 | "veyron.io/veyron/veyron2" |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 9 | "veyron.io/veyron/veyron2/ipc" |
| 10 | "veyron.io/veyron/veyron2/naming" |
| 11 | "veyron.io/veyron/veyron2/rt" |
| 12 | "veyron.io/veyron/veyron2/services/mgmt/binary" |
| 13 | "veyron.io/veyron/veyron2/services/mgmt/build" |
Mike Burrows | c7a5d72 | 2014-12-09 08:55:41 -0800 | [diff] [blame] | 14 | verror "veyron.io/veyron/veyron2/verror2" |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 15 | "veyron.io/veyron/veyron2/vlog" |
Cosmos Nicolaou | d6c3c9c | 2014-09-30 15:42:53 -0700 | [diff] [blame] | 16 | |
| 17 | "veyron.io/veyron/veyron/profiles" |
Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 18 | ) |
| 19 | |
| 20 | type mock struct{} |
| 21 | |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 22 | func (mock) Build(ctx build.BuilderBuildContext, arch build.Architecture, opsys build.OperatingSystem) ([]byte, error) { |
Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 23 | vlog.VI(2).Infof("Build(%v, %v) was called", arch, opsys) |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 24 | iterator := ctx.RecvStream() |
Jiri Simsa | 1bf5bd2 | 2014-08-16 13:18:45 -0700 | [diff] [blame] | 25 | for iterator.Advance() { |
| 26 | } |
| 27 | if err := iterator.Err(); err != nil { |
| 28 | vlog.Errorf("Advance() failed: %v", err) |
Mike Burrows | c7a5d72 | 2014-12-09 08:55:41 -0800 | [diff] [blame] | 29 | return nil, verror.Make(verror.Internal, ctx) |
Jiri Simsa | 1bf5bd2 | 2014-08-16 13:18:45 -0700 | [diff] [blame] | 30 | } |
Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 31 | return nil, nil |
| 32 | } |
| 33 | |
| 34 | func (mock) Describe(_ ipc.ServerContext, name string) (binary.Description, error) { |
| 35 | vlog.VI(2).Infof("Describe(%v) was called", name) |
| 36 | return binary.Description{}, nil |
| 37 | } |
| 38 | |
| 39 | type dispatcher struct{} |
| 40 | |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 41 | func startServer(runtime veyron2.Runtime, t *testing.T) (ipc.Server, naming.Endpoint) { |
| 42 | server, err := runtime.NewServer() |
Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 43 | if err != nil { |
| 44 | t.Fatalf("NewServer failed: %v", err) |
| 45 | } |
Cosmos Nicolaou | 28dabfc | 2014-12-15 22:51:07 -0800 | [diff] [blame] | 46 | endpoints, err := server.Listen(profiles.LocalListenSpec) |
Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 47 | if err != nil { |
Cosmos Nicolaou | d6c3c9c | 2014-09-30 15:42:53 -0700 | [diff] [blame] | 48 | t.Fatalf("Listen(%s) failed: %v", profiles.LocalListenSpec, err) |
Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 49 | } |
| 50 | unpublished := "" |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 51 | if err := server.Serve(unpublished, build.BuilderServer(&mock{}), nil); err != nil { |
Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 52 | t.Fatalf("Serve(%v) failed: %v", unpublished, err) |
| 53 | } |
Cosmos Nicolaou | 28dabfc | 2014-12-15 22:51:07 -0800 | [diff] [blame] | 54 | return server, endpoints[0] |
Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | func stopServer(t *testing.T, server ipc.Server) { |
| 58 | if err := server.Stop(); err != nil { |
| 59 | t.Errorf("Stop() failed: %v", err) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestBuildClient(t *testing.T) { |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 64 | var err error |
| 65 | runtime, err = rt.New() |
| 66 | if err != nil { |
| 67 | t.Fatalf("Unexpected error initializing runtime: %s", err) |
| 68 | } |
| 69 | server, endpoint := startServer(runtime, t) |
Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 70 | defer stopServer(t, server) |
| 71 | |
Robin Thellend | 18205cf | 2014-10-21 13:53:59 -0700 | [diff] [blame] | 72 | cmd := root() |
Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 73 | var stdout, stderr bytes.Buffer |
| 74 | cmd.Init(nil, &stdout, &stderr) |
| 75 | |
| 76 | // Test the 'Build' command. |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 77 | if err := cmd.Execute([]string{"build", naming.JoinAddressName(endpoint.String(), ""), "veyron.io/veyron/veyron/tools/build"}); err != nil { |
Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 78 | t.Fatalf("%v", err) |
| 79 | } |
| 80 | if expected, got := "", strings.TrimSpace(stdout.String()); got != expected { |
| 81 | t.Errorf("Unexpected output from build: got %q, expected %q", got, expected) |
| 82 | } |
| 83 | } |