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

Add some grains targeting tests #34077

Merged
merged 2 commits into from
Jun 21, 2016
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
37 changes: 30 additions & 7 deletions salt/modules/grains.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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:

Expand All @@ -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.
Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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:

Expand Down
53 changes: 53 additions & 0 deletions tests/integration/cli/grains.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,69 @@
'''
# 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

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
Expand Down
74 changes: 72 additions & 2 deletions tests/integration/modules/grains.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# -*- coding: utf-8 -*-

'''
Test the grains module
'''

# Import python libs
from __future__ import absolute_import
import os
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
Expand Down Expand Up @@ -100,6 +101,75 @@ def test_get(self):
['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)