Skip to content

Commit

Permalink
Merge pull request #39150 from rallytime/python3-compat-fixes
Browse files Browse the repository at this point in the history
Python 3 Test Fixes
  • Loading branch information
Mike Place authored Feb 3, 2017
2 parents 387b10b + 4595f81 commit fa52556
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 31 deletions.
12 changes: 6 additions & 6 deletions tests/unit/pillar/sqlcipher_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ def test_301_process_results_with_lists(self):
['a', 'b', 'c', 'f', 'g', 2],
['a', 'z', 'h', 'y', 'j', 3],
['a', 'z', 'h', 'y', 'k', 4]])
self.assertEqual(
self.assertEqual(sorted(
{'a': [
{'c': [
{'e': 1},
Expand All @@ -533,8 +533,8 @@ def test_301_process_results_with_lists(self):
{'j': 3, 'k': 4}
]
}
]},
return_data.result
]}),
sorted(return_data.result)
)

def test_302_process_results_with_lists_consecutive(self):
Expand All @@ -547,7 +547,7 @@ def test_302_process_results_with_lists_consecutive(self):
['a', 'b', 'c', 'f', 'g', 2],
['a', 'z', 'h', 'y', 'j', 3],
['a', 'z', 'h', 'y', 'k', 4]])
self.assertEqual(
self.assertEqual(sorted(
{'a': [
[[
{'e': 1},
Expand All @@ -558,8 +558,8 @@ def test_302_process_results_with_lists_consecutive(self):
{'j': 3, 'k': 4}
]
]
]},
return_data.result
]}),
sorted(return_data.result)
)


Expand Down
12 changes: 6 additions & 6 deletions tests/unit/pillar/sqlite3_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ def test_301_process_results_with_lists(self):
['a', 'b', 'c', 'f', 'g', 2],
['a', 'z', 'h', 'y', 'j', 3],
['a', 'z', 'h', 'y', 'k', 4]])
self.assertEqual(
self.assertEqual(sorted(
{'a': [
{'c': [
{'e': 1},
Expand All @@ -533,8 +533,8 @@ def test_301_process_results_with_lists(self):
{'j': 3, 'k': 4}
]
}
]},
return_data.result
]}),
sorted(return_data.result)
)

def test_302_process_results_with_lists_consecutive(self):
Expand All @@ -547,7 +547,7 @@ def test_302_process_results_with_lists_consecutive(self):
['a', 'b', 'c', 'f', 'g', 2],
['a', 'z', 'h', 'y', 'j', 3],
['a', 'z', 'h', 'y', 'k', 4]])
self.assertEqual(
self.assertEqual(sorted(
{'a': [
[[
{'e': 1},
Expand All @@ -558,8 +558,8 @@ def test_302_process_results_with_lists_consecutive(self):
{'j': 3, 'k': 4}
]
]
]},
return_data.result
]}),
sorted(return_data.result)
)


Expand Down
73 changes: 54 additions & 19 deletions tests/unit/states/kmod_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,35 @@ def test_present_multi(self):
mods = ['cheese', 'crackers']
ret = {'name': name,
'result': True,
'comment': '',
'changes': {}}

mock_mod_list = MagicMock(return_value=mods)
with patch.dict(kmod.__salt__, {'kmod.mod_list': mock_mod_list}):
comment = 'Kernel modules {0} are already present'.format(', '.join(mods))
ret.update({'comment': comment})
self.assertDictEqual(kmod.present(name, mods=mods), ret)
call_ret = kmod.present(name, mods=mods)

# Check comment independently: makes test more stable on PY3
comment = call_ret.pop('comment')
self.assertIn('cheese', comment)
self.assertIn('crackers', comment)
self.assertIn('are already present', comment)

# Assert against all other dictionary key/values
self.assertDictEqual(ret, call_ret)

