diff options
author | John McCrae <john.mccrae@progress.com> | 2021-09-15 11:17:55 -0700 |
---|---|---|
committer | John McCrae <john.mccrae@progress.com> | 2021-09-15 11:17:55 -0700 |
commit | 80603e5bbb7ee05bf851a3d5c60dc0df9cd7a039 (patch) | |
tree | 0db7e349bad7e492785abedaf830020340c68480 | |
parent | 892b04fa70c8292948c20e7084c858541cf983eb (diff) | |
download | chef-80603e5bbb7ee05bf851a3d5c60dc0df9cd7a039.tar.gz |
Updated the package source resource to accept credentials and updated the code to be more readable
Signed-off-by: John McCrae <john.mccrae@progress.com>
-rw-r--r-- | lib/chef/resource/powershell_package_source.rb | 294 | ||||
-rw-r--r-- | spec/functional/resource/powershell_package_source_spec.rb | 14 | ||||
-rw-r--r-- | spec/unit/resource/powershell_package_source_spec.rb | 125 |
3 files changed, 296 insertions, 137 deletions
diff --git a/lib/chef/resource/powershell_package_source.rb b/lib/chef/resource/powershell_package_source.rb index c4c4d2c841..13a2b42b6f 100644 --- a/lib/chef/resource/powershell_package_source.rb +++ b/lib/chef/resource/powershell_package_source.rb @@ -24,29 +24,107 @@ class Chef provides :powershell_package_source - description "Use the **powershell_package_source** resource to register a PowerShell package repository." + description "Use the **powershell_package_source** resource to register a PowerShell Repository or other Package Source type with. There are 2 distinct objects we care about here. The first is a Package Source like a PowerShell Repository + or a Nuget Source. The second object is a provider that PowerShell uses to get to that source with, like PowerShellGet, Nuget, Chocolatey, etc. " introduced "14.3" + examples <<~DOC + **Add a new PSRepository that is not trusted and which requires credentials to connect to**: + + ```ruby + powershell_package_source 'PowerShellModules' do + source_name "PowerShellModules" + source_location "https://pkgs.dev.azure.com/some-org/some-project/_packaging/some_feed/nuget/v2" + publish_location "https://pkgs.dev.azure.com/some-org/some-project/_packaging/some_feed/nuget/v2" + trusted false + user_name "someuser@somelocation.io" + user_pass "mypassword" + provider_name "PSRepository" + action :register + end + ``` + + **Add a new Package Source that uses Chocolatey as the Package Provider**: + + ```ruby + powershell_package_source 'PowerShellModules' do + source_name "PowerShellModules" + source_location "https://pkgs.dev.azure.com/some-org/some-project/_packaging/some_feed/nuget/v2" + publish_location "https://pkgs.dev.azure.com/some-org/some-project/_packaging/some_feed/nuget/v2" + trusted true + provider_name "chocolatey" + action :register + end + ``` + + **Add a new PowerShell Script source that is trusted**: + + ```ruby + powershell_package_source 'MyDodgyScript' do + source_name "MyDodgyScript" + script_source_location "https://pkgs.dev.azure.com/some-org/some-project/_packaging/some_feed/nuget/v2" + script_bublish_location "https://pkgs.dev.azure.com/some-org/some-project/_packaging/some_feed/nuget/v2" + trusted true + action :register + end + ``` + + **Update my existing PSRepository to make it Trusted after all**: + + ```ruby + powershell_package_source 'MyPSModule' do + source_name "MyPSModule" + trusted true + action :set + end + ``` + + **Update a Nuget package source with a new name and make it trusted**: + + ```ruby + powershell_package_source 'PowerShellModules -> GoldFishBowl' do + source_name "PowerShellModules" + new_name "GoldFishBowl" + provider_name "Nuget" + trusted true + action :set + end + ``` + + **Update a Nuget package source with a new name when the source is secured with a username and password**: + + ```ruby + powershell_package_source 'PowerShellModules -> GoldFishBowl' do + source_name "PowerShellModules" + new_name "GoldFishBowl" + trusted true + user_name "user@domain.io" + user_pass "somesecretpassword" + action :set + end + ``` + + **Unregister a package source**: + + ```ruby + powershell_package_source 'PowerShellModules' do + source_name "PowerShellModules" + action :unregister + end + ``` + DOC property :source_name, String, - description: "The name of the package source.", + description: "A label that names your package source.", name_property: true - property :url, String, - description: "The URL to the package source.", - required: [:register] + property :new_name, String, + description: "Used when updating the name of a NON-PSRepository" - property :trusted, [TrueClass, FalseClass], - description: "Whether or not to trust packages from this source.", - default: false - - property :provider_name, String, - equal_to: %w{ Programs msi NuGet msu PowerShellGet psl chocolatey }, - validation_message: "The following providers are supported: 'Programs', 'msi', 'NuGet', 'msu', 'PowerShellGet', 'psl' or 'chocolatey'", - description: "The package management provider for the source.", - default: "NuGet" + property :source_location, String, + description: "The URL to the location to retrieve modules from." property :publish_location, String, - description: "The URL where modules will be published to for this source. Only valid if the provider is `PowerShellGet`." + description: "The URL where modules will be published to. Only valid if the provider is `PowerShellGet`." property :script_source_location, String, description: "The URL where scripts are located for this source. Only valid if the provider is `PowerShellGet`." @@ -54,6 +132,22 @@ class Chef property :script_publish_location, String, description: "The location where scripts will be published to for this source. Only valid if the provider is `PowerShellGet`." + property :trusted, [TrueClass, FalseClass], + description: "Whether or not to trust packages from this source. Used when creating a NON-PSRepository Package Source", + default: false + + property :user_name, String, + description: "A username that, as part of a credential object, is used to register a repository or other package source with." + + property :user_pass, String, + description: "A password that, as part of a credential object, is used to register a repository or other package source with." + + property :provider_name, String, + equal_to: %w{ Programs msi NuGet msu PowerShellGet psl chocolatey winget }, + validation_message: "The following providers are supported: 'Programs', 'msi', 'NuGet', 'msu', 'PowerShellGet', 'psl', 'chocolatey' or 'winget'", + description: "The package management provider for the package source. The default is PowerShellGet and this option need only be set otheriwse in specific use cases.", + default: "NuGet" + load_current_value do cmd = load_resource_state_script(source_name) repo = powershell_exec!(cmd) @@ -62,7 +156,9 @@ class Chef else status = repo.result end - url status["url"] + source_name status["source_name"] + new_name status["new_name"] + source_location status["source_location"] trusted status["trusted"] provider_name status["provider_name"] publish_location status["publish_location"] @@ -70,97 +166,157 @@ class Chef script_publish_location status["script_publish_location"] end - action :register, description: "Registers and updates the PowerShell package source." do - # TODO: Ensure package provider is installed? - if psrepository_cmdlet_appropriate? - if package_source_exists? - converge_if_changed :url, :trusted, :publish_location, :script_source_location, :script_publish_location do - update_cmd = build_ps_repository_command("Set", new_resource) - res = powershell_exec(update_cmd) - raise "Failed to update #{new_resource.source_name}: #{res.errors}" if res.error? - end - else - converge_by("register source: #{new_resource.source_name}") do - register_cmd = build_ps_repository_command("Register", new_resource) - res = powershell_exec(register_cmd) - raise "Failed to register #{new_resource.source_name}: #{res.errors}" if res.error? - end + # Notes: + # There are 2 objects we care about with this code. 1) The Package Provider which can be Nuget, PowerShellGet, Chocolatey, et al. 2) The PackageSource where the files we want access to live. The Package Provider gets us access to the Package Source. + # Per the Microsoft docs you can only have one provider for one source. Enter the PSRepository. It is a sub-type of Package Source. + # If you register a new PSRepository you get both a PSRepository object AND a Package Source object which are distinct. If you call "Get-PSRepository -Name 'PSGallery'" from powershell, notice that the Pacakgeprovider is Nuget + # now go execute "Get-PackageSource -Name 'PSGallery'" and notice that the PackageProvider is PowerShellGet. If you set a new PSRepository without specifying a PackageProvider ("Register-PSRepository -Name 'foo' -source...") the command will create both + # a PackageSource and a PSRepository with different providers. + + # Unregistering a PackageSource (unregister-packagesource -name 'foo') where that source is also a PSRepository also causes that object to delete as well. This makes sense as PSRepository is a sub-type of packagesource. + # All PSRepositories are PackageSources, and all PackageSources with Provider PowerShellGet are PSRepositories. They are 2 different views of the same object. + + action :register, description: "Registers a PowerShell package source." do + package_details = get_package_source_details + output = package_details.result + if output == "PSRepository" || output == "PackageSource" + action_set + elsif new_resource.provider_name.downcase.strip == "powershellget" + converge_by("register source: #{new_resource.source_name}") do + register_cmd = build_ps_repository_command("Register", new_resource) + res = powershell_exec(register_cmd) + raise "Failed to register #{new_resource.source_name}: #{res.errors}" if res.error? end else - if package_source_exists? - converge_if_changed :url, :trusted, :provider_name do - update_cmd = build_package_source_command("Set", new_resource) - res = powershell_exec(update_cmd) - raise "Failed to update #{new_resource.source_name}: #{res.errors}" if res.error? + converge_by("register source: #{new_resource.source_name}") do + register_cmd = build_package_source_command("Register", new_resource) + res = powershell_exec(register_cmd) + raise "Failed to register #{new_resource.source_name}: #{res.errors}" if res.error? + end + end + end + + action :set, description: "Updates an existing PSRepository or Package Source" do + package_details = get_package_source_details + output = package_details.result + if output == "PSRepository" + converge_if_changed :source_location, :trusted, :publish_location, :script_source_location, :script_publish_location, :source_name do + set_cmd = build_ps_repository_command("Set", new_resource) + res = powershell_exec(set_cmd) + raise "Failed to Update #{new_resource.source_name}: #{res.errors}" if res.error? end - else - converge_by("register source: #{new_resource.source_name}") do - register_cmd = build_package_source_command("Register", new_resource) - res = powershell_exec(register_cmd) - raise "Failed to register #{new_resource.source_name}: #{res.errors}" if res.error? + elsif output == "PackageSource" + converge_if_changed :source_location, :trusted, :new_name, :provider_name do + set_cmd = build_package_source_command("Set", new_resource) + res = powershell_exec(set_cmd) + raise "Failed to Update #{new_resource.source_name}: #{res.errors}" if res.error? end - end end end action :unregister, description: "Unregisters the PowerShell package source." do - if package_source_exists? - unregister_cmd = "Get-PackageSource -Name '#{new_resource.source_name}' | Unregister-PackageSource" + package_details = get_package_source_details + output = package_details.result + if output == "PackageSource" || output == "PSRepository" + unregister_cmd = "Unregister-PackageSource -Name '#{new_resource.source_name}'" converge_by("unregister source: #{new_resource.source_name}") do res = powershell_exec(unregister_cmd) raise "Failed to unregister #{new_resource.source_name}: #{res.errors}" if res.error? end + else + raise "Failed to unregister #{new_resource.source_name}: Package Source does not exist" end end action_class do - def package_source_exists? - cmd = powershell_exec!("(Get-PackageSource -Name '#{new_resource.source_name}' -ErrorAction SilentlyContinue).Name") - !cmd.result.empty? && cmd.result.to_s.downcase.strip == new_resource.source_name.downcase - end - def psrepository_cmdlet_appropriate? - new_resource.provider_name == "PowerShellGet" + def get_package_source_details + powershell_exec! <<~EOH + $package_details = Get-PackageSource -Name '#{new_resource.source_name}' -ErrorAction SilentlyContinue + if ($package_details.ProviderName -match "PowerShellGet"){ + return "PSRepository" + } + elseif ($package_details.ProviderName ) { + return "PackageSource" + } + elseif ($null -eq $package_details) + { + return "Unregistered" + } + EOH end def build_ps_repository_command(cmdlet_type, new_resource) - cmd = "#{cmdlet_type}-PSRepository -Name '#{new_resource.source_name}'" - cmd << " -SourceLocation '#{new_resource.url}'" if new_resource.url - cmd << " -InstallationPolicy '#{new_resource.trusted ? "Trusted" : "Untrusted"}'" - cmd << " -PublishLocation '#{new_resource.publish_location}'" if new_resource.publish_location - cmd << " -ScriptSourceLocation '#{new_resource.script_source_location}'" if new_resource.script_source_location - cmd << " -ScriptPublishLocation '#{new_resource.script_publish_location}'" if new_resource.script_publish_location - cmd << " | Out-Null" + if new_resource.trusted == true + install_policy = "Trusted" + else + install_policy = "Untrusted" + end + if new_resource.user_name && new_resource.user_pass + cmd = "$user = '#{new_resource.user_name}';" + cmd << "[securestring]$secure_password = Convertto-SecureString -String '#{new_resource.user_pass}' -AsPlainText -Force;" + cmd << "$Credentials = New-Object System.Management.Automation.PSCredential -Argumentlist ($user, $secure_password);" + cmd << "#{cmdlet_type}-PSRepository -Name '#{new_resource.source_name}'" + cmd << " -SourceLocation '#{new_resource.source_location}'" if new_resource.source_location + cmd << " -InstallationPolicy '#{install_policy}'" + cmd << " -PublishLocation '#{new_resource.publish_location}'" if new_resource.publish_location + cmd << " -ScriptSourceLocation '#{new_resource.script_source_location}'" if new_resource.script_source_location + cmd << " -ScriptPublishLocation '#{new_resource.script_publish_location}'" if new_resource.script_publish_location + cmd << " -Credential $Credentials" + cmd << " | Out-Null" + else + cmd = "#{cmdlet_type}-PSRepository -Name '#{new_resource.source_name}'" + cmd << " -SourceLocation '#{new_resource.source_location}'" if new_resource.source_location + cmd << " -InstallationPolicy '#{install_policy}'" + cmd << " -PublishLocation '#{new_resource.publish_location}'" if new_resource.publish_location + cmd << " -ScriptSourceLocation '#{new_resource.script_source_location}'" if new_resource.script_source_location + cmd << " -ScriptPublishLocation '#{new_resource.script_publish_location}'" if new_resource.script_publish_location + cmd << " | Out-Null" + end cmd end def build_package_source_command(cmdlet_type, new_resource) - cmd = "#{cmdlet_type}-PackageSource -Name '#{new_resource.source_name}'" - cmd << " -Location '#{new_resource.url}'" if new_resource.url - cmd << " -Trusted:#{new_resource.trusted ? "$true" : "$false"}" - cmd << " -ProviderName '#{new_resource.provider_name}'" if new_resource.provider_name - cmd << " | Out-Null" - cmd + if new_resource.user_name && new_resource.user_pass + cmd = "$user = '#{new_resource.user_name}';" + cmd << "[securestring]$secure_password = Convertto-SecureString -String '#{new_resource.user_pass}' -AsPlainText -Force;" + cmd << "$Credentials = New-Object System.Management.Automation.PSCredential -Argumentlist ($user, $secure_password);" + cmd << "#{cmdlet_type}-PackageSource -Name '#{new_resource.source_name}'" + cmd << " -Location '#{new_resource.source_location}'" if new_resource.source_location + cmd << " -Trusted" if new_resource.trusted + cmd << " -ProviderName '#{new_resource.provider_name}'" if new_resource.provider_name + cmd << " -Credential $credentials" + cmd << " | Out-Null" + cmd + else + cmd = "#{cmdlet_type}-PackageSource -Name '#{new_resource.source_name}'" + cmd << " -NewName '#{new_resource.new_name}'" if new_resource.new_name + cmd << " -Location '#{new_resource.source_location}'" if new_resource.source_location + cmd << " -Trusted" if new_resource.trusted + cmd << " -ProviderName '#{new_resource.provider_name}'" if new_resource.provider_name + cmd << " | Out-Null" + cmd + end end end end private - def load_resource_state_script(name) + def load_resource_state_script(source_name) <<-EOH $PSDefaultParameterValues = @{ "*:WarningAction" = "SilentlyContinue" } - if(Get-PackageSource -Name '#{name}' -ErrorAction SilentlyContinue) { - if ((Get-PackageSource -Name '#{name}').ProviderName -eq 'PowerShellGet') { - (Get-PSRepository -Name '#{name}') | Select @{n='source_name';e={$_.Name}}, @{n='url';e={$_.SourceLocation}}, + if(Get-PackageSource -Name '#{source_name}' -ErrorAction SilentlyContinue) { + if ((Get-PackageSource -Name '#{source_name}').ProviderName -eq 'PowerShellGet') { + (Get-PSRepository -Name '#{source_name}') | Select @{n='source_name';e={$_.Name}}, @{n='source_location';e={$_.SourceLocation}}, @{n='trusted';e={$_.Trusted}}, @{n='provider_name';e={$_.PackageManagementProvider}}, @{n='publish_location';e={$_.PublishLocation}}, @{n='script_source_location';e={$_.ScriptSourceLocation}}, @{n='script_publish_location';e={$_.ScriptPublishLocation}} } else { - (Get-PackageSource -Name '#{name}') | Select @{n='source_name';e={$_.Name}}, @{n='url';e={$_.Location}}, - @{n='provider_name';e={$_.ProviderName}}, @{n='trusted';e={$_.IsTrusted}} + (Get-PackageSource -Name '#{source_name}') | Select @{n='source_name';e={$_.Name}}, @{n='new_name';e={$_.Name}}, @{n='source_location';e={$_.Location}}, + @{n='provider_name';e={$_.ProviderName}}, @{n='trusted';e={$_.IsTrusted}}, @{n='publish_location';e={$_.PublishLocation}} } } EOH diff --git a/spec/functional/resource/powershell_package_source_spec.rb b/spec/functional/resource/powershell_package_source_spec.rb index abc9dcabd1..c49d9fcc75 100644 --- a/spec/functional/resource/powershell_package_source_spec.rb +++ b/spec/functional/resource/powershell_package_source_spec.rb @@ -22,7 +22,7 @@ describe Chef::Resource::PowershellPackageSource, :windows_gte_10 do include Chef::Mixin::PowershellExec let(:source_name) { "fake" } - let(:url) { "https://www.nuget.org/api/v2" } + let(:source_location) { "https://www.nuget.org/api/v2" } let(:trusted) { true } let(:run_context) do @@ -32,7 +32,7 @@ describe Chef::Resource::PowershellPackageSource, :windows_gte_10 do subject do new_resource = Chef::Resource::PowershellPackageSource.new("test powershell package source", run_context) new_resource.source_name source_name - new_resource.url url + new_resource.source_location source_location new_resource.trusted trusted new_resource.provider_name provider_name new_resource @@ -61,7 +61,7 @@ describe Chef::Resource::PowershellPackageSource, :windows_gte_10 do it "updates an existing package source if changed" do subject.run_action(:register) subject.trusted !trusted - subject.run_action(:register) + subject.run_action(:set) expect(subject).to be_updated_by_last_action end end @@ -73,9 +73,11 @@ describe Chef::Resource::PowershellPackageSource, :windows_gte_10 do expect(get_installed_package_source_name).to be_empty end - it "does not unregister the package source if not already installed" do - subject.run_action(:unregister) - expect(subject).not_to be_updated_by_last_action + it "does not unregister the package source if not installed" do + # subject.run_action(:unregister) + # expect(subject).not_to be_updated_by_last_action + # expect(subject).to raise_error(RuntimeError) + expect{subject.run_action(:unregister)}.to raise_error(RuntimeError) end end end diff --git a/spec/unit/resource/powershell_package_source_spec.rb b/spec/unit/resource/powershell_package_source_spec.rb index 1032902a0f..bd05288689 100644 --- a/spec/unit/resource/powershell_package_source_spec.rb +++ b/spec/unit/resource/powershell_package_source_spec.rb @@ -33,14 +33,15 @@ describe Chef::Resource::PowershellPackageSource do expect(resource.action).to eql([:register]) end - it "supports :register and :unregister actions" do + it "supports :register, :set and :unregister actions" do expect { resource.action :register }.not_to raise_error + expect { resource.action :set }.not_to raise_error expect { resource.action :unregister }.not_to raise_error end it "the url property accepts strings" do - resource.url("https://mygallery.company.co/api/v2/") - expect(resource.url).to eql("https://mygallery.company.co/api/v2/") + resource.source_location("https://mygallery.company.co/api/v2/") + expect(resource.source_location).to eql("https://mygallery.company.co/api/v2/") end it "the trusted property accepts true and false" do @@ -54,7 +55,7 @@ describe Chef::Resource::PowershellPackageSource do expect(resource.trusted).to eql(false) end - it "provider_name accepts 'Programs', 'msi', 'NuGet', 'msu', 'PowerShellGet', 'psl', 'chocolatey'" do + it "provider_name accepts 'Programs', 'msi', 'NuGet', 'msu', 'PowerShellGet', 'psl', 'chocolatey', 'winget'" do expect { resource.provider_name("Programs") }.not_to raise_error expect { resource.provider_name("msi") }.not_to raise_error expect { resource.provider_name("NuGet") }.not_to raise_error @@ -62,6 +63,7 @@ describe Chef::Resource::PowershellPackageSource do expect { resource.provider_name("PowerShellGet") }.not_to raise_error expect { resource.provider_name("psl") }.not_to raise_error expect { resource.provider_name("chocolatey") }.not_to raise_error + expect { resource.provider_name("winget") }.not_to raise_error end it "the publish_location property accepts strings" do @@ -70,75 +72,76 @@ describe Chef::Resource::PowershellPackageSource do end it "the script_source_location property accepts strings" do - resource.publish_location("https://mygallery.company.co/api/v2/scripts") - expect(resource.publish_location).to eql("https://mygallery.company.co/api/v2/scripts") + resource.script_source_location("https://mygallery.company.co/api/v2/scripts") + expect(resource.script_source_location).to eql("https://mygallery.company.co/api/v2/scripts") end it "the script_publish_location property accepts strings" do - resource.publish_location("https://mygallery.company.co/api/v2/scripts") - expect(resource.publish_location).to eql("https://mygallery.company.co/api/v2/scripts") + resource.script_publish_location("https://mygallery.company.co/api/v2/scripts") + expect(resource.script_publish_location).to eql("https://mygallery.company.co/api/v2/scripts") end describe "#build_ps_repository_command" do before do resource.source_name("MyGallery") - resource.url("https://mygallery.company.co/api/v2/") + resource.source_location("https://github.com/chef/powershell_test") + resource.provider_name("PowerShellGet") end context "#register" do it "builds a minimal command" do - expect(provider.build_ps_repository_command("Register", resource)).to eql("Register-PSRepository -Name 'MyGallery' -SourceLocation 'https://mygallery.company.co/api/v2/' -InstallationPolicy 'Untrusted' | Out-Null") + expect(provider.build_ps_repository_command("Register", resource)).to eql("Register-PSRepository -Name 'MyGallery' -SourceLocation 'https://github.com/chef/powershell_test' -InstallationPolicy 'Untrusted' | Out-Null") end it "builds a command with trusted set to true" do resource.trusted(true) - expect(provider.build_ps_repository_command("Register", resource)).to eql("Register-PSRepository -Name 'MyGallery' -SourceLocation 'https://mygallery.company.co/api/v2/' -InstallationPolicy 'Trusted' | Out-Null") + expect(provider.build_ps_repository_command("Register", resource)).to eql("Register-PSRepository -Name 'MyGallery' -SourceLocation 'https://github.com/chef/powershell_test' -InstallationPolicy 'Trusted' | Out-Null") end it "builds a command with a publish location" do - resource.publish_location("https://mygallery.company.co/api/v2/package") - expect(provider.build_ps_repository_command("Register", resource)).to eql("Register-PSRepository -Name 'MyGallery' -SourceLocation 'https://mygallery.company.co/api/v2/' -InstallationPolicy 'Untrusted' -PublishLocation 'https://mygallery.company.co/api/v2/package' | Out-Null") + resource.publish_location("https://github.com/chef/powershell_test/package") + expect(provider.build_ps_repository_command("Register", resource)).to eql("Register-PSRepository -Name 'MyGallery' -SourceLocation 'https://github.com/chef/powershell_test' -InstallationPolicy 'Untrusted' -PublishLocation 'https://github.com/chef/powershell_test/package' | Out-Null") end it "builds a command with a script source location" do - resource.script_source_location("https://mygallery.company.co/api/v2/scripts") - expect(provider.build_ps_repository_command("Register", resource)).to eql("Register-PSRepository -Name 'MyGallery' -SourceLocation 'https://mygallery.company.co/api/v2/' -InstallationPolicy 'Untrusted' -ScriptSourceLocation 'https://mygallery.company.co/api/v2/scripts' | Out-Null") + resource.script_source_location("https://github.com/chef/powershell_test/scripts") + expect(provider.build_ps_repository_command("Register", resource)).to eql("Register-PSRepository -Name 'MyGallery' -SourceLocation 'https://github.com/chef/powershell_test' -InstallationPolicy 'Untrusted' -ScriptSourceLocation 'https://github.com/chef/powershell_test/scripts' | Out-Null") end it "builds a command with a script publish location" do - resource.script_publish_location("https://mygallery.company.co/api/v2/scripts/package") - expect(provider.build_ps_repository_command("Register", resource)).to eql("Register-PSRepository -Name 'MyGallery' -SourceLocation 'https://mygallery.company.co/api/v2/' -InstallationPolicy 'Untrusted' -ScriptPublishLocation 'https://mygallery.company.co/api/v2/scripts/package' | Out-Null") + resource.script_publish_location("https://github.com/chef/powershell_test/scripts/package") + expect(provider.build_ps_repository_command("Register", resource)).to eql("Register-PSRepository -Name 'MyGallery' -SourceLocation 'https://github.com/chef/powershell_test' -InstallationPolicy 'Untrusted' -ScriptPublishLocation 'https://github.com/chef/powershell_test/scripts/package' | Out-Null") end end context "#set" do it "builds a minimal command" do - expect(provider.build_ps_repository_command("Set", resource)).to eql("Set-PSRepository -Name 'MyGallery' -SourceLocation 'https://mygallery.company.co/api/v2/' -InstallationPolicy 'Untrusted' | Out-Null") + expect(provider.build_ps_repository_command("Set", resource)).to eql("Set-PSRepository -Name 'MyGallery' -SourceLocation 'https://github.com/chef/powershell_test' -InstallationPolicy 'Untrusted' | Out-Null") end it "builds a command to change the url" do - resource.url("https://othergallery.company.co/api/v2/") + resource.source_location("https://othergallery.company.co/api/v2/") expect(provider.build_ps_repository_command("Set", resource)).to eql("Set-PSRepository -Name 'MyGallery' -SourceLocation 'https://othergallery.company.co/api/v2/' -InstallationPolicy 'Untrusted' | Out-Null") end it "builds a command with trusted set to true" do resource.trusted(true) - expect(provider.build_ps_repository_command("Set", resource)).to eql("Set-PSRepository -Name 'MyGallery' -SourceLocation 'https://mygallery.company.co/api/v2/' -InstallationPolicy 'Trusted' | Out-Null") + expect(provider.build_ps_repository_command("Set", resource)).to eql("Set-PSRepository -Name 'MyGallery' -SourceLocation 'https://github.com/chef/powershell_test' -InstallationPolicy 'Trusted' | Out-Null") end it "builds a command with a publish location" do - resource.publish_location("https://mygallery.company.co/api/v2/package") - expect(provider.build_ps_repository_command("Set", resource)).to eql("Set-PSRepository -Name 'MyGallery' -SourceLocation 'https://mygallery.company.co/api/v2/' -InstallationPolicy 'Untrusted' -PublishLocation 'https://mygallery.company.co/api/v2/package' | Out-Null") + resource.publish_location("https://github.com/chef/powershell_test/package") + expect(provider.build_ps_repository_command("Set", resource)).to eql("Set-PSRepository -Name 'MyGallery' -SourceLocation 'https://github.com/chef/powershell_test' -InstallationPolicy 'Untrusted' -PublishLocation 'https://github.com/chef/powershell_test/package' | Out-Null") end it "builds a command with a script source location" do - resource.script_source_location("https://mygallery.company.co/api/v2/scripts") - expect(provider.build_ps_repository_command("Set", resource)).to eql("Set-PSRepository -Name 'MyGallery' -SourceLocation 'https://mygallery.company.co/api/v2/' -InstallationPolicy 'Untrusted' -ScriptSourceLocation 'https://mygallery.company.co/api/v2/scripts' | Out-Null") + resource.script_source_location("https://github.com/chef/powershell_test/scripts") + expect(provider.build_ps_repository_command("Set", resource)).to eql("Set-PSRepository -Name 'MyGallery' -SourceLocation 'https://github.com/chef/powershell_test' -InstallationPolicy 'Untrusted' -ScriptSourceLocation 'https://github.com/chef/powershell_test/scripts' | Out-Null") end it "builds a command with a script publish location" do - resource.script_publish_location("https://mygallery.company.co/api/v2/scripts/package") - expect(provider.build_ps_repository_command("Set", resource)).to eql("Set-PSRepository -Name 'MyGallery' -SourceLocation 'https://mygallery.company.co/api/v2/' -InstallationPolicy 'Untrusted' -ScriptPublishLocation 'https://mygallery.company.co/api/v2/scripts/package' | Out-Null") + resource.script_publish_location("https://github.com/chef/powershell_test/scripts/package") + expect(provider.build_ps_repository_command("Set", resource)).to eql("Set-PSRepository -Name 'MyGallery' -SourceLocation 'https://github.com/chef/powershell_test' -InstallationPolicy 'Untrusted' -ScriptPublishLocation 'https://github.com/chef/powershell_test/scripts/package' | Out-Null") end end end @@ -146,74 +149,72 @@ describe Chef::Resource::PowershellPackageSource do describe "#build_package_source_command" do before do resource.source_name("NuGet") - resource.url("http://nuget.org/api/v2/") + resource.source_location("http://nuget.org/api/v2/") end context "#register" do it "builds a minimal command" do - expect(provider.build_package_source_command("Register", resource)).to eql("Register-PackageSource -Name 'NuGet' -Location 'http://nuget.org/api/v2/' -Trusted:$false -ProviderName 'NuGet' | Out-Null") + expect(provider.build_package_source_command("Register", resource)).to eql("Register-PackageSource -Name 'NuGet' -Location 'http://nuget.org/api/v2/' -ProviderName 'NuGet' | Out-Null") end it "builds a command with trusted set to true" do resource.trusted(true) - expect(provider.build_package_source_command("Register", resource)).to eql("Register-PackageSource -Name 'NuGet' -Location 'http://nuget.org/api/v2/' -Trusted:$true -ProviderName 'NuGet' | Out-Null") + expect(provider.build_package_source_command("Register", resource)).to eql("Register-PackageSource -Name 'NuGet' -Location 'http://nuget.org/api/v2/' -Trusted -ProviderName 'NuGet' | Out-Null") end it "builds a command with a different provider" do resource.source_name("choco") - resource.url("https://chocolatey.org/api/v2/") + resource.source_location("https://chocolatey.org/api/v2/") resource.provider_name("chocolatey") - expect(provider.build_package_source_command("Register", resource)).to eql("Register-PackageSource -Name 'choco' -Location 'https://chocolatey.org/api/v2/' -Trusted:$false -ProviderName 'chocolatey' | Out-Null") + expect(provider.build_package_source_command("Register", resource)).to eql("Register-PackageSource -Name 'choco' -Location 'https://chocolatey.org/api/v2/' -ProviderName 'chocolatey' | Out-Null") end end context "#set" do it "builds a minimal command" do - expect(provider.build_package_source_command("Set", resource)).to eql("Set-PackageSource -Name 'NuGet' -Location 'http://nuget.org/api/v2/' -Trusted:$false -ProviderName 'NuGet' | Out-Null") + expect(provider.build_package_source_command("Set", resource)).to eql("Set-PackageSource -Name 'NuGet' -Location 'http://nuget.org/api/v2/' -ProviderName 'NuGet' | Out-Null") end it "builds a command to change the url" do - resource.url("https://nuget.company.co/api/v2/") - expect(provider.build_package_source_command("Set", resource)).to eql("Set-PackageSource -Name 'NuGet' -Location 'https://nuget.company.co/api/v2/' -Trusted:$false -ProviderName 'NuGet' | Out-Null") + resource.source_location("https://nuget.company.co/api/v2/") + expect(provider.build_package_source_command("Set", resource)).to eql("Set-PackageSource -Name 'NuGet' -Location 'https://nuget.company.co/api/v2/' -ProviderName 'NuGet' | Out-Null") end it "builds a command with trusted set to true" do resource.trusted(true) - expect(provider.build_package_source_command("Set", resource)).to eql("Set-PackageSource -Name 'NuGet' -Location 'http://nuget.org/api/v2/' -Trusted:$true -ProviderName 'NuGet' | Out-Null") + expect(provider.build_package_source_command("Set", resource)).to eql("Set-PackageSource -Name 'NuGet' -Location 'http://nuget.org/api/v2/' -Trusted -ProviderName 'NuGet' | Out-Null") end it "builds a command with a different provider" do resource.source_name("choco") - resource.url("https://chocolatey.org/api/v2/") + resource.source_location("https://chocolatey.org/api/v2/") resource.provider_name("chocolatey") - expect(provider.build_package_source_command("Set", resource)).to eql("Set-PackageSource -Name 'choco' -Location 'https://chocolatey.org/api/v2/' -Trusted:$false -ProviderName 'chocolatey' | Out-Null") + expect(provider.build_package_source_command("Set", resource)).to eql("Set-PackageSource -Name 'choco' -Location 'https://chocolatey.org/api/v2/' -ProviderName 'chocolatey' | Out-Null") end end end - describe "#psrepository_cmdlet_appropriate?" do - it "returns true if the provider_name is 'PowerShellGet'" do - resource.provider_name("PowerShellGet") - expect(provider.psrepository_cmdlet_appropriate?).to eql(true) - end - - it "returns false if the provider_name is something else" do - resource.provider_name("NuGet") - expect(provider.psrepository_cmdlet_appropriate?).to eql(false) - end - end - - describe "#package_source_exists?" do - it "returns true if it exists" do - allow(provider).to receive(:powershell_exec!).with("(Get-PackageSource -Name 'MyGallery' -ErrorAction SilentlyContinue).Name").and_return(double("powershell_exec!", result: "MyGallery\r\n")) - resource.source_name("MyGallery") - expect(provider.package_source_exists?).to eql(true) - end - - it "returns false if it doesn't exist" do - allow(provider).to receive(:powershell_exec!).with("(Get-PackageSource -Name 'MyGallery' -ErrorAction SilentlyContinue).Name").and_return(double("powershell_exec!", result: "")) - resource.source_name("MyGallery") - expect(provider.package_source_exists?).to eql(false) - end - end + # describe "get_package_source_details" do + # before do + # resource.source_name("MyGallery") + # resource.source_location("http://nuget.org/api/v2/") + # provider.build_package_source_command("Register", resource) + # end + + # # stub a call to the package_source_details + # expect(provider).to receive(:get_package_source_details).and_return("PackageSource") + # it "returns packagesource if it exists" do + # # dbl = double("testing PackageSource") + # # let(source_name) + # # # allow(provider).to receive(:powershell_exec!).with("(Get-PackageSource -Name 'MyGallery' -ErrorAction SilentlyContinue).Name").and_return(double("powershell_exec!", result: "PackageSource")) + # # resource.source_name("MyGallery") + # expect(provider.get_package_source_details.result).to eql("PackageSource") + # end + + # it "returns unregistered if it doesn't exist" do + # # allow(provider).to receive(:powershell_exec!).with("(Get-PackageSource -Name 'Foo' -ErrorAction SilentlyContinue).Name").and_return(double("powershell_exec!", result: "")) + # resource.source_name("Foo") + # expect(provider.get_package_source_details.result).to eql("Unregistered") + # end + # end end |