blob: 03b6bf179453f57ed7c732e9cc5f81cab0fcb422 [file] [log] [blame]
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001package rt_test
2
3import (
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -07004 "bufio"
Jiri Simsa5293dcb2014-05-10 09:56:38 -07005 "fmt"
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -07006 "io"
7 "os"
Jiri Simsa5293dcb2014-05-10 09:56:38 -07008 "syscall"
9 "testing"
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -070010 "time"
Jiri Simsa5293dcb2014-05-10 09:56:38 -070011
Cosmos Nicolaouaadfd4d2014-10-31 18:51:25 -070012 "veyron.io/veyron/veyron2"
13 "veyron.io/veyron/veyron2/config"
Asim Shankarcc044212014-10-15 23:25:26 -070014 "veyron.io/veyron/veyron2/options"
Jiri Simsa519c5072014-09-17 21:37:57 -070015 "veyron.io/veyron/veyron2/rt"
Cosmos Nicolaou39a00e02014-08-14 11:04:14 -070016
Cosmos Nicolaou39e3ae52014-11-14 13:30:01 -080017 "veyron.io/veyron/veyron/lib/appcycle"
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -070018 "veyron.io/veyron/veyron/lib/expect"
19 "veyron.io/veyron/veyron/lib/modules"
Jiri Simsa5293dcb2014-05-10 09:56:38 -070020)
21
22func init() {
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -070023 modules.RegisterChild("withRuntime", "", withRuntime)
24 modules.RegisterChild("withoutRuntime", "", withoutRuntime)
Jiri Simsa5293dcb2014-05-10 09:56:38 -070025}
26
Cosmos Nicolaouaadfd4d2014-10-31 18:51:25 -070027// A fack profile to explicitly request the Google runtime.
28type myprofile struct{}
29
30func (mp *myprofile) Name() string {
31 return "test"
32}
33
Cosmos Nicolaou4e8da642014-11-13 08:32:05 -080034func (mp *myprofile) Runtime() (string, []veyron2.ROpt) {
35 return "google", nil
Cosmos Nicolaouaadfd4d2014-10-31 18:51:25 -070036}
37
38func (mp *myprofile) Platform() *veyron2.Platform {
39 return &veyron2.Platform{"google", nil, "v1", "any", "rel1", ".2", "who knows", "this host"}
40}
41
42func (mp *myprofile) String() string {
43 return "myprofile on " + mp.Platform().String()
44}
45
Bogdan Caprita3e8f9642014-12-05 14:29:40 -080046func (mp *myprofile) Init(veyron2.Runtime, *config.Publisher) (veyron2.AppCycle, error) {
47 return appcycle.New(), nil
Cosmos Nicolaouaadfd4d2014-10-31 18:51:25 -070048}
49
Cosmos Nicolaou39e3ae52014-11-14 13:30:01 -080050func (mp *myprofile) Cleanup() {}
51
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -070052func simpleEchoProgram(stdin io.Reader, stdout io.Writer) {
53 fmt.Fprintf(stdout, "ready\n")
54 scanner := bufio.NewScanner(stdin)
55 if scanner.Scan() {
56 fmt.Fprintf(stdout, "%s\n", scanner.Text())
57 }
58 modules.WaitForEOF(stdin)
Jiri Simsa5293dcb2014-05-10 09:56:38 -070059}
60
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -070061func withRuntime(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
Cosmos Nicolaou39a00e02014-08-14 11:04:14 -070062 // Make sure that we use "google" runtime implementation in this
63 // package even though we have to use the public API which supports
64 // arbitrary runtime implementations.
Matt Rosencrantz3df85842014-12-04 16:10:45 -080065 r, err := rt.New(options.Profile{&myprofile{}})
66 if err != nil {
67 return err
68 }
69 defer r.Cleanup()
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -070070 simpleEchoProgram(stdin, stdout)
71 return nil
Jiri Simsa5293dcb2014-05-10 09:56:38 -070072}
73
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -070074func withoutRuntime(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
75 simpleEchoProgram(stdin, stdout)
76 return nil
Jiri Simsa5293dcb2014-05-10 09:56:38 -070077}
78
79func TestWithRuntime(t *testing.T) {
Cosmos Nicolaou344cc4a2014-11-26 15:38:43 -080080 sh, err := modules.NewShell(nil)
81 if err != nil {
82 t.Fatalf("unexpected error: %s", err)
83 }
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -070084 defer sh.Cleanup(os.Stderr, os.Stderr)
Cosmos Nicolaou612ad382014-10-29 19:41:35 -070085 h, err := sh.Start("withRuntime", nil)
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -070086 if err != nil {
87 t.Fatalf("unexpected error: %s", err)
88 }
89 defer h.Shutdown(os.Stderr, os.Stderr)
90 s := expect.NewSession(t, h.Stdout(), time.Minute)
91 s.Expect("ready")
92 syscall.Kill(h.Pid(), syscall.SIGHUP)
93 h.Stdin().Write([]byte("foo\n"))
94 s.Expect("foo")
95 h.CloseStdin()
96 s.ExpectEOF()
Jiri Simsa5293dcb2014-05-10 09:56:38 -070097}
98
99func TestWithoutRuntime(t *testing.T) {
Cosmos Nicolaou344cc4a2014-11-26 15:38:43 -0800100 sh, err := modules.NewShell(nil)
101 if err != nil {
102 t.Fatalf("unexpected error: %s", err)
103 }
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -0700104 defer sh.Cleanup(os.Stderr, os.Stderr)
Cosmos Nicolaou612ad382014-10-29 19:41:35 -0700105 h, err := sh.Start("withoutRuntime", nil)
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -0700106 if err != nil {
107 t.Fatalf("unexpected error: %s", err)
108 }
109 defer h.Shutdown(os.Stderr, os.Stderr)
110 s := expect.NewSession(t, h.Stdout(), time.Minute)
111 s.Expect("ready")
112 syscall.Kill(h.Pid(), syscall.SIGHUP)
113 s.ExpectEOF()
114 err = h.Shutdown(os.Stderr, os.Stderr)
115 want := "exit status 2"
116 if err == nil || err.Error() != want {
117 t.Errorf("got %s, want %s", err, want)
118
119 }
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700120}