mock_mod_list = MagicMock(return_value=[])
with patch.dict(kmod.__salt__, {'kmod.mod_list': mock_mod_list}):
with patch.dict(kmod.__opts__, {'test': True}):
comment = 'Kernel modules {0} are set to be loaded'.format(', '.join(mods))
ret.update({'comment': comment, 'result': None})
self.assertDictEqual(kmod.present(name, mods=mods), ret)
call_ret = kmod.present(name, mods=mods)
ret.update({'result': None})

# Check comment independently: makes test more stable on PY3
comment = call_ret.pop('comment')
self.assertIn('cheese', comment)
self.assertIn('crackers', comment)
self.assertIn('are set to be loaded', comment)

# Assert against all other dictionary key/values
self.assertDictEqual(ret, call_ret)

mock_mod_list = MagicMock(return_value=[])
mock_available = MagicMock(return_value=mods)
Expand All @@ -98,12 +112,19 @@ def test_present_multi(self):
'kmod.available': mock_available,
'kmod.load': mock_load}):
with patch.dict(kmod.__opts__, {'test': False}):
comment = 'Loaded kernel modules {0}'.format(', '.join(mods))
ret.update({'comment': comment,
'result': True,
call_ret = kmod.present(name, mods=mods)
ret.update({'result': True,
'changes': {mods[0]: 'loaded',
mods[1]: 'loaded'}})
self.assertDictEqual(kmod.present(name, mods=mods), ret)

# Check comment independently: makes test more stable on PY3
comment = call_ret.pop('comment')
self.assertIn('cheese', comment)
self.assertIn('crackers', comment)
self.assertIn('Loaded kernel modules', comment)

# Assert against all other dictionary key/values
self.assertDictEqual(ret, call_ret)

# 'absent' function tests: 2

Expand Down Expand Up @@ -152,27 +173,41 @@ def test_absent_multi(self):
mods = ['cheese', 'crackers']
ret = {'name': name,
'result': True,
'comment': '',
'changes': {}}

mock_mod_list = MagicMock(return_value=mods)
with patch.dict(kmod.__salt__, {'kmod.mod_list': mock_mod_list}):
with patch.dict(kmod.__opts__, {'test': True}):
comment = 'Kernel modules {0} are set to be removed'.format(', '.join(sorted(mods)))
ret.update({'comment': comment, 'result': None})
self.assertDictEqual(kmod.absent(name, mods=mods), ret)
ret.update({'result': None})
call_ret = kmod.absent(name, mods=mods)

# Check comment independently: makes test more stable on PY3
comment = call_ret.pop('comment')
self.assertIn('cheese', comment)
self.assertIn('crackers', comment)
self.assertIn('are set to be removed', comment)

# Assert against all other dictionary key/values
self.assertDictEqual(ret, call_ret)

mock_mod_list = MagicMock(return_value=mods)
mock_remove = MagicMock(return_value=mods)
with patch.dict(kmod.__salt__, {'kmod.mod_list': mock_mod_list,
'kmod.remove': mock_remove}):
with patch.dict(kmod.__opts__, {'test': False}):
comment = 'Removed kernel modules {0}'.format(', '.join(mods))
ret.update({'comment': comment,
'result': True,
call_ret = kmod.absent(name, mods=mods)
ret.update({'result': True,
'changes': {mods[0]: 'removed',
mods[1]: 'removed'}})
self.assertDictEqual(kmod.absent(name, mods=mods), ret)

# Check comment independently: makes test more stable on PY3
comment = call_ret.pop('comment')
self.assertIn('cheese', comment)
self.assertIn('crackers', comment)
self.assertIn('Removed kernel modules', comment)

# Assert against all other dictionary key/values
self.assertDictEqual(ret, call_ret)

mock_mod_list = MagicMock(return_value=[])
with patch.dict(kmod.__salt__, {'kmod.mod_list': mock_mod_list}):
Expand Down

0 comments on commit fa52556

Please sign in to comment.