Signed-off-by: Travis Thieman <travis.thieman@gmail.com>
| ... | ... |
@@ -182,6 +182,6 @@ func Update(path, IP, hostname string) error {
|
| 182 | 182 |
if err != nil {
|
| 183 | 183 |
return err |
| 184 | 184 |
} |
| 185 |
- var re = regexp.MustCompile(fmt.Sprintf("(\\S*)(\\t%s)", regexp.QuoteMeta(hostname)))
|
|
| 186 |
- return ioutil.WriteFile(path, re.ReplaceAll(old, []byte(IP+"$2")), 0644) |
|
| 185 |
+ var re = regexp.MustCompile(fmt.Sprintf("(\\S*)(\\t%s)(\\s|\\.)", regexp.QuoteMeta(hostname)))
|
|
| 186 |
+ return ioutil.WriteFile(path, re.ReplaceAll(old, []byte(IP+"$2"+"$3")), 0644) |
|
| 187 | 187 |
} |
| ... | ... |
@@ -137,6 +137,113 @@ func TestUpdate(t *testing.T) {
|
| 137 | 137 |
} |
| 138 | 138 |
} |
| 139 | 139 |
|
| 140 |
+// This regression test ensures that when a host is given a new IP |
|
| 141 |
+// via the Update function that other hosts which start with the |
|
| 142 |
+// same name as the targeted host are not erroneously updated as well. |
|
| 143 |
+// In the test example, if updating a host called "prefix", unrelated |
|
| 144 |
+// hosts named "prefixAndMore" or "prefix2" or anything else starting |
|
| 145 |
+// with "prefix" should not be changed. For more information see |
|
| 146 |
+// GitHub issue #603. |
|
| 147 |
+func TestUpdateIgnoresPrefixedHostname(t *testing.T) {
|
|
| 148 |
+ file, err := ioutil.TempFile("", "")
|
|
| 149 |
+ if err != nil {
|
|
| 150 |
+ t.Fatal(err) |
|
| 151 |
+ } |
|
| 152 |
+ defer os.Remove(file.Name()) |
|
| 153 |
+ |
|
| 154 |
+ if err := Build(file.Name(), "10.11.12.13", "testhostname", "testdomainname", []Record{
|
|
| 155 |
+ Record{
|
|
| 156 |
+ Hosts: "prefix", |
|
| 157 |
+ IP: "2.2.2.2", |
|
| 158 |
+ }, |
|
| 159 |
+ Record{
|
|
| 160 |
+ Hosts: "prefixAndMore", |
|
| 161 |
+ IP: "3.3.3.3", |
|
| 162 |
+ }, |
|
| 163 |
+ Record{
|
|
| 164 |
+ Hosts: "unaffectedHost", |
|
| 165 |
+ IP: "4.4.4.4", |
|
| 166 |
+ }, |
|
| 167 |
+ }); err != nil {
|
|
| 168 |
+ t.Fatal(err) |
|
| 169 |
+ } |
|
| 170 |
+ |
|
| 171 |
+ content, err := ioutil.ReadFile(file.Name()) |
|
| 172 |
+ if err != nil {
|
|
| 173 |
+ t.Fatal(err) |
|
| 174 |
+ } |
|
| 175 |
+ |
|
| 176 |
+ if expected := "2.2.2.2\tprefix\n3.3.3.3\tprefixAndMore\n4.4.4.4\tunaffectedHost\n"; !bytes.Contains(content, []byte(expected)) {
|
|
| 177 |
+ t.Fatalf("Expected to find '%s' got '%s'", expected, content)
|
|
| 178 |
+ } |
|
| 179 |
+ |
|
| 180 |
+ if err := Update(file.Name(), "5.5.5.5", "prefix"); err != nil {
|
|
| 181 |
+ t.Fatal(err) |
|
| 182 |
+ } |
|
| 183 |
+ |
|
| 184 |
+ content, err = ioutil.ReadFile(file.Name()) |
|
| 185 |
+ if err != nil {
|
|
| 186 |
+ t.Fatal(err) |
|
| 187 |
+ } |
|
| 188 |
+ |
|
| 189 |
+ if expected := "5.5.5.5\tprefix\n3.3.3.3\tprefixAndMore\n4.4.4.4\tunaffectedHost\n"; !bytes.Contains(content, []byte(expected)) {
|
|
| 190 |
+ t.Fatalf("Expected to find '%s' got '%s'", expected, content)
|
|
| 191 |
+ } |
|
| 192 |
+ |
|
| 193 |
+} |
|
| 194 |
+ |
|
| 195 |
+// This regression test covers the host prefix issue for the |
|
| 196 |
+// Delete function. In the test example, if deleting a host called |
|
| 197 |
+// "prefix", an unrelated host called "prefixAndMore" should not |
|
| 198 |
+// be deleted. For more information see GitHub issue #603. |
|
| 199 |
+func TestDeleteIgnoresPrefixedHostname(t *testing.T) {
|
|
| 200 |
+ file, err := ioutil.TempFile("", "")
|
|
| 201 |
+ if err != nil {
|
|
| 202 |
+ t.Fatal(err) |
|
| 203 |
+ } |
|
| 204 |
+ defer os.Remove(file.Name()) |
|
| 205 |
+ |
|
| 206 |
+ err = Build(file.Name(), "", "", "", nil) |
|
| 207 |
+ if err != nil {
|
|
| 208 |
+ t.Fatal(err) |
|
| 209 |
+ } |
|
| 210 |
+ |
|
| 211 |
+ if err := Add(file.Name(), []Record{
|
|
| 212 |
+ Record{
|
|
| 213 |
+ Hosts: "prefix", |
|
| 214 |
+ IP: "1.1.1.1", |
|
| 215 |
+ }, |
|
| 216 |
+ Record{
|
|
| 217 |
+ Hosts: "prefixAndMore", |
|
| 218 |
+ IP: "2.2.2.2", |
|
| 219 |
+ }, |
|
| 220 |
+ }); err != nil {
|
|
| 221 |
+ t.Fatal(err) |
|
| 222 |
+ } |
|
| 223 |
+ |
|
| 224 |
+ if err := Delete(file.Name(), []Record{
|
|
| 225 |
+ Record{
|
|
| 226 |
+ Hosts: "prefix", |
|
| 227 |
+ IP: "1.1.1.1", |
|
| 228 |
+ }, |
|
| 229 |
+ }); err != nil {
|
|
| 230 |
+ t.Fatal(err) |
|
| 231 |
+ } |
|
| 232 |
+ |
|
| 233 |
+ content, err := ioutil.ReadFile(file.Name()) |
|
| 234 |
+ if err != nil {
|
|
| 235 |
+ t.Fatal(err) |
|
| 236 |
+ } |
|
| 237 |
+ |
|
| 238 |
+ if expected := "2.2.2.2\tprefixAndMore\n"; !bytes.Contains(content, []byte(expected)) {
|
|
| 239 |
+ t.Fatalf("Expected to find '%s' got '%s'", expected, content)
|
|
| 240 |
+ } |
|
| 241 |
+ |
|
| 242 |
+ if expected := "1.1.1.1\tprefix\n"; bytes.Contains(content, []byte(expected)) {
|
|
| 243 |
+ t.Fatalf("Did not expect to find '%s' got '%s'", expected, content)
|
|
| 244 |
+ } |
|
| 245 |
+} |
|
| 246 |
+ |
|
| 140 | 247 |
func TestAddEmpty(t *testing.T) {
|
| 141 | 248 |
file, err := ioutil.TempFile("", "")
|
| 142 | 249 |
if err != nil {
|