summaryrefslogtreecommitdiff
path: root/spec/unit/puppet/type/vcsrepo_spec.rb
blob: 0070f49c205607c5a1b80bf3788802b674e12bfc (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#! /usr/bin/env ruby
require 'spec_helper'

describe Puppet::Type.type(:vcsrepo) do

  before :each do
    Puppet::Type.type(:vcsrepo).stubs(:defaultprovider).returns(providerclass)
  end

  let(:providerclass) do
    described_class.provide(:fake_vcsrepo_provider) do
      attr_accessor :property_hash
      def create; end
      def destroy; end
      def exists?
        get(:ensure) != :absent
      end
      mk_resource_methods
    end
  end

  let(:provider) do
    providerclass.new(:name => 'fake-vcs')
  end

  let(:resource) do
    described_class.new(:name => '/repo',
                        :ensure => :present,
                        :provider => provider)
  end

  let(:ensureprop) do
    resource.property(:ensure)
  end

  properties = [ :ensure ]

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

  parameters = [ :ensure ]

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

  describe 'default resource with required params' do
    it 'should have a valid name parameter' do
      expect(resource[:name]).to eq('/repo')
    end

    it 'should have ensure set to present' do
      expect(resource[:ensure]).to eq(:present)
    end

    it 'should have path set to /repo' do
      expect(resource[:path]).to eq('/repo')
    end

    defaults = {
      :owner => nil,
      :group => nil,
      :user => nil,
      :revision => nil,
    }

    defaults.each_pair do |param, value|
      it "should have #{param} parameter set to #{value}" do
        expect(resource[param]).to eq(value)
      end
    end
  end

  describe 'when changing the ensure' do
    it 'should be in sync if it is :absent and should be :absent' do
      ensureprop.should = :absent
      expect(ensureprop.safe_insync?(:absent)).to eq(true)
    end

    it 'should be in sync if it is :present and should be :present' do
      ensureprop.should = :present
      expect(ensureprop.safe_insync?(:present)).to eq(true)
    end

    it 'should be out of sync if it is :absent and should be :present' do
      ensureprop.should = :present
      expect(ensureprop.safe_insync?(:absent)).not_to eq(true)
    end

    it 'should be out of sync if it is :present and should be :absent' do
      ensureprop.should = :absent
      expect(ensureprop.safe_insync?(:present)).not_to eq(true)
    end
  end

  describe 'when running the type it should autorequire packages' do
    before :each do
      @catalog = Puppet::Resource::Catalog.new
      ['git', 'git-core', 'mercurial'].each do |pkg|
        @catalog.add_resource(Puppet::Type.type(:package).new(:name => pkg))
      end
    end

    it 'should require package packages' do
      @resource = described_class.new(:name => '/foo', :provider => provider)
      @catalog.add_resource(@resource)
      req = @resource.autorequire
      expect(req.size).to eq(3)
    end
  end
end