Merge pull request #62 from chef/oneget

Merged change 6ec36404-484f-4856-83cf-785c960634bc

From review branch oneget into master

Signed-off-by: drichter <drichter@chef.io>
This commit is contained in:
chef-delivery 2015-10-03 06:30:56 -07:00
commit 0753cc5022
5 changed files with 59 additions and 5 deletions

View file

@ -1,4 +1,5 @@
# encoding: utf-8
# author: Christoph Hartmann
# This resource talks with OneGet (https://github.com/OneGet/oneget)
# Its part of Windows Management Framework 5.0 and part of Windows 10
@ -12,6 +13,9 @@ class OneGetPackage < Vulcano.resource(1)
def initialize(package_name)
@package_name = package_name
# verify that this resource is only supported on Windows
return skip_resource 'The `oneget` resource is not supported on your OS.' if vulcano.os[:family] != 'windows'
end
def info
@ -19,6 +23,7 @@ class OneGetPackage < Vulcano.resource(1)
@info = {}
@info[:type] = 'oneget'
@info[:installed] = false
cmd = vulcano.run_command("Get-Package -Name '#{@package_name}' | ConvertTo-Json")
# cannot rely on exit code for now, successful command returns exit code 1
@ -27,13 +32,19 @@ class OneGetPackage < Vulcano.resource(1)
begin
pkgs = JSON.parse(cmd.stdout)
@info[:installed] = true
# sometimes we get multiple values
if pkgs.is_a?(Array)
# select the first entry
pkgs = pkgs.first
end
rescue JSON::ParserError => _e
return @info
end
@info[:name] = pkgs['Name']
@info[:version] = pkgs['Version']
@info[:installed] = true
@info[:name] = pkgs['Name'] if pkgs.key?('Name')
@info[:version] = pkgs['Version'] if pkgs.key?('Version')
@info
end

View file

@ -94,7 +94,8 @@ class MockLoader
'gem list --local -a -q ^rubocop$' => cmd.call('gem-list-local-a-q-rubocop'),
'npm ls -g --json bower' => cmd.call('npm-ls-g--json-bower'),
'pip show jinja2' => cmd.call('pip-show-jinja2'),
"Get-Package -Name 'Mozilla Firefox' | ConvertTo-Json" => cmd.call('get-package'),
"Get-Package -Name 'Mozilla Firefox' | ConvertTo-Json" => cmd.call('get-package-firefox'),
"Get-Package -Name 'Ruby 2.1.6-p336-x64' | ConvertTo-Json" => cmd.call('get-package-ruby'),
"New-Object -Type PSObject | Add-Member -MemberType NoteProperty -Name Service -Value (Get-Service -Name dhcp| Select-Object -Property Name, DisplayName, Status) -PassThru | Add-Member -MemberType NoteProperty -Name WMI -Value (Get-WmiObject -Class Win32_Service | Where-Object {$_.Name -eq 'dhcp' -or $_.DisplayName -eq 'dhcp'} | Select-Object -Property StartMode) -PassThru | ConvertTo-Json" => cmd.call('get-service-dhcp'),
"Get-WindowsFeature | Where-Object {$_.Name -eq 'dhcp' -or $_.DisplayName -eq 'dhcp'} | Select-Object -Property Name,DisplayName,Description,Installed,InstallState | ConvertTo-Json" => cmd.call('get-windows-feature'),
'lsmod' => cmd.call('lsmod'),

View file

@ -0,0 +1,18 @@
[
{
"ProviderName": "Programs",
"Status": "Installed",
"SearchKey": "Ruby 2.1.6-p336-x64",
"CanonicalId": "programs:Ruby 2.1.6-p336-x64/2.1.6-p336",
"Name": "Ruby 2.1.6-p336-x64",
"Version": "2.1.6-p336"
},
{
"ProviderName": "Programs",
"Status": "Installed",
"SearchKey": "Ruby 2.1.6-p336-x64",
"CanonicalId": "programs:Ruby 2.1.6-p336-x64/2.1.6-p336",
"Name": "Ruby 2.1.6-p336-x64",
"Version": "2.1.6-p336"
}
]

View file

@ -4,9 +4,9 @@ require 'helper'
require 'vulcano/resource'
describe 'Vulcano::Resources::Passwd' do
let(:resource) { load_resource('oneget', 'Mozilla Firefox') }
it 'verify oneget package detail parsing' do
resource = MockLoader.new(:windows).load_resource('oneget', 'Mozilla Firefox')
pkg = {
name: 'Mozilla Firefox 40.0.3 (x86 en-US)',
version: '40.0.3',
@ -14,6 +14,30 @@ describe 'Vulcano::Resources::Passwd' do
installed: true,
}
_(resource.installed?).must_equal true
_(resource.version).must_equal '40.0.3'
_(resource.info).must_equal pkg
end
# catch cases where we recieve multiple return values
# eg. happens if you do 'choco install ruby'
it 'filter dups of Get-Package' do
resource = MockLoader.new(:windows).load_resource('oneget', 'Ruby 2.1.6-p336-x64')
pkg = {
name: 'Ruby 2.1.6-p336-x64',
version: '2.1.6-p336',
type: 'oneget',
installed: true,
}
_(resource.installed?).must_equal true
_(resource.version).must_equal '2.1.6-p336'
_(resource.info).must_equal pkg
end
it 'should skip on linux' do
resource = MockLoader.new(:centos7).load_resource('oneget', 'Not available')
pkg = { type: 'oneget', installed: false }
_(resource.installed?).must_equal false
_(resource.version).must_equal nil
_(resource.info).must_equal pkg
end
end