Print out a different message for list keys if no keys are found.

Signed-off-by: Ying Li <ying.li@docker.com>
This commit is contained in:
Ying Li 2015-11-13 15:44:56 -08:00
parent 51cb6e7296
commit eb9de9f0e8
3 changed files with 34 additions and 1 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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])
}