forked from knative/client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* print the yaml formatted revision by default * allow for custom formatting to be passed in
- Loading branch information
1 parent
11b8912
commit ca4168c
Showing
3 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright © 2018 The Knative Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package commands | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/spf13/cobra" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
) | ||
|
||
var revisionDescribePrintFlags *genericclioptions.PrintFlags | ||
|
||
func NewRevisionDescribeCommand(p *KnParams) *cobra.Command { | ||
revisionDescribePrintFlags := genericclioptions.NewPrintFlags("").WithDefaultOutput("yaml") | ||
revisionDescribeCmd := &cobra.Command{ | ||
Use: "describe NAME", | ||
Short: "Describe revisions.", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
client, err := p.ServingFactory() | ||
if err != nil { | ||
return err | ||
} | ||
namespace := cmd.Flag("namespace").Value.String() | ||
|
||
if len(args) < 1 { | ||
return errors.New("requires the revision name.") | ||
} | ||
revision, err := client.Revisions(namespace).Get(args[0], v1.GetOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
printer, err := revisionDescribePrintFlags.ToPrinter() | ||
if err != nil { | ||
return err | ||
} | ||
revision.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{ | ||
Group: "knative.dev", | ||
Version: "v1alpha1", | ||
Kind: "Revision"}) | ||
err = printer.PrintObj(revision, cmd.OutOrStdout()) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
}, | ||
} | ||
revisionDescribePrintFlags.AddFlags(revisionDescribeCmd) | ||
return revisionDescribeCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright © 2018 The Knative Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package commands | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/knative/serving/pkg/apis/serving/v1alpha1" | ||
serving "github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1" | ||
"github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1/fake" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/equality" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
client_testing "k8s.io/client-go/testing" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
func fakeRevision(args []string, response *v1alpha1.Revision) (action client_testing.Action, output string, err error) { | ||
buf := new(bytes.Buffer) | ||
fakeServing := &fake.FakeServingV1alpha1{&client_testing.Fake{}} | ||
cmd := NewKnCommand(KnParams{ | ||
Output: buf, | ||
ServingFactory: func() (serving.ServingV1alpha1Interface, error) { return fakeServing, nil }, | ||
}) | ||
fakeServing.AddReactor("*", "*", | ||
func(a client_testing.Action) (bool, runtime.Object, error) { | ||
action = a | ||
return true, response, nil | ||
}) | ||
cmd.SetArgs(args) | ||
err = cmd.Execute() | ||
if err != nil { | ||
return | ||
} | ||
output = buf.String() | ||
return | ||
} | ||
|
||
func TestRevisionWithNoName(t *testing.T) { | ||
_, _, err := fakeRevision([]string{"revision", "describe"}, &v1alpha1.Revision{}) | ||
expectedError := "requires the revision name." | ||
if err == nil || err.Error() != expectedError { | ||
t.Fatal("expect to fail with missing revision name") | ||
return | ||
} | ||
} | ||
|
||
func TestRevisionYaml(t *testing.T) { | ||
expectedRevision := v1alpha1.Revision{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "foo", | ||
Namespace: "default", | ||
}, | ||
Spec: v1alpha1.RevisionSpec{ | ||
Container: corev1.Container{ | ||
Name: "some-container", | ||
Image: "knative/test:latest", | ||
}, | ||
}, | ||
Status: v1alpha1.RevisionStatus{ | ||
ServiceName: "foo-service", | ||
}, | ||
} | ||
|
||
action, data, err := fakeRevision([]string{"revision", "describe", "test-rev"}, &expectedRevision) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if action == nil { | ||
t.Fatal("No action") | ||
} else if !action.Matches("get", "revisions") { | ||
t.Fatalf("Bad action %v", action) | ||
} | ||
|
||
jsonData, err := yaml.YAMLToJSON([]byte(data)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
var returnedRevision v1alpha1.Revision | ||
err = json.Unmarshal(jsonData, &returnedRevision) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !equality.Semantic.DeepEqual(expectedRevision, returnedRevision) { | ||
t.Fatal("mismatched objects") | ||
} | ||
} |