|
| 1 | +/* |
| 2 | +Copyright 2019 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 hosts |
| 18 | + |
| 19 | +import ( |
| 20 | + "io/ioutil" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "strings" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "k8s.io/kops/pkg/diff" |
| 27 | +) |
| 28 | + |
| 29 | +func TestRemovesDuplicateGuardedBlocks(t *testing.T) { |
| 30 | + in := ` |
| 31 | +foo 10.2.3.4 |
| 32 | +
|
| 33 | +# Begin host entries managed by etcd-manager[etcd] - do not edit |
| 34 | +# End host entries managed by etcd-manager[etcd] |
| 35 | +# Begin host entries managed by etcd-manager[etcd] - do not edit |
| 36 | +# End host entries managed by etcd-manager[etcd] |
| 37 | +# Begin host entries managed by kops - do not edit |
| 38 | +# End host entries managed by kops |
| 39 | +# Begin host entries managed by kops - do not edit |
| 40 | +# End host entries managed by kops |
| 41 | +# Begin host entries managed by kops - do not edit |
| 42 | +# End host entries managed by kops |
| 43 | +# Begin host entries managed by kops - do not edit |
| 44 | +# End host entries managed by kops |
| 45 | +# Begin host entries managed by kops - do not edit |
| 46 | +# End host entries managed by kops |
| 47 | +# Begin host entries managed by kops - do not edit |
| 48 | +# End host entries managed by kops |
| 49 | +# Begin host entries managed by kops - do not edit |
| 50 | +# End host entries managed by kops |
| 51 | +# Begin host entries managed by kops - do not edit |
| 52 | +# End host entries managed by kops |
| 53 | +` |
| 54 | + |
| 55 | + expected := ` |
| 56 | +foo 10.2.3.4 |
| 57 | +
|
| 58 | +# Begin host entries managed by etcd-manager[etcd] - do not edit |
| 59 | +# End host entries managed by etcd-manager[etcd] |
| 60 | +# Begin host entries managed by etcd-manager[etcd] - do not edit |
| 61 | +# End host entries managed by etcd-manager[etcd] |
| 62 | +
|
| 63 | +# Begin host entries managed by kops - do not edit |
| 64 | +a\t10.0.1.1 10.0.1.2 |
| 65 | +b\t10.0.2.1 |
| 66 | +c\t |
| 67 | +# End host entries managed by kops |
| 68 | +` |
| 69 | + |
| 70 | + runTest(t, in, expected) |
| 71 | +} |
| 72 | + |
| 73 | +func TestRecoversFromBadNesting(t *testing.T) { |
| 74 | + in := ` |
| 75 | +foo 10.2.3.4 |
| 76 | +
|
| 77 | +# End host entries managed by kops |
| 78 | +# Begin host entries managed by kops - do not edit |
| 79 | +# Begin host entries managed by kops - do not edit |
| 80 | +# End host entries managed by kops |
| 81 | +# End host entries managed by kops |
| 82 | +# End host entries managed by kops |
| 83 | +# Begin host entries managed by kops - do not edit |
| 84 | +# End host entries managed by kops |
| 85 | +# Begin host entries managed by kops - do not edit |
| 86 | +# End host entries managed by kops |
| 87 | +# Begin host entries managed by kops - do not edit |
| 88 | +# End host entries managed by kops |
| 89 | +# Begin host entries managed by kops - do not edit |
| 90 | +# Begin host entries managed by kops - do not edit |
| 91 | +# Begin host entries managed by kops - do not edit |
| 92 | +# Begin host entries managed by kops - do not edit |
| 93 | +# End host entries managed by kops |
| 94 | +# Begin host entries managed by kops - do not edit |
| 95 | +# End host entries managed by kops |
| 96 | +
|
| 97 | +bar 10.1.2.3 |
| 98 | +` |
| 99 | + |
| 100 | + expected := ` |
| 101 | +foo 10.2.3.4 |
| 102 | +
|
| 103 | +
|
| 104 | +bar 10.1.2.3 |
| 105 | +
|
| 106 | +# Begin host entries managed by kops - do not edit |
| 107 | +a\t10.0.1.1 10.0.1.2 |
| 108 | +b\t10.0.2.1 |
| 109 | +c\t |
| 110 | +# End host entries managed by kops |
| 111 | +` |
| 112 | + |
| 113 | + runTest(t, in, expected) |
| 114 | +} |
| 115 | + |
| 116 | +func runTest(t *testing.T, in string, expected string) { |
| 117 | + expected = strings.Replace(expected, "\\t", "\t", -1) |
| 118 | + |
| 119 | + dir, err := ioutil.TempDir("", "") |
| 120 | + if err != nil { |
| 121 | + t.Fatalf("error creating temp dir: %v", err) |
| 122 | + } |
| 123 | + defer func() { |
| 124 | + err := os.RemoveAll(dir) |
| 125 | + if err != nil { |
| 126 | + t.Errorf("failed to remove temp dir %q: %v", dir, err) |
| 127 | + } |
| 128 | + }() |
| 129 | + |
| 130 | + p := filepath.Join(dir, "hosts") |
| 131 | + addrToHosts := map[string][]string{ |
| 132 | + "a": {"10.0.1.2", "10.0.1.1"}, |
| 133 | + "b": {"10.0.2.1"}, |
| 134 | + "c": {}, |
| 135 | + } |
| 136 | + |
| 137 | + if err := ioutil.WriteFile(p, []byte(in), 0755); err != nil { |
| 138 | + t.Fatalf("error writing hosts file: %v", err) |
| 139 | + } |
| 140 | + |
| 141 | + // We run it repeatedly to make sure we don't change it accidentally |
| 142 | + for i := 0; i < 100; i++ { |
| 143 | + if err := UpdateHostsFileWithRecords(p, addrToHosts); err != nil { |
| 144 | + t.Fatalf("error updating hosts file: %v", err) |
| 145 | + } |
| 146 | + |
| 147 | + b, err := ioutil.ReadFile(p) |
| 148 | + if err != nil { |
| 149 | + t.Fatalf("error reading output file: %v", err) |
| 150 | + } |
| 151 | + |
| 152 | + actual := string(b) |
| 153 | + if actual != expected { |
| 154 | + diffString := diff.FormatDiff(expected, actual) |
| 155 | + t.Logf("diff:\n%s\n", diffString) |
| 156 | + t.Errorf("unexpected output. expected=%q, actual=%q", expected, actual) |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments