saltfactories.utils.tempfiles¶
Temporary files utilities
- saltfactories.utils.tempfiles.temp_directory(name=None, basepath=None)[source]¶
This helper creates a temporary directory. It should be used as a context manager which returns the temporary directory path, and, once out of context, deletes it.
- Parameters
name (basepath) – The name of the directory to create
name – The base path of where to create the directory. Defaults to
gettempdir()
- Return type
Can be directly imported and used:
from saltfactories.utils.tempfiles import temp_directory def test_func(): with temp_directory() as temp_path: assert temp_path.is_dir() assert not temp_path.is_dir() is False
Or, it can be used as a pytest helper function:
import pytest def test_blah(): with pytest.helpers.temp_directory() as temp_path: assert temp_path.is_dir() assert not temp_path.is_dir() is False
- saltfactories.utils.tempfiles.temp_file(name=None, contents=None, directory=None, strip_first_newline=True)[source]¶
This helper creates a temporary file. It should be used as a context manager which returns the temporary file path, and, once out of context, deletes it.
- Parameters
name (str) – The temporary file name
contents (str) – The contents of the temporary file
directory (str,pathlib.Path) – The directory where to create the temporary file. Defaults to the value of
gettempdir()
strip_first_newline (bool) – Either strip the initial first new line char or not.
- Return type
Can be directly imported and used:
from saltfactories.utils.tempfiles import temp_file def test_func(): with temp_file(name="blah.txt") as temp_path: assert temp_path.is_file() assert not temp_path.is_file() is False
Or, it can be used as a pytest helper function:
import pytest def test_blah(): with pytest.helpers.temp_file("blah.txt") as temp_path: assert temp_path.is_file() assert not temp_path.is_file() is False
- class saltfactories.utils.tempfiles.SaltEnv(*, name, paths=NOTHING)[source]¶
Bases:
object
This helper class represent a Salt Environment, either for states or pillar. It’s base purpose it to handle temporary file creation/deletion during testing.
- Parameters
name (str) – The salt environment name, commonly, ‘base’ or ‘prod’
paths (list) –
The salt environment list of paths.
Note
The first entry in this list, is the path that will get used to create temporary files in, ie, the return value of the
saltfactories.utils.tempfiles.SaltEnv.write_path
attribute.
- Return type
None
- property write_path¶
The path where temporary files are created
- temp_file(name, contents=None, strip_first_newline=True)[source]¶
Create a temporary file within this saltenv.
Please check
saltfactories.utils.tempfiles.temp_file()
for documentation.
- class saltfactories.utils.tempfiles.SaltEnvs(*, envs)[source]¶
Bases:
object
This class serves as a container for multiple salt environments for states or pillar.
- Parameters
envs (dict) – The envs dictionary should be a mapping of a string as key, the saltenv, commonly ‘base’ or ‘prod’, and the value an instance of
SaltEnv
or a list of strings(paths). In the case where a list of strings(paths) is passed, it is converted to an instance ofSaltEnv
- Return type
None
To provide a better user experience, the salt environments can be accessed as attributes of this class.
envs = SaltEnvs( { "base": [ "/path/to/base/env", ], "prod": [ "/path/to/prod/env", ], } ) with envs.base.temp_file("foo.txt", "foo contents") as base_foo_path: ... with envs.prod.temp_file("foo.txt", "foo contents") as prod_foo_path: ...
- class saltfactories.utils.tempfiles.SaltStateTree(*, envs)[source]¶
Bases:
saltfactories.utils.tempfiles.SaltEnvs
Helper class which handles temporary file creation within the state tree.
- Parameters
envs (dict) –
A mapping of a
saltenv
to a list of paths.envs = { "base": [ "/path/to/base/env", "/another/path/to/base/env", ], "prod": [ "/path/to/prod/env", "/another/path/to/prod/env", ], }
- Return type
None
The state tree environments can be accessed by attribute:
# See example of envs definition above state_tree = SaltStateTree(envs=envs) # To access the base saltenv base = state_tree.envs["base"] # Alternatively, in a simpler form base = state_tree.base
When setting up the Salt configuration to use an instance of
SaltStateTree
, the following pseudo code can be followed.# Using the state_tree defined above: salt_config = { # ... other salt config entries ... "file_roots": state_tree.as_dict() # ... other salt config entries ... }
Attention
The temporary files created by the
temp_file()
are written to the first path passed when instantiating theSaltStateTree
, ie, the return value of thesaltfactories.utils.tempfiles.SaltStateTree.write_path
attribute.# Given the example mapping shown above ... with state_tree.base.temp_file("foo.sls") as path: assert str(path) == "/path/to/base/env/foo.sls"
- as_dict()¶
Returns a dictionary of the right types to update the salt configuration :return dict:
- class saltfactories.utils.tempfiles.SaltPillarTree(*, envs)[source]¶
Bases:
saltfactories.utils.tempfiles.SaltEnvs
Helper class which handles temporary file creation within the pillar tree.
- Parameters
envs (dict) –
A mapping of a
saltenv
to a list of paths.envs = { "base": [ "/path/to/base/env", "/another/path/to/base/env", ], "prod": [ "/path/to/prod/env", "/another/path/to/prod/env", ], }
- Return type
None
The pillar tree environments can be accessed by attribute:
# See example of envs definition above pillar_tree = SaltPillarTree(envs=envs) # To access the base saltenv base = pillar_tree.envs["base"] # Alternatively, in a simpler form base = pillar_tree.base
When setting up the Salt configuration to use an instance of
SaltPillarTree
, the following pseudo code can be followed.# Using the pillar_tree defined above: salt_config = { # ... other salt config entries ... "pillar_roots": pillar_tree.as_dict() # ... other salt config entries ... }
Attention
The temporary files created by the
temp_file()
are written to the first path passed when instantiating theSaltPillarTree
, ie, the return value of thesaltfactories.utils.tempfiles.SaltPillarTree.write_path
attribute.# Given the example mapping shown above ... with state_tree.base.temp_file("foo.sls") as path: assert str(path) == "/path/to/base/env/foo.sls"
- as_dict()¶
Returns a dictionary of the right types to update the salt configuration :return dict: