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