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 (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
2016-10-18 22:53:26 +00:00
|
|
|
|
|
|
|
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
|
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 {
|
|
|
|
out := &bytes.Buffer{}
|
2018-02-21 17:10:38 +00:00
|
|
|
f, _ := cmdtesting.NewAPIFactory()
|
2016-03-25 16:25:24 +00:00
|
|
|
cmd := NewCmdClusterInfoDump(f, os.Stdout)
|
|
|
|
cmd.Flag("output-directory").Value.Set(test)
|
2016-05-16 23:50:13 +00:00
|
|
|
writer := setupOutputWriter(cmd, out, "/some/file/that/should/be/ignored")
|
2016-03-25 16:25:24 +00:00
|
|
|
if writer != out {
|
|
|
|
t.Errorf("expected: %v, saw: %v", out, writer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
out := &bytes.Buffer{}
|
2018-02-21 17:10:38 +00:00
|
|
|
f, _ := cmdtesting.NewAPIFactory()
|
2016-03-25 16:25:24 +00:00
|
|
|
cmd := NewCmdClusterInfoDump(f, os.Stdout)
|
|
|
|
cmd.Flag("output-directory").Value.Set(dir)
|
2016-05-16 23:50:13 +00:00
|
|
|
writer := setupOutputWriter(cmd, out, file)
|
2016-03-25 16:25:24 +00:00
|
|
|
if writer == out {
|
|
|
|
t.Errorf("expected: %v, saw: %v", out, writer)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|