Skip to content

Commit

Permalink
CB-27775 Add newline to end of hosts file
Browse files Browse the repository at this point in the history
  • Loading branch information
Bajzathd committed Nov 27, 2024
1 parent fc4b5d4 commit 5db45fa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
3 changes: 2 additions & 1 deletion saltboot/hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ func updateHostsFile(hostName string, domain string, file string, ipv4address st
filteredLines = append(filteredLines, line)
}
}
hostsFile = strings.Join(filteredLines, "\n") + "\n" + ipv4HostString
var hostsLines = append(filteredLines, ipv4HostString)
hostsFile = strings.Join(hostsLines, "\n") + "\n"
log.Printf("[updateHostsFile] updated hosts file: %s", hostsFile)
err = writeFile(file, []byte(hostsFile), 0644)
if err != nil {
Expand Down
35 changes: 25 additions & 10 deletions saltboot/hostname_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func TestHostsFileWriteRemoveExistingIp(t *testing.T) {
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
10.0.0.1 hostname-1.compute.internal hostname-1`
10.0.0.1 hostname-1.compute.internal hostname-1
`
return []byte(hostsFile), nil
}
defer func() {
Expand All @@ -45,7 +46,9 @@ func TestHostsFileWriteRemoveExistingIp(t *testing.T) {
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
10.0.0.1 hostname-1.example.com hostname-1`
10.0.0.1 hostname-1.example.com hostname-1
`

if expected != result {
t.Errorf("Invalid hostname replacement, %s != %s", expected, result)
Expand All @@ -59,7 +62,8 @@ func TestHostsFileWriteRemoveExistingIpNotLastLine(t *testing.T) {
255.255.255.255 broadcasthost
::1 localhost
10.0.0.1 hostname-1.compute.internal hostname-1
10.0.0.2 hostname-2.compute.internal hostname-2`
10.0.0.2 hostname-2.compute.internal hostname-2
`
return []byte(hostsFile), nil
}
defer func() {
Expand All @@ -82,7 +86,9 @@ func TestHostsFileWriteRemoveExistingIpNotLastLine(t *testing.T) {
255.255.255.255 broadcasthost
::1 localhost
10.0.0.2 hostname-2.compute.internal hostname-2
10.0.0.1 hostname-1.example.com hostname-1`
10.0.0.1 hostname-1.example.com hostname-1
`

if expected != result {
t.Errorf("Invalid hostname replacement, %s != %s", expected, result)
Expand All @@ -97,7 +103,8 @@ func TestHostsFileWriteRemoveExistingIpMiddleLastLine(t *testing.T) {
::1 localhost
10.0.0.2 hostname-2.compute.internal hostname-2
10.0.0.1 hostname-1.compute.internal hostname-1
10.0.0.3 hostname-3.compute.internal hostname-3`
10.0.0.3 hostname-3.compute.internal hostname-3
`
return []byte(hostsFile), nil
}
defer func() {
Expand All @@ -121,7 +128,9 @@ func TestHostsFileWriteRemoveExistingIpMiddleLastLine(t *testing.T) {
::1 localhost
10.0.0.2 hostname-2.compute.internal hostname-2
10.0.0.3 hostname-3.compute.internal hostname-3
10.0.0.1 hostname-1.example.com hostname-1`
10.0.0.1 hostname-1.example.com hostname-1
`

if expected != result {
t.Errorf("Invalid hostname replacement, %s != %s", expected, result)
Expand All @@ -135,7 +144,8 @@ func TestHostsFileWriteIpNotPresent(t *testing.T) {
255.255.255.255 broadcasthost
::1 localhost
10.0.0.2 hostname-2
10.0.0.3 hostname-3`
10.0.0.3 hostname-3
`
return []byte(hostsFile), nil
}
defer func() {
Expand All @@ -159,7 +169,9 @@ func TestHostsFileWriteIpNotPresent(t *testing.T) {
::1 localhost
10.0.0.2 hostname-2
10.0.0.3 hostname-3
10.0.0.1 hostname-1.example.com hostname-1`
10.0.0.1 hostname-1.example.com hostname-1
`

if expected != result {
t.Errorf("Invalid hostname replacement, %s != %s", expected, result)
Expand All @@ -174,7 +186,8 @@ func TestHostsFileWriteExistingWithDefaultDomain(t *testing.T) {
::1 localhost
10.0.0.1 hostname-1.compute.internal hostname-1
10.0.0.2 hostname-2.compute.internal hostname-2
10.0.0.3 hostname-3.compute.internal hostname-3`
10.0.0.3 hostname-3.compute.internal hostname-3
`
return []byte(hostsFile), nil
}
defer func() {
Expand All @@ -198,7 +211,9 @@ func TestHostsFileWriteExistingWithDefaultDomain(t *testing.T) {
::1 localhost
10.0.0.2 hostname-2.compute.internal hostname-2
10.0.0.3 hostname-3.compute.internal hostname-3
10.0.0.1 hostname-1.compute.internal hostname-1`
10.0.0.1 hostname-1.compute.internal hostname-1
`

if expected != result {
t.Errorf("Invalid hostname replacement, %s != %s", expected, result)
Expand Down

0 comments on commit 5db45fa

Please sign in to comment.