File: package_spec.rb

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (180 lines) | stat: -rw-r--r-- 6,765 bytes parent folder | download | duplicates (2)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
require 'spec_helper'
require 'puppet/provider/package/windows/package'

describe Puppet::Provider::Package::Windows::Package do
  let(:hklm) { 'HKEY_LOCAL_MACHINE' }
  let(:hkcu) { 'HKEY_CURRENT_USER' }
  let(:path) { 'Software\Microsoft\Windows\CurrentVersion\Uninstall' }
  let(:key)  { double('key', :name => "#{hklm}\\#{path}\\Google") }
  let(:package)  { double('package') }

  context '::each' do
    it 'should generate an empty enumeration' do
      expect(described_class).to receive(:with_key)

      expect(described_class.to_a).to be_empty
    end

    it 'should yield each package it finds' do
      expect(described_class).to receive(:with_key).and_yield(key, {})

      expect(Puppet::Provider::Package::Windows::MsiPackage).to receive(:from_registry).with('Google', {}).and_return(package)

      yielded = nil
      described_class.each do |pkg|
        yielded = pkg
      end

      expect(yielded).to eq(package)
    end
  end

  context '::with_key', :if => Puppet::Util::Platform.windows? do
    it 'should search HKLM (64 & 32) and HKCU (64 & 32)' do
      expect(described_class).to receive(:open).with(hklm, path, described_class::KEY64 | described_class::KEY_READ).ordered
      expect(described_class).to receive(:open).with(hklm, path, described_class::KEY32 | described_class::KEY_READ).ordered
      expect(described_class).to receive(:open).with(hkcu, path, described_class::KEY64 | described_class::KEY_READ).ordered
      expect(described_class).to receive(:open).with(hkcu, path, described_class::KEY32 | described_class::KEY_READ).ordered

      described_class.with_key { |key, values| }
    end

    it 'should ignore file not found exceptions' do
      ex = Puppet::Util::Windows::Error.new('Failed to open registry key', Puppet::Util::Windows::Error::ERROR_FILE_NOT_FOUND)

      # make sure we don't stop after the first exception
      expect(described_class).to receive(:open).exactly(4).times().and_raise(ex)

      keys = []
      described_class.with_key { |key, values| keys << key }
      expect(keys).to be_empty
    end

    it 'should raise other types of exceptions' do
      ex = Puppet::Util::Windows::Error.new('Failed to open registry key', Puppet::Util::Windows::Error::ERROR_ACCESS_DENIED)
      expect(described_class).to receive(:open).and_raise(ex)

      expect {
        described_class.with_key{ |key, values| }
      }.to raise_error do |error|
        expect(error).to be_a(Puppet::Util::Windows::Error)
        expect(error.code).to eq(5) # ERROR_ACCESS_DENIED
      end
    end
  end

  context '::installer_class' do
    it 'should require the source parameter' do
      expect {
        described_class.installer_class({})
      }.to raise_error(Puppet::Error, /The source parameter is required when using the Windows provider./)
    end

    context 'MSI' do
      let (:klass) { Puppet::Provider::Package::Windows::MsiPackage }

      it 'should accept source ending in .msi' do
        expect(described_class.installer_class({:source => 'foo.msi'})).to eq(klass)
      end

      it 'should accept quoted source ending in .msi' do
        expect(described_class.installer_class({:source => '"foo.msi"'})).to eq(klass)
      end

      it 'should accept source case insensitively' do
        expect(described_class.installer_class({:source => '"foo.MSI"'})).to eq(klass)
      end

      it 'should reject source containing msi in the name' do
        expect {
          described_class.installer_class({:source => 'mymsi.txt'})
        }.to raise_error(Puppet::Error, /Don't know how to install 'mymsi.txt'/)
      end
    end

    context 'Unknown' do
      it 'should reject packages it does not know about' do
        expect {
          described_class.installer_class({:source => 'basram'})
        }.to raise_error(Puppet::Error, /Don't know how to install 'basram'/)
      end
    end
  end

  context '::munge' do
    it 'should shell quote strings with spaces and fix forward slashes' do
      expect(described_class.munge('c:/windows/the thing')).to eq('"c:\windows\the thing"')
    end
    it 'should leave properly formatted paths alone' do
      expect(described_class.munge('c:\windows\thething')).to eq('c:\windows\thething')
    end
  end

  context '::replace_forward_slashes' do
    it 'should replace forward with back slashes' do
      expect(described_class.replace_forward_slashes('c:/windows/thing/stuff')).to eq('c:\windows\thing\stuff')
    end
  end

  context '::quote' do
    it 'should shell quote strings with spaces' do
      expect(described_class.quote('foo bar')).to eq('"foo bar"')
    end

    it 'should shell quote strings with spaces and quotes' do
      expect(described_class.quote('"foo bar" baz')).to eq('"\"foo bar\" baz"')
    end

    it 'should not shell quote strings without spaces' do
      expect(described_class.quote('"foobar"')).to eq('"foobar"')
    end
  end

  context '::get_display_name' do
    it 'should return nil if values is nil' do
      expect(described_class.get_display_name(nil)).to be_nil
    end

    it 'should return empty if values is empty' do
      reg_values =  {}
      expect(described_class.get_display_name(reg_values)).to eq('')
    end

    it 'should return DisplayName when available' do
      reg_values =  { 'DisplayName' => 'Google' }
      expect(described_class.get_display_name(reg_values)).to eq('Google')
    end

    it 'should return DisplayName when available, even when QuietDisplayName is also available' do
      reg_values =  { 'DisplayName' => 'Google', 'QuietDisplayName' => 'Google Quiet' }
      expect(described_class.get_display_name(reg_values)).to eq('Google')
    end

    it 'should return QuietDisplayName when available if DisplayName is empty' do
      reg_values =  { 'DisplayName' => '', 'QuietDisplayName' =>'Google Quiet' }
      expect(described_class.get_display_name(reg_values)).to eq('Google Quiet')
    end

    it 'should return QuietDisplayName when DisplayName is not available' do
      reg_values =  { 'QuietDisplayName' =>'Google Quiet' }
      expect(described_class.get_display_name(reg_values)).to eq('Google Quiet')
    end

    it 'should return empty when DisplayName is empty and QuietDisplay name is not available' do
      reg_values =  { 'DisplayName' => '' }
      expect(described_class.get_display_name(reg_values)).to eq('')
    end

    it 'should return empty when DisplayName is empty and QuietDisplay name is empty' do
      reg_values =  { 'DisplayName' => '', 'QuietDisplayName' =>'' }
      expect(described_class.get_display_name(reg_values)).to eq('')
    end
  end

  it 'should implement instance methods' do
    pkg = described_class.new('orca', '5.0')

    expect(pkg.name).to eq('orca')
    expect(pkg.version).to eq('5.0')
  end
end