github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/trust/inspect_test.go (about) 1 package trust 2 3 import ( 4 "io/ioutil" 5 "testing" 6 7 "github.com/docker/cli/cli/trust" 8 "github.com/docker/cli/internal/test" 9 "github.com/docker/cli/internal/test/notary" 10 "github.com/theupdateframework/notary/client" 11 "gotest.tools/v3/assert" 12 "gotest.tools/v3/golden" 13 ) 14 15 func TestTrustInspectCommandErrors(t *testing.T) { 16 testCases := []struct { 17 name string 18 args []string 19 expectedError string 20 }{ 21 { 22 name: "not-enough-args", 23 expectedError: "requires at least 1 argument", 24 }, 25 { 26 name: "sha-reference", 27 args: []string{"870d292919d01a0af7e7f056271dc78792c05f55f49b9b9012b6d89725bd9abd"}, 28 expectedError: "invalid repository name", 29 }, 30 { 31 name: "invalid-img-reference", 32 args: []string{"ALPINE"}, 33 expectedError: "invalid reference format", 34 }, 35 } 36 for _, tc := range testCases { 37 cmd := newInspectCommand( 38 test.NewFakeCli(&fakeClient{})) 39 cmd.Flags().Set("pretty", "true") 40 cmd.SetArgs(tc.args) 41 cmd.SetOut(ioutil.Discard) 42 assert.ErrorContains(t, cmd.Execute(), tc.expectedError) 43 } 44 } 45 46 func TestTrustInspectCommandRepositoryErrors(t *testing.T) { 47 testCases := []struct { 48 doc string 49 args []string 50 notaryRepository func(trust.ImageRefAndAuth, []string) (client.Repository, error) 51 err string 52 golden string 53 }{ 54 { 55 doc: "OfflineErrors", 56 args: []string{"nonexistent-reg-name.io/image"}, 57 notaryRepository: notary.GetOfflineNotaryRepository, 58 err: "No signatures or cannot access nonexistent-reg-name.io/image", 59 }, 60 { 61 doc: "OfflineErrorsWithImageTag", 62 args: []string{"nonexistent-reg-name.io/image:tag"}, 63 notaryRepository: notary.GetOfflineNotaryRepository, 64 err: "No signatures or cannot access nonexistent-reg-name.io/image:tag", 65 }, 66 { 67 doc: "UninitializedErrors", 68 args: []string{"reg/unsigned-img"}, 69 notaryRepository: notary.GetUninitializedNotaryRepository, 70 err: "No signatures or cannot access reg/unsigned-img", 71 golden: "trust-inspect-uninitialized.golden", 72 }, 73 { 74 doc: "UninitializedErrorsWithImageTag", 75 args: []string{"reg/unsigned-img:tag"}, 76 notaryRepository: notary.GetUninitializedNotaryRepository, 77 err: "No signatures or cannot access reg/unsigned-img:tag", 78 golden: "trust-inspect-uninitialized.golden", 79 }, 80 } 81 82 for _, tc := range testCases { 83 t.Run(tc.doc, func(t *testing.T) { 84 cli := test.NewFakeCli(&fakeClient{}) 85 cli.SetNotaryClient(tc.notaryRepository) 86 cmd := newInspectCommand(cli) 87 cmd.SetArgs(tc.args) 88 cmd.SetOut(ioutil.Discard) 89 assert.ErrorContains(t, cmd.Execute(), tc.err) 90 if tc.golden != "" { 91 golden.Assert(t, cli.OutBuffer().String(), tc.golden) 92 } 93 }) 94 } 95 } 96 97 func TestTrustInspectCommand(t *testing.T) { 98 testCases := []struct { 99 doc string 100 args []string 101 notaryRepository func(trust.ImageRefAndAuth, []string) (client.Repository, error) 102 golden string 103 }{ 104 { 105 doc: "EmptyNotaryRepo", 106 args: []string{"reg/img:unsigned-tag"}, 107 notaryRepository: notary.GetEmptyTargetsNotaryRepository, 108 golden: "trust-inspect-empty-repo.golden", 109 }, 110 { 111 doc: "FullRepoWithoutSigners", 112 args: []string{"signed-repo"}, 113 notaryRepository: notary.GetLoadedWithNoSignersNotaryRepository, 114 golden: "trust-inspect-full-repo-no-signers.golden", 115 }, 116 { 117 doc: "OneTagWithoutSigners", 118 args: []string{"signed-repo:green"}, 119 notaryRepository: notary.GetLoadedWithNoSignersNotaryRepository, 120 golden: "trust-inspect-one-tag-no-signers.golden", 121 }, 122 { 123 doc: "FullRepoWithSigners", 124 args: []string{"signed-repo"}, 125 notaryRepository: notary.GetLoadedNotaryRepository, 126 golden: "trust-inspect-full-repo-with-signers.golden", 127 }, 128 { 129 doc: "MultipleFullReposWithSigners", 130 args: []string{"signed-repo", "signed-repo"}, 131 notaryRepository: notary.GetLoadedNotaryRepository, 132 golden: "trust-inspect-multiple-repos-with-signers.golden", 133 }, 134 { 135 doc: "UnsignedTagInSignedRepo", 136 args: []string{"signed-repo:unsigned"}, 137 notaryRepository: notary.GetLoadedNotaryRepository, 138 golden: "trust-inspect-unsigned-tag-with-signers.golden", 139 }, 140 } 141 142 for _, tc := range testCases { 143 t.Run(tc.doc, func(t *testing.T) { 144 cli := test.NewFakeCli(&fakeClient{}) 145 cli.SetNotaryClient(tc.notaryRepository) 146 cmd := newInspectCommand(cli) 147 cmd.SetArgs(tc.args) 148 assert.NilError(t, cmd.Execute()) 149 golden.Assert(t, cli.OutBuffer().String(), tc.golden) 150 }) 151 } 152 }