Skip to content
This repository has been archived by the owner on Mar 2, 2021. It is now read-only.

fix to set hostname to debian reference guide #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cmd/hostname_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ package cmd

import (
"fmt"
"github.com/spf13/cobra"
"io/ioutil"
"os/exec"
"regexp"
"strings"

"github.com/spf13/cobra"
)

// setCmd represents the set command
Expand Down Expand Up @@ -71,7 +72,7 @@ func setHostname(args ...string) {
panic(err)
}

hostname_line := fmt.Sprintf("127.0.0.1 %s # added by device-init", hostname)
hostname_line := fmt.Sprintf("127.0.1.1 %s.local %s # added by device-init", hostname, hostname)

if !is_present_in_hosts_file(hostname_line) && !is_present_in_hosts_file(hostname) {
addHostname(hostname_line)
Expand Down
15 changes: 7 additions & 8 deletions specs/device-init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,23 @@

expect(device_init_cmd_result_one).to contain("Set")
expect(device_init_cmd_result_two).to contain("Set")
expect(hosts_file_content.scan(/black-widow/).count).to eq(1)
expect(hosts_file_content.scan(/black-widow/).count).to eq(2)
end

it "replaces existing device-init hostname entry if it already exists" do
device_init_cmd_result = command('device-init hostname set black-mamba').stdout
hosts_file_content = command('cat /etc/hosts').stdout

expect(device_init_cmd_result).to contain("Set")
expect(hosts_file_content.scan(/black-mamba/).count).to eq(1)
expect(hosts_file_content.scan(/black-mamba/).count).to eq(2)
expect(hosts_file_content.scan(/black-widow/).count).to eq(0)
end

it "it does not replaces existing hostname entry if it already exists" do
hostname = 'black-pearl'
hosts_file = %Q(
127.0.0.1 localhost
127.0.0.1 #{hostname}
127.0.1.1 #{hostname}.local #{hostname}
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
Expand All @@ -68,7 +68,7 @@

expect(device_init_cmd_result).to contain("Set")
expect(hosts_file_content.scan(/\# added by device-init/).count).to eq(0)
expect(hosts_file_content.scan(/#{Regexp.quote(hostname)}/).count).to eq(1)
expect(hosts_file_content.scan(/#{Regexp.quote(hostname)}/).count).to eq(2)
end
end

Expand Down Expand Up @@ -272,7 +272,7 @@
let(:cluster_lab_enabled) { File.read(File.join(File.dirname(__FILE__), 'testdata', 'cluster_lab_enabled.yaml')) }
let(:cluster_lab_disabled) { File.read(File.join(File.dirname(__FILE__), 'testdata', 'cluster_lab_disabled.yaml')) }
let(:cluster_lab_command) { File.read(File.join(File.dirname(__FILE__), 'testdata', 'fake_cluster_lab_command')) }

before(:each) do
echo_cluster_lab_cmd = command(%Q(echo -n '#{cluster_lab_command}' > /usr/local/bin/cluster-lab))
expect(echo_cluster_lab_cmd.exit_status).to be(0)
Expand All @@ -291,7 +291,7 @@

device_init_cmd = command('device-init --config')
expect(device_init_cmd.exit_status).to be(0)

cat_cmd = command('cat /tmp/alive.log')
expect(cat_cmd.exit_status).to be(0)
expect(cat_cmd.stdout).to contain('Cluster-Lab is alive!')
Expand All @@ -309,4 +309,3 @@
end
end
end