diff_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package diff
  14. import (
  15. "bytes"
  16. "io/ioutil"
  17. "os"
  18. "path"
  19. "path/filepath"
  20. "strings"
  21. "testing"
  22. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  23. "k8s.io/apimachinery/pkg/runtime"
  24. "k8s.io/cli-runtime/pkg/genericclioptions"
  25. "k8s.io/utils/exec"
  26. )
  27. type FakeObject struct {
  28. name string
  29. merged map[string]interface{}
  30. live map[string]interface{}
  31. }
  32. var _ Object = &FakeObject{}
  33. func (f *FakeObject) Name() string {
  34. return f.name
  35. }
  36. func (f *FakeObject) Merged() (runtime.Object, error) {
  37. return &unstructured.Unstructured{Object: f.merged}, nil
  38. }
  39. func (f *FakeObject) Live() runtime.Object {
  40. return &unstructured.Unstructured{Object: f.live}
  41. }
  42. func TestDiffProgram(t *testing.T) {
  43. os.Setenv("KUBECTL_EXTERNAL_DIFF", "echo")
  44. streams, _, stdout, _ := genericclioptions.NewTestIOStreams()
  45. diff := DiffProgram{
  46. IOStreams: streams,
  47. Exec: exec.New(),
  48. }
  49. err := diff.Run("one", "two")
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. if output := stdout.String(); output != "one two\n" {
  54. t.Fatalf(`stdout = %q, expected "one two\n"`, output)
  55. }
  56. }
  57. func TestPrinter(t *testing.T) {
  58. printer := Printer{}
  59. obj := &unstructured.Unstructured{Object: map[string]interface{}{
  60. "string": "string",
  61. "list": []int{1, 2, 3},
  62. "int": 12,
  63. }}
  64. buf := bytes.Buffer{}
  65. printer.Print(obj, &buf)
  66. want := `int: 12
  67. list:
  68. - 1
  69. - 2
  70. - 3
  71. string: string
  72. `
  73. if buf.String() != want {
  74. t.Errorf("Print() = %q, want %q", buf.String(), want)
  75. }
  76. }
  77. func TestDiffVersion(t *testing.T) {
  78. diff, err := NewDiffVersion("MERGED")
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. defer diff.Dir.Delete()
  83. obj := FakeObject{
  84. name: "bla",
  85. live: map[string]interface{}{"live": true},
  86. merged: map[string]interface{}{"merged": true},
  87. }
  88. err = diff.Print(&obj, Printer{})
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. fcontent, err := ioutil.ReadFile(path.Join(diff.Dir.Name, obj.Name()))
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. econtent := "merged: true\n"
  97. if string(fcontent) != econtent {
  98. t.Fatalf("File has %q, expected %q", string(fcontent), econtent)
  99. }
  100. }
  101. func TestDirectory(t *testing.T) {
  102. dir, err := CreateDirectory("prefix")
  103. defer dir.Delete()
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. _, err = os.Stat(dir.Name)
  108. if err != nil {
  109. t.Fatal(err)
  110. }
  111. if !strings.HasPrefix(filepath.Base(dir.Name), "prefix") {
  112. t.Fatalf(`Directory doesn't start with "prefix": %q`, dir.Name)
  113. }
  114. entries, err := ioutil.ReadDir(dir.Name)
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. if len(entries) != 0 {
  119. t.Fatalf("Directory should be empty, has %d elements", len(entries))
  120. }
  121. _, err = dir.NewFile("ONE")
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. _, err = dir.NewFile("TWO")
  126. if err != nil {
  127. t.Fatal(err)
  128. }
  129. entries, err = ioutil.ReadDir(dir.Name)
  130. if err != nil {
  131. t.Fatal(err)
  132. }
  133. if len(entries) != 2 {
  134. t.Fatalf("ReadDir should have two elements, has %d elements", len(entries))
  135. }
  136. err = dir.Delete()
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. _, err = os.Stat(dir.Name)
  141. if err == nil {
  142. t.Fatal("Directory should be gone, still present.")
  143. }
  144. }
  145. func TestDiffer(t *testing.T) {
  146. diff, err := NewDiffer("LIVE", "MERGED")
  147. if err != nil {
  148. t.Fatal(err)
  149. }
  150. defer diff.TearDown()
  151. obj := FakeObject{
  152. name: "bla",
  153. live: map[string]interface{}{"live": true},
  154. merged: map[string]interface{}{"merged": true},
  155. }
  156. err = diff.Diff(&obj, Printer{})
  157. if err != nil {
  158. t.Fatal(err)
  159. }
  160. fcontent, err := ioutil.ReadFile(path.Join(diff.From.Dir.Name, obj.Name()))
  161. if err != nil {
  162. t.Fatal(err)
  163. }
  164. econtent := "live: true\n"
  165. if string(fcontent) != econtent {
  166. t.Fatalf("File has %q, expected %q", string(fcontent), econtent)
  167. }
  168. fcontent, err = ioutil.ReadFile(path.Join(diff.To.Dir.Name, obj.Name()))
  169. if err != nil {
  170. t.Fatal(err)
  171. }
  172. econtent = "merged: true\n"
  173. if string(fcontent) != econtent {
  174. t.Fatalf("File has %q, expected %q", string(fcontent), econtent)
  175. }
  176. }