|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package dryrun |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "reflect" |
| 24 | + "testing" |
| 25 | +) |
| 26 | + |
| 27 | +func TestPrintDryRunFiles(t *testing.T) { |
| 28 | + tmpdir, err := os.MkdirTemp("", "") |
| 29 | + if err != nil { |
| 30 | + t.Fatalf("Couldn't create tmpdir: %v", err) |
| 31 | + } |
| 32 | + defer func() { |
| 33 | + if err = os.RemoveAll(tmpdir); err != nil { |
| 34 | + t.Fatalf("Couldn't remove tmpdir: %v", err) |
| 35 | + } |
| 36 | + }() |
| 37 | + fileContents := "apiVersion: kubeadm.k8s.io/unknownVersion" |
| 38 | + filename := "testfile" |
| 39 | + cfgPath := filepath.Join(tmpdir, filename) |
| 40 | + err = os.WriteFile(cfgPath, []byte(fileContents), 0644) |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("Couldn't write context to file: %v", err) |
| 43 | + } |
| 44 | + |
| 45 | + tests := []struct { |
| 46 | + name string |
| 47 | + files []FileToPrint |
| 48 | + wantW string |
| 49 | + wantErr bool |
| 50 | + }{ |
| 51 | + { |
| 52 | + name: "RealPath is empty", |
| 53 | + files: []FileToPrint{ |
| 54 | + { |
| 55 | + RealPath: "", |
| 56 | + PrintPath: cfgPath, |
| 57 | + }, |
| 58 | + }, |
| 59 | + wantW: "", |
| 60 | + wantErr: false, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "RealPath is a file that does not exist", |
| 64 | + files: []FileToPrint{ |
| 65 | + { |
| 66 | + RealPath: tmpdir + "/missingfile", |
| 67 | + PrintPath: cfgPath, |
| 68 | + }, |
| 69 | + }, |
| 70 | + wantW: "", |
| 71 | + wantErr: true, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "RealPath is a readable file", |
| 75 | + files: []FileToPrint{ |
| 76 | + { |
| 77 | + RealPath: cfgPath, |
| 78 | + PrintPath: "", |
| 79 | + }, |
| 80 | + }, |
| 81 | + wantW: "[dryrun] Would write file \"" + cfgPath + "\" with content:\n" + |
| 82 | + " apiVersion: kubeadm.k8s.io/unknownVersion\n", |
| 83 | + wantErr: false, |
| 84 | + }, |
| 85 | + } |
| 86 | + for _, tt := range tests { |
| 87 | + t.Run(tt.name, func(t *testing.T) { |
| 88 | + w := &bytes.Buffer{} |
| 89 | + if err := PrintDryRunFiles(tt.files, w); (err != nil) != tt.wantErr { |
| 90 | + t.Errorf("error: %v, expected error: %v", err, tt.wantErr) |
| 91 | + return |
| 92 | + } |
| 93 | + if gotW := w.String(); gotW != tt.wantW { |
| 94 | + t.Errorf("output: %v, expected output: %v", gotW, tt.wantW) |
| 95 | + } |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestNewFileToPrint(t *testing.T) { |
| 101 | + tests := []struct { |
| 102 | + realPath string |
| 103 | + printPath string |
| 104 | + want FileToPrint |
| 105 | + }{ |
| 106 | + { |
| 107 | + realPath: "", |
| 108 | + printPath: "", |
| 109 | + want: FileToPrint{}, |
| 110 | + }, |
| 111 | + { |
| 112 | + realPath: "/etc/kubernetes", |
| 113 | + printPath: "/tmp/kubernetes", |
| 114 | + want: FileToPrint{ |
| 115 | + "/etc/kubernetes", |
| 116 | + "/tmp/kubernetes", |
| 117 | + }, |
| 118 | + }, |
| 119 | + } |
| 120 | + for _, tt := range tests { |
| 121 | + t.Run("TestNewFileToPrint", func(t *testing.T) { |
| 122 | + if got := NewFileToPrint(tt.realPath, tt.printPath); !reflect.DeepEqual(got, tt.want) { |
| 123 | + t.Errorf("got: %v, want: %v", got, tt.want) |
| 124 | + } |
| 125 | + }) |
| 126 | + } |
| 127 | +} |
0 commit comments