Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1 | package rt_test |
| 2 | |
| 3 | import ( |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 4 | "bufio" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 5 | "fmt" |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 6 | "io" |
| 7 | "os" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 8 | "syscall" |
| 9 | "testing" |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 10 | "time" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 11 | |
Cosmos Nicolaou | aadfd4d | 2014-10-31 18:51:25 -0700 | [diff] [blame] | 12 | "veyron.io/veyron/veyron2" |
| 13 | "veyron.io/veyron/veyron2/config" |
Asim Shankar | cc04421 | 2014-10-15 23:25:26 -0700 | [diff] [blame] | 14 | "veyron.io/veyron/veyron2/options" |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 15 | "veyron.io/veyron/veyron2/rt" |
Cosmos Nicolaou | 39a00e0 | 2014-08-14 11:04:14 -0700 | [diff] [blame] | 16 | |
Cosmos Nicolaou | 39e3ae5 | 2014-11-14 13:30:01 -0800 | [diff] [blame] | 17 | "veyron.io/veyron/veyron/lib/appcycle" |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 18 | "veyron.io/veyron/veyron/lib/expect" |
| 19 | "veyron.io/veyron/veyron/lib/modules" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | func init() { |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 23 | modules.RegisterChild("withRuntime", "", withRuntime) |
| 24 | modules.RegisterChild("withoutRuntime", "", withoutRuntime) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 25 | } |
| 26 | |
Cosmos Nicolaou | aadfd4d | 2014-10-31 18:51:25 -0700 | [diff] [blame] | 27 | // A fack profile to explicitly request the Google runtime. |
| 28 | type myprofile struct{} |
| 29 | |
| 30 | func (mp *myprofile) Name() string { |
| 31 | return "test" |
| 32 | } |
| 33 | |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 34 | func (mp *myprofile) Runtime() (string, []veyron2.ROpt) { |
| 35 | return "google", nil |
Cosmos Nicolaou | aadfd4d | 2014-10-31 18:51:25 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | func (mp *myprofile) Platform() *veyron2.Platform { |
| 39 | return &veyron2.Platform{"google", nil, "v1", "any", "rel1", ".2", "who knows", "this host"} |
| 40 | } |
| 41 | |
| 42 | func (mp *myprofile) String() string { |
| 43 | return "myprofile on " + mp.Platform().String() |
| 44 | } |
| 45 | |
Bogdan Caprita | 3e8f964 | 2014-12-05 14:29:40 -0800 | [diff] [blame^] | 46 | func (mp *myprofile) Init(veyron2.Runtime, *config.Publisher) (veyron2.AppCycle, error) { |
| 47 | return appcycle.New(), nil |
Cosmos Nicolaou | aadfd4d | 2014-10-31 18:51:25 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Cosmos Nicolaou | 39e3ae5 | 2014-11-14 13:30:01 -0800 | [diff] [blame] | 50 | func (mp *myprofile) Cleanup() {} |
| 51 | |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 52 | func 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 Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 61 | func withRuntime(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error { |
Cosmos Nicolaou | 39a00e0 | 2014-08-14 11:04:14 -0700 | [diff] [blame] | 62 | // 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 Rosencrantz | 3df8584 | 2014-12-04 16:10:45 -0800 | [diff] [blame] | 65 | r, err := rt.New(options.Profile{&myprofile{}}) |
| 66 | if err != nil { |
| 67 | return err |
| 68 | } |
| 69 | defer r.Cleanup() |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 70 | simpleEchoProgram(stdin, stdout) |
| 71 | return nil |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 74 | func withoutRuntime(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error { |
| 75 | simpleEchoProgram(stdin, stdout) |
| 76 | return nil |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | func TestWithRuntime(t *testing.T) { |
Cosmos Nicolaou | 344cc4a | 2014-11-26 15:38:43 -0800 | [diff] [blame] | 80 | sh, err := modules.NewShell(nil) |
| 81 | if err != nil { |
| 82 | t.Fatalf("unexpected error: %s", err) |
| 83 | } |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 84 | defer sh.Cleanup(os.Stderr, os.Stderr) |
Cosmos Nicolaou | 612ad38 | 2014-10-29 19:41:35 -0700 | [diff] [blame] | 85 | h, err := sh.Start("withRuntime", nil) |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 86 | 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 Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | func TestWithoutRuntime(t *testing.T) { |
Cosmos Nicolaou | 344cc4a | 2014-11-26 15:38:43 -0800 | [diff] [blame] | 100 | sh, err := modules.NewShell(nil) |
| 101 | if err != nil { |
| 102 | t.Fatalf("unexpected error: %s", err) |
| 103 | } |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 104 | defer sh.Cleanup(os.Stderr, os.Stderr) |
Cosmos Nicolaou | 612ad38 | 2014-10-29 19:41:35 -0700 | [diff] [blame] | 105 | h, err := sh.Start("withoutRuntime", nil) |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 106 | 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 Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 120 | } |