summaryrefslogtreecommitdiff
path: root/spec/functional/resource/powershell_package_source_spec.rb
blob: 7ba1a652bc18ad84586591c7b88d8629ceeb91bb (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
#
# Author:: Matt Wrock (<matt@mattwrock.com>)
# Copyright:: Copyright (c) Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require "spec_helper"
require "chef/mixin/powershell_exec"

describe Chef::Resource::PowershellPackageSource, :windows_gte_10 do
  include Chef::Mixin::PowershellExec

  let(:source_name) { "fake" }
  let(:source_location) { "https://www.nuget.org/api/v2" }
  let(:trusted) { true }

  let(:run_context) do
    Chef::RunContext.new(Chef::Node.new, {}, Chef::EventDispatch::Dispatcher.new)
  end

  subject do
    new_resource = Chef::Resource::PowershellPackageSource.new("test powershell package source", run_context)
    new_resource.source_name source_name
    new_resource.source_location source_location
    new_resource.trusted trusted
    new_resource.provider_name provider_name
    new_resource
  end

  let(:provider) do
    provider = subject.provider_for_action(subject.action)
    provider
  end

  shared_examples "package_source" do
    context "register a package source" do
      after { remove_package_source }

      it "registers the package source" do
        subject.run_action(:register)
        expect(get_installed_package_source_name).to eq(source_name)
      end

      it "does not register the package source if already installed" do
        subject.run_action(:register)
        subject.run_action(:register)
        expect(subject).not_to be_updated_by_last_action
      end

      it "updates an existing package source if changed" do
        subject.run_action(:register)
        subject.trusted !trusted
        subject.run_action(:set)
        expect(subject).to be_updated_by_last_action
      end
    end

    context "unregister a package source" do
      it "unregisters the package source" do
        subject.run_action(:register)
        subject.run_action(:unregister)
        expect(get_installed_package_source_name).to be_empty
      end

      it "does not unregister the package source if not installed" do
        expect { subject.run_action(:unregister) }.to_not raise_error()
      end
    end
  end

  context "with NuGet provider" do
    let(:provider_name) { "NuGet" }

    before(:all) do
      powershell_exec!("[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;Install-PackageProvider -Name NuGet -Force")
    end

    it_behaves_like "package_source"
  end

  context "with PowerShellGet provider" do
    let(:provider_name) { "PowerShellGet" }

    it_behaves_like "package_source"
  end

  def get_installed_package_source_name
    powershell_exec!("(Get-PackageSource -Name #{source_name} -ErrorAction SilentlyContinue).Name").result
  end

  def remove_package_source
    pkg_to_remove = Chef::Resource::PowershellPackageSource.new(source_name, run_context)
    pkg_to_remove.run_action(:unregister)
  end
end