summaryrefslogtreecommitdiffstats
path: root/spec/unit/util/network_device/config_spec.rb
blob: d69358a92c17fc3bcb85d44c37986364dfac3f64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env rspec
require 'spec_helper'

require 'puppet/util/network_device/config'

describe Puppet::Util::NetworkDevice::Config do
  before(:each) do
    Puppet[:deviceconfig] = "/dummy"
    FileTest.stubs(:exists?).with("/dummy").returns(true)
  end

  describe "when initializing" do
    before :each do
      Puppet::Util::NetworkDevice::Config.any_instance.stubs(:read)
    end

    it "should use the deviceconfig setting as pathname" do
      Puppet.expects(:[]).with(:deviceconfig).returns("/dummy")

      Puppet::Util::NetworkDevice::Config.new
    end

    it "should raise an error if no file is defined finally" do
      Puppet.expects(:[]).with(:deviceconfig).returns(nil)

      lambda { Puppet::Util::NetworkDevice::Config.new }.should raise_error(Puppet::DevError)
    end

    it "should read and parse the file" do
      Puppet::Util::NetworkDevice::Config.any_instance.expects(:read)

      Puppet::Util::NetworkDevice::Config.new
    end
  end

  describe "when parsing device" do
    before :each do
      @config = Puppet::Util::NetworkDevice::Config.new
      @config.stubs(:changed?).returns(true)
      @fd = stub 'fd'
      File.stubs(:open).yields(@fd)
    end

    it "should skip comments" do
      @fd.stubs(:each).yields('  # comment')

      OpenStruct.expects(:new).never

      @config.read
    end

    it "should increment line number even on commented lines" do
      @fd.stubs(:each).multiple_yields('  # comment','[router.puppetlabs.com]')

      @config.read
      @config.devices.should be_include('router.puppetlabs.com')
    end

    it "should skip blank lines" do
      @fd.stubs(:each).yields('  ')

      @config.read
      @config.devices.should be_empty
    end

    it "should produce the correct line number" do
      @fd.stubs(:each).multiple_yields('  ', '[router.puppetlabs.com]')

      @config.read
      @config.devices['router.puppetlabs.com'].line.should == 2
    end

    it "should throw an error if the current device already exists" do
      @fd.stubs(:each).multiple_yields('[router.puppetlabs.com]', '[router.puppetlabs.com]')

      lambda { @config.read }.should raise_error
    end

    it "should create a new device for each found device line" do
      @fd.stubs(:each).multiple_yields('[router.puppetlabs.com]', '[swith.puppetlabs.com]')

      @config.read
      @config.devices.size.should == 2
    end

    it "should parse the device type" do
      @fd.stubs(:each).multiple_yields('[router.puppetlabs.com]', 'type cisco')

      @config.read
      @config.devices['router.puppetlabs.com'].provider.should == 'cisco'
    end

    it "should parse the device url" do
      @fd.stubs(:each).multiple_yields('[router.puppetlabs.com]', 'type cisco', 'url ssh://test/')

      @config.read
      @config.devices['router.puppetlabs.com'].url.should == 'ssh://test/'
    end
  end

end