github.com/lazyboychen7/engine@v17.12.1-ce-rc2+incompatible/integration/util/request/client.go (about)

     1  package request
     2  
     3  import (
     4  	"net"
     5  	"net/http"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/docker/docker/api"
    10  	"github.com/docker/docker/client"
    11  	"github.com/docker/go-connections/sockets"
    12  	"github.com/docker/go-connections/tlsconfig"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  // NewAPIClient returns a docker API client configured from environment variables
    17  func NewAPIClient(t *testing.T) client.APIClient {
    18  	clt, err := client.NewEnvClient()
    19  	require.NoError(t, err)
    20  	return clt
    21  }
    22  
    23  // NewTLSAPIClient returns a docker API client configured with the
    24  // provided TLS settings
    25  func NewTLSAPIClient(t *testing.T, host, cacertPath, certPath, keyPath string) (client.APIClient, error) {
    26  	opts := tlsconfig.Options{
    27  		CAFile:             cacertPath,
    28  		CertFile:           certPath,
    29  		KeyFile:            keyPath,
    30  		ExclusiveRootPools: true,
    31  	}
    32  	config, err := tlsconfig.Client(opts)
    33  	require.Nil(t, err)
    34  	tr := &http.Transport{
    35  		TLSClientConfig: config,
    36  		DialContext: (&net.Dialer{
    37  			KeepAlive: 30 * time.Second,
    38  			Timeout:   30 * time.Second,
    39  		}).DialContext,
    40  	}
    41  	proto, addr, _, err := client.ParseHost(host)
    42  	require.Nil(t, err)
    43  
    44  	sockets.ConfigureTransport(tr, proto, addr)
    45  
    46  	httpClient := &http.Client{
    47  		Transport:     tr,
    48  		CheckRedirect: client.CheckRedirect,
    49  	}
    50  	verStr := api.DefaultVersion
    51  	customHeaders := map[string]string{}
    52  	return client.NewClient(host, verStr, httpClient, customHeaders)
    53  }