github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/trust/formatter_test.go (about) 1 package trust 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/docker/cli/cli/command/formatter" 8 "github.com/docker/docker/pkg/stringid" 9 "gotest.tools/v3/assert" 10 is "gotest.tools/v3/assert/cmp" 11 ) 12 13 func TestTrustTag(t *testing.T) { 14 digest := stringid.GenerateRandomID() 15 trustedTag := "tag" 16 17 var ctx trustTagContext 18 19 cases := []struct { 20 trustTagCtx trustTagContext 21 expValue string 22 call func() string 23 }{ 24 { 25 trustTagContext{ 26 s: SignedTagInfo{ 27 Name: trustedTag, 28 Digest: digest, 29 Signers: nil, 30 }, 31 }, 32 digest, 33 ctx.Digest, 34 }, 35 { 36 trustTagContext{ 37 s: SignedTagInfo{ 38 Name: trustedTag, 39 Digest: digest, 40 Signers: nil, 41 }, 42 }, 43 trustedTag, 44 ctx.SignedTag, 45 }, 46 // Empty signers makes a row with empty string 47 { 48 trustTagContext{ 49 s: SignedTagInfo{ 50 Name: trustedTag, 51 Digest: digest, 52 Signers: nil, 53 }, 54 }, 55 "", 56 ctx.Signers, 57 }, 58 { 59 trustTagContext{ 60 s: SignedTagInfo{ 61 Name: trustedTag, 62 Digest: digest, 63 Signers: []string{"alice", "bob", "claire"}, 64 }, 65 }, 66 "alice, bob, claire", 67 ctx.Signers, 68 }, 69 // alphabetic signing on Signers 70 { 71 trustTagContext{ 72 s: SignedTagInfo{ 73 Name: trustedTag, 74 Digest: digest, 75 Signers: []string{"claire", "bob", "alice"}, 76 }, 77 }, 78 "alice, bob, claire", 79 ctx.Signers, 80 }, 81 } 82 83 for _, c := range cases { 84 ctx = c.trustTagCtx 85 v := c.call() 86 if v != c.expValue { 87 t.Fatalf("Expected %s, was %s\n", c.expValue, v) 88 } 89 } 90 } 91 92 func TestTrustTagContextWrite(t *testing.T) { 93 cases := []struct { 94 context formatter.Context 95 expected string 96 }{ 97 // Errors 98 { 99 formatter.Context{ 100 Format: "{{InvalidFunction}}", 101 }, 102 `template parsing error: template: :1: function "InvalidFunction" not defined`, 103 }, 104 { 105 formatter.Context{ 106 Format: "{{nil}}", 107 }, 108 `template parsing error: template: :1:2: executing "" at <nil>: nil is not a command`, 109 }, 110 // Table Format 111 { 112 formatter.Context{ 113 Format: NewTrustTagFormat(), 114 }, 115 `SIGNED TAG DIGEST SIGNERS 116 tag1 deadbeef alice 117 tag2 aaaaaaaa alice, bob 118 tag3 bbbbbbbb 119 `, 120 }, 121 } 122 123 signedTags := []SignedTagInfo{ 124 {Name: "tag1", Digest: "deadbeef", Signers: []string{"alice"}}, 125 {Name: "tag2", Digest: "aaaaaaaa", Signers: []string{"alice", "bob"}}, 126 {Name: "tag3", Digest: "bbbbbbbb", Signers: []string{}}, 127 } 128 129 for _, tc := range cases { 130 tc := tc 131 t.Run(string(tc.context.Format), func(t *testing.T) { 132 var out bytes.Buffer 133 tc.context.Output = &out 134 135 if err := TagWrite(tc.context, signedTags); err != nil { 136 assert.Error(t, err, tc.expected) 137 } else { 138 assert.Equal(t, out.String(), tc.expected) 139 } 140 }) 141 } 142 } 143 144 // With no trust data, the TagWrite will print an empty table: 145 // it's up to the caller to decide whether or not to print this versus an error 146 func TestTrustTagContextEmptyWrite(t *testing.T) { 147 emptyCase := struct { 148 context formatter.Context 149 expected string 150 }{ 151 formatter.Context{ 152 Format: NewTrustTagFormat(), 153 }, 154 `SIGNED TAG DIGEST SIGNERS 155 `, 156 } 157 158 emptySignedTags := []SignedTagInfo{} 159 out := bytes.NewBufferString("") 160 emptyCase.context.Output = out 161 err := TagWrite(emptyCase.context, emptySignedTags) 162 assert.NilError(t, err) 163 assert.Check(t, is.Equal(emptyCase.expected, out.String())) 164 } 165 166 func TestSignerInfoContextEmptyWrite(t *testing.T) { 167 emptyCase := struct { 168 context formatter.Context 169 expected string 170 }{ 171 formatter.Context{ 172 Format: NewSignerInfoFormat(), 173 }, 174 `SIGNER KEYS 175 `, 176 } 177 emptySignerInfo := []SignerInfo{} 178 out := bytes.NewBufferString("") 179 emptyCase.context.Output = out 180 err := SignerInfoWrite(emptyCase.context, emptySignerInfo) 181 assert.NilError(t, err) 182 assert.Check(t, is.Equal(emptyCase.expected, out.String())) 183 } 184 185 func TestSignerInfoContextWrite(t *testing.T) { 186 cases := []struct { 187 context formatter.Context 188 expected string 189 }{ 190 // Errors 191 { 192 formatter.Context{ 193 Format: "{{InvalidFunction}}", 194 }, 195 `template parsing error: template: :1: function "InvalidFunction" not defined`, 196 }, 197 { 198 formatter.Context{ 199 Format: "{{nil}}", 200 }, 201 `template parsing error: template: :1:2: executing "" at <nil>: nil is not a command`, 202 }, 203 // Table Format 204 { 205 formatter.Context{ 206 Format: NewSignerInfoFormat(), 207 Trunc: true, 208 }, 209 `SIGNER KEYS 210 alice key11, key12 211 bob key21 212 eve foobarbazqux, key31, key32 213 `, 214 }, 215 // No truncation 216 { 217 formatter.Context{ 218 Format: NewSignerInfoFormat(), 219 }, 220 `SIGNER KEYS 221 alice key11, key12 222 bob key21 223 eve foobarbazquxquux, key31, key32 224 `, 225 }, 226 } 227 228 signerInfo := []SignerInfo{ 229 {Name: "alice", Keys: []string{"key11", "key12"}}, 230 {Name: "bob", Keys: []string{"key21"}}, 231 {Name: "eve", Keys: []string{"key31", "key32", "foobarbazquxquux"}}, 232 } 233 for _, tc := range cases { 234 tc := tc 235 t.Run(string(tc.context.Format), func(t *testing.T) { 236 var out bytes.Buffer 237 tc.context.Output = &out 238 239 if err := SignerInfoWrite(tc.context, signerInfo); err != nil { 240 assert.Error(t, err, tc.expected) 241 } else { 242 assert.Equal(t, out.String(), tc.expected) 243 } 244 }) 245 } 246 }