-
Notifications
You must be signed in to change notification settings - Fork 30
Added Dns class for managing DNS search and nameservers #127
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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 | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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