File: augeas_spec.rb

package info (click to toggle)
puppet 5.5.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,316 kB
  • sloc: ruby: 254,925; sh: 1,608; xml: 219; makefile: 153; sql: 103
file content (120 lines) | stat: -rw-r--r-- 4,187 bytes parent folder | download
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
require 'spec_helper'

augeas = Puppet::Type.type(:augeas)

describe augeas do
  describe "when augeas is present", :if => Puppet.features.augeas? do
    it "should have a default provider inheriting from Puppet::Provider" do
      expect(augeas.defaultprovider.ancestors).to be_include(Puppet::Provider)
    end

    it "should have a valid provider" do
      expect(augeas.new(:name => "foo").provider.class.ancestors).to be_include(Puppet::Provider)
    end
  end

  describe "basic structure" do
    it "should be able to create an instance" do
      provider_class = Puppet::Type::Augeas.provider(Puppet::Type::Augeas.providers[0])
      expect(Puppet::Type::Augeas).to receive(:defaultprovider).and_return(provider_class)
      expect(augeas.new(:name => "bar")).not_to be_nil
    end

    it "should have a parse_commands feature" do
      expect(augeas.provider_feature(:parse_commands)).not_to be_nil
    end

    it "should have a need_to_run? feature" do
      expect(augeas.provider_feature(:need_to_run?)).not_to be_nil
    end

    it "should have an execute_changes feature" do
      expect(augeas.provider_feature(:execute_changes)).not_to be_nil
    end

    properties = [:returns]
    params = [:name, :context, :onlyif, :changes, :root, :load_path, :type_check, :show_diff]

    properties.each do |property|
      it "should have a #{property} property" do
        expect(augeas.attrclass(property).ancestors).to be_include(Puppet::Property)
      end

      it "should have documentation for its #{property} property" do
        expect(augeas.attrclass(property).doc).to be_instance_of(String)
      end
    end

    params.each do |param|
      it "should have a #{param} parameter" do
        expect(augeas.attrclass(param).ancestors).to be_include(Puppet::Parameter)
      end

      it "should have documentation for its #{param} parameter" do
        expect(augeas.attrclass(param).doc).to be_instance_of(String)
      end
    end
  end

  describe "default values" do
    before do
      provider_class = augeas.provider(augeas.providers[0])
      expect(augeas).to receive(:defaultprovider).and_return(provider_class)
    end

    it "should be blank for context" do
      expect(augeas.new(:name => :context)[:context]).to eq("")
    end

    it "should be blank for onlyif" do
      expect(augeas.new(:name => :onlyif)[:onlyif]).to eq("")
    end

    it "should be blank for load_path" do
      expect(augeas.new(:name => :load_path)[:load_path]).to eq("")
    end

    it "should be / for root" do
      expect(augeas.new(:name => :root)[:root]).to eq("/")
    end

    it "should be false for type_check" do
      expect(augeas.new(:name => :type_check)[:type_check]).to eq(:false)
    end
  end

  describe "provider interaction" do
    it "should return 0 if it does not need to run" do
      provider = double("provider", :need_to_run? => false)
      resource = double('resource', :resource => nil, :provider => provider, :line => nil, :file => nil)
      changes = augeas.attrclass(:returns).new(:resource => resource)
      expect(changes.retrieve).to eq(0)
    end

    it "should return :need_to_run if it needs to run" do
      provider = double("provider", :need_to_run? => true)
      resource = double('resource', :resource => nil, :provider => provider, :line => nil, :file => nil)
      changes = augeas.attrclass(:returns).new(:resource => resource)
      expect(changes.retrieve).to eq(:need_to_run)
    end
  end

  describe "loading specific files" do
    it "should require lens when incl is used" do
      expect { augeas.new(:name => :no_lens, :incl => "/etc/hosts")}.to raise_error(Puppet::Error)
    end

    it "should require incl when lens is used" do
      expect { augeas.new(:name => :no_incl, :lens => "Hosts.lns") }.to raise_error(Puppet::Error)
    end

    it "should set the context when a specific file is used" do
      fake_provider = double(
        "fake_provider",
        :name => nil,
      )
      allow(augeas).to receive(:defaultprovider).and_return(fake_provider)
      expect(augeas.new(:name => :no_incl, :lens => "Hosts.lns", :incl => "/etc/hosts")[:context]).to eq("/files/etc/hosts")
    end
  end
end