2013-11-27 11:19:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2013-11-07 16:15:30 +00:00
|
|
|
# Import Salt Testing libs
|
|
|
|
from salttesting import TestCase
|
|
|
|
from salttesting.helpers import ensure_in_syspath
|
|
|
|
|
|
|
|
ensure_in_syspath('../../')
|
|
|
|
|
|
|
|
# Import Salt libs
|
|
|
|
from salt.exceptions import SaltException
|
|
|
|
from salt.modules import grains as grainsmod
|
2013-11-27 12:11:45 +00:00
|
|
|
|
2013-11-07 16:15:30 +00:00
|
|
|
grainsmod.__grains__ = {
|
|
|
|
'os_family': 'MockedOS'
|
|
|
|
}
|
2013-11-27 12:11:45 +00:00
|
|
|
|
|
|
|
|
2013-11-07 16:15:30 +00:00
|
|
|
class GrainsModuleTestCase(TestCase):
|
|
|
|
|
|
|
|
def test_filter_by(self):
|
2013-11-27 12:48:53 +00:00
|
|
|
dict1 = {'A': 'B', 'C': {'D': {'E': 'F', 'G': 'H'}}}
|
|
|
|
mdict = {'D': {'E': 'I'}, 'J': 'K'}
|
2013-11-07 16:15:30 +00:00
|
|
|
|
|
|
|
# test None result with non existent grain and no default
|
2013-11-27 12:48:53 +00:00
|
|
|
res = grainsmod.filter_by(dict1, grain='xxx')
|
|
|
|
self.assertIs(res, None)
|
2013-11-07 16:15:30 +00:00
|
|
|
|
|
|
|
# test None result with os_family grain and no matching result
|
2013-11-27 12:48:53 +00:00
|
|
|
res = grainsmod.filter_by(dict1)
|
|
|
|
self.assertIs(res, None)
|
2013-11-07 16:15:30 +00:00
|
|
|
|
|
|
|
# test with non existent grain, and a given default key
|
2013-11-27 12:48:53 +00:00
|
|
|
res = grainsmod.filter_by(dict1, grain='xxx', default='C')
|
|
|
|
self.assertEqual(res, {'D': {'E': 'F', 'G': 'H'}})
|
2013-11-07 16:15:30 +00:00
|
|
|
|
2014-08-11 17:46:43 +00:00
|
|
|
# add a merge dictionary, F disappears
|
2013-11-27 12:48:53 +00:00
|
|
|
res = grainsmod.filter_by(dict1, grain='xxx', merge=mdict, default='C')
|
|
|
|
self.assertEqual(res, {'D': {'E': 'I', 'G': 'H'}, 'J': 'K'})
|
2014-08-11 17:46:43 +00:00
|
|
|
# dict1 was altered, reestablish
|
2013-11-27 12:48:53 +00:00
|
|
|
dict1 = {'A': 'B', 'C': {'D': {'E': 'F', 'G': 'H'}}}
|
2013-11-07 16:15:30 +00:00
|
|
|
|
|
|
|
# default is not present in dict1, check we only have merge in result
|
2013-11-27 12:48:53 +00:00
|
|
|
res = grainsmod.filter_by(dict1, grain='xxx', merge=mdict, default='Z')
|
|
|
|
self.assertEqual(res, mdict)
|
2013-11-07 16:15:30 +00:00
|
|
|
|
|
|
|
# default is not present in dict1, and no merge, should get None
|
2013-11-27 12:48:53 +00:00
|
|
|
res = grainsmod.filter_by(dict1, grain='xxx', default='Z')
|
|
|
|
self.assertIs(res, None)
|
2013-11-07 16:15:30 +00:00
|
|
|
|
|
|
|
#test giving a list as merge argument raise exception
|
|
|
|
self.assertRaises(
|
|
|
|
SaltException,
|
|
|
|
grainsmod.filter_by,
|
|
|
|
dict1,
|
|
|
|
'xxx',
|
|
|
|
['foo'],
|
|
|
|
'C'
|
|
|
|
)
|
|
|
|
|
|
|
|
#Now, re-test with an existing grain (os_family), but with no match.
|
2013-11-27 12:48:53 +00:00
|
|
|
res = grainsmod.filter_by(dict1)
|
|
|
|
self.assertIs(res, None)
|
|
|
|
res = grainsmod.filter_by(dict1, default='C')
|
|
|
|
self.assertEqual(res, {'D': {'E': 'F', 'G': 'H'}})
|
|
|
|
res = grainsmod.filter_by(dict1, merge=mdict, default='C')
|
|
|
|
self.assertEqual(res, {'D': {'E': 'I', 'G': 'H'}, 'J': 'K'})
|
2014-08-11 17:46:43 +00:00
|
|
|
# dict1 was altered, reestablish
|
2013-11-27 12:48:53 +00:00
|
|
|
dict1 = {'A': 'B', 'C': {'D': {'E': 'F', 'G': 'H'}}}
|
|
|
|
res = grainsmod.filter_by(dict1, merge=mdict, default='Z')
|
|
|
|
self.assertEqual(res, mdict)
|
|
|
|
res = grainsmod.filter_by(dict1, default='Z')
|
|
|
|
self.assertIs(res, None)
|
2014-08-11 17:46:43 +00:00
|
|
|
# this one is in fact a traceback in updatedict, merging a string with a dictionary
|
2013-11-07 16:15:30 +00:00
|
|
|
self.assertRaises(
|
|
|
|
TypeError,
|
|
|
|
grainsmod.filter_by,
|
|
|
|
dict1,
|
|
|
|
merge=mdict,
|
|
|
|
default='A'
|
|
|
|
)
|
|
|
|
|
|
|
|
#Now, re-test with a matching grain.
|
2013-11-27 12:48:53 +00:00
|
|
|
dict1 = {'A': 'B', 'MockedOS': {'D': {'E': 'F', 'G': 'H'}}}
|
|
|
|
res = grainsmod.filter_by(dict1)
|
|
|
|
self.assertEqual(res, {'D': {'E': 'F', 'G': 'H'}})
|
|
|
|
res = grainsmod.filter_by(dict1, default='A')
|
|
|
|
self.assertEqual(res, {'D': {'E': 'F', 'G': 'H'}})
|
|
|
|
res = grainsmod.filter_by(dict1, merge=mdict, default='A')
|
|
|
|
self.assertEqual(res, {'D': {'E': 'I', 'G': 'H'}, 'J': 'K'})
|
2014-08-11 17:46:43 +00:00
|
|
|
# dict1 was altered, reestablish
|
2013-11-27 12:48:53 +00:00
|
|
|
dict1 = {'A': 'B', 'MockedOS': {'D': {'E': 'F', 'G': 'H'}}}
|
|
|
|
res = grainsmod.filter_by(dict1, merge=mdict, default='Z')
|
|
|
|
self.assertEqual(res, {'D': {'E': 'I', 'G': 'H'}, 'J': 'K'})
|
2014-08-11 17:46:43 +00:00
|
|
|
# dict1 was altered, reestablish
|
2013-11-27 12:48:53 +00:00
|
|
|
dict1 = {'A': 'B', 'MockedOS': {'D': {'E': 'F', 'G': 'H'}}}
|
|
|
|
res = grainsmod.filter_by(dict1, default='Z')
|
|
|
|
self.assertEqual(res, {'D': {'E': 'F', 'G': 'H'}})
|
2013-11-07 16:15:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from integration import run_tests
|
|
|
|
run_tests(GrainsModuleTestCase, needs_daemon=False)
|