2017-11-15 14:50:33 -05:00
|
|
|
package trust
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/cli/internal/test"
|
2018-03-08 08:35:17 -05:00
|
|
|
"github.com/docker/cli/internal/test/notary"
|
2018-03-05 18:53:52 -05:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
2017-11-15 14:50:33 -05:00
|
|
|
"github.com/gotestyourself/gotestyourself/golden"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTrustInspectCommandErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "not-enough-args",
|
|
|
|
expectedError: "requires exactly 1 argument",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "too-many-args",
|
|
|
|
args: []string{"remote1", "remote2"},
|
|
|
|
expectedError: "requires exactly 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 := newViewCommand(
|
|
|
|
test.NewFakeCli(&fakeClient{}))
|
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-11-15 14:50:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTrustInspectCommandOfflineErrors(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{})
|
2018-03-08 08:35:17 -05:00
|
|
|
cli.SetNotaryClient(notary.GetOfflineNotaryRepository)
|
2017-11-15 14:50:33 -05:00
|
|
|
cmd := newInspectCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"nonexistent-reg-name.io/image"})
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), "No signatures or cannot access nonexistent-reg-name.io/image")
|
2017-11-15 14:50:33 -05:00
|
|
|
|
|
|
|
cli = test.NewFakeCli(&fakeClient{})
|
2018-03-08 08:35:17 -05:00
|
|
|
cli.SetNotaryClient(notary.GetOfflineNotaryRepository)
|
2017-11-15 14:50:33 -05:00
|
|
|
cmd = newInspectCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"nonexistent-reg-name.io/image:tag"})
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), "No signatures or cannot access nonexistent-reg-name.io/image")
|
2017-11-15 14:50:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTrustInspectCommandUninitializedErrors(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{})
|
2018-03-08 08:35:17 -05:00
|
|
|
cli.SetNotaryClient(notary.GetUninitializedNotaryRepository)
|
2017-11-15 14:50:33 -05:00
|
|
|
cmd := newInspectCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"reg/unsigned-img"})
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), "No signatures or cannot access reg/unsigned-img")
|
2017-11-17 15:04:51 -05:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "trust-inspect-uninitialized.golden")
|
2017-11-15 14:50:33 -05:00
|
|
|
|
|
|
|
cli = test.NewFakeCli(&fakeClient{})
|
2018-03-08 08:35:17 -05:00
|
|
|
cli.SetNotaryClient(notary.GetUninitializedNotaryRepository)
|
2017-11-15 14:50:33 -05:00
|
|
|
cmd = newInspectCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"reg/unsigned-img:tag"})
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), "No signatures or cannot access reg/unsigned-img:tag")
|
2017-11-17 15:04:51 -05:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "trust-inspect-uninitialized.golden")
|
2017-11-15 14:50:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTrustInspectCommandEmptyNotaryRepo(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{})
|
2018-03-08 08:35:17 -05:00
|
|
|
cli.SetNotaryClient(notary.GetEmptyTargetsNotaryRepository)
|
2017-11-15 14:50:33 -05:00
|
|
|
cmd := newInspectCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"reg/img:unsigned-tag"})
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-11-15 14:50:33 -05:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "trust-inspect-empty-repo.golden")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTrustInspectCommandFullRepoWithoutSigners(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{})
|
2018-03-08 08:35:17 -05:00
|
|
|
cli.SetNotaryClient(notary.GetLoadedWithNoSignersNotaryRepository)
|
2017-11-15 14:50:33 -05:00
|
|
|
cmd := newInspectCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"signed-repo"})
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-11-15 14:50:33 -05:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "trust-inspect-full-repo-no-signers.golden")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTrustInspectCommandOneTagWithoutSigners(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{})
|
2018-03-08 08:35:17 -05:00
|
|
|
cli.SetNotaryClient(notary.GetLoadedWithNoSignersNotaryRepository)
|
2017-11-15 14:50:33 -05:00
|
|
|
cmd := newInspectCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"signed-repo:green"})
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-11-15 14:50:33 -05:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "trust-inspect-one-tag-no-signers.golden")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTrustInspectCommandFullRepoWithSigners(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{})
|
2018-03-08 08:35:17 -05:00
|
|
|
cli.SetNotaryClient(notary.GetLoadedNotaryRepository)
|
2017-11-15 14:50:33 -05:00
|
|
|
cmd := newInspectCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"signed-repo"})
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-11-15 14:50:33 -05:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "trust-inspect-full-repo-with-signers.golden")
|
|
|
|
}
|
|
|
|
|
2017-11-16 13:09:57 -05:00
|
|
|
func TestTrustInspectCommandMultipleFullReposWithSigners(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{})
|
2018-03-08 08:35:17 -05:00
|
|
|
cli.SetNotaryClient(notary.GetLoadedNotaryRepository)
|
2017-11-16 13:09:57 -05:00
|
|
|
cmd := newInspectCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"signed-repo", "signed-repo"})
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-11-16 13:09:57 -05:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "trust-inspect-multiple-repos-with-signers.golden")
|
|
|
|
}
|
|
|
|
|
2017-11-15 14:50:33 -05:00
|
|
|
func TestTrustInspectCommandUnsignedTagInSignedRepo(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{})
|
2018-03-08 08:35:17 -05:00
|
|
|
cli.SetNotaryClient(notary.GetLoadedNotaryRepository)
|
2017-11-15 14:50:33 -05:00
|
|
|
cmd := newInspectCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"signed-repo:unsigned"})
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-11-15 14:50:33 -05:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "trust-inspect-unsigned-tag-with-signers.golden")
|
|
|
|
}
|