~gandelman-a/ubuntu/precise/facter/merge922788

« back to all changes in this revision

Viewing changes to spec/unit/domain_spec.rb

  • Committer: Package Import Robot
  • Author(s): Adam Gandelman
  • Date: 2011-10-18 10:32:42 UTC
  • mfrom: (1.1.10 upstream) (3.1.10 sid)
  • Revision ID: package-import@ubuntu.com-20111018103242-vvgvfa9pf057xnmj
Tags: 1.6.1-1ubuntu1
* Merge from Debian unstable (LP: #877621). Remaining changes:
  - debian/rules: use what we had in natty; we dont want ruby-pkg-tools
    in main. (LP: #408402)
* debian/control: Continue using ruby + libopenssl-ruby as Build-Depends
  even tho Debian has moved to gem2deb (not in main). Move ruby-json to
  Suggests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
 
2
 
 
3
describe "Domain name facts" do
 
4
 
 
5
  describe "on linux" do
 
6
    before do
 
7
      Facter.fact(:kernel).stubs(:value).returns("Linux")
 
8
      FileTest.stubs(:exists?).with("/etc/resolv.conf").returns(true)
 
9
    end
 
10
 
 
11
    it "should use the hostname binary" do
 
12
      Facter::Util::Resolution.expects(:exec).with("hostname").returns "test.example.com"
 
13
      Facter.fact(:domain).value.should == "example.com"
 
14
    end
 
15
 
 
16
    it "should fall back to the dnsdomainname binary" do
 
17
      Facter::Util::Resolution.expects(:exec).with("hostname").returns("myhost")
 
18
      Facter::Util::Resolution.expects(:exec).with("dnsdomainname").returns("example.com")
 
19
      Facter.fact(:domain).value.should == "example.com"
 
20
    end
 
21
 
 
22
 
 
23
    it "should fall back to /etc/resolv.conf" do
 
24
      Facter::Util::Resolution.expects(:exec).with("hostname").at_least_once.returns("myhost")
 
25
      Facter::Util::Resolution.expects(:exec).with("dnsdomainname").at_least_once.returns("")
 
26
      File.expects(:open).with('/etc/resolv.conf').at_least_once
 
27
      Facter.fact(:domain).value
 
28
    end
 
29
 
 
30
    it "should attempt to resolve facts in a specific order" do
 
31
      seq = sequence('domain')
 
32
      Facter::Util::Resolution.stubs(:exec).with("hostname").in_sequence(seq).at_least_once
 
33
      Facter::Util::Resolution.stubs(:exec).with("dnsdomainname").in_sequence(seq).at_least_once
 
34
      File.expects(:open).with('/etc/resolv.conf').in_sequence(seq).at_least_once
 
35
      Facter.fact(:domain).value
 
36
    end
 
37
 
 
38
    describe "when using /etc/resolv.conf" do
 
39
      before do
 
40
        Facter::Util::Resolution.stubs(:exec).with("hostname")
 
41
        Facter::Util::Resolution.stubs(:exec).with("dnsdomainname")
 
42
        @mock_file = mock()
 
43
        File.stubs(:open).with("/etc/resolv.conf").yields(@mock_file)
 
44
      end
 
45
 
 
46
      it "should use the domain field over the search field" do
 
47
        lines = [
 
48
          "nameserver 4.2.2.1",
 
49
          "search example.org",
 
50
          "domain example.com",
 
51
        ]
 
52
        @mock_file.expects(:each).multiple_yields(*lines)
 
53
        Facter.fact(:domain).value.should == 'example.com'
 
54
      end
 
55
 
 
56
      it "should fall back to the search field" do
 
57
        lines = [
 
58
          "nameserver 4.2.2.1",
 
59
          "search example.org",
 
60
        ]
 
61
        @mock_file.expects(:each).multiple_yields(*lines)
 
62
        Facter.fact(:domain).value.should == 'example.org'
 
63
      end
 
64
 
 
65
      it "should use the first domain in the search field" do
 
66
        lines = [
 
67
          "search example.org example.net",
 
68
        ]
 
69
        @mock_file.expects(:each).multiple_yields(*lines)
 
70
        Facter.fact(:domain).value.should == 'example.org'
 
71
      end
 
72
    end
 
73
  end
 
74
 
 
75
  describe "on Windows" do
 
76
    it "should use the DNSDomain for the first nic where ip is enabled" do
 
77
      Facter.fact(:kernel).stubs(:value).returns("windows")
 
78
 
 
79
      nic = stubs 'nic'
 
80
      nic.stubs(:DNSDomain).returns("foo.com")
 
81
 
 
82
      nic2 = stubs 'nic'
 
83
      nic2.stubs(:DNSDomain).returns("bar.com")
 
84
 
 
85
      require 'facter/util/wmi'
 
86
      Facter::Util::WMI.stubs(:execquery).with("select DNSDomain from Win32_NetworkAdapterConfiguration where IPEnabled = True").returns([nic, nic2])
 
87
 
 
88
      Facter.fact(:domain).value.should == 'foo.com'
 
89
    end
 
90
  end
 
91
end