github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/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/assert"
    10  	is "gotest.tools/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  	for _, testcase := range cases {
   122  		signedTags := []SignedTagInfo{
   123  			{Name: "tag1", Digest: "deadbeef", Signers: []string{"alice"}},
   124  			{Name: "tag2", Digest: "aaaaaaaa", Signers: []string{"alice", "bob"}},
   125  			{Name: "tag3", Digest: "bbbbbbbb", Signers: []string{}},
   126  		}
   127  		out := bytes.NewBufferString("")
   128  		testcase.context.Output = out
   129  		err := TagWrite(testcase.context, signedTags)
   130  		if err != nil {
   131  			assert.Error(t, err, testcase.expected)
   132  		} else {
   133  			assert.Check(t, is.Equal(testcase.expected, out.String()))
   134  		}
   135  	}
   136  }
   137  
   138  // With no trust data, the TagWrite will print an empty table:
   139  // it's up to the caller to decide whether or not to print this versus an error
   140  func TestTrustTagContextEmptyWrite(t *testing.T) {
   141  
   142  	emptyCase := struct {
   143  		context  formatter.Context
   144  		expected string
   145  	}{
   146  		formatter.Context{
   147  			Format: NewTrustTagFormat(),
   148  		},
   149  		`SIGNED TAG          DIGEST              SIGNERS
   150  `,
   151  	}
   152  
   153  	emptySignedTags := []SignedTagInfo{}
   154  	out := bytes.NewBufferString("")
   155  	emptyCase.context.Output = out
   156  	err := TagWrite(emptyCase.context, emptySignedTags)
   157  	assert.NilError(t, err)
   158  	assert.Check(t, is.Equal(emptyCase.expected, out.String()))
   159  }
   160  
   161  func TestSignerInfoContextEmptyWrite(t *testing.T) {
   162  	emptyCase := struct {
   163  		context  formatter.Context
   164  		expected string
   165  	}{
   166  		formatter.Context{
   167  			Format: NewSignerInfoFormat(),
   168  		},
   169  		`SIGNER              KEYS
   170  `,
   171  	}
   172  	emptySignerInfo := []SignerInfo{}
   173  	out := bytes.NewBufferString("")
   174  	emptyCase.context.Output = out
   175  	err := SignerInfoWrite(emptyCase.context, emptySignerInfo)
   176  	assert.NilError(t, err)
   177  	assert.Check(t, is.Equal(emptyCase.expected, out.String()))
   178  }
   179  
   180  func TestSignerInfoContextWrite(t *testing.T) {
   181  	cases := []struct {
   182  		context  formatter.Context
   183  		expected string
   184  	}{
   185  		// Errors
   186  		{
   187  			formatter.Context{
   188  				Format: "{{InvalidFunction}}",
   189  			},
   190  			`Template parsing error: template: :1: function "InvalidFunction" not defined
   191  `,
   192  		},
   193  		{
   194  			formatter.Context{
   195  				Format: "{{nil}}",
   196  			},
   197  			`Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
   198  `,
   199  		},
   200  		// Table Format
   201  		{
   202  			formatter.Context{
   203  				Format: NewSignerInfoFormat(),
   204  				Trunc:  true,
   205  			},
   206  			`SIGNER              KEYS
   207  alice               key11, key12
   208  bob                 key21
   209  eve                 foobarbazqux, key31, key32
   210  `,
   211  		},
   212  		// No truncation
   213  		{
   214  			formatter.Context{
   215  				Format: NewSignerInfoFormat(),
   216  			},
   217  			`SIGNER              KEYS
   218  alice               key11, key12
   219  bob                 key21
   220  eve                 foobarbazquxquux, key31, key32
   221  `,
   222  		},
   223  	}
   224  
   225  	for _, testcase := range cases {
   226  		signerInfo := []SignerInfo{
   227  			{Name: "alice", Keys: []string{"key11", "key12"}},
   228  			{Name: "bob", Keys: []string{"key21"}},
   229  			{Name: "eve", Keys: []string{"key31", "key32", "foobarbazquxquux"}},
   230  		}
   231  		out := bytes.NewBufferString("")
   232  		testcase.context.Output = out
   233  		err := SignerInfoWrite(testcase.context, signerInfo)
   234  		if err != nil {
   235  			assert.Error(t, err, testcase.expected)
   236  		} else {
   237  			assert.Check(t, is.Equal(testcase.expected, out.String()))
   238  		}
   239  	}
   240  }