github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/swarmkit/manager/controlapi/server_test.go (about)

     1  package controlapi
     2  
     3  import (
     4  	"context"
     5  	"io/ioutil"
     6  	"net"
     7  	"os"
     8  	"testing"
     9  	"time"
    10  
    11  	"google.golang.org/grpc"
    12  
    13  	"github.com/docker/swarmkit/api"
    14  	"github.com/docker/swarmkit/ca"
    15  	cautils "github.com/docker/swarmkit/ca/testutils"
    16  	"github.com/docker/swarmkit/manager/state/store"
    17  	stateutils "github.com/docker/swarmkit/manager/state/testutils"
    18  	"github.com/stretchr/testify/assert"
    19  )
    20  
    21  type testServer struct {
    22  	Server *Server
    23  	Client api.ControlClient
    24  	Store  *store.MemoryStore
    25  
    26  	grpcServer *grpc.Server
    27  	clientConn *grpc.ClientConn
    28  
    29  	tempUnixSocket string
    30  }
    31  
    32  func (ts *testServer) Stop() {
    33  	ts.clientConn.Close()
    34  	ts.grpcServer.Stop()
    35  	ts.Store.Close()
    36  	os.RemoveAll(ts.tempUnixSocket)
    37  }
    38  
    39  func newTestServer(t *testing.T) *testServer {
    40  	ts := &testServer{}
    41  
    42  	// Create a testCA just to get a usable RootCA object
    43  	tc := cautils.NewTestCA(t)
    44  	securityConfig, err := tc.NewNodeConfig(ca.ManagerRole)
    45  	tc.Stop()
    46  	assert.NoError(t, err)
    47  
    48  	ts.Store = store.NewMemoryStore(&stateutils.MockProposer{})
    49  	assert.NotNil(t, ts.Store)
    50  
    51  	ts.Server = NewServer(ts.Store, nil, securityConfig, nil, nil)
    52  	assert.NotNil(t, ts.Server)
    53  
    54  	temp, err := ioutil.TempFile("", "test-socket")
    55  	assert.NoError(t, err)
    56  	assert.NoError(t, temp.Close())
    57  	assert.NoError(t, os.Remove(temp.Name()))
    58  
    59  	ts.tempUnixSocket = temp.Name()
    60  
    61  	lis, err := net.Listen("unix", temp.Name())
    62  	assert.NoError(t, err)
    63  
    64  	ts.grpcServer = grpc.NewServer()
    65  	api.RegisterControlServer(ts.grpcServer, ts.Server)
    66  	go func() {
    67  		// Serve will always return an error (even when properly stopped).
    68  		// Explicitly ignore it.
    69  		_ = ts.grpcServer.Serve(lis)
    70  	}()
    71  
    72  	conn, err := grpc.Dial(temp.Name(), grpc.WithInsecure(), grpc.WithTimeout(10*time.Second),
    73  		grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
    74  			return net.DialTimeout("unix", addr, timeout)
    75  		}))
    76  	assert.NoError(t, err)
    77  	ts.clientConn = conn
    78  
    79  	ts.Client = api.NewControlClient(conn)
    80  
    81  	// Create ingress network
    82  	ts.Client.CreateNetwork(context.Background(),
    83  		&api.CreateNetworkRequest{
    84  			Spec: &api.NetworkSpec{
    85  				Ingress: true,
    86  				Annotations: api.Annotations{
    87  					Name: "test-ingress",
    88  				},
    89  			},
    90  		})
    91  
    92  	return ts
    93  }