Commit graph

546 commits

Author SHA1 Message Date
Sebastiaan van Stijn
e942499c66
Merge pull request #45991 from thaJeztah/client_cleanups
client: return explicit "nil" for no errors, and remove nil check
2023-07-19 12:17:32 +02:00
Sebastiaan van Stijn
2da589d454
client: return explicit "nil" for no errors, and remove nil check
- remove some intermediate variables
- explicitly return "nil" if there's no error
- remove redundant check for response-headers being nil

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-07-17 12:24:28 +02:00
Sebastiaan van Stijn
077049d0b9
client: WithTLSClientConfig: return early if no transport is set
tlsconfig.Client() does various things, including reading certs and
checking them. So we may as well return early if we're not gonna be
able to use the config.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-07-15 18:31:57 +02:00
Sebastiaan van Stijn
6846c63252
client: WithTLSClientConfig, WithTLSClientConfigFromEnv: inline vars
Remove intermediate variables.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-07-15 18:31:57 +02:00
Sebastiaan van Stijn
9d1e690751
client: TestGetAPIPath: don't use un-keyed structs
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-07-15 18:31:56 +02:00
Sebastiaan van Stijn
6552441b46
client: TestClientRedirect: cleanup
- don't use un-keyed structs
- use http consts where possible
- use errors.As instead of manually checking the error-type

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-07-15 18:31:56 +02:00
Sebastiaan van Stijn
8d514f4e2e
client: touch-up docs, and user doc-links
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-07-15 18:31:51 +02:00
Sebastiaan van Stijn
c219b09d4a
client: Client.postHijacked: use Client.buildRequest
Use Client.buildRequest instead of a local copy of the same logic so
that we're using the same logic, and there's less chance of diverging.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-07-15 02:39:08 +02:00
Sebastiaan van Stijn
e11555218b
client: Client.setupHijackConn: explicitly ignore errors
Just making my IDE and some linters slightly happier.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-07-15 02:37:03 +02:00
Sebastiaan van Stijn
92975f0c11
client: define a "dummy" hostname to use for local connections
For local communications (npipe://, unix://), the hostname is not used,
but we need valid and meaningful hostname.

The current code used the client's `addr` as hostname in some cases, which
could contain the path for the unix-socket (`/var/run/docker.sock`), which
gets rejected by go1.20.6 and go1.19.11 because of a security fix for
[CVE-2023-29406 ][1], which was implemented in  https://go.dev/issue/60374.

Prior versions go Go would clean the host header, and strip slashes in the
process, but go1.20.6 and go1.19.11 no longer do, and reject the host
header.

This patch introduces a `DummyHost` const, and uses this dummy host for
cases where we don't need an actual hostname.

Before this patch (using go1.20.6):

    make GO_VERSION=1.20.6 TEST_FILTER=TestAttach test-integration
    === RUN   TestAttachWithTTY
        attach_test.go:46: assertion failed: error is not nil: http: invalid Host header
    --- FAIL: TestAttachWithTTY (0.11s)
    === RUN   TestAttachWithoutTTy
        attach_test.go:46: assertion failed: error is not nil: http: invalid Host header
    --- FAIL: TestAttachWithoutTTy (0.02s)
    FAIL

With this patch applied:

    make GO_VERSION=1.20.6 TEST_FILTER=TestAttach test-integration
    INFO: Testing against a local daemon
    === RUN   TestAttachWithTTY
    --- PASS: TestAttachWithTTY (0.12s)
    === RUN   TestAttachWithoutTTy
    --- PASS: TestAttachWithoutTTy (0.02s)
    PASS

[1]: https://github.com/advisories/GHSA-f8f7-69v5-w4vx

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-07-14 18:57:53 +02:00
Sebastiaan van Stijn
2a59188760
client: TestSetHostHeader: don't use un-keyed literals
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-07-14 18:56:47 +02:00
Sebastiaan van Stijn
1370b3c679
client: minor test improvements for requests
- use assert.Check() where possible to not fail early
- improve checks for error-types
- rename "testURL" var to be more descriptive, and use a const

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-07-13 11:40:00 +02:00
Sebastiaan van Stijn
f7f0a17ea2
client: TestSetHostHeader: use sub-tests
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-07-13 11:28:13 +02:00
Sebastiaan van Stijn
83477ce8d0
client: remove custom "headers" type, and use "http.Header" instead
Use http.Header, which is more descriptive on intent, and we're already
importing the package in the client. Removing the "header" type also fixes
various locations where the type was shadowed by local variables named
"headers".

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-07-11 13:14:28 +02:00
Sebastiaan van Stijn
e4ce676d3d
client: ServiceCreate, ServiceUpdate: omit "version" header on API >= 1.30
The "version" header was added in c0afd9c873,
but used the wrong information to get the API version.

This issue was fixed in a9d20916c3, which switched
the API handler code to get the API version from the context. That change is part
of Docker Engine 20.10 (API v1.30 and up)

This patch updates the code to only set the header on APi v1.29 and older, as it's
not used by newer API versions.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-07-11 13:00:00 +02:00
Sebastiaan van Stijn
c90229ed9a
api/types: move system info types to api/types/system
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-07-07 13:01:36 +02:00
Brian Goff
2216d3ca8d Add health start interval
This adds an additional interval to be used by healthchecks during the
start period.
Typically when a container is just starting you want to check if it is
ready more quickly than a typical healthcheck might run. Without this
users have to balance between running healthchecks to frequently vs
taking a very long time to mark a container as healthy for the first
time.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-07-05 23:44:17 +00:00
Sebastiaan van Stijn
4e69e16fde
client: use string-literals for easier grep'ing
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-07-05 12:27:01 +02:00
Sebastiaan van Stijn
0af6d9e291
client: format code with gofumpt
Formatting the code with https://github.com/mvdan/gofumpt

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-06-29 00:25:21 +02:00
Sebastiaan van Stijn
a6048fc792
client: add WithUserAgent() option
When constructing the client, and setting the User-Agent, care must be
taken to apply the header in the right location, as custom headers can
be set in the CLI configuration, and merging these custom headers should
not override the User-Agent header.

This patch adds a dedicated `WithUserAgent()` option, which stores the
user-agent separate from other headers, centralizing the merging of
other headers, so that other parts of the (CLI) code don't have to be
concerned with merging them in the right order.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-06-09 09:59:29 +02:00
Sebastiaan van Stijn
ab35df454d
remove pre-go1.17 build-tags
Removed pre-go1.17 build-tags with go fix;

    go mod init
    go fix -mod=readonly ./...
    rm go.mod

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-05-19 20:38:51 +02:00
Sebastiaan van Stijn
1b3c2743cc
Merge pull request #45550 from corhere/fix-empty-container-decode
Allow empty body in `POST /commit` again
2023-05-18 21:46:00 +02:00
Cory Snider
3ceb3810d7 client: don't marshal typed nils in request body
The internal Client request methods which accept an object as a body use
nil to signal that the request should not have a body. But it is easy to
accidentally pass a typed-nil value as the object, e.g. if the object
comes from a function argument or struct field of a concrete type. The
result is that these requests will, surprisingly, have a JSON body of
`null`. Treat typed-nil pointers the same as untyped nils for the
purposes of determining whether or not the request should include a
body.

Stop assuming that POST requests should always have a body. POST /commit
does not require a body, for example.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-05-17 14:58:12 -04:00
Sebastiaan van Stijn
66ff1e063e
client: update error-assertions in tests
- use is.ErrorType
- replace uses of client.IsErrNotFound for errdefs.IsNotFound, as
  the client no longer returns the old error-type.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-05-10 22:13:38 +02:00
Sebastiaan van Stijn
685b3d820a
client: make IsErrNotFound an alias for errdefs.IsNotFound
None of the client will return the old error-types, so there's no need
to keep the compatibility code. We can consider deprecating this function
in favor of the errdefs equivalent this.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-05-10 21:52:43 +02:00
Jeyanthinath Muthuram
307b09e7eb
fixing consistent aliases for OCI spec imports
Signed-off-by: Jeyanthinath Muthuram <jeyanthinath10@gmail.com>
2023-05-08 15:27:52 +05:30
Sebastiaan van Stijn
8a4b095a94
Merge pull request #45353 from thaJeztah/api_container_change_type
api/types/container: create type for changes endpoint
2023-05-04 19:48:40 +02:00
Sebastiaan van Stijn
66cf0e3f55
client: slightly improve ContainerDiff tests
- use gotest.tools for asserting
- check result returned

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-05-03 21:25:07 +02:00
Sebastiaan van Stijn
dbb48e4b29
api/types/container: create type for changes endpoint
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-05-03 21:23:42 +02:00
Tianon Gravi
4597f50deb
Merge pull request #45392 from thaJeztah/filters_inline_and_simplify
inline filters.Args where possible, and use filters.Arg() when constructing
2023-04-27 23:31:59 +00:00
Tianon Gravi
751888979c
Merge pull request #44382 from thaJeztah/client_rewrite
client: defaultHTTPClient() accept URL
2023-04-27 23:27:59 +00:00
Sebastiaan van Stijn
081b5e3d4e
client: inline filters in tests
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-04-25 15:11:32 +02:00
Cory Snider
6b9968e8b1 client: deprecate NewClient properly
The 'Deprecated:' line in NewClient's doc comment was not in a new
paragraph, so GoDoc, linters, and IDEs were unaware that it was
deprecated. The package documentation also continued to reference
NewClient. Update the doc comments to finish documenting that NewClient
is deprecated.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-03-31 16:15:01 -04:00
Brian Goff
713c9280ca Move buildkit client opts to new package
Not everyone who imports the client wants to pull in all of buildkit, so
move it to a separate package.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2023-03-29 16:59:44 +00:00
Brian Goff
0fa7a4e3b4 client: Add buildkit ClientOpts
This adds a function to the client package which can be used to create a
buildkit client from our moby client.

Example:

```go
package main

import (
  "context"

  "github.com/moby/moby/client"
  bkclient "github.com/moby/buildkit/client"
)

func main() {
  c := client.NewWithOpts()
  bc, _ := bkclient.New(context.Background(), ""
    client.BuildkitClientOpts(c),
  )
  // ...
}
```

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2023-03-23 21:51:56 +00:00
Tianon Gravi
13f4262354
Merge pull request #44739 from nicks/nicks/invalid-character
client: improve error messaging on crash
2023-01-26 15:22:08 -08:00
Nick Santos
9900c7a348
client: improve error messaging on crash
Repro steps:
- Run Docker Desktop
- Run `docker run busybox tail -f /dev/null`
- Run `pkill "Docker Desktop"

Expected:
An error message that indicates that Docker Desktop is shutting down.

Actual:
An error message that looks like this:

```
error waiting for container: invalid character 's' looking for beginning of value
```

here's an example:

https://github.com/docker/for-mac/issues/6575#issuecomment-1324879001

After this change, you get an error message like:

```
error waiting for container: copying response body from Docker: unexpected EOF
```

which is a bit more explicit.

Signed-off-by: Nick Santos <nick.santos@docker.com>
2023-01-26 15:52:01 -05:00
Sebastiaan van Stijn
722d477bc6
client: defaultHTTPClient(): don't ignore transport errors
This function was suppressing errors coming from ConfigureTransport, with the
assumption that it will only be used with the Default configuration, and only return
errors for invalid / unsupported schemes (e.g., using "npipe" on a Linux environment);
d109e429dd/vendor/github.com/docker/go-connections/sockets/sockets_unix.go (L27-L29)

Those errors won't happen when the default is passed, so this is mostly theoretical.
Let's return the error instead (which should always be nil), just to be on the save
side, and to make sure that any other use of this function will return errors that
occurred, which may also be when parsing proxy environment variables;
d109e429dd/vendor/github.com/docker/go-connections/sockets/sockets.go (L29)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-01-01 13:39:21 +01:00
Sebastiaan van Stijn
31ee158394
client: defaultHTTPClient() accept URL
We're parsing the URL either way, so may just as well use the result.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-01-01 13:35:16 +01:00
Sebastiaan van Stijn
2a7908c939
Merge pull request #44182 from thaJeztah/subtests_TestNewClientWithOpsFromEnv
client: TestNewClientWithOpsFromEnv(): use sub-tests
2022-12-27 13:12:50 +01:00
Sebastiaan van Stijn
19cd5ff164
client: use strings.Cut()
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-12-21 11:09:01 +01:00
Paweł Gronowski
3d97f1e22d
client/list: Handle SharedSize
This makes the `ImageList` function to add `shared-size=1` to the url
query when user caller sets the SharedSize.
SharedSize support was introduced in API version 1.42. This field was
added to the options struct, but client wasn't adjusted.

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
2022-11-30 17:08:00 +01:00
Sebastiaan van Stijn
542c735926
Merge pull request #44256 from thaJeztah/redundant_sprintfs
replace redundant fmt.Sprintf() with strconv
2022-10-25 16:48:15 -04:00
Sebastiaan van Stijn
de705907a5
client: remove solaris build-tag, simplify and gofumpt
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-10-09 22:41:55 +02:00
Sebastiaan van Stijn
07b2e4cb79
client: use strconv instead of fmt.Sprintf()
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-10-08 17:41:39 +02:00
Sebastiaan van Stijn
cd51c9fafb
client: fix empty-lines (revive)
client/events.go:19:115: empty-lines: extra empty line at the start of a block (revive)
    client/events_test.go:60:31: empty-lines: extra empty line at the start of a block (revive)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-28 01:58:52 +02:00
Sebastiaan van Stijn
0af7b4961a
client: TestNewClientWithOpsFromEnv(): use sub-tests
Had this stashed as part of some other changes, so thought I'd push
as a PR; with this patch:

    === RUN   TestNewClientWithOpsFromEnv/invalid_cert_path
    === RUN   TestNewClientWithOpsFromEnv/default_api_version_with_cert_path
    === RUN   TestNewClientWithOpsFromEnv/default_api_version_with_cert_path_and_tls_verify
    === RUN   TestNewClientWithOpsFromEnv/default_api_version_with_cert_path_and_host
    === RUN   TestNewClientWithOpsFromEnv/invalid_docker_host
    === RUN   TestNewClientWithOpsFromEnv/invalid_docker_host,_with_good_format
    === RUN   TestNewClientWithOpsFromEnv/override_api_version
    --- PASS: TestNewClientWithOpsFromEnv (0.00s)
        --- PASS: TestNewClientWithOpsFromEnv/default_api_version (0.00s)
        --- PASS: TestNewClientWithOpsFromEnv/invalid_cert_path (0.00s)
        --- PASS: TestNewClientWithOpsFromEnv/default_api_version_with_cert_path (0.00s)
        --- PASS: TestNewClientWithOpsFromEnv/default_api_version_with_cert_path_and_tls_verify (0.00s)
        --- PASS: TestNewClientWithOpsFromEnv/default_api_version_with_cert_path_and_host (0.00s)
        --- PASS: TestNewClientWithOpsFromEnv/invalid_docker_host (0.00s)
        --- PASS: TestNewClientWithOpsFromEnv/invalid_docker_host,_with_good_format (0.00s)
        --- PASS: TestNewClientWithOpsFromEnv/override_api_version (0.00s)
PASS

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-22 19:48:39 +02:00
Sebastiaan van Stijn
2597a71623
client: ignore kernel-memory on API >= 1.42
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-13 11:42:13 +02:00
Akihiro Suda
52f8a4283e
Merge pull request #44021 from thaJeztah/client_remove_deprecated_errorutils
client: remove deprecated error-utilities
2022-08-31 17:01:56 +09:00
Sebastiaan van Stijn
e14924570c
client: remove deprecated WithDialer() option
It was deprecated in edac92409a, which
was part of 18.09 and up, so should be safe by now to remove this.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-08-23 23:27:35 +02:00
Sebastiaan van Stijn
7f0cf432e9
client: remove redundant pluginPermissionDenied
It was only used in a single location, and only a "convenience" type,
not used to detect a specific error.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-08-23 23:21:21 +02:00
Sebastiaan van Stijn
c3f0cd7457
client: remove deprecated IsErrUnauthorized, IsErrNotImplemented
These were deprecated in ee230d8fdd,
which is in the 22.06 branch, so we can safely remove it from
master to have them removed in the release after that.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-08-23 23:21:19 +02:00
Sebastiaan van Stijn
340711db3d
api: add types/volume.ListOptions for a more consistent API
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-08-03 23:30:28 +02:00
Sebastiaan van Stijn
e3a7a1c6ae
client: linting: fix "invalid auth header" error
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-07-29 23:05:22 +02:00
Sebastiaan van Stijn
eaf1a604f2
client: use types/registry.AuthConfig
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-07-29 23:05:15 +02:00
Sebastiaan van Stijn
857cb260c7
api: add const for 'X-Registry-Auth'
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-07-29 23:04:34 +02:00
Tianon Gravi
a26d82c2f8
Merge pull request #43789 from thaJeztah/client_deadcode
client: errors: remove dead code
2022-07-13 11:54:39 -07:00
Sebastiaan van Stijn
ee230d8fdd
client: errors: remove dead code
- Update IsErrNotFound() to check for the current type before falling back to
  detecting the deprecated type.
- Remove unauthorizedError and notImplementedError types, which were not used.
- IsErrPluginPermissionDenied() was added in 7c36a1af03,
  but not used at the time, and still appears to be unused.
- Deprecate IsErrUnauthorized in favor of errdefs.IsUnauthorized()
- Deprecate IsErrNotImplemented in favor of errdefs,IsNotImplemented()

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-07-09 00:54:36 +02:00
Sebastiaan van Stijn
52c1a2fae8
gofmt GoDoc comments with go1.19
Older versions of Go don't format comments, so committing this as
a separate commit, so that we can already make these changes before
we upgrade to Go 1.19.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-07-08 19:56:23 +02:00
Sebastiaan van Stijn
10c56efa97
linting: error strings should not be capitalized (revive)
client/request.go:183:28: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
                    err = errors.Wrap(err, "In the default daemon configuration on Windows, the docker client must be run with elevated privileges to connect.")
                                           ^
    client/request.go:186:28: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
                    err = errors.Wrap(err, "This error may indicate that the docker daemon is not running.")
                                           ^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-07-04 10:15:06 +02:00
Paweł Gronowski
56a20dbc19 container/exec: Support ConsoleSize
Now client have the possibility to set the console size of the executed
process immediately at the creation. This makes a difference for example
when executing commands that output some kind of text user interface
which is bounded by the console dimensions.

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
2022-06-24 11:54:25 +02:00
Sebastiaan van Stijn
a5f6500958
replace deprecated gotest.tools' env.Patch() with t.SetEnv()
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-05-28 12:12:39 +02:00
Paweł Gronowski
85a7f5a09a daemon/linux: Set console size on creation
On Linux the daemon was not respecting the HostConfig.ConsoleSize
property and relied on cli initializing the tty size after the container
was created. This caused a delay between container creation and
the tty actually being resized.

This is also a small change to the api description, because
HostConfig.ConsoleSize is no longer Windows-only.

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
2022-05-19 07:57:27 +02:00
Sebastiaan van Stijn
d9524d92a9
api/types/swarm: Version: implement stringer interface
makes the code a bit more DRY.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-05-13 02:40:14 +02:00
Drew Erny
240a9fcb83
Add Swarm cluster volume supports
Adds code to support Cluster Volumes in Swarm using CSI drivers.

Signed-off-by: Drew Erny <derny@mirantis.com>
2022-05-13 00:55:44 +02:00
Nicolas De Loof
af5d83a641
Make it explicit raw|multiplexed stream implementation being used
fix #35761

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
2022-05-12 11:36:31 +02:00
Sebastiaan van Stijn
41b96bff55
update uses of container.ContainerCreateCreatedBody to CreateResponse
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-04-28 22:39:20 +02:00
Sebastiaan van Stijn
64e96932bd
api: rename volume.VolumeCreateBody to volume.CreateOptions
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-04-28 22:39:14 +02:00
Sebastiaan van Stijn
18281c92fa
api: rename volume.VolumeListOKBody to volume.ListResponse
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-04-28 22:39:12 +02:00
Sebastiaan van Stijn
3cae9fef16
imports: remove "volumetypes" aliases for api/types/volume
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-04-28 22:39:04 +02:00
Sebastiaan van Stijn
7293857456
api: rename ContainerWaitOKBody to container.WaitResponse
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-04-28 22:38:20 +02:00
Brian Goff
b3332b851a
Merge pull request #43517 from Juneezee/test/t.Setenv
test: use `T.Setenv` to set env vars in tests
2022-04-28 12:02:01 -07:00
Eng Zer Jun
36049a04d2
test: use T.Setenv to set env vars in tests
This commit replaces `os.Setenv` with `t.Setenv` in tests. The
environment variable is automatically restored to its original value
when the test and all its subtests complete.

Reference: https://pkg.go.dev/testing#T.Setenv
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2022-04-23 17:44:16 +08:00
Sebastiaan van Stijn
176f66df9c
api/types: replace uses of deprecated types.Volume with volume.Volume
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-04-21 19:50:59 +02:00
Sebastiaan van Stijn
e8fa708ae5
client: ContainerStop(), ContainerRestart(): support stop-signal
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-04-20 21:29:34 +02:00
Sebastiaan van Stijn
9060126639
client, integration-cli: remove unneeded import aliases
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-04-20 21:29:33 +02:00
Brian Goff
c4e3a16373
Merge pull request #43494 from thaJeztah/client_cp_error_handling
client: CopyToContainer(), CopyFromContainer(): remove status-code handling
2022-04-20 10:36:19 -07:00
Sebastiaan van Stijn
b365924ec3
client: ContainerKill(): don't send signal query-param if none was set
Just a small clean-up (there's more endpoints to do this for, but
I was working on changes in this area on the CLI when I noticed we
were setting this query-parameter unconditionally.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-04-19 17:40:33 +02:00
Sebastiaan van Stijn
013d648888
client: CopyToContainer(), CopyFromContainer(): remove status-code handling
This was added in 93c3e6c91e, at which time only
some basic handling of non-succesful status codes was present;
93c3e6c91e/api/client/utils.go (L112-L121)

Given that since 38e6d474af non-successful status-
codes are already handled, and a 204 ("no content") status should not be an error,
this special case should no longer be needed.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-04-19 08:28:20 +02:00
Sebastiaan van Stijn
890231f46b
client: container ps: don't set "limit" if none was set
both -1 and 0 are accepted as "no limit", so don't send the
limit option if no limit was set. For simplicity, we're ignoring
values <= 0.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-31 19:59:17 +02:00
Sebastiaan van Stijn
1e645fb70f
client: Ping(): add handling for swarm status headers
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-25 23:57:58 +01:00
Sebastiaan van Stijn
45067cda33
client: remove wrapResponseError()
The wrapResponseError() utility converted some specific errors, but in
doing so, could hide the actual error message returned by the daemon.
In addition, starting with 38e6d474af,
HTTP status codes were already mapped to their corresponding errdefs
types on the client-side, making this conversion redundant.

This patch removes the wrapResponseError() utility; it's worth noting
that some error-messages will change slightly (as they now return the
error as returned by the daemon), but may cointain more details as
before, and in some cases prevents hiding the actual error.

Before this change:

    docker container rm nosuchcontainer
    Error: No such container: nosuchcontainer

    docker container cp mycontainer:/no/such/path .
    Error: No such container:path: mycontainer:/no/such/path

    docker container cp ./Dockerfile mycontainer:/no/such/path
    Error: No such container:path: mycontainer:/no/such

    docker image rm nosuchimage
    Error: No such image: nosuchimage

    docker network rm nosuchnetwork
    Error: No such network: nosuchnetwork

    docker volume rm nosuchvolume
    Error: No such volume: nosuchvolume

    docker plugin rm nosuchplugin
    Error: No such plugin: nosuchplugin

    docker checkpoint rm nosuchcontainer nosuchcheckpoint
    Error response from daemon: No such container: nosuchcontainer

    docker checkpoint rm mycontainer nosuchcheckpoint
    Error response from daemon: checkpoint nosuchcheckpoint does not exist for container mycontainer

    docker service rm nosuchservice
    Error: No such service: nosuchservice

    docker node rm nosuchnode
    Error: No such node: nosuchnode

    docker config rm nosuschconfig
    Error: No such config: nosuschconfig

    docker secret rm nosuchsecret
    Error: No such secret: nosuchsecret

After this change:

    docker container rm nosuchcontainer
    Error response from daemon: No such container: nosuchcontainer

    docker container cp mycontainer:/no/such/path .
    Error response from daemon: Could not find the file /no/such/path in container mycontainer

    docker container cp ./Dockerfile mycontainer:/no/such/path
    Error response from daemon: Could not find the file /no/such in container mycontainer

    docker image rm nosuchimage
    Error response from daemon: No such image: nosuchimage:latest

    docker network rm nosuchnetwork
    Error response from daemon: network nosuchnetwork not found

    docker volume rm nosuchvolume
    Error response from daemon: get nosuchvolume: no such volume

    docker plugin rm nosuchplugin
    Error response from daemon: plugin "nosuchplugin" not found

    docker checkpoint rm nosuchcontainer nosuchcheckpoint
    Error response from daemon: No such container: nosuchcontainer

    docker checkpoint rm mycontainer nosuchcheckpoint
    Error response from daemon: checkpoint nosuchcheckpoint does not exist for container mycontainer

    docker service rm nosuchservice
    Error response from daemon: service nosuchservice not found

    docker node rm nosuchnode
    Error response from daemon: node nosuchnode not found

    docker config rm nosuchconfig
    Error response from daemon: config nosuchconfig not found

    docker secret rm nosuchsecret
    Error response from daemon: secret nosuchsecret not found

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-20 19:04:52 +01:00
Sebastiaan van Stijn
165fe27979
Merge pull request #43380 from thaJeztah/client_search_limits
client: ImageSearch(): don't send limit if none was specified
2022-03-17 10:43:14 +01:00
Sebastiaan van Stijn
ce7a919a15
Merge pull request #43342 from thaJeztah/client_test_cleanup
client: cleanup and fix some tests
2022-03-16 14:47:43 +01:00
Sebastiaan van Stijn
8ba83f63a0
client: ImageSearch(): don't send limit if none was specified
The API defines a default limit for searches, but when removing the
default from the cli, the client still sends "0" as a limit, which
is not allowed by existing versions of the API:

    docker search --limit=0 busybox
    Error response from daemon: Limit 0 is outside the range of [1, 100]

This patch changes the client so that no limit is sent if none was set ("0"),
allowing the daemon to use its (or the registry's) default.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-15 14:53:46 +01:00
Sebastiaan van Stijn
5979d6e7e3
client: add const for environment variables, and document them
This adds consts for the environment variables that are supported
by the client. These environment variables are unlikely to change,
or at least, unlikely to be removed, but having consts allows for
them to be documented.

I did not change all occurrences of these variables to use the const,
as they're used in various tests, and it's ok to use a fixture for
those, but it's nice to have a const available for (external) consumers
of the client package, and to have their purpose (and caveats)
documented in the code.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-07 12:35:55 +01:00
Sebastiaan van Stijn
c2c7e9d449
client: improve GoDoc, and minor touch-ups
- Improve documentation of various functions to better describe their behavior.
- Rename some variables to be more descriptive (as this is client code, used
  by external consumers, it's nice to be a bit more explicit).
- Remove a redundant check in `WithVersionFromEnv()`, as `WithVersion()`
  already checks for empty values.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-07 12:35:38 +01:00
Sebastiaan van Stijn
8512cf076c
client: TestNegotiateAPIVersion(), TestClientRedirect(): use sub-tests
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-07 11:13:52 +01:00
Sebastiaan van Stijn
25a336ab6a
client: TestGetAPIPath(): update test to use more realistic results
This test was setting the non-exported `Client.basePath` directly, however,
it was setting it to a value that would never realistically happen, because
`NewClientWithOpts()` initializes the Client with the default API version:
ea5b4765d9/client/client.go (L119-L130)

Which is used by `getAPIPath()` to construct the URL/path:
ea5b4765d9/client/client.go (L176-L190)

While this didn't render the test "invalid", using a Client that's constructed
in the usual way, makes it more representative.

Given that we deprecated (but still support) the non-versioned API paths, with
the exception of the `/_ping` API endpoint, we should probably change `getAPIPath()`
to default to the "current version", instead of allowing it to use an empty string.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-07 11:13:02 +01:00
Sebastiaan van Stijn
65e4ea27cd
client: various small test-improvements
- avoid accessing non-exported fields where possible, and test using accessors
  instead, so that we're closer to how it's actually used.
- use a variable or const for "expected" in some tests, so that "expected" is
  printed as part of the test-failure output (instead of just a "value").
- swap the order of "actual" and "expected" for consistency, and to make it
  easier to see what the "expected" value is in some cases ("expected" on the
  right, so that it reads `val (actual) != val (expected)`).
- don't set fields in the Ping response that are not relevant to the test.
- rename some variables for consistency.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-07 10:58:03 +01:00
Sebastiaan van Stijn
948c2c45bb
client: use canonical names for HTTP Headers
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-06 10:49:02 +01:00
Sebastiaan van Stijn
a0230f3d9a
remove unneeded "digest" alias for "go-digest"
I think this was there for historic reasons (may have been goimports expected
this, and we used to have a linter that wanted it), but it's not needed, so
let's remove it (to make my IDE less complaining about unneeded aliases).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-04 14:49:42 +01:00
Sebastiaan van Stijn
a832635e51
client.ContainerWait(): don't send empty "condition" query parameter
The client would always send a value, even if no `condition` was set;

    Calling POST /v1.41/containers/foo/wait?condition=

This patch changes the client to not send the parameter if it's empty (and the
API default value should be used).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-02-14 21:47:53 +01:00
Akihiro Suda
3b02526c1e
Merge pull request #42224 from thaJeztah/more_client_opts
client: extract FromEnv parts to separate WithXX options
2022-02-11 13:17:05 +09:00
Sebastiaan van Stijn
eae20b1a1b
client: extract FromEnv parts to separate WithXX options
Implements three options;

- WithTLSClientConfigFromEnv()
- WithHostFromEnv()
- WithVersionFromEnv()

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-02-10 17:15:40 +01:00
Sebastiaan van Stijn
99935ff803
client.NewClientWithOpts(): remove redundant type assertion (gosimple)
As caught by gosimple:

    client/client.go:138:14: S1040: type assertion to the same type: c.client.Transport already has type http.RoundTripper (gosimple)
        if _, ok := c.client.Transport.(http.RoundTripper); !ok {
                    ^

This check was originally added in dc9f5c2ca3, to
check if the passed option was a `http.Transport`, and later changed in
e345cd12f9 to check for `http.RoundTripper` instead.

Client.client is a http.Client, for which the Transport field is a RoundTripper,
so this check is redundant.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-02-08 09:43:25 +01:00
Sebastiaan van Stijn
2cff05e960
client/request.go:157:8: SA1019: err.Temporary is deprecated (staticcheck)
It's deprecated in Go 1.18:

    client/request.go:157:8: SA1019: err.Temporary is deprecated: Temporary errors are not well-defined. Most "temporary" errors are timeouts, and the few exceptions are surprising. Do not use this method. (staticcheck)
        if !err.Temporary() {
            ^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-01-31 15:59:51 +01:00
Akihiro Suda
33a3680e08
Merge pull request #42738 from thaJeztah/client_networkinspect_test
client: use subtests for TestNetworkInspect
2021-11-09 18:37:44 +09:00
Akihiro Suda
0cd1bd42b4
Merge pull request #42770 from thaJeztah/eventtype_enums
api/types/events: add "Type" type for event-type enum
2021-08-28 00:23:56 +09:00
Sebastiaan van Stijn
4045c4ceaf
client: use subtests for TestNetworkInspect
Unify the NetworkInspect tests to remove some boilerplating

Before this change:

    go test -v -run TestNetworkInspect ./client/
    === RUN   TestNetworkInspectError
    --- PASS: TestNetworkInspectError (0.00s)
    === RUN   TestNetworkInspectNotFoundError
    --- PASS: TestNetworkInspectNotFoundError (0.00s)
    === RUN   TestNetworkInspectWithEmptyID
    --- PASS: TestNetworkInspectWithEmptyID (0.00s)
    === RUN   TestNetworkInspect
    --- PASS: TestNetworkInspect (0.00s)
    PASS
    ok  	github.com/docker/docker/client	0.010s

With this change:

    go test -v -run TestNetworkInspect ./client/
    === RUN   TestNetworkInspect
    === RUN   TestNetworkInspect/empty_ID
    === RUN   TestNetworkInspect/no_options
    === RUN   TestNetworkInspect/verbose
    === RUN   TestNetworkInspect/global_scope
    === RUN   TestNetworkInspect/unknown_network
    === RUN   TestNetworkInspect/server_error
    --- PASS: TestNetworkInspect (0.00s)
        --- PASS: TestNetworkInspect/empty_ID (0.00s)
        --- PASS: TestNetworkInspect/no_options (0.00s)
        --- PASS: TestNetworkInspect/verbose (0.00s)
        --- PASS: TestNetworkInspect/global_scope (0.00s)
        --- PASS: TestNetworkInspect/unknown_network (0.00s)
        --- PASS: TestNetworkInspect/server_error (0.00s)
    PASS
    ok  	github.com/docker/docker/client	0.012s

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-08-27 16:56:11 +02:00