summaryrefslogtreecommitdiff
path: root/spec/unit/provider/package/windows/msi_package_spec.rb
blob: 40a323ace67b23c3fe01810de95eeeefc824cd18 (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
#! /usr/bin/env ruby
require 'spec_helper'
require 'puppet/provider/package/windows/msi_package'

describe Puppet::Provider::Package::Windows::MsiPackage do
  subject { described_class }

  let (:name)        { 'mysql-5.1.58-win-x64' }
  let (:version)     { '5.1.58' }
  let (:source)      { 'E:\mysql-5.1.58-win-x64.msi' }
  let (:productcode) { '{E437FFB6-5C49-4DAC-ABAE-33FF065FE7CC}' }
  let (:packagecode) { '{5A6FD560-763A-4BC1-9E03-B18DFFB7C72C}' }

  def expect_installer
    inst = mock
    inst.expects(:ProductState).returns(5)
    inst.expects(:ProductInfo).with(productcode, 'PackageCode').returns(packagecode)
    subject.expects(:installer).returns(inst)
  end

  context '::installer', :if => Puppet.features.microsoft_windows? do
    it 'should return an instance of the COM interface' do
      subject.installer.should_not be_nil
    end
  end

  context '::from_registry' do
    it 'should return an instance of MsiPackage' do
      subject.expects(:valid?).returns(true)
      expect_installer

      pkg = subject.from_registry(productcode, {'DisplayName' => name, 'DisplayVersion' => version})
      pkg.name.should == name
      pkg.version.should == version
      pkg.productcode.should == productcode
      pkg.packagecode.should == packagecode
    end

    it 'should return nil if it is not a valid MSI' do
      subject.expects(:valid?).returns(false)

      subject.from_registry(productcode, {}).should be_nil
    end
  end

  context '::valid?' do
    let(:values) do { 'DisplayName' => name, 'DisplayVersion' => version, 'WindowsInstaller' => 1 } end

    {
      'DisplayName'      => ['My App', ''],
      'SystemComponent'  => [nil, 1],
      'WindowsInstaller' => [1, nil],
    }.each_pair do |k, arr|
      it "should accept '#{k}' with value '#{arr[0]}'" do
        values[k] = arr[0]
        subject.valid?(productcode, values).should be_true
      end

      it "should reject '#{k}' with value '#{arr[1]}'" do
        values[k] = arr[1]
        subject.valid?(productcode, values).should be_false
      end
    end

    it 'should reject packages whose name is not a productcode' do
     subject.valid?('AddressBook', values).should be_false
   end

   it 'should accept packages whose name is a productcode' do
     subject.valid?(productcode, values).should be_true
   end
  end

  context '#match?' do
    it 'should match package codes case-insensitively' do
      pkg = subject.new(name, version, productcode, packagecode.upcase)

      pkg.match?({:name => packagecode.downcase}).should be_true
    end

    it 'should match product codes case-insensitively' do
      pkg = subject.new(name, version, productcode.upcase, packagecode)

      pkg.match?({:name => productcode.downcase}).should be_true
    end

    it 'should match product name' do
      pkg = subject.new(name, version, productcode, packagecode)

      pkg.match?({:name => name}).should be_true
    end

    it 'should return false otherwise' do
      pkg = subject.new(name, version, productcode, packagecode)

      pkg.match?({:name => 'not going to find it'}).should be_false
    end
  end

  context '#install_command' do
    it 'should install using the source' do
      cmd = subject.install_command({:source => source})

      cmd.should == ['msiexec.exe', '/qn', '/norestart', '/i', source]
    end
  end

  context '#uninstall_command' do
    it 'should uninstall using the productcode' do
      pkg = subject.new(name, version, productcode, packagecode)

      pkg.uninstall_command.should == ['msiexec.exe', '/qn', '/norestart', '/x', productcode]
    end
  end
end