|
@@ -1,9 +1,9 @@
|
|
|
-package request // import "github.com/docker/docker/integration-cli/request"
|
|
|
+package request // import "github.com/docker/docker/internal/test/request"
|
|
|
|
|
|
import (
|
|
|
- "bytes"
|
|
|
+ "context"
|
|
|
"crypto/tls"
|
|
|
- "encoding/json"
|
|
|
+ "fmt"
|
|
|
"io"
|
|
|
"io/ioutil"
|
|
|
"net"
|
|
@@ -11,92 +11,75 @@ import (
|
|
|
"net/url"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
|
- "strings"
|
|
|
"time"
|
|
|
|
|
|
- dclient "github.com/docker/docker/client"
|
|
|
+ "github.com/docker/docker/client"
|
|
|
+ "github.com/docker/docker/internal/test/environment"
|
|
|
"github.com/docker/docker/opts"
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
|
|
"github.com/docker/go-connections/sockets"
|
|
|
"github.com/docker/go-connections/tlsconfig"
|
|
|
+ "github.com/gotestyourself/gotestyourself/assert"
|
|
|
"github.com/pkg/errors"
|
|
|
)
|
|
|
|
|
|
-// Method creates a modifier that sets the specified string as the request method
|
|
|
-func Method(method string) func(*http.Request) error {
|
|
|
- return func(req *http.Request) error {
|
|
|
- req.Method = method
|
|
|
- return nil
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-// RawString sets the specified string as body for the request
|
|
|
-func RawString(content string) func(*http.Request) error {
|
|
|
- return RawContent(ioutil.NopCloser(strings.NewReader(content)))
|
|
|
+// NewAPIClient returns a docker API client configured from environment variables
|
|
|
+func NewAPIClient(t assert.TestingT, ops ...func(*client.Client) error) client.APIClient {
|
|
|
+ ops = append([]func(*client.Client) error{client.FromEnv}, ops...)
|
|
|
+ clt, err := client.NewClientWithOpts(ops...)
|
|
|
+ assert.NilError(t, err)
|
|
|
+ return clt
|
|
|
}
|
|
|
|
|
|
-// RawContent sets the specified reader as body for the request
|
|
|
-func RawContent(reader io.ReadCloser) func(*http.Request) error {
|
|
|
- return func(req *http.Request) error {
|
|
|
- req.Body = reader
|
|
|
- return nil
|
|
|
+// DaemonTime provides the current time on the daemon host
|
|
|
+func DaemonTime(ctx context.Context, t assert.TestingT, client client.APIClient, testEnv *environment.Execution) time.Time {
|
|
|
+ if testEnv.IsLocalDaemon() {
|
|
|
+ return time.Now()
|
|
|
}
|
|
|
-}
|
|
|
|
|
|
-// ContentType sets the specified Content-Type request header
|
|
|
-func ContentType(contentType string) func(*http.Request) error {
|
|
|
- return func(req *http.Request) error {
|
|
|
- req.Header.Set("Content-Type", contentType)
|
|
|
- return nil
|
|
|
- }
|
|
|
-}
|
|
|
+ info, err := client.Info(ctx)
|
|
|
+ assert.NilError(t, err)
|
|
|
|
|
|
-// JSON sets the Content-Type request header to json
|
|
|
-func JSON(req *http.Request) error {
|
|
|
- return ContentType("application/json")(req)
|
|
|
+ dt, err := time.Parse(time.RFC3339Nano, info.SystemTime)
|
|
|
+ assert.NilError(t, err, "invalid time format in GET /info response")
|
|
|
+ return dt
|
|
|
}
|
|
|
|
|
|
-// JSONBody creates a modifier that encodes the specified data to a JSON string and set it as request body. It also sets
|
|
|
-// the Content-Type header of the request.
|
|
|
-func JSONBody(data interface{}) func(*http.Request) error {
|
|
|
- return func(req *http.Request) error {
|
|
|
- jsonData := bytes.NewBuffer(nil)
|
|
|
- if err := json.NewEncoder(jsonData).Encode(data); err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- req.Body = ioutil.NopCloser(jsonData)
|
|
|
- req.Header.Set("Content-Type", "application/json")
|
|
|
- return nil
|
|
|
- }
|
|
|
+// DaemonUnixTime returns the current time on the daemon host with nanoseconds precision.
|
|
|
+// It return the time formatted how the client sends timestamps to the server.
|
|
|
+func DaemonUnixTime(ctx context.Context, t assert.TestingT, client client.APIClient, testEnv *environment.Execution) string {
|
|
|
+ dt := DaemonTime(ctx, t, client, testEnv)
|
|
|
+ return fmt.Sprintf("%d.%09d", dt.Unix(), int64(dt.Nanosecond()))
|
|
|
}
|
|
|
|
|
|
// Post creates and execute a POST request on the specified host and endpoint, with the specified request modifiers
|
|
|
-func Post(endpoint string, modifiers ...func(*http.Request) error) (*http.Response, io.ReadCloser, error) {
|
|
|
+func Post(endpoint string, modifiers ...func(*Options)) (*http.Response, io.ReadCloser, error) {
|
|
|
return Do(endpoint, append(modifiers, Method(http.MethodPost))...)
|
|
|
}
|
|
|
|
|
|
// Delete creates and execute a DELETE request on the specified host and endpoint, with the specified request modifiers
|
|
|
-func Delete(endpoint string, modifiers ...func(*http.Request) error) (*http.Response, io.ReadCloser, error) {
|
|
|
+func Delete(endpoint string, modifiers ...func(*Options)) (*http.Response, io.ReadCloser, error) {
|
|
|
return Do(endpoint, append(modifiers, Method(http.MethodDelete))...)
|
|
|
}
|
|
|
|
|
|
// Get creates and execute a GET request on the specified host and endpoint, with the specified request modifiers
|
|
|
-func Get(endpoint string, modifiers ...func(*http.Request) error) (*http.Response, io.ReadCloser, error) {
|
|
|
+func Get(endpoint string, modifiers ...func(*Options)) (*http.Response, io.ReadCloser, error) {
|
|
|
return Do(endpoint, modifiers...)
|
|
|
}
|
|
|
|
|
|
// Do creates and execute a request on the specified endpoint, with the specified request modifiers
|
|
|
-func Do(endpoint string, modifiers ...func(*http.Request) error) (*http.Response, io.ReadCloser, error) {
|
|
|
- return DoOnHost(DaemonHost(), endpoint, modifiers...)
|
|
|
-}
|
|
|
-
|
|
|
-// DoOnHost creates and execute a request on the specified host and endpoint, with the specified request modifiers
|
|
|
-func DoOnHost(host, endpoint string, modifiers ...func(*http.Request) error) (*http.Response, io.ReadCloser, error) {
|
|
|
- req, err := newRequest(host, endpoint, modifiers...)
|
|
|
+func Do(endpoint string, modifiers ...func(*Options)) (*http.Response, io.ReadCloser, error) {
|
|
|
+ opts := &Options{
|
|
|
+ host: DaemonHost(),
|
|
|
+ }
|
|
|
+ for _, mod := range modifiers {
|
|
|
+ mod(opts)
|
|
|
+ }
|
|
|
+ req, err := newRequest(endpoint, opts)
|
|
|
if err != nil {
|
|
|
return nil, nil, err
|
|
|
}
|
|
|
- client, err := newHTTPClient(host)
|
|
|
+ client, err := newHTTPClient(opts.host)
|
|
|
if err != nil {
|
|
|
return nil, nil, err
|
|
|
}
|
|
@@ -111,11 +94,17 @@ func DoOnHost(host, endpoint string, modifiers ...func(*http.Request) error) (*h
|
|
|
return resp, body, err
|
|
|
}
|
|
|
|
|
|
+// ReadBody read the specified ReadCloser content and returns it
|
|
|
+func ReadBody(b io.ReadCloser) ([]byte, error) {
|
|
|
+ defer b.Close()
|
|
|
+ return ioutil.ReadAll(b)
|
|
|
+}
|
|
|
+
|
|
|
// newRequest creates a new http Request to the specified host and endpoint, with the specified request modifiers
|
|
|
-func newRequest(host, endpoint string, modifiers ...func(*http.Request) error) (*http.Request, error) {
|
|
|
- hostUrl, err := dclient.ParseHostURL(host)
|
|
|
+func newRequest(endpoint string, opts *Options) (*http.Request, error) {
|
|
|
+ hostURL, err := client.ParseHostURL(opts.host)
|
|
|
if err != nil {
|
|
|
- return nil, errors.Wrapf(err, "failed parsing url %q", host)
|
|
|
+ return nil, errors.Wrapf(err, "failed parsing url %q", opts.host)
|
|
|
}
|
|
|
req, err := http.NewRequest("GET", endpoint, nil)
|
|
|
if err != nil {
|
|
@@ -127,13 +116,14 @@ func newRequest(host, endpoint string, modifiers ...func(*http.Request) error) (
|
|
|
} else {
|
|
|
req.URL.Scheme = "http"
|
|
|
}
|
|
|
- req.URL.Host = hostUrl.Host
|
|
|
+ req.URL.Host = hostURL.Host
|
|
|
|
|
|
- for _, config := range modifiers {
|
|
|
+ for _, config := range opts.requestModifiers {
|
|
|
if err := config(req); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
return req, nil
|
|
|
}
|
|
|
|
|
@@ -141,12 +131,12 @@ func newRequest(host, endpoint string, modifiers ...func(*http.Request) error) (
|
|
|
// TODO: Share more code with client.defaultHTTPClient
|
|
|
func newHTTPClient(host string) (*http.Client, error) {
|
|
|
// FIXME(vdemeester) 10*time.Second timeout of SockRequest… ?
|
|
|
- hostUrl, err := dclient.ParseHostURL(host)
|
|
|
+ hostURL, err := client.ParseHostURL(host)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
transport := new(http.Transport)
|
|
|
- if hostUrl.Scheme == "tcp" && os.Getenv("DOCKER_TLS_VERIFY") != "" {
|
|
|
+ if hostURL.Scheme == "tcp" && os.Getenv("DOCKER_TLS_VERIFY") != "" {
|
|
|
// Setup the socket TLS configuration.
|
|
|
tlsConfig, err := getTLSConfig()
|
|
|
if err != nil {
|
|
@@ -155,20 +145,37 @@ func newHTTPClient(host string) (*http.Client, error) {
|
|
|
transport = &http.Transport{TLSClientConfig: tlsConfig}
|
|
|
}
|
|
|
transport.DisableKeepAlives = true
|
|
|
- err = sockets.ConfigureTransport(transport, hostUrl.Scheme, hostUrl.Host)
|
|
|
+ err = sockets.ConfigureTransport(transport, hostURL.Scheme, hostURL.Host)
|
|
|
return &http.Client{Transport: transport}, err
|
|
|
}
|
|
|
|
|
|
-// NewClient returns a new Docker API client
|
|
|
-// Deprecated: Use Execution.APIClient()
|
|
|
-func NewClient() (dclient.APIClient, error) {
|
|
|
- return dclient.NewClientWithOpts(dclient.WithHost(DaemonHost()))
|
|
|
+func getTLSConfig() (*tls.Config, error) {
|
|
|
+ dockerCertPath := os.Getenv("DOCKER_CERT_PATH")
|
|
|
+
|
|
|
+ if dockerCertPath == "" {
|
|
|
+ return nil, errors.New("DOCKER_TLS_VERIFY specified, but no DOCKER_CERT_PATH environment variable")
|
|
|
+ }
|
|
|
+
|
|
|
+ option := &tlsconfig.Options{
|
|
|
+ CAFile: filepath.Join(dockerCertPath, "ca.pem"),
|
|
|
+ CertFile: filepath.Join(dockerCertPath, "cert.pem"),
|
|
|
+ KeyFile: filepath.Join(dockerCertPath, "key.pem"),
|
|
|
+ }
|
|
|
+ tlsConfig, err := tlsconfig.Client(*option)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ return tlsConfig, nil
|
|
|
}
|
|
|
|
|
|
-// ReadBody read the specified ReadCloser content and returns it
|
|
|
-func ReadBody(b io.ReadCloser) ([]byte, error) {
|
|
|
- defer b.Close()
|
|
|
- return ioutil.ReadAll(b)
|
|
|
+// DaemonHost return the daemon host string for this test execution
|
|
|
+func DaemonHost() string {
|
|
|
+ daemonURLStr := "unix://" + opts.DefaultUnixSocket
|
|
|
+ if daemonHostVar := os.Getenv("DOCKER_HOST"); daemonHostVar != "" {
|
|
|
+ daemonURLStr = daemonHostVar
|
|
|
+ }
|
|
|
+ return daemonURLStr
|
|
|
}
|
|
|
|
|
|
// SockConn opens a connection on the specified socket
|
|
@@ -199,32 +206,3 @@ func SockConn(timeout time.Duration, daemon string) (net.Conn, error) {
|
|
|
return c, errors.Errorf("unknown scheme %v (%s)", daemonURL.Scheme, daemon)
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-func getTLSConfig() (*tls.Config, error) {
|
|
|
- dockerCertPath := os.Getenv("DOCKER_CERT_PATH")
|
|
|
-
|
|
|
- if dockerCertPath == "" {
|
|
|
- return nil, errors.New("DOCKER_TLS_VERIFY specified, but no DOCKER_CERT_PATH environment variable")
|
|
|
- }
|
|
|
-
|
|
|
- option := &tlsconfig.Options{
|
|
|
- CAFile: filepath.Join(dockerCertPath, "ca.pem"),
|
|
|
- CertFile: filepath.Join(dockerCertPath, "cert.pem"),
|
|
|
- KeyFile: filepath.Join(dockerCertPath, "key.pem"),
|
|
|
- }
|
|
|
- tlsConfig, err := tlsconfig.Client(*option)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
-
|
|
|
- return tlsConfig, nil
|
|
|
-}
|
|
|
-
|
|
|
-// DaemonHost return the daemon host string for this test execution
|
|
|
-func DaemonHost() string {
|
|
|
- daemonURLStr := "unix://" + opts.DefaultUnixSocket
|
|
|
- if daemonHostVar := os.Getenv("DOCKER_HOST"); daemonHostVar != "" {
|
|
|
- daemonURLStr = daemonHostVar
|
|
|
- }
|
|
|
- return daemonURLStr
|
|
|
-}
|