add no resources found message to rollout-status command (#117884)

* add no resources found message to rollout-status command

* return err if not nil before no resource message
This commit is contained in:
gxwilkerson33 2023-05-10 08:02:58 -05:00 committed by GitHub
parent 626b2db1ca
commit a5575425b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 1 deletions

View File

@ -174,7 +174,9 @@ func (o *RolloutStatusOptions) Run() error {
return err return err
} }
return r.Visit(func(info *resource.Info, _ error) error { resourceFound := false
err = r.Visit(func(info *resource.Info, _ error) error {
resourceFound = true
mapping := info.ResourceMapping() mapping := info.ResourceMapping()
statusViewer, err := o.StatusViewerFn(mapping) statusViewer, err := o.StatusViewerFn(mapping)
if err != nil { if err != nil {
@ -228,4 +230,14 @@ func (o *RolloutStatusOptions) Run() error {
return err return err
}) })
}) })
if err != nil {
return err
}
if !resourceFound {
fmt.Fprintf(o.ErrOut, "No resources found in %s namespace.\n", o.Namespace)
}
return nil
} }

View File

@ -258,3 +258,29 @@ func TestRolloutStatusWatchDisabledUnavailable(t *testing.T) {
t.Errorf("expected output: %s, but got: %s", expectedMsg, buf.String()) t.Errorf("expected output: %s, but got: %s", expectedMsg, buf.String())
} }
} }
func TestRolloutStatusEmptyList(t *testing.T) {
ns := scheme.Codecs.WithoutConversion()
tf := cmdtesting.NewTestFactory().WithNamespace("test")
tf.ClientConfigVal = cmdtesting.DefaultClientConfig()
info, _ := runtime.SerializerInfoForMediaType(ns.SupportedMediaTypes(), runtime.ContentTypeJSON)
encoder := ns.EncoderForVersion(info.Serializer, rolloutStatusGroupVersionEncoder)
tf.Client = &fake.RESTClient{
GroupVersion: rolloutStatusGroupVersionEncoder,
NegotiatedSerializer: ns,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
dep := &appsv1.DeploymentList{}
body := io.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, dep))))
return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: body}, nil
}),
}
streams, _, _, err := genericiooptions.NewTestIOStreams()
cmd := NewCmdRolloutStatus(tf, streams)
cmd.Run(cmd, []string{"deployment"})
expectedMsg := "No resources found in test namespace.\n"
if err.String() != expectedMsg {
t.Errorf("expected output: %s, but got: %s", expectedMsg, err.String())
}
}