Skip to content

Commit

Permalink
test __env__ cache
Browse files Browse the repository at this point in the history
  • Loading branch information
cmcmarrow committed Sep 1, 2023
1 parent e9e8832 commit 6d76add
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 10 deletions.
75 changes: 65 additions & 10 deletions tests/pytests/functional/utils/test_gifts.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from salt.fileserver.gitfs import PER_REMOTE_ONLY, PER_REMOTE_OVERRIDES
from salt.utils.gitfs import GitFS
from salt.utils.gitfs import GitFS, GitPython, Pygit2
from salt.utils.immutabletypes import ImmutableDict, ImmutableList

pytestmark = [
Expand Down Expand Up @@ -45,14 +45,16 @@ def gitfs_opts(salt_factories, tmp_path):


@pytest.fixture
def gitpython_gifts_opts(gitfs_opts):
gitfs_opts["verified_gifts_provider"] = "gitpython"
def gitpython_gitfs_opts(gitfs_opts):
gitfs_opts["verified_gitfs_provider"] = "gitpython"
GitFS.instance_map.clear()
return gitfs_opts


@pytest.fixture
def pygit2_gifts_opts(gitfs_opts):
gitfs_opts["verified_gifts_provider"] = "pygit2"
def pygit2_gitfs_opts(gitfs_opts):
gitfs_opts["verified_gitfs_provider"] = "pygit2"
GitFS.instance_map.clear()
return gitfs_opts


Expand All @@ -65,14 +67,67 @@ def _test_gitfs_simple(gitfs_opts):
)
g.fetch_remotes()
assert len(g.remotes) == 1
assert g.file_list({"saltenv": "main"}) == [".gitignore", "README.md"]
assert set(g.file_list({"saltenv": "main"})) == {".gitignore", "README.md"}


@skipif_no_gitpython
def test_gitpython_gitfs_simple(gitpython_gifts_opts):
_test_gitfs_simple(gitpython_gifts_opts)
def test_gitpython_gitfs_simple(gitpython_gitfs_opts):
_test_gitfs_simple(gitpython_gitfs_opts)


@skipif_no_pygit2
def test_pygit2_gitfs_simple(pygit2_gifts_opts):
_test_gitfs_simple(pygit2_gifts_opts)
def test_pygit2_gitfs_simple(pygit2_gitfs_opts):
_test_gitfs_simple(pygit2_gitfs_opts)


def _test_gitfs_simple_base(gitfs_opts):
g = GitFS(
gitfs_opts,
["https://github.com/saltstack/salt-test-pillar-gitfs.git"],
per_remote_overrides=PER_REMOTE_OVERRIDES,
per_remote_only=PER_REMOTE_ONLY,
)
g.fetch_remotes()
assert len(g.remotes) == 1
assert set(g.file_list({"saltenv": "base"})) == {
".gitignore",
"README.md",
"file.sls",
"top.sls",
}


@skipif_no_gitpython
def test_gitpython_gitfs_simple_base(gitpython_gitfs_opts):
_test_gitfs_simple_base(gitpython_gitfs_opts)


@skipif_no_pygit2
def test_pygit2_gitfs_simple_base(pygit2_gitfs_opts):
_test_gitfs_simple_base(pygit2_gitfs_opts)


@skipif_no_gitpython
def test_gitpython_gitfs_provider(gitpython_gitfs_opts):
g = GitFS(
gitpython_gitfs_opts,
["https://github.com/saltstack/salt-test-pillar-gitfs.git"],
per_remote_overrides=PER_REMOTE_OVERRIDES,
per_remote_only=PER_REMOTE_ONLY,
)
assert len(g.remotes) == 1
assert g.provider == "gitpython"
assert isinstance(g.remotes[0], GitPython)


@skipif_no_pygit2
def test_pygit2_gitfs_provider(pygit2_gitfs_opts):
g = GitFS(
pygit2_gitfs_opts,
["https://github.com/saltstack/salt-test-pillar-gitfs.git"],
per_remote_overrides=PER_REMOTE_OVERRIDES,
per_remote_only=PER_REMOTE_ONLY,
)
assert len(g.remotes) == 1
assert g.provider == "pygit2"
assert isinstance(g.remotes[0], Pygit2)
126 changes: 126 additions & 0 deletions tests/pytests/functional/utils/test_pillar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import os

