github.com/grailbio/base@v0.0.11/cmd/ticket-server/testutil/util.go (about)

     1  package testutil
     2  
     3  import (
     4  	"fmt"
     5  	"os/exec"
     6  	"strings"
     7  	"testing"
     8  
     9  	assert "github.com/stretchr/testify/assert"
    10  
    11  	v23 "v.io/v23"
    12  	v23context "v.io/v23/context"
    13  	"v.io/v23/naming"
    14  	"v.io/v23/rpc"
    15  	"v.io/v23/security"
    16  )
    17  
    18  // RunBlesserServer runs a test v23 server, returns a context and an endpoint
    19  func RunBlesserServer(ctx *v23context.T, t *testing.T, stub interface{}) (*v23context.T, naming.Endpoint) {
    20  	ctx = v23.WithListenSpec(ctx, rpc.ListenSpec{
    21  		Addrs: rpc.ListenAddrs{{"tcp", "localhost:0"}},
    22  	})
    23  	ctx, blesserServer, err := v23.WithNewServer(ctx, "", stub, security.AllowEveryone())
    24  	assert.NoError(t, err)
    25  	blesserEndpoints := blesserServer.Status().Endpoints
    26  	assert.Equal(t, 1, len(blesserEndpoints))
    27  	return ctx, blesserEndpoints[0]
    28  }
    29  
    30  // RunAndCapture runs a command and captures the stdout and stderr
    31  func RunAndCapture(t *testing.T, cmd *exec.Cmd) (stdout, stderr string) {
    32  	var stdoutBuf, stderrBuf strings.Builder
    33  	cmd.Stdout = &stdoutBuf
    34  	cmd.Stderr = &stderrBuf
    35  	err := cmd.Run()
    36  	stdout, stderr = stdoutBuf.String(), stderrBuf.String()
    37  	assert.NoError(t, err, fmt.Sprintf("stdout: '%s', stderr: '%s'", stdout, stderr))
    38  	return
    39  }