fussybeaver / bollard

Docker daemon API in Rust
Apache License 2.0
907 stars 134 forks source link

Add distribution inspect function #385

Closed bpmooch closed 8 months ago

bpmooch commented 8 months ago

Add the ability to call /distribution/{name}/json

closes #382

fussybeaver commented 8 months ago

Fabulous, looks good, but it would be useful to have a test case for this...

bpmooch commented 8 months ago

I can add a test. I noticed you have mocks that use http response string literals. How are you generating those? Surely not by hand....

fussybeaver commented 8 months ago

The mock-based testing we've mostly used to test the JSON and Newline parsers (you can generate payloads by adding an env_logger to the project)... Since the API models change over time, it's more future-proof to write a small integration test instead and check that some part of the response exists. There are some (slightly dated) examples e.g. tests/system_test.rs

bpmooch commented 8 months ago

@fussybeaver what is your workflow for debugging tests locally?

bpmooch commented 8 months ago

DockerResponseServerError { status_code: 500, message: "Head \"http://localhost:5000/v2/hello-world/manifests/linux\": no basic auth credentials" }

I'm surprised I'm running into this error related to auth credentials. Have you seen this before?

fussybeaver commented 8 months ago

So, the integration tests by default run with a local registry (localhost:5000), as this has removed some flakiness with using the remote docker hub registry and it's useful for some registry related tests, though if you're testing locally, you generally don't need to set this up. You set the DISABLE_REGISTRY environment variable, which toggles the docker hub.

The command is something like:

env RUST_LOG=bollard=debug DISABLE_REGISTRY=1 cargo test -- --test-threads 1 --test integration_test_inspect_registry_image --nocaptur
e

For debugging, you can temporarily add an env_logger and emit logs or just use println, or I suppose a debugger.

bpmooch commented 8 months ago

@fussybeaver I have copied directly from inspect_image_test and yet I get a no basic auth error in CI. Is there some test setup that I'm not doing correctly? I am somewhat vexed because the test passes locally fine for me.

Running tests locally produces:

RUST_LOG=bollard=debug DISABLE_REGISTRY=1 cargo test -- --test-threads 1 --test integration_test_inspect_registry_image --nocapture

     Running tests/image_test.rs (target/debug/deps/image_test-86db1a07de76ccf0)

running 1 test
test integration_test_inspect_registry_image ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 14 filtered out; finished in 1.09s
fussybeaver commented 8 months ago

Hmm... Maybe this is a bit more complicated and you need to pass in the X-Registry-Auth header with credentials for the registry to make this work with a private registry. See https://docs.docker.com/engine/api/v1.44/#section/Authentication

We do the same thing in the build image method: https://github.com/fussybeaver/bollard/blob/master/src/image.rs#L575-L615

Another option is just mark this implementation for the docker hub and adapt the test to not use the private registry (ie. just point to hello-world:linux) - we can always add support for private registries in a later PR.

bpmooch commented 8 months ago

Thanks for the tip about the auth header, that's exactly what I needed to fix things up

fussybeaver commented 8 months ago

Thanks this looks great.

bpmooch commented 8 months ago

@fussybeaver How often do you cut releases? I am about to use this change in a repo I'm working on. Should I point cargo to my fork for a while?