import pytest

from salt.pillar.git_pillar import GLOBAL_ONLY, PER_REMOTE_ONLY, PER_REMOTE_OVERRIDES
from salt.utils.gitfs import GitPillar, GitPython, Pygit2
from salt.utils.immutabletypes import ImmutableDict, ImmutableList

pytestmark = [
pytest.mark.slow_test,
]


try:
import git # pylint: disable=unused-import

HAS_GITPYTHON = True
except ImportError:
HAS_GITPYTHON = False


try:
import pygit2 # pylint: disable=unused-import

HAS_PYGIT2 = True
except ImportError:
HAS_PYGIT2 = False


skipif_no_gitpython = pytest.mark.skipif(not HAS_GITPYTHON, reason="Missing gitpython")
skipif_no_pygit2 = pytest.mark.skipif(not HAS_PYGIT2, reason="Missing pygit2")


@pytest.fixture
def pillar_opts(salt_factories, tmp_path):
config_defaults = {"cachedir": str(tmp_path)}
factory = salt_factories.salt_master_daemon(
"pillar-functional-master", defaults=config_defaults
)
config_defaults = dict(factory.config)
for key, item in config_defaults.items():
if isinstance(item, ImmutableDict):
config_defaults[key] = dict(item)
elif isinstance(item, ImmutableList):
config_defaults[key] = list(item)
return config_defaults


@pytest.fixture
def gitpython_pillar_opts(pillar_opts):
pillar_opts["verified_git_pillar_provider"] = "gitpython"
return pillar_opts


@pytest.fixture
def pygit2_pillar_opts(pillar_opts):
pillar_opts["verified_git_pillar_provider"] = "pygit2"
return pillar_opts


def _get_pillar(opts, *remotes):
return GitPillar(
opts,
remotes,
per_remote_overrides=PER_REMOTE_OVERRIDES,
per_remote_only=PER_REMOTE_ONLY,
global_only=GLOBAL_ONLY,
)


@skipif_no_gitpython
def test_gitpython_pillar_provider(gitpython_pillar_opts):
p = _get_pillar(
gitpython_pillar_opts, "https://github.com/saltstack/salt-test-pillar-gitfs.git"
)
assert len(p.remotes) == 1
assert p.provider == "gitpython"
assert isinstance(p.remotes[0], GitPython)


@skipif_no_pygit2
def test_pygit2_pillar_provider(pygit2_pillar_opts):
p = _get_pillar(
pygit2_pillar_opts, "https://github.com/saltstack/salt-test-pillar-gitfs.git"
)
assert len(p.remotes) == 1
assert p.provider == "pygit2"
assert isinstance(p.remotes[0], Pygit2)


def _test_env(opts):
p = _get_pillar(
opts, "__env__ https://github.com/saltstack/salt-test-pillar-gitfs.git"
)
assert len(p.remotes) == 1
p.checkout()
repo = p.remotes[0]
files = set(os.listdir(repo.get_cachedir()))
for f in (".gitignore", "README.md", "file.sls", "top.sls"):
assert f in files
opts["pillarenv"] = "main"
p2 = _get_pillar(
opts, "__env__ https://github.com/saltstack/salt-test-pillar-gitfs.git"
)
assert len(p.remotes) == 1
p2.checkout()
repo2 = p2.remotes[0]
files = set(os.listdir(repo2.get_cachedir()))
for f in (".gitignore", "README.md"):
assert f in files
for f in ("file.sls", "top.sls", "back.sls", "rooms.sls"):
assert f not in files
assert repo.get_cachedir() != repo2.get_cachedir()
files = set(os.listdir(repo.get_cachedir()))
for f in (".gitignore", "README.md", "file.sls", "top.sls"):
assert f in files


@skipif_no_gitpython
def test_gitpython_env(gitpython_pillar_opts):
_test_env(gitpython_pillar_opts)


@skipif_no_pygit2
def test_pygit2_env(pygit2_pillar_opts):
_test_env(pygit2_pillar_opts)

0 comments on commit 6d76add

Please sign in to comment.