github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/util/testhelpers/rpcserver/server.go (about)

     1  package rpcserver
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"log"
     7  	"net"
     8  	"net/rpc"
     9  	"os"
    10  	"strconv"
    11  
    12  	"code.cloudfoundry.org/cli/plugin"
    13  	"code.cloudfoundry.org/cli/plugin/models"
    14  )
    15  
    16  //go:generate counterfeiter . Handlers
    17  
    18  type Handlers interface {
    19  	IsMinCliVersion(args string, retVal *bool) error
    20  	SetPluginMetadata(pluginMetadata plugin.PluginMetadata, retVal *bool) error
    21  	DisableTerminalOutput(disable bool, retVal *bool) error
    22  	CallCoreCommand(args []string, retVal *bool) error
    23  	GetOutputAndReset(args bool, retVal *[]string) error
    24  	GetCurrentOrg(args string, retVal *plugin_models.Organization) error
    25  	GetCurrentSpace(args string, retVal *plugin_models.Space) error
    26  	Username(args string, retVal *string) error
    27  	UserGuid(args string, retVal *string) error
    28  	UserEmail(args string, retVal *string) error
    29  	IsLoggedIn(args string, retVal *bool) error
    30  	IsSSLDisabled(args string, retVal *bool) error
    31  	HasOrganization(args string, retVal *bool) error
    32  	HasSpace(args string, retVal *bool) error
    33  	ApiEndpoint(args string, retVal *string) error
    34  	HasAPIEndpoint(args string, retVal *bool) error
    35  	ApiVersion(args string, retVal *string) error
    36  	LoggregatorEndpoint(args string, retVal *string) error
    37  	DopplerEndpoint(args string, retVal *string) error
    38  	AccessToken(args string, retVal *string) error
    39  	GetApp(appName string, retVal *plugin_models.GetAppModel) error
    40  	GetApps(args string, retVal *[]plugin_models.GetAppsModel) error
    41  	GetOrgs(args string, retVal *[]plugin_models.GetOrgs_Model) error
    42  	GetSpaces(args string, retVal *[]plugin_models.GetSpaces_Model) error
    43  	GetServices(args string, retVal *[]plugin_models.GetServices_Model) error
    44  	GetOrgUsers(args []string, retVal *[]plugin_models.GetOrgUsers_Model) error
    45  	GetSpaceUsers(args []string, retVal *[]plugin_models.GetSpaceUsers_Model) error
    46  	GetOrg(orgName string, retVal *plugin_models.GetOrg_Model) error
    47  	GetSpace(spaceName string, retVal *plugin_models.GetSpace_Model) error
    48  	GetService(serviceInstance string, retVal *plugin_models.GetService_Model) error
    49  }
    50  
    51  type TestServer struct {
    52  	listener net.Listener
    53  	Handlers Handlers
    54  	stopCh   chan struct{}
    55  	server   *rpc.Server
    56  }
    57  
    58  func NewTestRPCServer(handlers Handlers) (*TestServer, error) {
    59  	ts := &TestServer{
    60  		Handlers: handlers,
    61  	}
    62  
    63  	// discard the warning about non-rpc method in counterfeiter fakes module
    64  	log.SetOutput(ioutil.Discard)
    65  	defer log.SetOutput(os.Stdout)
    66  
    67  	server := rpc.NewServer()
    68  	err := server.RegisterName("CliRpcCmd", ts.Handlers)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  
    73  	ts.server = server
    74  	return ts, nil
    75  }
    76  
    77  func (ts *TestServer) Stop() {
    78  	close(ts.stopCh)
    79  	ts.listener.Close()
    80  }
    81  
    82  func (ts *TestServer) Port() string {
    83  	return strconv.Itoa(ts.listener.Addr().(*net.TCPAddr).Port)
    84  }
    85  
    86  func (ts *TestServer) Start() error {
    87  	var err error
    88  
    89  	ts.stopCh = make(chan struct{})
    90  
    91  	ts.listener, err = net.Listen("tcp", "127.0.0.1:0")
    92  	if err != nil {
    93  		return err
    94  	}
    95  
    96  	go func() {
    97  		for {
    98  			conn, err := ts.listener.Accept()
    99  			if err != nil {
   100  				select {
   101  				case <-ts.stopCh:
   102  					return
   103  				default:
   104  					fmt.Println(err)
   105  				}
   106  			} else {
   107  				go ts.server.ServeConn(conn)
   108  			}
   109  		}
   110  	}()
   111  
   112  	return nil
   113  }