use IOStreams for cli commands

This commit is contained in:
David Eads
2018-05-08 09:02:34 -04:00
parent 8d064823bb
commit facd04be43
33 changed files with 295 additions and 296 deletions

View File

@@ -32,6 +32,7 @@ import (
core "k8s.io/client-go/testing"
"k8s.io/kubernetes/pkg/api/legacyscheme"
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
"k8s.io/kubernetes/pkg/kubectl/scheme"
metricsv1alpha1api "k8s.io/metrics/pkg/apis/metrics/v1alpha1"
metricsv1beta1api "k8s.io/metrics/pkg/apis/metrics/v1beta1"
@@ -79,9 +80,9 @@ func TestTopNodeAllMetrics(t *testing.T) {
}
tf.Namespace = "test"
tf.ClientConfigVal = defaultClientConfig()
buf := bytes.NewBuffer([]byte{})
streams, _, buf, _ := genericclioptions.NewTestIOStreams()
cmd := NewCmdTopNode(tf, nil, buf)
cmd := NewCmdTopNode(tf, nil, streams)
cmd.Run(cmd, []string{})
// Check the presence of node names in the output.
@@ -132,7 +133,7 @@ func TestTopNodeAllMetricsCustomDefaults(t *testing.T) {
}
tf.Namespace = "test"
tf.ClientConfigVal = defaultClientConfig()
buf := bytes.NewBuffer([]byte{})
streams, _, buf, _ := genericclioptions.NewTestIOStreams()
opts := &TopNodeOptions{
HeapsterOptions: HeapsterTopOptions{
@@ -140,8 +141,9 @@ func TestTopNodeAllMetricsCustomDefaults(t *testing.T) {
Scheme: "https",
Service: "custom-heapster-service",
},
IOStreams: streams,
}
cmd := NewCmdTopNode(tf, opts, buf)
cmd := NewCmdTopNode(tf, opts, streams)
cmd.Run(cmd, []string{})
// Check the presence of node names in the output.
@@ -195,9 +197,9 @@ func TestTopNodeWithNameMetrics(t *testing.T) {
}
tf.Namespace = "test"
tf.ClientConfigVal = defaultClientConfig()
buf := bytes.NewBuffer([]byte{})
streams, _, buf, _ := genericclioptions.NewTestIOStreams()
cmd := NewCmdTopNode(tf, nil, buf)
cmd := NewCmdTopNode(tf, nil, streams)
cmd.Run(cmd, []string{expectedMetrics.Name})
// Check the presence of node names in the output.
@@ -262,9 +264,9 @@ func TestTopNodeWithLabelSelectorMetrics(t *testing.T) {
}
tf.Namespace = "test"
tf.ClientConfigVal = defaultClientConfig()
buf := bytes.NewBuffer([]byte{})
streams, _, buf, _ := genericclioptions.NewTestIOStreams()
cmd := NewCmdTopNode(tf, nil, buf)
cmd := NewCmdTopNode(tf, nil, streams)
cmd.Flags().Set("selector", label)
cmd.Run(cmd, []string{})
@@ -315,14 +317,16 @@ func TestTopNodeAllMetricsFromMetricsServer(t *testing.T) {
})
tf.Namespace = "test"
tf.ClientConfigVal = defaultClientConfig()
buf := bytes.NewBuffer([]byte{})
streams, _, buf, _ := genericclioptions.NewTestIOStreams()
cmd := NewCmdTopNode(tf, nil, buf)
cmd := NewCmdTopNode(tf, nil, streams)
// TODO in the long run, we want to test most of our commands like this. Wire the options struct with specific mocks
// TODO then check the particular Run functionality and harvest results from fake clients
cmdOptions := &TopNodeOptions{}
if err := cmdOptions.Complete(tf, cmd, []string{}, buf); err != nil {
cmdOptions := &TopNodeOptions{
IOStreams: streams,
}
if err := cmdOptions.Complete(tf, cmd, []string{}); err != nil {
t.Fatal(err)
}
cmdOptions.MetricsClient = fakemetricsClientset
@@ -381,14 +385,16 @@ func TestTopNodeWithNameMetricsFromMetricsServer(t *testing.T) {
})
tf.Namespace = "test"
tf.ClientConfigVal = defaultClientConfig()
buf := bytes.NewBuffer([]byte{})
streams, _, buf, _ := genericclioptions.NewTestIOStreams()
cmd := NewCmdTopNode(tf, nil, buf)
cmd := NewCmdTopNode(tf, nil, streams)
// TODO in the long run, we want to test most of our commands like this. Wire the options struct with specific mocks
// TODO then check the particular Run functionality and harvest results from fake clients
cmdOptions := &TopNodeOptions{}
if err := cmdOptions.Complete(tf, cmd, []string{expectedMetrics.Name}, buf); err != nil {
cmdOptions := &TopNodeOptions{
IOStreams: streams,
}
if err := cmdOptions.Complete(tf, cmd, []string{expectedMetrics.Name}); err != nil {
t.Fatal(err)
}
cmdOptions.MetricsClient = fakemetricsClientset
@@ -458,15 +464,17 @@ func TestTopNodeWithLabelSelectorMetricsFromMetricsServer(t *testing.T) {
})
tf.Namespace = "test"
tf.ClientConfigVal = defaultClientConfig()
buf := bytes.NewBuffer([]byte{})
streams, _, buf, _ := genericclioptions.NewTestIOStreams()
cmd := NewCmdTopNode(tf, nil, buf)
cmd := NewCmdTopNode(tf, nil, streams)
cmd.Flags().Set("selector", label)
// TODO in the long run, we want to test most of our commands like this. Wire the options struct with specific mocks
// TODO then check the particular Run functionality and harvest results from fake clients
cmdOptions := &TopNodeOptions{}
if err := cmdOptions.Complete(tf, cmd, []string{}, buf); err != nil {
cmdOptions := &TopNodeOptions{
IOStreams: streams,
}
if err := cmdOptions.Complete(tf, cmd, []string{}); err != nil {
t.Fatal(err)
}
cmdOptions.MetricsClient = fakemetricsClientset