Skip to content

Commit

Permalink
Verify hosts to delete instead of suffixes when cleaning hosts file
Browse files Browse the repository at this point in the history
Clean(".crc.testing") is rejected by the hosts filter. This is a
mistake.
Without adding a second filter function, we can verify hosts to be
deleted before writing the file.
  • Loading branch information
guillaumerose committed Mar 17, 2021
1 parent b3a9dc0 commit d614eb6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 4 additions & 4 deletions pkg/hosts/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,6 @@ func (h *Hosts) Remove(hosts []string) error {
}

func (h *Hosts) Clean(rawSuffixes []string) error {
if err := h.verifyHosts(rawSuffixes); err != nil {
return err
}

if err := h.checkIsWritable(); err != nil {
return err
}
Expand All @@ -125,6 +121,10 @@ func (h *Hosts) Clean(rawSuffixes []string) error {
}
}

if err := h.verifyHosts(toDelete); err != nil {
return err
}

for _, host := range toDelete {
if err := h.File.RemoveByHostname(host); err != nil {
return err
Expand Down
4 changes: 3 additions & 1 deletion pkg/hosts/hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestSuffixFilter(t *testing.T) {
defer os.RemoveAll(dir)

hostsFile := filepath.Join(dir, "hosts")
assert.NoError(t, ioutil.WriteFile(hostsFile, []byte(`127.0.0.1 localhost`), 0600))
assert.NoError(t, ioutil.WriteFile(hostsFile, []byte(`127.0.0.1 localhost localhost.localdomain`), 0600))

file, err := hostsfile.NewCustomHosts(hostsFile)
assert.NoError(t, err)
Expand All @@ -99,6 +99,8 @@ func TestSuffixFilter(t *testing.T) {
assert.Error(t, host.Add("127.0.0.1", []string{"host.poison"}))
assert.Error(t, host.Add("127.0.0.1", []string{"CAPITAL.crc.testing"}))
assert.Error(t, host.Remove([]string{"localhost"}))
assert.NoError(t, host.Clean([]string{".crc.testing"}))
assert.Error(t, host.Clean([]string{".localdomain"}))
}

func hosts(t *testing.T, hostsFile string) Hosts {
Expand Down

0 comments on commit d614eb6

Please sign in to comment.