diff --git a/cmd/notary/integration_test.go b/cmd/notary/integration_test.go index da834e93c7..6046a58520 100644 --- a/cmd/notary/integration_test.go +++ b/cmd/notary/integration_test.go @@ -172,6 +172,12 @@ func getUniqueKeys(t *testing.T, tempDir string) ([]string, []string) { output, err := runCommand(t, tempDir, "key", "list") assert.NoError(t, err) lines := splitLines(output) + if len(lines) == 1 && lines[0] == "No signing keys found." { + return []string{}, []string{} + } + if len(lines) < 3 { // 2 lines of header, at least 1 line with keys + t.Logf("This output is not what is expected by the test:\n%s", output) + } var ( rootMap = make(map[string]bool) diff --git a/cmd/notary/keys.go b/cmd/notary/keys.go index 6de84c6929..aab55651ba 100644 --- a/cmd/notary/keys.go +++ b/cmd/notary/keys.go @@ -169,6 +169,11 @@ func prettyPrintKeys(keyStores []trustmanager.KeyStore, writer io.Writer) { }) } } + if len(info) == 0 { + writer.Write([]byte("No signing keys found.\n")) + return + } + sort.Stable(keyInfoSorter(info)) table := tablewriter.NewWriter(writer) diff --git a/cmd/notary/keys_test.go b/cmd/notary/keys_test.go index d3c1a55126..64a0abb43c 100644 --- a/cmd/notary/keys_test.go +++ b/cmd/notary/keys_test.go @@ -61,7 +61,7 @@ func (l *otherMemoryStore) Name() string { // Given a list of key stores, the keys should be pretty-printed with their // roles, locations, IDs, and guns first in sorted order in the key store -func TestPrettyPrintKeys(t *testing.T) { +func TestPrettyPrintRootAndSigningKeys(t *testing.T) { ret := passphrase.ConstantRetriever("pass") keyStores := []trustmanager.KeyStore{ trustmanager.NewKeyMemoryStore(ret), @@ -103,6 +103,12 @@ func TestPrettyPrintKeys(t *testing.T) { lines := strings.Split(strings.TrimSpace(string(text)), "\n") assert.Len(t, lines, len(expected)+2) + + // starts with headers + assert.True(t, reflect.DeepEqual(strings.Fields(lines[0]), + []string{"ROLE", "GUN", "KEY", "ID", "LOCATION"})) + assert.Equal(t, "----", lines[1][:4]) + for i, line := range lines[2:] { // we are purposely not putting spaces in test data so easier to split splitted := strings.Fields(line) @@ -111,3 +117,19 @@ func TestPrettyPrintKeys(t *testing.T) { } } } + +// If there are no keys in any of the key stores, a message that there are no +// signing keys should be displayed. +func TestPrettyPrintZeroKeys(t *testing.T) { + ret := passphrase.ConstantRetriever("pass") + emptyKeyStore := trustmanager.NewKeyMemoryStore(ret) + + var b bytes.Buffer + prettyPrintKeys([]trustmanager.KeyStore{emptyKeyStore}, &b) + text, err := ioutil.ReadAll(&b) + assert.NoError(t, err) + + lines := strings.Split(strings.TrimSpace(string(text)), "\n") + assert.Len(t, lines, 1) + assert.Equal(t, "No signing keys found.", lines[0]) +}