2017-07-20 23:53:45 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Import python libs
|
|
|
|
from __future__ import absolute_import, print_function
|
|
|
|
|
|
|
|
# Import Salt Testing libs
|
2017-07-25 21:11:25 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2017-07-20 23:53:45 +00:00
|
|
|
from tests.support.unit import skipIf, TestCase
|
2017-07-25 21:11:25 +00:00
|
|
|
from tests.support.mock import NO_MOCK, NO_MOCK_REASON, MagicMock, patch
|
2017-07-20 23:53:45 +00:00
|
|
|
|
|
|
|
# Import salt libs
|
2017-07-25 21:11:25 +00:00
|
|
|
from salt.exceptions import CommandExecutionError
|
2017-07-20 23:53:45 +00:00
|
|
|
import salt.modules.saltcheck as saltcheck
|
|
|
|
|
2017-07-25 21:11:25 +00:00
|
|
|
saltcheck.__salt__ = {}
|
2017-07-20 23:53:45 +00:00
|
|
|
|
|
|
|
class SaltCheckTestCase(TestCase):
|
|
|
|
''' SaltCheckTestCase'''
|
|
|
|
|
|
|
|
def test_update_master_cache(self):
|
|
|
|
self.assertTrue(saltcheck.update_master_cache)
|
|
|
|
|
2017-07-25 21:11:25 +00:00
|
|
|
|
2017-07-21 17:11:27 +00:00
|
|
|
def test_call_salt_command(self):
|
2017-07-25 21:11:25 +00:00
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
2017-07-26 22:00:15 +00:00
|
|
|
'sys.list_modules': MagicMock(return_value=['module1']),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
2017-07-25 21:11:25 +00:00
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
returned = sc.call_salt_command(fun="test.echo", args=['hello'], kwargs=None)
|
|
|
|
self.assertEqual(returned, 'hello')
|
|
|
|
|
|
|
|
def test_call_salt_command2(self):
|
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
2017-07-26 22:00:15 +00:00
|
|
|
'sys.list_modules': MagicMock(return_value=['module1']),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
2017-07-25 21:11:25 +00:00
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
returned = sc.call_salt_command(fun="test.echo", args=['hello'], kwargs=None)
|
|
|
|
self.assertNotEqual(returned, 'not-hello')
|
2017-07-21 17:11:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test__assert_equal1(self):
|
2017-07-26 22:00:15 +00:00
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
a = {'a': 1, 'b': 2}
|
|
|
|
b = {'a': 1, 'b': 2}
|
|
|
|
mybool = sc._SaltCheck__assert_equal(a, b)
|
|
|
|
self.assertTrue(mybool)
|
2017-07-21 17:11:27 +00:00
|
|
|
|
|
|
|
def test__assert_equal2(self):
|
2017-07-26 22:00:15 +00:00
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
a = {'a': 1, 'b': 2}
|
|
|
|
b = {'a': 1, 'b': 2, 'c': 3}
|
|
|
|
mybool = sc._SaltCheck__assert_equal(False, True)
|
|
|
|
self.assertNotEqual(mybool, True)
|
2017-07-21 17:11:27 +00:00
|
|
|
|
|
|
|
def test__assert_not_equal1(self):
|
2017-07-26 22:00:15 +00:00
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
a = {'a': 1, 'b': 2}
|
|
|
|
b = {'a': 1, 'b': 2, 'c': 3}
|
|
|
|
mybool = sc._SaltCheck__assert_not_equal(a, b)
|
|
|
|
self.assertTrue(mybool)
|
2017-07-21 17:11:27 +00:00
|
|
|
|
|
|
|
def test__assert_not_equal2(self):
|
2017-07-26 22:00:15 +00:00
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
a = {'a': 1, 'b': 2}
|
|
|
|
b = {'a': 1, 'b': 2}
|
|
|
|
mybool = sc._SaltCheck__assert_not_equal(a, b)
|
|
|
|
self.assertNotEqual(mybool, True)
|
2017-07-21 17:11:27 +00:00
|
|
|
|
|
|
|
def test__assert_true1(self):
|
2017-07-26 22:00:15 +00:00
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
mybool = sc._SaltCheck__assert_equal(True, True)
|
|
|
|
self.assertTrue(mybool)
|
2017-07-21 17:11:27 +00:00
|
|
|
|
|
|
|
def test__assert_true2(self):
|
2017-07-26 22:00:15 +00:00
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
mybool = sc._SaltCheck__assert_equal(False, True)
|
|
|
|
self.assertNotEqual(mybool, True)
|
2017-07-21 17:11:27 +00:00
|
|
|
|
|
|
|
def test__assert_false1(self):
|
2017-07-26 22:00:15 +00:00
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
mybool = sc._SaltCheck__assert_false(False)
|
|
|
|
self.assertTrue(mybool)
|
2017-07-21 17:11:27 +00:00
|
|
|
|
|
|
|
def test__assert_false2(self):
|
2017-07-26 22:00:15 +00:00
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
mybool = sc._SaltCheck__assert_false(True)
|
|
|
|
self.assertNotEqual(mybool, True)
|
2017-07-21 17:11:27 +00:00
|
|
|
|
|
|
|
def test__assert_in1(self):
|
2017-07-26 22:00:15 +00:00
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
a = "bob"
|
|
|
|
mylist = ['alice', 'bob', 'charles', 'dana']
|
|
|
|
mybool = sc._SaltCheck__assert_in(a, mylist)
|
|
|
|
self.assertTrue(mybool, True)
|
2017-07-21 17:11:27 +00:00
|
|
|
|
|
|
|
def test__assert_in2(self):
|
2017-07-26 22:00:15 +00:00
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
a = "elaine"
|
|
|
|
mylist = ['alice', 'bob', 'charles', 'dana']
|
|
|
|
mybool = sc._SaltCheck__assert_in(a, mylist)
|
|
|
|
self.assertNotEqual(mybool, True)
|
2017-07-21 17:11:27 +00:00
|
|
|
|
|
|
|
def test__assert_not_in1(self):
|
2017-07-26 22:00:15 +00:00
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
a = "elaine"
|
|
|
|
mylist = ['alice', 'bob', 'charles', 'dana']
|
|
|
|
mybool = sc._SaltCheck__assert_not_in(a, mylist)
|
|
|
|
self.assertTrue(mybool, True)
|
2017-07-21 17:11:27 +00:00
|
|
|
|
|
|
|
def test__assert_not_in2(self):
|
2017-07-26 22:00:15 +00:00
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
a = "bob"
|
|
|
|
mylist = ['alice', 'bob', 'charles', 'dana']
|
|
|
|
mybool = sc._SaltCheck__assert_not_in(a, mylist)
|
|
|
|
self.assertNotEqual(mybool, True)
|
2017-07-21 17:11:27 +00:00
|
|
|
|
|
|
|
def test__assert_greater1(self):
|
2017-07-26 22:00:15 +00:00
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
a = 110
|
|
|
|
b = 100
|
|
|
|
mybool = sc._SaltCheck__assert_greater(a, b)
|
|
|
|
self.assertTrue(mybool, True)
|
2017-07-21 17:11:27 +00:00
|
|
|
|
|
|
|
def test__assert_greater2(self):
|
2017-07-26 22:00:15 +00:00
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
a = 100
|
|
|
|
b = 110
|
|
|
|
mybool = sc._SaltCheck__assert_greater(a, b)
|
|
|
|
self.assertNotEqual(mybool, True)
|
2017-07-21 17:11:27 +00:00
|
|
|
|
|
|
|
def test__assert_greater3(self):
|
2017-07-26 22:00:15 +00:00
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
a = 100
|
|
|
|
b = 100
|
|
|
|
mybool = sc._SaltCheck__assert_greater(a, b)
|
|
|
|
self.assertNotEqual(mybool, True)
|
2017-07-21 17:11:27 +00:00
|
|
|
|
|
|
|
def test__assert_greater_equal_equal1(self):
|
2017-07-26 22:00:15 +00:00
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
a = 110
|
|
|
|
b = 100
|
|
|
|
mybool = sc._SaltCheck__assert_greater_equal(a, b)
|
|
|
|
self.assertTrue(mybool, True)
|
2017-07-21 17:11:27 +00:00
|
|
|
|
|
|
|
def test__assert_greater_equal2(self):
|
2017-07-26 22:00:15 +00:00
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
a = 100
|
|
|
|
b = 110
|
|
|
|
mybool = sc._SaltCheck__assert_greater_equal(a, b)
|
|
|
|
self.assertNotEqual(mybool, True)
|
2017-07-21 17:11:27 +00:00
|
|
|
|
|
|
|
def test__assert_greater_equal3(self):
|
2017-07-26 22:00:15 +00:00
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
a = 100
|
|
|
|
b = 100
|
|
|
|
mybool = sc._SaltCheck__assert_greater_equal(a, b)
|
|
|
|
self.assertEqual(mybool, True)
|
2017-07-21 17:11:27 +00:00
|
|
|
|
|
|
|
def test__assert_less1(self):
|
2017-07-26 22:00:15 +00:00
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
a = 99
|
|
|
|
b = 100
|
|
|
|
mybool = sc._SaltCheck__assert_less(a, b)
|
|
|
|
self.assertTrue(mybool, True)
|
2017-07-21 17:11:27 +00:00
|
|
|
|
|
|
|
def test__assert_less2(self):
|
2017-07-26 22:00:15 +00:00
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
a = 110
|
|
|
|
b = 99
|
|
|
|
mybool = sc._SaltCheck__assert_less(a, b)
|
|
|
|
self.assertNotEqual(mybool, True)
|
2017-07-20 23:53:45 +00:00
|
|
|
|
2017-07-21 17:11:27 +00:00
|
|
|
def test__assert_less3(self):
|
2017-07-26 22:00:15 +00:00
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
a = 100
|
|
|
|
b = 100
|
|
|
|
mybool = sc._SaltCheck__assert_less(a, b)
|
|
|
|
self.assertNotEqual(mybool, True)
|
2017-07-20 23:53:45 +00:00
|
|
|
|
2017-07-21 17:11:27 +00:00
|
|
|
def test__assert_less_equal1(self):
|
2017-07-26 22:00:15 +00:00
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
a = 99
|
|
|
|
b = 100
|
|
|
|
mybool = sc._SaltCheck__assert_less_equal(a, b)
|
|
|
|
self.assertTrue(mybool, True)
|
2017-07-20 23:53:45 +00:00
|
|
|
|
2017-07-21 17:11:27 +00:00
|
|
|
def test__assert_less_equal2(self):
|
2017-07-26 22:00:15 +00:00
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
a = 110
|
|
|
|
b = 99
|
|
|
|
mybool = sc._SaltCheck__assert_less_equal(a, b)
|
|
|
|
self.assertNotEqual(mybool, True)
|
2017-07-20 23:53:45 +00:00
|
|
|
|
2017-07-21 17:11:27 +00:00
|
|
|
def test__assert_less_equal3(self):
|
2017-07-26 22:00:15 +00:00
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
|
|
|
}):
|
|
|
|
sc = saltcheck.SaltCheck()
|
|
|
|
a = 100
|
|
|
|
b = 100
|
|
|
|
mybool = sc._SaltCheck__assert_less_equal(a, b)
|
|
|
|
self.assertEqual(mybool, True)
|
|
|
|
|
|
|
|
def test_run_test_1(self):
|
|
|
|
with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
'sys.list_modules': MagicMock(return_value=['test']),
|
|
|
|
'sys.list_functions': MagicMock(return_value=['test.echo']),
|
|
|
|
'cp.cache_master': MagicMock(return_value=[True])
|
|
|
|
}):
|
|
|
|
returned = saltcheck.run_test(test={"module_and_function": "test.echo", "assertion": "assertEqual", "expected-return": "This works!", "args":["This works!"] })
|
|
|
|
self.assertEqual(returned, True)
|
|
|
|
|
|
|
|
# pillar injection not supported yet
|
|
|
|
#def test_run_test_2(self):
|
|
|
|
# with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
|
|
|
|
# 'sys.list_modules': MagicMock(return_value=['pillar']),
|
|
|
|
# 'sys.list_functions': MagicMock(return_value=['pillar.get'])
|
|
|
|
# }):
|
|
|
|
# returned = saltcheck.run_test(test={"module_and_function": "pillar.get", "pillar-data": "mykey:myvalue", "assertion": "assertEqual", "expected-return": "myvalue", "args":["mykey"] })
|
|
|
|
# self.assertEqual(returned, True)
|
|
|
|
|