func (rig *testRig) ServeNewShim(token string) *shimRig {
	shimConn := rig.ServeNewClient()
	shim := newShimRig(rig.t, shimConn, token)
	err := shim.connect()
	assert.Nil(rig.t, err)
	return shim
}
ServeNewShim is a specialization of ServerNewClient that returns a mock shim
already connected to the proxy.
package main
import (
	"encoding/json"
	"net"
	"os"
	"strings"
	"sync"
	"syscall"
	"testing"
	"time"
	"github.com/clearcontainers/proxy/api"
	goapi "github.com/clearcontainers/proxy/client"
	"github.com/containers/virtcontainers/pkg/hyperstart"
	"github.com/containers/virtcontainers/pkg/hyperstart/mock"
	"github.com/sirupsen/logrus"
	"github.com/sirupsen/logrus/hooks/test"
	"github.com/stretchr/testify/assert"
)
const unixURLPrefix = "unix:"
type testRig struct {
	t  *testing.T
	wg sync.WaitGroup
	runHyperstart bool
	Hyperstart    *mock.Hyperstart
	proxy      *proxy
	protocol   *protocol
	proxyConns []net.Conn  
	Client *goapi.Client
	detector          *FdLeakDetector
	startFds, stopFds *FdSnapshot
}
func newTestRig(t *testing.T) *testRig {
	proto := newProtocol()
	proto.HandleCommand(api.CmdRegisterVM, registerVM)
	proto.HandleCommand(api.CmdAttachVM, attachVM)
	proto.HandleCommand(api.CmdUnregisterVM, unregisterVM)
	proto.HandleCommand(api.CmdHyper, hyper)
	proto.HandleCommand(api.CmdConnectShim, connectShim)
	proto.HandleCommand(api.CmdDisconnectShim, disconnectShim)
	proto.HandleCommand(api.CmdSignal, cmdSignal)
	proto.HandleStream(api.StreamStdin, forwardStdin)
	proto.HandleStream(api.StreamLog, handleLogEntry)
	return &testRig{
		t:             t,
		protocol:      proto,
		proxy:         newProxy(),
		runHyperstart: true,
		detector:      NewFdLeadDetector(),
	}
}
func (rig *testRig) SetRunHyperstart(run bool) {
	rig.runHyperstart = run
}
func (rig *testRig) Start() {
	var err error
	rig.startFds, err = rig.detector.Snapshot()
	assert.Nil(rig.t, err)
	if rig.runHyperstart {
		rig.Hyperstart = mock.NewHyperstart(rig.t)
		rig.Hyperstart.Start()
		rig.wg.Add(1)
		go func() {
			rig.Hyperstart.SendMessage(int(hyperstart.ReadyCode), []byte{})
			rig.wg.Done()
		}()
	}
	clientConn := rig.ServeNewClient()
	rig.Client = goapi.NewClient(clientConn)
}
func (rig *testRig) Stop() {
	var err error
	rig.Client.Close()
	for _, conn := range rig.proxyConns {
		conn.Close()
	}
	if rig.runHyperstart {
		rig.Hyperstart.Stop()
	}
	rig.wg.Wait()
	if rig.proxy != nil {
		rig.proxy.wg.Wait()
	}
	rig.stopFds, err = rig.detector.Snapshot()
	assert.Nil(rig.t, err)
	assert.True(rig.t,
		rig.detector.Compare(os.Stdout, rig.startFds, rig.stopFds))
}
func SyncWithProxy(client *goapi.Client) {
	_, _ = client.AttachVM("non-existing-id", nil)
}
func (rig *testRig) RegisterVM() (token string) {
	ctlSocketPath, ioSocketPath := rig.Hyperstart.GetSocketPaths()
	ret, err := rig.Client.RegisterVM(testContainerID, ctlSocketPath, ioSocketPath,
		&goapi.RegisterVMOptions{NumIOStreams: 1})
	assert.Nil(rig.t, err)
	assert.Equal(rig.t, 1, len(ret.IO.Tokens))
	return ret.IO.Tokens[0]
}
func (rig *testRig) ServeNewClient() net.Conn {
	clientConn, proxyConn, err := Socketpair()
	assert.Nil(rig.t, err)
	rig.proxyConns = append(rig.proxyConns, proxyConn)
	rig.wg.Add(1)
	go func() {
		rig.proxy.serveNewClient(rig.protocol, proxyConn)
		rig.wg.Done()
	}()
	return clientConn
}