mirror of https://github.com/docker/cli.git
refactor trust inspect command unit tests to table-driven style
Signed-off-by: Arash Deshmeh <adeshmeh@ca.ibm.com>
This commit is contained in:
parent
1d04f7d66b
commit
f8f0d72cf9
|
@ -4,8 +4,10 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/docker/cli/cli/trust"
|
||||||
"github.com/docker/cli/internal/test"
|
"github.com/docker/cli/internal/test"
|
||||||
"github.com/docker/cli/internal/test/notary"
|
"github.com/docker/cli/internal/test/notary"
|
||||||
|
"github.com/theupdateframework/notary/client"
|
||||||
"gotest.tools/assert"
|
"gotest.tools/assert"
|
||||||
"gotest.tools/golden"
|
"gotest.tools/golden"
|
||||||
)
|
)
|
||||||
|
@ -41,91 +43,110 @@ func TestTrustInspectCommandErrors(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTrustInspectCommandOfflineErrors(t *testing.T) {
|
func TestTrustInspectCommandRepositoryErrors(t *testing.T) {
|
||||||
cli := test.NewFakeCli(&fakeClient{})
|
testCases := []struct {
|
||||||
cli.SetNotaryClient(notary.GetOfflineNotaryRepository)
|
doc string
|
||||||
cmd := newInspectCommand(cli)
|
args []string
|
||||||
cmd.SetArgs([]string{"nonexistent-reg-name.io/image"})
|
notaryRepository func(trust.ImageRefAndAuth, []string) (client.Repository, error)
|
||||||
cmd.SetOutput(ioutil.Discard)
|
err string
|
||||||
assert.ErrorContains(t, cmd.Execute(), "No signatures or cannot access nonexistent-reg-name.io/image")
|
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",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
cli = test.NewFakeCli(&fakeClient{})
|
for _, tc := range testCases {
|
||||||
cli.SetNotaryClient(notary.GetOfflineNotaryRepository)
|
t.Run(tc.doc, func(t *testing.T) {
|
||||||
cmd = newInspectCommand(cli)
|
cli := test.NewFakeCli(&fakeClient{})
|
||||||
cmd.SetArgs([]string{"nonexistent-reg-name.io/image:tag"})
|
cli.SetNotaryClient(tc.notaryRepository)
|
||||||
cmd.SetOutput(ioutil.Discard)
|
cmd := newInspectCommand(cli)
|
||||||
assert.ErrorContains(t, cmd.Execute(), "No signatures or cannot access nonexistent-reg-name.io/image")
|
cmd.SetArgs(tc.args)
|
||||||
|
cmd.SetOutput(ioutil.Discard)
|
||||||
|
assert.ErrorContains(t, cmd.Execute(), tc.err)
|
||||||
|
if tc.golden != "" {
|
||||||
|
golden.Assert(t, cli.OutBuffer().String(), tc.golden)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTrustInspectCommandUninitializedErrors(t *testing.T) {
|
func TestTrustInspectCommand(t *testing.T) {
|
||||||
cli := test.NewFakeCli(&fakeClient{})
|
testCases := []struct {
|
||||||
cli.SetNotaryClient(notary.GetUninitializedNotaryRepository)
|
doc string
|
||||||
cmd := newInspectCommand(cli)
|
args []string
|
||||||
cmd.SetArgs([]string{"reg/unsigned-img"})
|
notaryRepository func(trust.ImageRefAndAuth, []string) (client.Repository, error)
|
||||||
cmd.SetOutput(ioutil.Discard)
|
golden string
|
||||||
assert.ErrorContains(t, cmd.Execute(), "No signatures or cannot access reg/unsigned-img")
|
}{
|
||||||
golden.Assert(t, cli.OutBuffer().String(), "trust-inspect-uninitialized.golden")
|
{
|
||||||
|
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",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
cli = test.NewFakeCli(&fakeClient{})
|
for _, tc := range testCases {
|
||||||
cli.SetNotaryClient(notary.GetUninitializedNotaryRepository)
|
t.Run(tc.doc, func(t *testing.T) {
|
||||||
cmd = newInspectCommand(cli)
|
cli := test.NewFakeCli(&fakeClient{})
|
||||||
cmd.SetArgs([]string{"reg/unsigned-img:tag"})
|
cli.SetNotaryClient(tc.notaryRepository)
|
||||||
cmd.SetOutput(ioutil.Discard)
|
cmd := newInspectCommand(cli)
|
||||||
assert.ErrorContains(t, cmd.Execute(), "No signatures or cannot access reg/unsigned-img:tag")
|
cmd.SetArgs(tc.args)
|
||||||
golden.Assert(t, cli.OutBuffer().String(), "trust-inspect-uninitialized.golden")
|
assert.NilError(t, cmd.Execute())
|
||||||
}
|
golden.Assert(t, cli.OutBuffer().String(), tc.golden)
|
||||||
|
})
|
||||||
func TestTrustInspectCommandEmptyNotaryRepo(t *testing.T) {
|
}
|
||||||
cli := test.NewFakeCli(&fakeClient{})
|
|
||||||
cli.SetNotaryClient(notary.GetEmptyTargetsNotaryRepository)
|
|
||||||
cmd := newInspectCommand(cli)
|
|
||||||
cmd.SetArgs([]string{"reg/img:unsigned-tag"})
|
|
||||||
cmd.SetOutput(ioutil.Discard)
|
|
||||||
assert.NilError(t, cmd.Execute())
|
|
||||||
golden.Assert(t, cli.OutBuffer().String(), "trust-inspect-empty-repo.golden")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTrustInspectCommandFullRepoWithoutSigners(t *testing.T) {
|
|
||||||
cli := test.NewFakeCli(&fakeClient{})
|
|
||||||
cli.SetNotaryClient(notary.GetLoadedWithNoSignersNotaryRepository)
|
|
||||||
cmd := newInspectCommand(cli)
|
|
||||||
cmd.SetArgs([]string{"signed-repo"})
|
|
||||||
assert.NilError(t, cmd.Execute())
|
|
||||||
golden.Assert(t, cli.OutBuffer().String(), "trust-inspect-full-repo-no-signers.golden")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTrustInspectCommandOneTagWithoutSigners(t *testing.T) {
|
|
||||||
cli := test.NewFakeCli(&fakeClient{})
|
|
||||||
cli.SetNotaryClient(notary.GetLoadedWithNoSignersNotaryRepository)
|
|
||||||
cmd := newInspectCommand(cli)
|
|
||||||
cmd.SetArgs([]string{"signed-repo:green"})
|
|
||||||
assert.NilError(t, cmd.Execute())
|
|
||||||
golden.Assert(t, cli.OutBuffer().String(), "trust-inspect-one-tag-no-signers.golden")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTrustInspectCommandFullRepoWithSigners(t *testing.T) {
|
|
||||||
cli := test.NewFakeCli(&fakeClient{})
|
|
||||||
cli.SetNotaryClient(notary.GetLoadedNotaryRepository)
|
|
||||||
cmd := newInspectCommand(cli)
|
|
||||||
cmd.SetArgs([]string{"signed-repo"})
|
|
||||||
assert.NilError(t, cmd.Execute())
|
|
||||||
golden.Assert(t, cli.OutBuffer().String(), "trust-inspect-full-repo-with-signers.golden")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTrustInspectCommandMultipleFullReposWithSigners(t *testing.T) {
|
|
||||||
cli := test.NewFakeCli(&fakeClient{})
|
|
||||||
cli.SetNotaryClient(notary.GetLoadedNotaryRepository)
|
|
||||||
cmd := newInspectCommand(cli)
|
|
||||||
cmd.SetArgs([]string{"signed-repo", "signed-repo"})
|
|
||||||
assert.NilError(t, cmd.Execute())
|
|
||||||
golden.Assert(t, cli.OutBuffer().String(), "trust-inspect-multiple-repos-with-signers.golden")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTrustInspectCommandUnsignedTagInSignedRepo(t *testing.T) {
|
|
||||||
cli := test.NewFakeCli(&fakeClient{})
|
|
||||||
cli.SetNotaryClient(notary.GetLoadedNotaryRepository)
|
|
||||||
cmd := newInspectCommand(cli)
|
|
||||||
cmd.SetArgs([]string{"signed-repo:unsigned"})
|
|
||||||
assert.NilError(t, cmd.Execute())
|
|
||||||
golden.Assert(t, cli.OutBuffer().String(), "trust-inspect-unsigned-tag-with-signers.golden")
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue