update to stderr instead of stdout, update tests

Signed-off-by: Riyaz Faizullabhoy <riyaz.faizullabhoy@docker.com>
This commit is contained in:
Riyaz Faizullabhoy 2017-10-25 19:13:24 +02:00
parent b4ef2ddb8b
commit 079471ebeb
6 changed files with 23 additions and 26 deletions

View File

@ -16,6 +16,7 @@ import (
"github.com/docker/notary/trustmanager" "github.com/docker/notary/trustmanager"
"github.com/docker/notary/tuf/data" "github.com/docker/notary/tuf/data"
tufutils "github.com/docker/notary/tuf/utils" tufutils "github.com/docker/notary/tuf/utils"
"github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -85,7 +86,7 @@ func validateAndGenerateKey(streams command.Streams, keyName string, workingDir
pubPEM, err := generateKeyAndOutputPubPEM(keyName, privKeyFileStore) pubPEM, err := generateKeyAndOutputPubPEM(keyName, privKeyFileStore)
if err != nil { if err != nil {
fmt.Fprintf(streams.Out(), err.Error()) fmt.Fprintf(streams.Out(), err.Error())
return fmt.Errorf("Error generating key for: %s", keyName) return errors.Wrapf(err, "failed to generate key for %s", keyName)
} }
// Output the public key to a file in the CWD or specified dir // Output the public key to a file in the CWD or specified dir
@ -124,7 +125,7 @@ func writePubKeyPEMToDir(pubPEM pem.Block, keyName, workingDir string) (string,
pubFileName := strings.Join([]string{keyName, "pub"}, ".") pubFileName := strings.Join([]string{keyName, "pub"}, ".")
pubFilePath := filepath.Join(workingDir, pubFileName) pubFilePath := filepath.Join(workingDir, pubFileName)
if err := ioutil.WriteFile(pubFilePath, pem.EncodeToMemory(&pubPEM), notary.PrivNoExecPerms); err != nil { if err := ioutil.WriteFile(pubFilePath, pem.EncodeToMemory(&pubPEM), notary.PrivNoExecPerms); err != nil {
return "", fmt.Errorf("Error writing public key to location: %s", pubFilePath) return "", errors.Wrapf(err, "failed to write public key to %s", pubFilePath)
} }
return pubFilePath, nil return pubFilePath, nil
} }

View File

