Merge pull request #36190 from eradman/openbsdpkg

pkg.install: enable OpenBSD branch and flavor specifications
This commit is contained in:
Mike Place 2016-09-10 09:06:43 +09:00 committed by GitHub
commit 81fb34ecd0
3 changed files with 148 additions and 16 deletions

View file

@ -2,11 +2,24 @@
'''
Package support for OpenBSD
.. important::
If you feel that Salt should be using this module to manage packages on a
minion, and it is using a different module (or gives an error similar to
*'pkg.install' is not available*), see :ref:`here
<module-provider-override>`.
.. note::
The package repository is configured on each host using ``/etc/pkg.conf``
.. versionchanged:: 2016.3.5
Package versions on OpenBSD are not normally specified explicitly; instead
packages may be available in multiple *flavors*, and *branches* which are
specified by the format of the package name. This module allows you to use
the same formatting as ``pkg_add(1)``, and will select the empty flavor and
default branch by default. Examples:
.. code-block:: yaml
- rsync
- vim--no_x11
- ruby%2.3
'''
from __future__ import absolute_import
@ -21,14 +34,13 @@ from salt.exceptions import CommandExecutionError, MinionError
log = logging.getLogger(__name__)
# FIXME: replace guesswork with `pkg_info -z` to correctly identify package
# flavors and branches
__PKG_RE = re.compile('^((?:[^-]+|-(?![0-9]))+)-([0-9][^-]*)(?:-(.*))?$')
# Define the module's virtual name
__virtualname__ = 'pkg'
# XXX need a way of setting PKG_PATH instead of inheriting from the environment
def __virtual__():
'''
@ -102,8 +114,7 @@ def latest_version(*names, **kwargs):
for name in names:
ret[name] = ''
stems = [x.split('--')[0] for x in names]
cmd = 'pkg_info -q -I {0}'.format(' '.join(stems))
cmd = 'pkg_info -q -I {0}'.format(' '.join(names))
out = __salt__['cmd.run_stdout'](cmd, python_shell=False, output_loglevel='trace')
for line in out.splitlines():
try:
@ -122,9 +133,6 @@ def latest_version(*names, **kwargs):
return ret[names[0]]
return ret
# available_version is being deprecated
available_version = salt.utils.alias_function(latest_version, 'available_version')
def version(*names, **kwargs):
'''
@ -163,7 +171,8 @@ def install(name=None, pkgs=None, sources=None, **kwargs):
salt '*' pkg.install pkgs='["<package name>", "<package name>"]'
CLI Example, Install more than one package from a alternate source (e.g. salt file-server, HTTP, FTP, local filesystem):
CLI Example, Install more than one package from a alternate source (e.g.
salt file-server, HTTP, FTP, local filesystem):
.. code-block:: bash
@ -182,9 +191,12 @@ def install(name=None, pkgs=None, sources=None, **kwargs):
old = list_pkgs()
errors = []
for pkg in pkg_params:
# A special case for OpenBSD package "branches" is also required in
# salt/states/pkg.py
if pkg_type == 'repository':
stem, flavor = (pkg.split('--') + [''])[:2]
pkg = '--'.join((stem, flavor))
stem, branch = (pkg.split('%') + [''])[:2]
base, flavor = (stem.split('--') + [''])[:2]
pkg = '{0}--{1}%{2}'.format(base, flavor, branch)
cmd = 'pkg_add -x -I {0}'.format(pkg)
out = __salt__['cmd.run_all'](
cmd,

View file

@ -580,6 +580,8 @@ def _verify_install(desired, new_pkgs, ignore_epoch=False):
if __grains__['os'] == 'FreeBSD' and origin:
cver = [k for k, v in six.iteritems(new_pkgs) if v['origin'] == pkgname]
elif __grains__['os'] == 'OpenBSD':
cver = new_pkgs.get(pkgname.split('%')[0])
elif __grains__['os_family'] == 'Debian':
cver = new_pkgs.get(pkgname.split('=')[0])
else:

View file

@ -0,0 +1,118 @@
# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Eric Radman <ericshane@eradman.com>`
'''
# Import Python Libs
from __future__ import absolute_import
# Import Salt Testing Libs
from salttesting import TestCase, skipIf
from salttesting.mock import (
MagicMock,
patch,
call,
NO_MOCK,
NO_MOCK_REASON
)
# Import Salt Libs
from salt.modules import openbsdpkg
# Globals
openbsdpkg.__grains__ = dict()
openbsdpkg.__salt__ = dict()
openbsdpkg.__context__ = dict()
@skipIf(NO_MOCK, NO_MOCK_REASON)
class OpenbsdpkgTestCase(TestCase):
'''
Test cases for salt.modules.openbsdpkg
'''
def test_list_pkgs(self):
'''
Test for listing installed packages.
'''
def _add_data(data, key, value):
data[key] = value
pkg_info_out = [
'png-1.6.23',
'vim-7.4.1467p1-gtk2', # vim--gtk2
'ruby-2.3.1p1' # ruby%2.3
]
run_stdout_mock = MagicMock(return_value='\n'.join(pkg_info_out))
patches = {
'cmd.run_stdout': run_stdout_mock,
'pkg_resource.add_pkg': _add_data,
'pkg_resource.sort_pkglist': MagicMock(),
'pkg_resource.stringify': MagicMock(),
}
with patch.dict(openbsdpkg.__salt__, patches):
pkgs = openbsdpkg.list_pkgs()
self.assertDictEqual(pkgs, {
'png': '1.6.23',
'vim--gtk2': '7.4.1467p1',
'ruby': '2.3.1p1'})
run_stdout_mock.assert_called_once_with('pkg_info -q -a',
output_loglevel='trace')
def test_install_pkgs(self):
'''
Test package install behavior for the following conditions:
- only base package name is given ('png')
- a flavor is specified ('vim--gtk2')
- a branch is specified ('ruby%2.3')
'''
class ListPackages(object):
def __init__(self):
self._iteration = 0
def __call__(self):
pkg_lists = [
{'vim': '7.4.1467p1-gtk2'},
{'png': '1.6.23', 'vim': '7.4.1467p1-gtk2', 'ruby': '2.3.1p1'}
]
pkgs = pkg_lists[self._iteration]
self._iteration += 1
return pkgs
parsed_targets = (
{'vim--gtk2': None, 'png': None, 'ruby%2.3': None},
"repository"
)
cmd_out = {
'retcode': 0,
'stdout': 'quirks-2.241 signed on 2016-07-26T16:56:10Z',
'stderr': ''
}
run_all_mock = MagicMock(return_value=cmd_out)
patches = {
'cmd.run_all': run_all_mock,
'pkg_resource.parse_targets': MagicMock(return_value=parsed_targets),
'pkg_resource.stringify': MagicMock(),
'pkg_resource.sort_pkglist': MagicMock(),
}
with patch.dict(openbsdpkg.__salt__, patches):
with patch('salt.modules.openbsdpkg.list_pkgs', ListPackages()):
added = openbsdpkg.install()
expected = {
'png': {'new': '1.6.23', 'old': ''},
'ruby': {'new': '2.3.1p1', 'old': ''}
}
self.assertDictEqual(added, expected)
expected_calls = [
call('pkg_add -x -I png--%', output_loglevel='trace', python_shell=False),
call('pkg_add -x -I ruby--%2.3', output_loglevel='trace', python_shell=False),
call('pkg_add -x -I vim--gtk2%', output_loglevel='trace', python_shell=False),
]
run_all_mock.assert_has_calls(expected_calls, any_order=True)
self.assertEqual(run_all_mock.call_count, 3)
if __name__ == '__main__':
from integration import run_tests
run_tests(OpenbsdpkgTestCase, needs_daemon=False)