1
0
mirror of https://github.com/docker/cli.git synced 2026-01-28 04:20:55 +03:00
Files
cli/cmd/docker-trust/trust/inspect_test.go
Sebastiaan van Stijn c9bb291154 implement docker trust as plugin
move the `trust` subcommands to a plugin, so that the subcommands can
be installed separate from the `docker trust` integration in push/pull
(for situations where trust verification happens on the daemon side).

    make binary
    go build -o /usr/libexec/docker/cli-plugins/docker-trust ./cmd/docker-trust

    docker info
    Client:
     Version:    28.2.0-dev
     Context:    default
     Debug Mode: false
     Plugins:
      buildx: Docker Buildx (Docker Inc.)
        Version:  v0.24.0
        Path:     /usr/libexec/docker/cli-plugins/docker-buildx
      trust: Manage trust on Docker images (Docker Inc.)
        Version:  unknown-version
        Path:     /usr/libexec/docker/cli-plugins/docker-trust

    docker trust --help
    Usage:  docker trust [OPTIONS] COMMAND

    Extended build capabilities with BuildKit

    Options:
      -D, --debug   Enable debug logging

    Management Commands:
      key         Manage keys for signing Docker images
      signer      Manage entities who can sign Docker images

    Commands:
      inspect     Return low-level information about keys and signatures
      revoke      Remove trust for an image
      sign        Sign an image

    Run 'docker trust COMMAND --help' for more information on a command.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-11-06 15:24:46 +01:00

154 lines
4.7 KiB
Go

package trust
import (
"io"
"testing"
"github.com/docker/cli/cmd/docker-trust/internal/test"
"github.com/docker/cli/cmd/docker-trust/internal/test/notary"
"github.com/theupdateframework/notary/client"
"gotest.tools/v3/assert"
"gotest.tools/v3/golden"
)
func TestTrustInspectCommandErrors(t *testing.T) {
testCases := []struct {
name string
args []string
expectedError string
}{
{
name: "not-enough-args",
expectedError: "requires at least 1 argument",
},
{
name: "sha-reference",
args: []string{"870d292919d01a0af7e7f056271dc78792c05f55f49b9b9012b6d89725bd9abd"},
expectedError: "invalid repository name",
},
{
name: "invalid-img-reference",
args: []string{"ALPINE"},
expectedError: "invalid reference format",
},
}
for _, tc := range testCases {
cmd := newInspectCommand(
test.NewFakeCli(&fakeClient{}))
cmd.Flags().Set("pretty", "true")
cmd.SetArgs(tc.args)
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
}
}
func TestTrustInspectCommandRepositoryErrors(t *testing.T) {
testCases := []struct {
doc string
args []string
notaryRepository func() (client.Repository, error)
err string
golden string
}{
{
doc: "OfflineErrors",
args: []string{"nonexistent-reg-name.io/image"},
notaryRepository: notary.GetOfflineNotaryRepository,
err: "no signatures or cannot access nonexistent-reg-name.io/image",
},
{
doc: "OfflineErrorsWithImageTag",
args: []string{"nonexistent-reg-name.io/image:tag"},
notaryRepository: notary.GetOfflineNotaryRepository,
err: "no signatures or cannot access nonexistent-reg-name.io/image:tag",
},
{
doc: "UninitializedErrors",
args: []string{"reg/unsigned-img"},
notaryRepository: notary.GetUninitializedNotaryRepository,
err: "no signatures or cannot access reg/unsigned-img",
golden: "trust-inspect-uninitialized.golden",
},
{
doc: "UninitializedErrorsWithImageTag",
args: []string{"reg/unsigned-img:tag"},
notaryRepository: notary.GetUninitializedNotaryRepository,
err: "no signatures or cannot access reg/unsigned-img:tag",
golden: "trust-inspect-uninitialized.golden",
},
}
for _, tc := range testCases {
t.Run(tc.doc, func(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{})
cli.SetNotaryClient(tc.notaryRepository)
cmd := newInspectCommand(cli)
cmd.SetArgs(tc.args)
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
assert.ErrorContains(t, cmd.Execute(), tc.err)
if tc.golden != "" {
golden.Assert(t, cli.OutBuffer().String(), tc.golden)
}
})
}
}
func TestTrustInspectCommand(t *testing.T) {
testCases := []struct {
doc string
args []string
notaryRepository func() (client.Repository, error)
golden string
}{
{
doc: "EmptyNotaryRepo",
args: []string{"reg/img:unsigned-tag"},
notaryRepository: notary.GetEmptyTargetsNotaryRepository,
golden: "trust-inspect-empty-repo.golden",
},
{
doc: "FullRepoWithoutSigners",
args: []string{"signed-repo"},
notaryRepository: notary.GetLoadedWithNoSignersNotaryRepository,
golden: "trust-inspect-full-repo-no-signers.golden",
},
{
doc: "OneTagWithoutSigners",
args: []string{"signed-repo:green"},
notaryRepository: notary.GetLoadedWithNoSignersNotaryRepository,
golden: "trust-inspect-one-tag-no-signers.golden",
},
{
doc: "FullRepoWithSigners",
args: []string{"signed-repo"},
notaryRepository: notary.GetLoadedNotaryRepository,
golden: "trust-inspect-full-repo-with-signers.golden",
},
{
doc: "MultipleFullReposWithSigners",
args: []string{"signed-repo", "signed-repo"},
notaryRepository: notary.GetLoadedNotaryRepository,
golden: "trust-inspect-multiple-repos-with-signers.golden",
},
{
doc: "UnsignedTagInSignedRepo",
args: []string{"signed-repo:unsigned"},
notaryRepository: notary.GetLoadedNotaryRepository,
golden: "trust-inspect-unsigned-tag-with-signers.golden",
},
}
for _, tc := range testCases {
t.Run(tc.doc, func(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{})
cli.SetNotaryClient(tc.notaryRepository)
cmd := newInspectCommand(cli)
cmd.SetArgs(tc.args)
assert.NilError(t, cmd.Execute())
golden.Assert(t, cli.OutBuffer().String(), tc.golden)
})
}
}