Allow resource.Builder to stream YAML from the CLI

Add both JSON and YAML STDIN tests in test-cmd
This commit is contained in:
Clayton Coleman
2015-01-14 00:03:56 -05:00
parent 22cf8b94e6
commit ec803cb809
3 changed files with 47 additions and 4 deletions

View File

@@ -25,6 +25,8 @@ import (
"reflect"
"testing"
"github.com/ghodss/yaml"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
@@ -115,6 +117,26 @@ func streamTestData() (io.Reader, *api.PodList, *api.ServiceList) {
return r, pods, svc
}
func JSONToYAMLOrDie(in []byte) []byte {
data, err := yaml.JSONToYAML(in)
if err != nil {
panic(err)
}
return data
}
func streamYAMLTestData() (io.Reader, *api.PodList, *api.ServiceList) {
pods, svc := testData()
r, w := io.Pipe()
go func() {
defer w.Close()
w.Write(JSONToYAMLOrDie([]byte(runtime.EncodeOrDie(latest.Codec, pods))))
w.Write([]byte("\n---\n"))
w.Write(JSONToYAMLOrDie([]byte(runtime.EncodeOrDie(latest.Codec, svc))))
}()
return r, pods, svc
}
type testVisitor struct {
InjectErr error
Infos []*Info
@@ -370,6 +392,23 @@ func TestStream(t *testing.T) {
}
}
func TestYAMLStream(t *testing.T) {
r, pods, rc := streamYAMLTestData()
b := NewBuilder(latest.RESTMapper, api.Scheme, fakeClient()).
NamespaceParam("test").Stream(r, "STDIN").Flatten()
test := &testVisitor{}
singular := false
err := b.Do().IntoSingular(&singular).Visit(test.Handle)
if err != nil || singular || len(test.Infos) != 3 {
t.Fatalf("unexpected response: %v %f %#v", err, singular, test.Infos)
}
if !reflect.DeepEqual([]runtime.Object{&pods.Items[0], &pods.Items[1], &rc.Items[0]}, test.Objects()) {
t.Errorf("unexpected visited objects: %#v", test.Objects())
}
}
func TestMultipleObject(t *testing.T) {
r, pods, svc := streamTestData()
obj, err := NewBuilder(latest.RESTMapper, api.Scheme, fakeClient()).

View File

@@ -17,7 +17,6 @@ limitations under the License.
package resource
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
@@ -31,6 +30,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/yaml"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
)
@@ -344,7 +344,7 @@ func NewStreamVisitor(r io.Reader, mapper *Mapper, source string, ignoreErrors b
// Visit implements Visitor over a stream.
func (v *StreamVisitor) Visit(fn VisitorFunc) error {
d := json.NewDecoder(v.Reader)
d := yaml.NewYAMLOrJSONDecoder(v.Reader, 4096)
for {
ext := runtime.RawExtension{}
if err := d.Decode(&ext); err != nil {