| package tls_test |
| |
| import ( |
| "bytes" |
| "context" |
| "fmt" |
| "io" |
| |
| "go.chromium.org/chromiumos/infra/proto/go/tls" |
| "google.golang.org/grpc" |
| ) |
| |
| func ExampleCommonClient_ExecDutCommand() { |
| // The RTD will receive this in its invocation spec. |
| var ( |
| addr string |
| dutName string |
| ) |
| conn, err := grpc.Dial(addr) |
| if err != nil { |
| panic(err) |
| } |
| defer conn.Close() |
| c := tls.NewCommonClient(conn) |
| ctx := context.Background() |
| req := tls.ExecDutCommandRequest{ |
| Name: dutName, |
| Command: "echo", |
| Args: []string{"hello world"}, |
| } |
| stream, err := c.ExecDutCommand(ctx, &req) |
| if err != nil { |
| panic("RPC error") |
| } |
| // Handle stream in various ways. |
| _ = stream |
| } |
| |
| func ExampleCommonClient_ExecDutCommand_CheckSuccess() { |
| // stream from ExecDutCommand, |
| // See example for CommonClient.ExecDutCommand. |
| var stream tls.Common_ExecDutCommandClient |
| var exitInfo *tls.ExecDutCommandResponse_ExitInfo |
| readStream: |
| for { |
| resp, err := stream.Recv() |
| switch err { |
| case nil: |
| if v := resp.GetExitInfo(); v != nil { |
| exitInfo = v |
| } |
| case io.EOF: |
| break readStream |
| default: |
| panic("RPC error") |
| } |
| } |
| if exitInfo.GetStatus() != 0 { |
| panic("command failed") |
| } |
| } |
| |
| func ExampleCommonClient_ExecDutCommand_CheckSpecificStatus() { |
| // stream from ExecDutCommand, |
| // See example for CommonClient.ExecDutCommand. |
| var stream tls.Common_ExecDutCommandClient |
| var exitInfo *tls.ExecDutCommandResponse_ExitInfo |
| readStream: |
| for { |
| resp, err := stream.Recv() |
| switch err { |
| case nil: |
| if v := resp.GetExitInfo(); v != nil { |
| exitInfo = v |
| } |
| case io.EOF: |
| break readStream |
| default: |
| panic("RPC error") |
| } |
| } |
| switch { |
| case !exitInfo.GetStarted(): |
| fmt.Printf("process not started: %v", exitInfo.GetErrorMessage()) |
| case exitInfo.GetSignaled(): |
| fmt.Printf("process received signal %v", exitInfo.GetStatus()) |
| default: |
| fmt.Printf("process exited with %v", exitInfo.GetStatus()) |
| } |
| } |
| |
| func ExampleCommonClient_ExecDutCommand_ReadOutput() { |
| // Stream from ExecDutCommand. |
| // See example for CommonClient.ExecDutCommand. |
| var stream tls.Common_ExecDutCommandClient |
| var ( |
| stdout bytes.Buffer |
| stderr bytes.Buffer |
| ) |
| readStream: |
| for { |
| resp, err := stream.Recv() |
| switch err { |
| case nil: |
| stdout.Write(resp.Stdout) |
| stderr.Write(resp.Stderr) |
| case io.EOF: |
| break readStream |
| default: |
| panic("RPC error") |
| } |
| } |
| } |