@ -13,6 +13,7 @@ import (
"github.com/docker/notary/storage" "github.com/docker/notary/storage"
"github.com/docker/notary/trustmanager" "github.com/docker/notary/trustmanager"
tufutils "github.com/docker/notary/tuf/utils" tufutils "github.com/docker/notary/tuf/utils"
"github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -54,10 +55,10 @@ func loadPrivKey(streams command.Streams, keyPath string, options keyLoadOptions
passRet := trust.GetPassphraseRetriever(streams.In(), streams.Out()) passRet := trust.GetPassphraseRetriever(streams.In(), streams.Out())
keyBytes, err := getPrivKeyBytesFromPath(keyPath) keyBytes, err := getPrivKeyBytesFromPath(keyPath)
if err != nil { if err != nil {
return fmt.Errorf("error reading key from %s: %s", keyPath, err) return errors.Wrapf(err, "error reading key from %s", keyPath)
} }
if err := loadPrivKeyBytesToStore(keyBytes, privKeyImporters, keyPath, options.keyName, passRet); err != nil { if err := loadPrivKeyBytesToStore(keyBytes, privKeyImporters, keyPath, options.keyName, passRet); err != nil {
return fmt.Errorf("error importing key from %s: %s", keyPath, err) return errors.Wrapf(err, "error importing key from %s", keyPath)
} }
fmt.Fprintf(streams.Out(), "Successfully imported key from %s\n", keyPath) fmt.Fprintf(streams.Out(), "Successfully imported key from %s\n", keyPath)
return nil return nil
@ -78,11 +79,7 @@ func getPrivKeyBytesFromPath(keyPath string) ([]byte, error) {
} }
defer from.Close() defer from.Close()
keyBytes, err := ioutil.ReadAll(from) return ioutil.ReadAll(from)
if err != nil {
return nil, err
}
return keyBytes, nil
} }
func loadPrivKeyBytesToStore(privKeyBytes []byte, privKeyImporters []trustmanager.Importer, keyPath, keyName string, passRet notary.PassRetriever) error { func loadPrivKeyBytesToStore(privKeyBytes []byte, privKeyImporters []trustmanager.Importer, keyPath, keyName string, passRet notary.PassRetriever) error {

View File

@ -63,7 +63,7 @@ func addSigner(cli command.Cli, options signerAddOptions) error {
var errImages []string var errImages []string
for _, imageName := range options.images { for _, imageName := range options.images {
if err := addSignerToImage(cli, signerName, imageName, options.keys.GetAll()); err != nil { if err := addSignerToImage(cli, signerName, imageName, options.keys.GetAll()); err != nil {
fmt.Fprintln(cli.Out(), err.Error()) fmt.Fprintln(cli.Err(), err.Error())
errImages = append(errImages, imageName) errImages = append(errImages, imageName)
} else { } else {
fmt.Fprintf(cli.Out(), "Successfully added signer: %s to %s\n", signerName, imageName) fmt.Fprintf(cli.Out(), "Successfully added signer: %s to %s\n", signerName, imageName)
@ -124,7 +124,7 @@ func ingestPublicKeys(pubKeyPaths []string) ([]data.PublicKey, error) {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return nil, fmt.Errorf("file for public key does not exist: %s", pubKeyPath) return nil, fmt.Errorf("file for public key does not exist: %s", pubKeyPath)
} }
return nil, fmt.Errorf("unable to read public key from file: %s", pubKeyPath) return nil, errors.Wrapf(err, "unable to read public key from file: %s", pubKeyPath)
} }
// Parse PEM bytes into type PublicKey // Parse PEM bytes into type PublicKey

View File

@ -82,10 +82,10 @@ func TestSignerAddCommandNoTargetsKey(t *testing.T) {
assert.EqualError(t, cmd.Execute(), "Failed to add signer to: alpine, linuxkit/alpine") assert.EqualError(t, cmd.Execute(), "Failed to add signer to: alpine, linuxkit/alpine")
assert.Contains(t, cli.OutBuffer().String(), "Adding signer \"alice\" to alpine...") assert.Contains(t, cli.OutBuffer().String(), "Adding signer \"alice\" to alpine...")
assert.Contains(t, cli.OutBuffer().String(), "no valid public key found") assert.Contains(t, cli.ErrBuffer().String(), "no valid public key found")
assert.Contains(t, cli.OutBuffer().String(), "Adding signer \"alice\" to linuxkit/alpine...") assert.Contains(t, cli.OutBuffer().String(), "Adding signer \"alice\" to linuxkit/alpine...")
assert.Contains(t, cli.OutBuffer().String(), "no valid public key found") assert.Contains(t, cli.ErrBuffer().String(), "no valid public key found")
} }
func TestSignerAddCommandBadKeyPath(t *testing.T) { func TestSignerAddCommandBadKeyPath(t *testing.T) {
@ -102,8 +102,8 @@ func TestSignerAddCommandBadKeyPath(t *testing.T) {
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
assert.EqualError(t, cmd.Execute(), "Failed to add signer to: alpine") assert.EqualError(t, cmd.Execute(), "Failed to add signer to: alpine")
expectedError := "\nAdding signer \"alice\" to alpine...\nfile for public key does not exist: /path/to/key.pem" expectedError := "file for public key does not exist: /path/to/key.pem"
assert.Contains(t, cli.OutBuffer().String(), expectedError) assert.Contains(t, cli.ErrBuffer().String(), expectedError)
} }
func TestSignerAddCommandInvalidRepoName(t *testing.T) { func TestSignerAddCommandInvalidRepoName(t *testing.T) {
@ -120,11 +120,9 @@ func TestSignerAddCommandInvalidRepoName(t *testing.T) {
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
assert.EqualError(t, cmd.Execute(), "Failed to add signer to: 870d292919d01a0af7e7f056271dc78792c05f55f49b9b9012b6d89725bd9abd") assert.EqualError(t, cmd.Execute(), "Failed to add signer to: 870d292919d01a0af7e7f056271dc78792c05f55f49b9b9012b6d89725bd9abd")
expectedOutput := fmt.Sprintf("\nAdding signer \"alice\" to %s...\n"+ expectedErr := fmt.Sprintf("invalid repository name (%s), cannot specify 64-byte hexadecimal strings\n", imageName)
"invalid repository name (%s), cannot specify 64-byte hexadecimal strings\n",
imageName, imageName)
assert.Equal(t, expectedOutput, cli.OutBuffer().String()) assert.Equal(t, expectedErr, cli.ErrBuffer().String())
} }
func TestIngestPublicKeys(t *testing.T) { func TestIngestPublicKeys(t *testing.T) {

View File

@ -12,6 +12,7 @@ import (
"github.com/docker/cli/cli/trust" "github.com/docker/cli/cli/trust"
"github.com/docker/notary/client" "github.com/docker/notary/client"
"github.com/docker/notary/tuf/data" "github.com/docker/notary/tuf/data"
"github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -42,7 +43,7 @@ func removeSigner(cli command.Cli, options signerRemoveOptions) error {
var errImages []string var errImages []string
for _, image := range options.images { for _, image := range options.images {
if err := removeSingleSigner(cli, image, options.signer, options.forceYes); err != nil { if err := removeSingleSigner(cli, image, options.signer, options.forceYes); err != nil {
fmt.Fprintln(cli.Out(), err.Error()) fmt.Fprintln(cli.Err(), err.Error())
errImages = append(errImages, image) errImages = append(errImages, image)
} }
} }
@ -93,7 +94,7 @@ func removeSingleSigner(cli command.Cli, imageName, signerName string, forceYes
} }
delegationRoles, err := notaryRepo.GetDelegationRoles() delegationRoles, err := notaryRepo.GetDelegationRoles()
if err != nil { if err != nil {
return fmt.Errorf("Error retrieving signers for %s", imageName) return errors.Wrapf(err, "error retrieving signers for %s", imageName)
} }
var role data.Role var role data.Role
for _, delRole := range delegationRoles { for _, delRole := range delegationRoles {
@ -121,7 +122,7 @@ func removeSingleSigner(cli command.Cli, imageName, signerName string, forceYes
return nil return nil
} }
} else if err != nil { } else if err != nil {
fmt.Fprintln(cli.Out(), err.Error()) return err
} }
if err = notaryRepo.RemoveDelegationKeys(releasesRoleTUFName, role.KeyIDs); err != nil { if err = notaryRepo.RemoveDelegationKeys(releasesRoleTUFName, role.KeyIDs); err != nil {
return err return err

View File

@ -42,7 +42,7 @@ func TestTrustSignerRemoveErrors(t *testing.T) {
{ {
name: "not-an-image", name: "not-an-image",
args: []string{"user", "notanimage"}, args: []string{"user", "notanimage"},
expectedError: "Error retrieving signers for notanimage", expectedError: "error retrieving signers for notanimage",
}, },
{ {
name: "sha-reference", name: "sha-reference",
@ -62,7 +62,7 @@ func TestTrustSignerRemoveErrors(t *testing.T) {
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
cmd.Execute() cmd.Execute()
assert.Contains(t, cli.OutBuffer().String(), tc.expectedError) assert.Contains(t, cli.ErrBuffer().String(), tc.expectedError)
} }
} }
@ -83,8 +83,8 @@ func TestRemoveMultipleSigners(t *testing.T) {
cli.SetNotaryClient(getLoadedNotaryRepository) cli.SetNotaryClient(getLoadedNotaryRepository)
err := removeSigner(cli, signerRemoveOptions{signer: "test", images: []string{"signed-repo", "signed-repo"}, forceYes: true}) err := removeSigner(cli, signerRemoveOptions{signer: "test", images: []string{"signed-repo", "signed-repo"}, forceYes: true})
assert.EqualError(t, err, "Error removing signer from: signed-repo, signed-repo") assert.EqualError(t, err, "Error removing signer from: signed-repo, signed-repo")
assert.Contains(t, cli.OutBuffer().String(), assert.Contains(t, cli.ErrBuffer().String(),
"\nRemoving signer \"test\" from signed-repo...\nNo signer test for image signed-repo\n\nRemoving signer \"test\" from signed-repo...\nNo signer test for image signed-repo") "No signer test for image signed-repo")
} }
func TestRemoveLastSignerWarning(t *testing.T) { func TestRemoveLastSignerWarning(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{}) cli := test.NewFakeCli(&fakeClient{})