Merge pull request #34077 from rallytime/grains-tests

Add some grains targeting tests
This commit is contained in:
Mike Place 2016-06-21 09:06:30 -07:00 committed by GitHub
commit 3669048654
3 changed files with 155 additions and 9 deletions

View file

@ -193,7 +193,9 @@ def setvals(grains, destructive=False):
'''
Set new grains values in the grains config file
:param Destructive: If an operation results in a key being removed, delete the key, too. Defaults to False.
destructive
If an operation results in a key being removed, delete the key, too.
Defaults to False.
CLI Example:
@ -279,7 +281,15 @@ def setval(key, val, destructive=False):
'''
Set a grains value in the grains config file
:param Destructive: If an operation results in a key being removed, delete the key, too. Defaults to False.
key
The grain key to be set.
val
The value to set the grain key to.
destructive
If an operation results in a key being removed, delete the key, too.
Defaults to False.
CLI Example:
@ -305,11 +315,13 @@ def append(key, val, convert=False, delimiter=DEFAULT_TARGET_DELIM):
val
The value to append to the grain key
:param convert: If convert is True, convert non-list contents into a list.
convert
If convert is True, convert non-list contents into a list.
If convert is False and the grain contains non-list contents, an error
is given. Defaults to False.
:param delimiter: The key can be a nested dict key. Use this parameter to
delimiter
The key can be a nested dict key. Use this parameter to
specify the delimiter you use, instead of the default ``:``.
You can now append values to a list in nested dictionary grains. If the
list doesn't exist at this level, it will be created.
@ -351,12 +363,19 @@ def remove(key, val, delimiter=DEFAULT_TARGET_DELIM):
Remove a value from a list in the grains config file
:param delimiter: The key can be a nested dict key. Use this parameter to
key
The grain key to remove.
val
The value to remove.
delimiter
The key can be a nested dict key. Use this parameter to
specify the delimiter you use, instead of the default ``:``.
You can now append values to a list in nested dictionary grains. If the
list doesn't exist at this level, it will be created.
.. versionadded:: Boron
.. versionadded:: 2015.8.2
CLI Example:
@ -387,7 +406,11 @@ def delval(key, destructive=False):
Delete a grain from the grains config file
:param destructive: Delete the key, too. Defaults to False.
key
The grain key from which to delete the value.
destructive
Delete the key, too. Defaults to False.
CLI Example:

View file

@ -14,9 +14,11 @@
'''
# Import Python libs
from __future__ import absolute_import
import os
# Import Salt Libs
import integration
import salt.utils
# Import Salt Testing Libs
from salttesting.helpers import ensure_in_syspath
@ -24,6 +26,57 @@ from salttesting.helpers import ensure_in_syspath
ensure_in_syspath('../../')
class GrainsTargetingTest(integration.ShellCase):
'''
Integration tests for targeting with grains.
'''
def test_grains_targeting_os_running(self):
'''
Tests running "salt -G 'os:<system-os>' test.ping and minions both return True
'''
test_ret = ['sub_minion:', ' True', 'minion:', ' True']
os_grain = ''
for item in self.run_salt('minion grains.get os'):
if item != 'minion:':
os_grain = item.strip()
ret = self.run_salt('-G \'os:{0}\' test.ping'.format(os_grain))
self.assertEqual(sorted(ret), sorted(test_ret))
def test_grains_targeting_minion_id_running(self):
'''
Tests return of each running test minion targeting with minion id grain
'''
minion = self.run_salt('-G \'id:minion\' test.ping')
self.assertEqual(sorted(minion), sorted(['minion:', ' True']))
sub_minion = self.run_salt('-G \'id:sub_minion\' test.ping')
self.assertEqual(sorted(sub_minion), sorted(['sub_minion:', ' True']))
def test_grains_targeting_disconnected(self):
'''
Tests return of minion using grains targeting on a disconnected minion.
'''
test_ret = 'Minion did not return. [Not connected]'
# Create a minion key, but do not start the "fake" minion. This mimics a
# disconnected minion.
key_file = os.path.join(self.master_opts['pki_dir'], 'minions', 'disconnected')
salt.utils.fopen(key_file, 'a').close()
# ping disconnected minion and ensure it times out and returns with correct message
try:
ret = ''
for item in self.run_salt('-G \'id:disconnected\' test.ping'):
if item != 'disconnected:':
ret = item.strip()
self.assertEqual(ret, test_ret)
finally:
os.unlink(key_file)
class SSHGrainsTest(integration.SSHCase):
'''
Test salt-ssh grains functionality

View file

@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
'''
Test the grains module
'''
# Import python libs
from __future__ import absolute_import
import os
@ -10,7 +10,8 @@ import time
# Import Salt Testing libs
from salttesting import skipIf
from salttesting.helpers import ensure_in_syspath
from salttesting.helpers import destructiveTest, ensure_in_syspath
ensure_in_syspath('../../')
# Import salt libs
@ -100,6 +101,75 @@ class TestModulesGrains(integration.ModuleCase):
['level1:level2']),
'foo')
class GrainsAppendTestCase(integration.ModuleCase):
'''
Tests written specifically for the grains.append function.
'''
GRAIN_KEY = 'salttesting-grain-key'
GRAIN_VAL = 'my-grain-val'
@destructiveTest
def tearDown(self):
test_grain = self.run_function('grains.get', [self.GRAIN_KEY])
if test_grain and test_grain == [self.GRAIN_VAL]:
self.run_function('grains.remove', [self.GRAIN_KEY, self.GRAIN_VAL])
@destructiveTest
def test_grains_append(self):
'''
Tests the return of a simple grains.append call.
'''
ret = self.run_function('grains.append', [self.GRAIN_KEY, self.GRAIN_VAL])
self.assertEqual(ret[self.GRAIN_KEY], [self.GRAIN_VAL])
@destructiveTest
def test_grains_append_val_already_present(self):
'''
Tests the return of a grains.append call when the value is already present in the grains list.
'''
messaging = 'The val {0} was already in the list salttesting-grain-key'.format(self.GRAIN_VAL)
# First, make sure the test grain is present
self.run_function('grains.append', [self.GRAIN_KEY, self.GRAIN_VAL])
# Now try to append again
ret = self.run_function('grains.append', [self.GRAIN_KEY, self.GRAIN_VAL])
self.assertEqual(messaging, ret)
@destructiveTest
def test_grains_append_val_is_list(self):
'''
Tests the return of a grains.append call when val is passed in as a list.
'''
second_grain = self.GRAIN_VAL + '-2'
ret = self.run_function('grains.append', [self.GRAIN_KEY, [self.GRAIN_VAL, second_grain]])
self.assertEqual(ret[self.GRAIN_KEY], [self.GRAIN_VAL, second_grain])
@destructiveTest
def test_grains_append_call_twice(self):
'''
Tests the return of a grains.append call when the value is already present
but also ensure the grain is not listed twice.
'''
# First, add the test grain.
self.run_function('grains.append', [self.GRAIN_KEY, self.GRAIN_VAL])
# Call the function again, which results in a string message, as tested in
# test_grains_append_val_already_present above.
self.run_function('grains.append', [self.GRAIN_KEY, self.GRAIN_VAL])
# Now make sure the grain doesn't show up twice.
grains = self.run_function('grains.items')
count = 0
for grain in grains.keys():
if grain == self.GRAIN_KEY:
count += 1
# We should only have hit the grain key once.
self.assertEqual(count, 1)
if __name__ == '__main__':
from integration import run_tests
run_tests(TestModulesGrains)