Skip to content

Commit

Permalink
Merge pull request saltstack#42 from saltstack/RDTISALT-3200-tests
Browse files Browse the repository at this point in the history
RDTISALT-3200 write tests for mt pt 2
  • Loading branch information
mattp- authored and GitHub Enterprise committed Sep 10, 2019
2 parents 097d93a + 1d4db20 commit fe8f74f
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 56 deletions.
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This is only for bloomberg, we can make Jenkins run the unit tests inside of a container and remove this later
version: '2.4'
services:
salt-proper-tests:
image: "artprod.dev.bloomberg.com/bb-inf/salt-minion:2018.3.3"
volumes:
- .:/srv/sysca-salt
- ./tests/bbcpu/:/bb/bin/
3 changes: 2 additions & 1 deletion requirements/dev_bloomberg.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ pytest>=3.5.0
git+https://e904693b4b9f451885e6a05696ab0a60c3be959c:@bbgithub.dev.bloomberg.com/saltstack/pytest-salt.git@master#egg=pytest-salt
testinfra>=1.7.0
bloomberg.hostinfo

# httpretty Needs to be here for now even though it's a dependency of boto.
# A pip install on a fresh system will decide to target httpretty 0.8.10 to
# satisfy other requirements, and httpretty 0.8.10 has bugs in setup.py that
# prevent it from being successfully installed (at least on Python 3.4).
httpretty; python_version >= '3.4'
pylint==1.6.5
pylint==1.6.5
2 changes: 1 addition & 1 deletion salt/fileserver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def update_intervals(self, back=None):
ret[fsb] = self.servers[fstr]()
return ret

def envs(self, back=None, sources=False):
def envs(self, back=None, sources=False, **kwargs):
'''
Return the environments for the named backend or all backends
'''
Expand Down
2 changes: 1 addition & 1 deletion salt/master.py
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ def _file_envs(self, load=None):
if any('environments' in ext for ext in self.opts['ext_pillar']):
return self.pillars['environments'](load.get('id'), {})
else:
return self.fs_.envs()
return self.fs_.envs(**load)

def __verify_minion(self, id_, token):
'''
Expand Down
46 changes: 32 additions & 14 deletions salt/pillar/environments.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,31 +104,31 @@ def __get_by_node(self, node):
def tenancy_groups_set():
groups = IndexedSet()

# assume config is a sane schema
# scalar = tag, dict = 'tag' key is tag
for tenancy in __opts__['evaporator']['tenancies']:
if isinstance(tenancy, dict):
groups.add(tenancy['environment'])
else:
groups.add(tenancy)
try:
groups.add(tenancy['environment']['groups'])
except KeyError:
pass

return groups


def global_tenancy_groups_set():
groups = IndexedSet()

# assume config is a sane schema
# scalar = tag, dict = 'tag' key is tag
for tenancy in __opts__['evaporator']['tenancies']:
if isinstance(tenancy, dict) and tenancy.get('global'):
groups.add(tenancy['environment'])
try:
if tenancy['global']:
groups.add(tenancy['environment']['groups'])
except KeyError:
pass

return groups


# first try node-id if it exists in grains, then try the minion_id
def resolve_node(minion_id):

if __grains__.get('bb', {}).get('node-id'):
node_id = __grains__['bb']['node-id']
try:
Expand All @@ -145,6 +145,18 @@ def resolve_node(minion_id):
# if we've gotten this far its an unknown node
return None

def stage_envs(stage, envs):
"""
Takes in an iterable of env names and prepends a stage to the beginning.
Example:
env = [salt-core, natm]
stage = 'sn2'
>> {'environments': ['salt-core-sn2', 'natm-sn2']}
"""
staged_envs = ['{}-{}'.format(env, stage) for env in envs]
return {'environments': staged_envs}


# the goal here is to
# 1. if a local top for the given repo exists, include it
Expand All @@ -161,13 +173,19 @@ def ext_pillar(minion_id, pillar):
:return: a dictionary which is included by the salt master in the pillars returned to the minion
"""

node = resolve_node(minion_id)
global_tenancy_groups = global_tenancy_groups_set()

# hostinfo resolving a node that is None will throw an error
if not minion_id:
return stage_envs('nostage', global_tenancy_groups)

node = resolve_node(minion_id)

if node is None:
return []
return stage_envs('nostage', global_tenancy_groups)

# any matching tenancy_group is a 1 to 1 association with environment
# we use an IndexedSet to ensure global roots are always highest priority
environments = IndexedSet(global_tenancy_groups_set() | ( node.groups_set() & tenancy_groups_set()))
environments = IndexedSet(global_tenancy_groups | ( node.groups_set() & tenancy_groups_set()))

return {'environments': list(environments)}
return stage_envs(node.stage, environments)
Loading

0 comments on commit fe8f74f

Please sign in to comment.