Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix another hosts.Remove() issue #48

Merged
merged 9 commits into from
Dec 20, 2023
18 changes: 11 additions & 7 deletions pkg/hosts/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,25 +173,29 @@ func (h *Hosts) Remove(hosts []string) error {
return err
}

var hostEntries = make(map[string]struct{})
for _, key := range hosts {
hostEntries[key] = struct{}{}
}

start, end := h.findCrcSection()

h.Lock()
defer h.Unlock()
// delete from CRC section
if start > 0 && end > 0 {
for i := start; i < end; i++ {
for i := end - 1; i >= start; i-- {
line := h.File.GetHostsFileLineByRow(i)
if line.Type == libhosty.LineTypeComment {
continue
}

for _, hostToRemove := range hosts {
for hostIdx, hostname := range line.Hostnames {
if hostname == hostToRemove {
h.removeHostFromLine(line, hostIdx, i)
break
}
for hostIdx := len(line.Hostnames) - 1; hostIdx >= 0; hostIdx-- {
hostname := line.Hostnames[hostIdx]
if _, ok := hostEntries[hostname]; ok {
h.removeHostFromLine(line, hostIdx, i)
}

}
}
} else {
Expand Down
126 changes: 59 additions & 67 deletions pkg/hosts/hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ const (
)

func TestAdd(t *testing.T) {
dir, err := os.MkdirTemp("", "hosts")
assert.NoError(t, err)
defer os.RemoveAll(dir)

hostsFile := filepath.Join(dir, "hosts")
hostsFile := filepath.Join(t.TempDir(), "hosts")
assert.NoError(t, os.WriteFile(hostsFile, []byte(`127.0.0.1 entry1`), 0600))

host := hosts(t, hostsFile)
Expand All @@ -40,11 +36,7 @@ func TestAdd(t *testing.T) {
}

func TestAddMoreThen9Hosts(t *testing.T) {
dir, err := os.MkdirTemp("", "hosts")
assert.NoError(t, err)
defer os.RemoveAll(dir)

hostsFile := filepath.Join(dir, "hosts")
hostsFile := filepath.Join(t.TempDir(), "hosts")
assert.NoError(t, os.WriteFile(hostsFile, []byte(hostsTemplate), 0600))

host := hosts(t, hostsFile)
Expand All @@ -57,11 +49,7 @@ func TestAddMoreThen9Hosts(t *testing.T) {
}

func TestAddMoreThan18Hosts(t *testing.T) {
dir, err := os.MkdirTemp("", "hosts")
assert.NoError(t, err)
defer os.RemoveAll(dir)

hostsFile := filepath.Join(dir, "hosts")
hostsFile := filepath.Join(t.TempDir(), "hosts")
assert.NoError(t, os.WriteFile(hostsFile, []byte(hostsTemplate), 0600))

host := hosts(t, hostsFile)
Expand All @@ -75,11 +63,7 @@ func TestAddMoreThan18Hosts(t *testing.T) {
}

func TestAddMoreThen9HostsInMultipleLines(t *testing.T) {
dir, err := os.MkdirTemp("", "hosts")
assert.NoError(t, err)
defer os.RemoveAll(dir)

hostsFile := filepath.Join(dir, "hosts")
hostsFile := filepath.Join(t.TempDir(), "hosts")
assert.NoError(t, os.WriteFile(hostsFile, []byte(hostsTemplate+eol()+crcSection("127.0.0.1 entry1 entry10 entry2 entry3 entry4 entry5 entry6 entry7", "127.0.0.1 entry11 entry12 entry13 entry14 entry15 entry16 entry17 entry18")+eol()), 0600))

host := hosts(t, hostsFile)
Expand All @@ -92,11 +76,7 @@ func TestAddMoreThen9HostsInMultipleLines(t *testing.T) {
}

func TestRemove(t *testing.T) {
dir, err := os.MkdirTemp("", "hosts")
assert.NoError(t, err)
defer os.RemoveAll(dir)

hostsFile := filepath.Join(dir, "hosts")
hostsFile := filepath.Join(t.TempDir(), "hosts")
assert.NoError(t, os.WriteFile(hostsFile, []byte(hostsTemplate), 0600))

host := hosts(t, hostsFile)
Expand All @@ -110,11 +90,7 @@ func TestRemove(t *testing.T) {
}

func TestClean(t *testing.T) {
dir, err := os.MkdirTemp("", "hosts")
assert.NoError(t, err)
defer os.RemoveAll(dir)

hostsFile := filepath.Join(dir, "hosts")
hostsFile := filepath.Join(t.TempDir(), "hosts")
assert.NoError(t, os.WriteFile(hostsFile, []byte(hostsTemplate+eol()+crcSection("127.0.0.1 entry1.suffix1 entry2.suffix2")), 0600))

host := hosts(t, hostsFile)
Expand All @@ -127,11 +103,7 @@ func TestClean(t *testing.T) {
}

func TestCleanWithoutCrcSection(t *testing.T) {
dir, err := os.MkdirTemp("", "hosts")
assert.NoError(t, err)
defer os.RemoveAll(dir)

hostsFile := filepath.Join(dir, "hosts")
hostsFile := filepath.Join(t.TempDir(), "hosts")
assert.NoError(t, os.WriteFile(hostsFile, []byte(hostsTemplate), 0600))

host := hosts(t, hostsFile)
Expand All @@ -144,11 +116,7 @@ func TestCleanWithoutCrcSection(t *testing.T) {
}

func TestContains(t *testing.T) {
dir, err := os.MkdirTemp("", "hosts")
assert.NoError(t, err)
defer os.RemoveAll(dir)

hostsFile := filepath.Join(dir, "hosts")
hostsFile := filepath.Join(t.TempDir(), "hosts")
assert.NoError(t, os.WriteFile(hostsFile, []byte(`127.0.0.1 entry1.suffix1 entry2.suffix2`), 0600))

host := hosts(t, hostsFile)
Expand All @@ -159,11 +127,7 @@ func TestContains(t *testing.T) {
}

func TestSuffixFilter(t *testing.T) {
dir, err := os.MkdirTemp("", "hosts")
assert.NoError(t, err)
defer os.RemoveAll(dir)

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

config, _ := libhosty.NewHostsFileConfig(hostsFile)
Expand All @@ -184,11 +148,7 @@ func TestSuffixFilter(t *testing.T) {
}

func TestAddMoreThan9HostsWithFullLine(t *testing.T) {
dir, err := os.MkdirTemp("", "hosts")
assert.NoError(t, err)
defer os.RemoveAll(dir)

hostsFile := filepath.Join(dir, "hosts")
hostsFile := filepath.Join(t.TempDir(), "hosts")
assert.NoError(t, os.WriteFile(hostsFile, []byte(hostsTemplate+eol()+crcSection("127.0.0.1 entry1 entry2 entry3 entry4 entry5 entry6 entry7 entry8 entry9")+eol()), 0600))

host := hosts(t, hostsFile)
Expand All @@ -201,11 +161,7 @@ func TestAddMoreThan9HostsWithFullLine(t *testing.T) {
}

func TestAddMoreThan9HostsWithOverfullLine(t *testing.T) {
dir, err := os.MkdirTemp("", "hosts")
assert.NoError(t, err)
defer os.RemoveAll(dir)

hostsFile := filepath.Join(dir, "hosts")
hostsFile := filepath.Join(t.TempDir(), "hosts")
assert.NoError(t, os.WriteFile(hostsFile, []byte(hostsTemplate+eol()+crcSection("127.0.0.1 entry1 entry2 entry3 entry4 entry5 entry6 entry7 entry8 entry9 entry10")+eol()), 0600))

host := hosts(t, hostsFile)
Expand All @@ -218,11 +174,7 @@ func TestAddMoreThan9HostsWithOverfullLine(t *testing.T) {
}

func TestRemoveOnOldHostFile(t *testing.T) {
dir, err := os.MkdirTemp("", "hosts")
assert.NoError(t, err)
defer os.RemoveAll(dir)

hostsFile := filepath.Join(dir, "hosts")
hostsFile := filepath.Join(t.TempDir(), "hosts")
assert.NoError(t, os.WriteFile(hostsFile, []byte(hostsTemplate+eol()+"127.0.0.1 entry1 entry2"), 0600))

host := hosts(t, hostsFile)
Expand All @@ -234,16 +186,52 @@ func TestRemoveOnOldHostFile(t *testing.T) {
assert.Equal(t, hostsTemplate, string(content))
}

func TestDeleteSliceBoundErrorOnRemove(t *testing.T) {
dir, err := os.MkdirTemp("", "hosts")
func TestRemoveMultipleForwardSameLine(t *testing.T) {
hostsFile := filepath.Join(t.TempDir(), "hosts")
assert.NoError(t, os.WriteFile(hostsFile, []byte(hostsTemplate+eol()+crcSection("192.168.130.11 entry1 entry2")), 0600))
host := hosts(t, hostsFile)

assert.NoError(t, host.Remove([]string{"entry1", "entry2"}))

content, err := os.ReadFile(hostsFile)
assert.NoError(t, err)
assert.Equal(t, hostsTemplate+eol()+crcSection(), string(content))
}

func TestRemoveMultipleBackwardsSameLine(t *testing.T) {
hostsFile := filepath.Join(t.TempDir(), "hosts")
assert.NoError(t, os.WriteFile(hostsFile, []byte(hostsTemplate+eol()+crcSection("192.168.130.11 entry1 entry2")), 0600))
host := hosts(t, hostsFile)

assert.NoError(t, host.Remove([]string{"entry2", "entry1"}))

content, err := os.ReadFile(hostsFile)
assert.NoError(t, err)
defer os.RemoveAll(dir)
assert.Equal(t, hostsTemplate+eol()+crcSection(), string(content))
}

func TestRemoveMultipleLines(t *testing.T) {
hostsFile := filepath.Join(t.TempDir(), "hosts")
assert.NoError(t, os.WriteFile(hostsFile, []byte(hostsTemplate+eol()+crcSection("192.168.130.11 api.crc.testing", "192.168.130.11 oauth-openshift.apps-crc.testing")), 0600))
host := hosts(t, hostsFile)

assert.NoError(t, host.Remove([]string{"api.crc.testing", "oauth-openshift.apps-crc.testing"}))

content, err := os.ReadFile(hostsFile)
assert.NoError(t, err)
assert.Equal(t, hostsTemplate+eol()+crcSection(), string(content))
}

hostsFile := filepath.Join(dir, "hosts")
assert.NoError(t, os.WriteFile(hostsFile, []byte(hostsTemplate+eol()+crcSection("192.168.130.11 api.crc.testing canary-openshift-ingress-canary.apps-crc.testing console-openshift-console.apps-crc.testing default-route-openshift-image-registry.apps-crc.testing downloads-openshift-console.apps-crc.testing oauth-openshift.apps-crc.testing")), 0600))
func TestRemoveMultipleNoCrcSection(t *testing.T) {
hostsFile := filepath.Join(t.TempDir(), "hosts")
assert.NoError(t, os.WriteFile(hostsFile, []byte("192.168.130.11 entry1 entry2"+eol()+"192.168.130.11 entry3 entry4"), 0600))
host := hosts(t, hostsFile)

assert.NoError(t, host.Remove([]string{"api.crc.testing", "oauth-openshift.apps-crc.testing", "console-openshift-console.apps-crc.testing", "downloads-openshift-console.apps-crc.testing", "canary-openshift-ingress-canary.apps-crc.testing", "default-route-openshift-image-registry.apps-crc.testing"}))
assert.NoError(t, host.Remove([]string{"entry1", "entry2", "entry3", "entry4"}))

content, err := os.ReadFile(hostsFile)
assert.NoError(t, err)
assert.Equal(t, "", string(content))
}

func hosts(t *testing.T, hostsFile string) Hosts {
Expand All @@ -266,5 +254,9 @@ func eol() string {
}

func crcSection(lines ...string) string {
return fmt.Sprintf("# Added by CRC"+eol()+"%s"+eol()+"# End of CRC section", strings.Join(lines, eol()))
var content = ""
if len(lines) != 0 {
content = strings.Join(lines, eol()) + eol()
}
return fmt.Sprintf("# Added by CRC"+eol()+"%s"+"# End of CRC section", content)
}