2016-03-25 16:25:24 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2016 The Kubernetes Authors.
|
2016-03-25 16:25:24 +00:00
|
|
|
|
|
|
|
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 cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
2016-10-18 22:53:26 +00:00
|
|
|
|
|
|
|
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
|
2018-04-19 00:02:37 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
2016-03-25 16:25:24 +00:00
|
|
|
)
|
|
|
|
|
2016-05-16 23:50:13 +00:00
|
|
|
func TestSetupOutputWriterNoOp(t *testing.T) {
|
2016-03-25 16:25:24 +00:00
|
|
|
tests := []string{"", "-"}
|
|
|
|
for _, test := range tests {
|
2018-04-24 19:38:38 +00:00
|
|
|
_, _, buf, _ := genericclioptions.NewTestIOStreams()
|
2018-02-22 14:52:10 +00:00
|
|
|
f := cmdtesting.NewTestFactory()
|
2018-03-08 22:23:55 +00:00
|
|
|
defer f.Cleanup()
|
|
|
|
|
2018-04-24 19:38:38 +00:00
|
|
|
cmd := NewCmdClusterInfoDump(f, genericclioptions.NewTestIOStreamsDiscard())
|
2016-03-25 16:25:24 +00:00
|
|
|
cmd.Flag("output-directory").Value.Set(test)
|
2018-04-24 19:38:38 +00:00
|
|
|
writer := setupOutputWriter(cmd, buf, "/some/file/that/should/be/ignored")
|
|
|
|
if writer != buf {
|
|
|
|
t.Errorf("expected: %v, saw: %v", buf, writer)
|
2016-03-25 16:25:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-16 23:50:13 +00:00
|
|
|
func TestSetupOutputWriterFile(t *testing.T) {
|
2016-03-25 16:25:24 +00:00
|
|
|
file := "output.json"
|
|
|
|
dir, err := ioutil.TempDir(os.TempDir(), "out")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
fullPath := path.Join(dir, file)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2018-04-24 19:38:38 +00:00
|
|
|
_, _, buf, _ := genericclioptions.NewTestIOStreams()
|
2018-02-22 14:52:10 +00:00
|
|
|
f := cmdtesting.NewTestFactory()
|
2018-03-08 22:23:55 +00:00
|
|
|
defer f.Cleanup()
|
|
|
|
|
2018-04-24 19:38:38 +00:00
|
|
|
cmd := NewCmdClusterInfoDump(f, genericclioptions.NewTestIOStreamsDiscard())
|
2016-03-25 16:25:24 +00:00
|
|
|
cmd.Flag("output-directory").Value.Set(dir)
|
2018-04-24 19:38:38 +00:00
|
|
|
writer := setupOutputWriter(cmd, buf, file)
|
|
|
|
if writer == buf {
|
|
|
|
t.Errorf("expected: %v, saw: %v", buf, writer)
|
2016-03-25 16:25:24 +00:00
|
|
|
}
|
|
|
|
output := "some data here"
|
|
|
|
writer.Write([]byte(output))
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(fullPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if string(data) != output {
|
|
|
|
t.Errorf("expected: %v, saw: %v", output, data)
|
|
|
|
}
|
|
|
|
}
|