Skip to content
Merged
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
1 change: 1 addition & 0 deletions lib/linux_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
require 'linux_admin/scap'
require 'linux_admin/time_date'
require 'linux_admin/ip_address'
require 'linux_admin/dns'

module LinuxAdmin
extend Common
Expand Down
31 changes: 31 additions & 0 deletions lib/linux_admin/dns.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module LinuxAdmin
class Dns
attr_accessor :filename
attr_accessor :nameservers
attr_accessor :search_order

def initialize(filename = "/etc/resolv.conf")
@filename = filename
reload
end

def reload
@search_order = []
@nameservers = []

File.read(@filename).split("\n").each do |line|
if line =~ /^search .*/
@search_order += line.split(/^search\s+/)[1].split
elsif line =~ /^nameserver .*/
@nameservers.push(line.split[1])
end
end
end

def save
contents = "search #{@search_order.join(" ")}\n"
@nameservers.each { |ns| contents += "nameserver #{ns}\n" }
File.write(@filename, contents)
end
end
end
75 changes: 75 additions & 0 deletions spec/dns_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
describe LinuxAdmin::Dns do
RESOLV_CONF = <<DNS_END
# Generated by NetworkManager
search test.example.com test.two.example.com. example.com.
nameserver 192.168.1.2
nameserver 10.10.1.2
nameserver 192.168.252.3
DNS_END

SEARCH_ORDER = %w(test.example.com test.two.example.com. example.com.)
NAMESERVERS = %w(192.168.1.2 10.10.1.2 192.168.252.3)

NEW_CONF = <<DNS_END
search new.test.example.com other.test.example.com
nameserver 192.168.3.4
nameserver 10.10.11.12
DNS_END

NEW_ORDER = %w(new.test.example.com other.test.example.com)
NEW_NS = %w(192.168.3.4 10.10.11.12)

FILE = "/etc/dns_file"

subject do
allow(File).to receive(:read).and_return(RESOLV_CONF)
described_class.new(FILE)
end

describe ".new" do
it "sets the filename" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These three tests could also be merged in a followup PR

expect(subject.filename).to eq(FILE)
end

it "parses the nameservers" do
expect(subject.nameservers).to eq(NAMESERVERS)
end

it "parses the search order" do
expect(subject.search_order).to eq(SEARCH_ORDER)
end
end

describe "#reload" do
it "reloads the nameservers" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two tests could also be merged in a followup PR

expect(subject.nameservers).to eq(NAMESERVERS)

allow(File).to receive(:read).and_return(NEW_CONF)
subject.reload

expect(subject.nameservers).to eq(NEW_NS)
end

it "reloads the search order" do
expect(subject.search_order).to eq(SEARCH_ORDER)

allow(File).to receive(:read).and_return(NEW_CONF)
subject.reload

expect(subject.search_order).to eq(NEW_ORDER)
end
end

describe "#save" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't let these tests overwrite the test data, instead:

expect(File).to receive(:write).with(expected_string)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the save method writes the entire file, you don't really need multiple tests for search lines and nameserver lines. You can just test that given these things in the these things in the attrs, here is the exact content that should be written to this file

it "writes the correct contents" do
expect(File).to receive(:write) do |file, contents|
expect(file).to eq(subject.filename)
expect(contents).to eq(NEW_CONF)
end

subject.search_order = NEW_ORDER
subject.nameservers = NEW_NS
subject.save
end
end
end