123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- /*
- Copyright 2017 The Kubernetes 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 diff
- import (
- "bytes"
- "io/ioutil"
- "os"
- "path"
- "path/filepath"
- "strings"
- "testing"
- "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
- "k8s.io/apimachinery/pkg/runtime"
- "k8s.io/cli-runtime/pkg/genericclioptions"
- "k8s.io/utils/exec"
- )
- type FakeObject struct {
- name string
- merged map[string]interface{}
- live map[string]interface{}
- }
- var _ Object = &FakeObject{}
- func (f *FakeObject) Name() string {
- return f.name
- }
- func (f *FakeObject) Merged() (runtime.Object, error) {
- return &unstructured.Unstructured{Object: f.merged}, nil
- }
- func (f *FakeObject) Live() runtime.Object {
- return &unstructured.Unstructured{Object: f.live}
- }
- func TestDiffProgram(t *testing.T) {
- os.Setenv("KUBECTL_EXTERNAL_DIFF", "echo")
- streams, _, stdout, _ := genericclioptions.NewTestIOStreams()
- diff := DiffProgram{
- IOStreams: streams,
- Exec: exec.New(),
- }
- err := diff.Run("one", "two")
- if err != nil {
- t.Fatal(err)
- }
- if output := stdout.String(); output != "one two\n" {
- t.Fatalf(`stdout = %q, expected "one two\n"`, output)
- }
- }
- func TestPrinter(t *testing.T) {
- printer := Printer{}
- obj := &unstructured.Unstructured{Object: map[string]interface{}{
- "string": "string",
- "list": []int{1, 2, 3},
- "int": 12,
- }}
- buf := bytes.Buffer{}
- printer.Print(obj, &buf)
- want := `int: 12
- list:
- - 1
- - 2
- - 3
- string: string
- `
- if buf.String() != want {
- t.Errorf("Print() = %q, want %q", buf.String(), want)
- }
- }
- func TestDiffVersion(t *testing.T) {
- diff, err := NewDiffVersion("MERGED")
- if err != nil {
- t.Fatal(err)
- }
- defer diff.Dir.Delete()
- obj := FakeObject{
- name: "bla",
- live: map[string]interface{}{"live": true},
- merged: map[string]interface{}{"merged": true},
- }
- err = diff.Print(&obj, Printer{})
- if err != nil {
- t.Fatal(err)
- }
- fcontent, err := ioutil.ReadFile(path.Join(diff.Dir.Name, obj.Name()))
- if err != nil {
- t.Fatal(err)
- }
- econtent := "merged: true\n"
- if string(fcontent) != econtent {
- t.Fatalf("File has %q, expected %q", string(fcontent), econtent)
- }
- }
- func TestDirectory(t *testing.T) {
- dir, err := CreateDirectory("prefix")
- defer dir.Delete()
- if err != nil {
- t.Fatal(err)
- }
- _, err = os.Stat(dir.Name)
- if err != nil {
- t.Fatal(err)
- }
- if !strings.HasPrefix(filepath.Base(dir.Name), "prefix") {
- t.Fatalf(`Directory doesn't start with "prefix": %q`, dir.Name)
- }
- entries, err := ioutil.ReadDir(dir.Name)
- if err != nil {
- t.Fatal(err)
- }
- if len(entries) != 0 {
- t.Fatalf("Directory should be empty, has %d elements", len(entries))
- }
- _, err = dir.NewFile("ONE")
- if err != nil {
- t.Fatal(err)
- }
- _, err = dir.NewFile("TWO")
- if err != nil {
- t.Fatal(err)
- }
- entries, err = ioutil.ReadDir(dir.Name)
- if err != nil {
- t.Fatal(err)
- }
- if len(entries) != 2 {
- t.Fatalf("ReadDir should have two elements, has %d elements", len(entries))
- }
- err = dir.Delete()
- if err != nil {
- t.Fatal(err)
- }
- _, err = os.Stat(dir.Name)
- if err == nil {
- t.Fatal("Directory should be gone, still present.")
- }
- }
- func TestDiffer(t *testing.T) {
- diff, err := NewDiffer("LIVE", "MERGED")
- if err != nil {
- t.Fatal(err)
- }
- defer diff.TearDown()
- obj := FakeObject{
- name: "bla",
- live: map[string]interface{}{"live": true},
- merged: map[string]interface{}{"merged": true},
- }
- err = diff.Diff(&obj, Printer{})
- if err != nil {
- t.Fatal(err)
- }
- fcontent, err := ioutil.ReadFile(path.Join(diff.From.Dir.Name, obj.Name()))
- if err != nil {
- t.Fatal(err)
- }
- econtent := "live: true\n"
- if string(fcontent) != econtent {
- t.Fatalf("File has %q, expected %q", string(fcontent), econtent)
- }
- fcontent, err = ioutil.ReadFile(path.Join(diff.To.Dir.Name, obj.Name()))
- if err != nil {
- t.Fatal(err)
- }
- econtent = "merged: true\n"
- if string(fcontent) != econtent {
- t.Fatalf("File has %q, expected %q", string(fcontent), econtent)
- }
- }
|