Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port #53622 to master (runners.git_pillar: Also match the repo parameter against the repo name) #56605

Merged
merged 1 commit into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion salt/runners/git_pillar.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def update(branch=None, repo=None):
changes were fetched. ``False`` now is reserved only for instances in
which there were errors.

.. versionchanged:: Sodium
The repo parameter also matches against the repo name.

Fetch one or all configured git_pillar remotes.

.. note::
Expand All @@ -55,6 +58,8 @@ def update(branch=None, repo=None):

# Update specific branch and repo
salt-run git_pillar.update branch='branch' repo='https://foo.com/bar.git'
# Update specific repo, by name
salt-run git_pillar.update repo=myrepo
# Update all repos
salt-run git_pillar.update
# Run with debug logging
Expand All @@ -79,7 +84,7 @@ def update(branch=None, repo=None):
if branch != remote.branch:
continue
if repo is not None:
if repo != remote.url:
if repo != remote.url and repo != getattr(remote, "name", None):
continue
try:
result = remote.fetch()
Expand Down
93 changes: 93 additions & 0 deletions tests/unit/runners/test_git_pillar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# -*- coding: utf-8 -*-
"""
unit tests for the git_pillar runner
"""

# Import Python Libs
from __future__ import absolute_import, print_function, unicode_literals

import copy
import errno
import logging
import tempfile

# Import Salt Libs
import salt.runners.git_pillar as git_pillar
import salt.utils.files
import salt.utils.gitfs

# Import Salt Testing Libs
from tests.support.gitfs import _OPTS
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.mock import patch
from tests.support.runtests import RUNTIME_VARS
from tests.support.unit import TestCase

log = logging.getLogger(__name__)


class GitPillarTest(TestCase, LoaderModuleMockMixin):
"""
Validate the git_pillar runner
"""

@classmethod
def setUpClass(cls):
cls.tmp_cachedir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)

@classmethod
def tearDownClass(cls):
try:
salt.utils.files.rm_rf(cls.tmp_cachedir)
except OSError as exc:
if exc.errno == errno.EACCES:
log.error("Access error removing file %s", cls.tmp_cachedir)
elif exc.errno != errno.EEXIST:
raise

def setup_loader_modules(self):
opts = copy.copy(_OPTS)
opts["cachedir"] = self.tmp_cachedir
opts["verified_git_pillar_provider"] = "gitfoo"
opts["ext_pillar"] = [
{
"git": [
"master https://someurl/some",
{"dev https://otherurl/other": [{"name": "somename"}]},
]
}
]
return {git_pillar: {"__opts__": opts}}

def test_update(self):
"""
test git_pillar.update
"""

class MockGitProvider(
salt.utils.gitfs.GitProvider
): # pylint: disable=abstract-method
def init_remote(self):
new = False
self.repo = True
return new

def fetch(self):
return True

def clear_lock(self, lock_type="update"):
pass # return success, failed

git_providers = {"gitfoo": MockGitProvider}

repo1 = {"master https://someurl/some": True}
repo2 = {"dev https://otherurl/other": True}
all_repos = {
"master https://someurl/some": True,
"dev https://otherurl/other": True,
}
with patch.object(salt.utils.gitfs, "GIT_PROVIDERS", git_providers):
self.assertEqual(git_pillar.update(), all_repos)
self.assertEqual(git_pillar.update(branch="master"), repo1)
self.assertEqual(git_pillar.update(branch="dev"), repo2)
self.assertEqual(git_pillar.update(repo="somename"), repo2)