report outputFormat in PrintFlags err

This commit is contained in:
juanvallejo 2018-04-24 15:38:38 -04:00
parent 27bd4ded04
commit ac6ca38dd7
11 changed files with 60 additions and 59 deletions

View File

@ -693,7 +693,7 @@ run_pod_tests() {
kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'valid-pod:' kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'valid-pod:'
## Patch can modify a local object ## Patch can modify a local object
kubectl patch --local -f pkg/kubectl/validation/testdata/v1/validPod.yaml --patch='{"spec": {"restartPolicy":"Never"}}' -o jsonpath='{.spec.restartPolicy}' | grep -q "Never" kubectl patch --local -f pkg/kubectl/validation/testdata/v1/validPod.yaml --patch='{"spec": {"restartPolicy":"Never"}}' -o yaml | grep -q "Never"
## Patch fails with error message "not patched" and exit code 1 ## Patch fails with error message "not patched" and exit code 1
output_message=$(! kubectl patch "${kube_flags[@]}" pod valid-pod -p='{"spec":{"replicas":7}}' 2>&1) output_message=$(! kubectl patch "${kube_flags[@]}" pod valid-pod -p='{"spec":{"replicas":7}}' 2>&1)
@ -2139,8 +2139,7 @@ run_recursive_resources_tests() {
kube::test::get_object_assert deployment "{{range.items}}{{$id_field}}:{{end}}" 'nginx:' kube::test::get_object_assert deployment "{{range.items}}{{$id_field}}:{{end}}" 'nginx:'
kube::test::get_object_assert deployment "{{range.items}}{{$image_field0}}:{{end}}" "${IMAGE_DEPLOYMENT_R1}:" kube::test::get_object_assert deployment "{{range.items}}{{$image_field0}}:{{end}}" "${IMAGE_DEPLOYMENT_R1}:"
# Command # Command
output_message=$(kubectl convert --local -f hack/testdata/deployment-revision1.yaml --output-version=apps/v1beta1 -o go-template='{{ .apiVersion }}' "${kube_flags[@]}") output_message=$(kubectl convert --local -f hack/testdata/deployment-revision1.yaml --output-version=apps/v1beta1 -o yaml "${kube_flags[@]}")
echo $output_message
# Post-condition: apiVersion is still extensions/v1beta1 in the live deployment, but command output is the new value # Post-condition: apiVersion is still extensions/v1beta1 in the live deployment, but command output is the new value
kube::test::get_object_assert 'deployment nginx' "{{ .apiVersion }}" 'extensions/v1beta1' kube::test::get_object_assert 'deployment nginx' "{{ .apiVersion }}" 'extensions/v1beta1'
kube::test::if_has_string "${output_message}" "apps/v1beta1" kube::test::if_has_string "${output_message}" "apps/v1beta1"
@ -2851,7 +2850,7 @@ run_rc_tests() {
# Pre-condition: don't use --port flag # Pre-condition: don't use --port flag
output_message=$(kubectl expose -f test/fixtures/doc-yaml/admin/high-availability/etcd.yaml --selector=test=etcd 2>&1 "${kube_flags[@]}") output_message=$(kubectl expose -f test/fixtures/doc-yaml/admin/high-availability/etcd.yaml --selector=test=etcd 2>&1 "${kube_flags[@]}")
# Post-condition: expose succeeded # Post-condition: expose succeeded
kube::test::if_has_string "${output_message}" 'etcd-server\" exposed' kube::test::if_has_string "${output_message}" 'etcd-server exposed'
# Post-condition: generated service has both ports from the exposed pod # Post-condition: generated service has both ports from the exposed pod
kube::test::get_object_assert 'service etcd-server' "{{$port_name}} {{$port_field}}" 'port-1 2380' kube::test::get_object_assert 'service etcd-server' "{{$port_name}} {{$port_field}}" 'port-1 2380'
kube::test::get_object_assert 'service etcd-server' "{{$second_port_name}} {{$second_port_field}}" 'port-2 2379' kube::test::get_object_assert 'service etcd-server' "{{$second_port_name}} {{$second_port_field}}" 'port-2 2379'

View File

@ -60,8 +60,8 @@ type CertificateOptions struct {
csrNames []string csrNames []string
outputStyle string outputStyle string
clientSetFunc func() (internalclientset.Interface, error) clientSet internalclientset.Interface
builderFunc func() *resource.Builder builder *resource.Builder
genericclioptions.IOStreams genericclioptions.IOStreams
} }
@ -79,8 +79,11 @@ func (o *CertificateOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, arg
return printer.PrintObj(obj, out) return printer.PrintObj(obj, out)
} }
o.builderFunc = f.NewBuilder o.builder = f.NewBuilder()
o.clientSetFunc = f.ClientSet o.clientSet, err = f.ClientSet()
if err != nil {
return err
}
return nil return nil
} }
@ -127,7 +130,7 @@ func NewCmdCertificateApprove(f cmdutil.Factory, ioStreams genericclioptions.IOS
} }
func (o *CertificateOptions) RunCertificateApprove(force bool) error { func (o *CertificateOptions) RunCertificateApprove(force bool) error {
return o.modifyCertificateCondition(o.builderFunc, o.clientSetFunc, force, func(csr *certificates.CertificateSigningRequest) (*certificates.CertificateSigningRequest, bool) { return o.modifyCertificateCondition(o.builder, o.clientSet, force, func(csr *certificates.CertificateSigningRequest) (*certificates.CertificateSigningRequest, bool) {
var alreadyApproved bool var alreadyApproved bool
for _, c := range csr.Status.Conditions { for _, c := range csr.Status.Conditions {
if c.Type == certificates.CertificateApproved { if c.Type == certificates.CertificateApproved {
@ -177,7 +180,7 @@ func NewCmdCertificateDeny(f cmdutil.Factory, ioStreams genericclioptions.IOStre
} }
func (o *CertificateOptions) RunCertificateDeny(force bool) error { func (o *CertificateOptions) RunCertificateDeny(force bool) error {
return o.modifyCertificateCondition(o.builderFunc, o.clientSetFunc, force, func(csr *certificates.CertificateSigningRequest) (*certificates.CertificateSigningRequest, bool) { return o.modifyCertificateCondition(o.builder, o.clientSet, force, func(csr *certificates.CertificateSigningRequest) (*certificates.CertificateSigningRequest, bool) {
var alreadyDenied bool var alreadyDenied bool
for _, c := range csr.Status.Conditions { for _, c := range csr.Status.Conditions {
if c.Type == certificates.CertificateDenied { if c.Type == certificates.CertificateDenied {
@ -197,13 +200,9 @@ func (o *CertificateOptions) RunCertificateDeny(force bool) error {
}) })
} }
func (options *CertificateOptions) modifyCertificateCondition(builderFunc func() *resource.Builder, clientSetFunc func() (internalclientset.Interface, error), force bool, modify func(csr *certificates.CertificateSigningRequest) (*certificates.CertificateSigningRequest, bool)) error { func (options *CertificateOptions) modifyCertificateCondition(builder *resource.Builder, clientSet internalclientset.Interface, force bool, modify func(csr *certificates.CertificateSigningRequest) (*certificates.CertificateSigningRequest, bool)) error {
var found int var found int
c, err := clientSetFunc() r := builder.
if err != nil {
return err
}
r := builderFunc().
Internal(). Internal().
ContinueOnError(). ContinueOnError().
FilenameParam(false, &options.FilenameOptions). FilenameParam(false, &options.FilenameOptions).
@ -212,14 +211,14 @@ func (options *CertificateOptions) modifyCertificateCondition(builderFunc func()
Flatten(). Flatten().
Latest(). Latest().
Do() Do()
err = r.Visit(func(info *resource.Info, err error) error { err := r.Visit(func(info *resource.Info, err error) error {
if err != nil { if err != nil {
return err return err
} }
csr := info.Object.(*certificates.CertificateSigningRequest) csr := info.Object.(*certificates.CertificateSigningRequest)
csr, hasCondition := modify(csr) csr, hasCondition := modify(csr)
if !hasCondition || force { if !hasCondition || force {
csr, err = c.Certificates(). csr, err = clientSet.Certificates().
CertificateSigningRequests(). CertificateSigningRequests().
UpdateApproval(csr) UpdateApproval(csr)
if err != nil { if err != nil {

View File

@ -17,7 +17,6 @@ limitations under the License.
package cmd package cmd
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
@ -30,15 +29,15 @@ import (
func TestSetupOutputWriterNoOp(t *testing.T) { func TestSetupOutputWriterNoOp(t *testing.T) {
tests := []string{"", "-"} tests := []string{"", "-"}
for _, test := range tests { for _, test := range tests {
out := &bytes.Buffer{} _, _, buf, _ := genericclioptions.NewTestIOStreams()
f := cmdtesting.NewTestFactory() f := cmdtesting.NewTestFactory()
defer f.Cleanup() defer f.Cleanup()
cmd := NewCmdClusterInfoDump(f, genericclioptions.IOStreams{Out: os.Stdout, ErrOut: os.Stderr}) cmd := NewCmdClusterInfoDump(f, genericclioptions.NewTestIOStreamsDiscard())
cmd.Flag("output-directory").Value.Set(test) cmd.Flag("output-directory").Value.Set(test)
writer := setupOutputWriter(cmd, out, "/some/file/that/should/be/ignored") writer := setupOutputWriter(cmd, buf, "/some/file/that/should/be/ignored")
if writer != out { if writer != buf {
t.Errorf("expected: %v, saw: %v", out, writer) t.Errorf("expected: %v, saw: %v", buf, writer)
} }
} }
} }
@ -52,15 +51,15 @@ func TestSetupOutputWriterFile(t *testing.T) {
fullPath := path.Join(dir, file) fullPath := path.Join(dir, file)
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
out := &bytes.Buffer{} _, _, buf, _ := genericclioptions.NewTestIOStreams()
f := cmdtesting.NewTestFactory() f := cmdtesting.NewTestFactory()
defer f.Cleanup() defer f.Cleanup()
cmd := NewCmdClusterInfoDump(f, genericclioptions.IOStreams{Out: os.Stdout, ErrOut: os.Stderr}) cmd := NewCmdClusterInfoDump(f, genericclioptions.NewTestIOStreamsDiscard())
cmd.Flag("output-directory").Value.Set(dir) cmd.Flag("output-directory").Value.Set(dir)
writer := setupOutputWriter(cmd, out, file) writer := setupOutputWriter(cmd, buf, file)
if writer == out { if writer == buf {
t.Errorf("expected: %v, saw: %v", out, writer) t.Errorf("expected: %v, saw: %v", buf, writer)
} }
output := "some data here" output := "some data here"
writer.Write([]byte(output)) writer.Write([]byte(output))

View File

@ -525,10 +525,9 @@ func TestCopyToPod(t *testing.T) {
} }
tf.ClientConfigVal = defaultClientConfig() tf.ClientConfigVal = defaultClientConfig()
buf := bytes.NewBuffer([]byte{}) ioStreams, _, _, _ := genericclioptions.NewTestIOStreams()
errBuf := bytes.NewBuffer([]byte{})
cmd := NewCmdCp(tf, genericclioptions.IOStreams{Out: buf, ErrOut: errBuf}) cmd := NewCmdCp(tf, ioStreams)
srcFile, err := ioutil.TempDir("", "test") srcFile, err := ioutil.TempDir("", "test")
if err != nil { if err != nil {
@ -556,7 +555,7 @@ func TestCopyToPod(t *testing.T) {
} }
for name, test := range tests { for name, test := range tests {
opts := NewCopyOptions(genericclioptions.IOStreams{Out: buf, ErrOut: errBuf}) opts := NewCopyOptions(ioStreams)
src := fileSpec{ src := fileSpec{
File: srcFile, File: srcFile,
} }

View File

@ -17,7 +17,6 @@ limitations under the License.
package cmd package cmd
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -198,8 +197,8 @@ func TestCordon(t *testing.T) {
} }
tf.ClientConfigVal = defaultClientConfig() tf.ClientConfigVal = defaultClientConfig()
buf := bytes.NewBuffer([]byte{}) ioStreams, _, _, _ := genericclioptions.NewTestIOStreams()
cmd := test.cmd(tf, genericclioptions.IOStreams{Out: buf, ErrOut: buf}) cmd := test.cmd(tf, ioStreams)
saw_fatal := false saw_fatal := false
func() { func() {
@ -708,9 +707,8 @@ func TestDrain(t *testing.T) {
} }
tf.ClientConfigVal = defaultClientConfig() tf.ClientConfigVal = defaultClientConfig()
buf := bytes.NewBuffer([]byte{}) ioStreams, _, _, errBuf := genericclioptions.NewTestIOStreams()
errBuf := bytes.NewBuffer([]byte{}) cmd := NewCmdDrain(tf, ioStreams)
cmd := NewCmdDrain(tf, genericclioptions.IOStreams{Out: buf, ErrOut: errBuf})
saw_fatal := false saw_fatal := false
fatal_msg := "" fatal_msg := ""

View File

@ -328,14 +328,15 @@ func TestLabelErrors(t *testing.T) {
tf.Namespace = "test" tf.Namespace = "test"
tf.ClientConfigVal = defaultClientConfig() tf.ClientConfigVal = defaultClientConfig()
ioStreams, _, _, _ := genericclioptions.NewTestIOStreams()
buf := bytes.NewBuffer([]byte{}) buf := bytes.NewBuffer([]byte{})
cmd := NewCmdLabel(tf, genericclioptions.IOStreams{Out: buf, ErrOut: buf}) cmd := NewCmdLabel(tf, ioStreams)
cmd.SetOutput(buf) cmd.SetOutput(buf)
for k, v := range testCase.flags { for k, v := range testCase.flags {
cmd.Flags().Set(k, v) cmd.Flags().Set(k, v)
} }
opts := NewLabelOptions(genericclioptions.IOStreams{Out: buf, ErrOut: buf}) opts := NewLabelOptions(ioStreams)
err := opts.Complete(tf, cmd, testCase.args) err := opts.Complete(tf, cmd, testCase.args)
if err == nil { if err == nil {
err = opts.Validate() err = opts.Validate()
@ -390,9 +391,9 @@ func TestLabelForResourceFromFile(t *testing.T) {
tf.Namespace = "test" tf.Namespace = "test"
tf.ClientConfigVal = defaultClientConfig() tf.ClientConfigVal = defaultClientConfig()
buf := bytes.NewBuffer([]byte{}) ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
cmd := NewCmdLabel(tf, genericclioptions.IOStreams{Out: buf, ErrOut: buf}) cmd := NewCmdLabel(tf, ioStreams)
opts := NewLabelOptions(genericclioptions.IOStreams{Out: buf, ErrOut: buf}) opts := NewLabelOptions(ioStreams)
opts.Filenames = []string{"../../../test/e2e/testing-manifests/statefulset/cassandra/controller.yaml"} opts.Filenames = []string{"../../../test/e2e/testing-manifests/statefulset/cassandra/controller.yaml"}
err := opts.Complete(tf, cmd, []string{"a=b"}) err := opts.Complete(tf, cmd, []string{"a=b"})
if err == nil { if err == nil {
@ -423,9 +424,9 @@ func TestLabelLocal(t *testing.T) {
tf.Namespace = "test" tf.Namespace = "test"
tf.ClientConfigVal = defaultClientConfig() tf.ClientConfigVal = defaultClientConfig()
buf := bytes.NewBuffer([]byte{}) ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
cmd := NewCmdLabel(tf, genericclioptions.IOStreams{Out: buf, ErrOut: buf}) cmd := NewCmdLabel(tf, ioStreams)
opts := NewLabelOptions(genericclioptions.IOStreams{Out: buf, ErrOut: buf}) opts := NewLabelOptions(ioStreams)
opts.Filenames = []string{"../../../test/e2e/testing-manifests/statefulset/cassandra/controller.yaml"} opts.Filenames = []string{"../../../test/e2e/testing-manifests/statefulset/cassandra/controller.yaml"}
opts.local = true opts.local = true
err := opts.Complete(tf, cmd, []string{"a=b"}) err := opts.Complete(tf, cmd, []string{"a=b"})
@ -481,10 +482,10 @@ func TestLabelMultipleObjects(t *testing.T) {
tf.Namespace = "test" tf.Namespace = "test"
tf.ClientConfigVal = defaultClientConfig() tf.ClientConfigVal = defaultClientConfig()
buf := bytes.NewBuffer([]byte{}) ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
opts := NewLabelOptions(genericclioptions.IOStreams{Out: buf, ErrOut: buf}) opts := NewLabelOptions(ioStreams)
opts.all = true opts.all = true
cmd := NewCmdLabel(tf, genericclioptions.IOStreams{Out: buf, ErrOut: buf}) cmd := NewCmdLabel(tf, ioStreams)
err := opts.Complete(tf, cmd, []string{"pods", "a=b"}) err := opts.Complete(tf, cmd, []string{"pods", "a=b"})
if err == nil { if err == nil {
err = opts.Validate() err = opts.Validate()

View File

@ -23,11 +23,17 @@ import (
) )
type NoCompatiblePrinterError struct { type NoCompatiblePrinterError struct {
Options interface{} OutputFormat *string
Options interface{}
} }
func (e NoCompatiblePrinterError) Error() string { func (e NoCompatiblePrinterError) Error() string {
return fmt.Sprintf("unable to match a printer suitable for the options specified: %#v", e.Options) output := ""
if e.OutputFormat != nil {
output = *e.OutputFormat
}
return fmt.Sprintf("unable to match a printer suitable for the output format %q and the options specified: %#v", output, e.Options)
} }
func IsNoCompatiblePrinterError(err error) bool { func IsNoCompatiblePrinterError(err error) bool {
@ -67,7 +73,7 @@ func (f *PrintFlags) ToPrinter() (ResourcePrinter, error) {
return p, err return p, err
} }
return nil, NoCompatiblePrinterError{f} return nil, NoCompatiblePrinterError{Options: f, OutputFormat: f.OutputFormat}
} }
func (f *PrintFlags) AddFlags(cmd *cobra.Command) { func (f *PrintFlags) AddFlags(cmd *cobra.Command) {

View File

@ -44,7 +44,7 @@ func (f *JSONYamlPrintFlags) ToPrinter(outputFormat string) (ResourcePrinter, er
case "yaml": case "yaml":
printer = &YAMLPrinter{} printer = &YAMLPrinter{}
default: default:
return nil, NoCompatiblePrinterError{f} return nil, NoCompatiblePrinterError{Options: f, OutputFormat: &outputFormat}
} }
// wrap the printer in a versioning printer that understands when to convert and when not to convert // wrap the printer in a versioning printer that understands when to convert and when not to convert

View File

@ -39,7 +39,7 @@ type JSONPathPrintFlags struct {
// Returns false if the specified templateFormat does not match a template format. // Returns false if the specified templateFormat does not match a template format.
func (f *JSONPathPrintFlags) ToPrinter(templateFormat string) (ResourcePrinter, error) { func (f *JSONPathPrintFlags) ToPrinter(templateFormat string) (ResourcePrinter, error) {
if (f.TemplateArgument == nil || len(*f.TemplateArgument) == 0) && len(templateFormat) == 0 { if (f.TemplateArgument == nil || len(*f.TemplateArgument) == 0) && len(templateFormat) == 0 {
return nil, NoCompatiblePrinterError{f} return nil, NoCompatiblePrinterError{Options: f, OutputFormat: &templateFormat}
} }
templateValue := "" templateValue := ""
@ -66,7 +66,7 @@ func (f *JSONPathPrintFlags) ToPrinter(templateFormat string) (ResourcePrinter,
} }
if _, supportedFormat := templateFormats[templateFormat]; !supportedFormat { if _, supportedFormat := templateFormats[templateFormat]; !supportedFormat {
return nil, NoCompatiblePrinterError{f} return nil, NoCompatiblePrinterError{Options: f, OutputFormat: &templateFormat}
} }
if len(templateValue) == 0 { if len(templateValue) == 0 {

View File

@ -63,7 +63,7 @@ func (f *NamePrintFlags) ToPrinter(outputFormat string) (ResourcePrinter, error)
case "": case "":
return namePrinter, nil return namePrinter, nil
default: default:
return nil, NoCompatiblePrinterError{f} return nil, NoCompatiblePrinterError{Options: f, OutputFormat: &outputFormat}
} }
} }

View File

@ -39,7 +39,7 @@ type GoTemplatePrintFlags struct {
// Returns false if the specified templateFormat does not match a template format. // Returns false if the specified templateFormat does not match a template format.
func (f *GoTemplatePrintFlags) ToPrinter(templateFormat string) (ResourcePrinter, error) { func (f *GoTemplatePrintFlags) ToPrinter(templateFormat string) (ResourcePrinter, error) {
if (f.TemplateArgument == nil || len(*f.TemplateArgument) == 0) && len(templateFormat) == 0 { if (f.TemplateArgument == nil || len(*f.TemplateArgument) == 0) && len(templateFormat) == 0 {
return nil, NoCompatiblePrinterError{f} return nil, NoCompatiblePrinterError{Options: f, OutputFormat: &templateFormat}
} }
templateValue := "" templateValue := ""
@ -68,7 +68,7 @@ func (f *GoTemplatePrintFlags) ToPrinter(templateFormat string) (ResourcePrinter
} }
if _, supportedFormat := supportedFormats[templateFormat]; !supportedFormat { if _, supportedFormat := supportedFormats[templateFormat]; !supportedFormat {
return nil, NoCompatiblePrinterError{f} return nil, NoCompatiblePrinterError{Options: f, OutputFormat: &templateFormat}
} }
if len(templateValue) == 0 { if len(templateValue) == 0 {