Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_get_private_key(self):
self.patch_object(openstack_utils.os.path, "isfile",
return_value=True)
m = mock.mock_open(read_data='myprivkey')
with mock.patch(
'zaza.openstack.utilities.openstack.open', m, create=True
):
self.assertEqual(
openstack_utils.get_private_key('mykeys'),
'myprivkey')
def test_get_aarch64_uuid_collection_no_file(self):
mock.mock_open(read_data="no file")
mock.mock_open.side_effect = IOError()
firmware_provider_class = firmware_info.get_firmware_collector(arch='aarch64')
firmware_provider_class.arch = 'aarch64'
result = firmware_provider_class.get_all()
self.assertTrue('dmi.system.uuid' not in result)
def test_random_gender():
"""test random gender input."""
random_gender = get_random_string(
exclude_list=('male', 'm', 'female', 'f', ''))
invalid_input_message = (
'Invalid input, please enter male, female or .')
gender_question = 'What is my gender ((m)ale/(f)emale)?: '
m = mock.mock_open()
with mock.patch('melissa.profile_populator.raw_input') as mock_input, \
mock.patch('sys.stdout', new_callable=StringIO) as mock_stdout, \
mock.patch(
'melissa.profile_populator.open',
m, create=True
), \
mock.patch('melissa.profile_populator.json') as mock_json:
side_effect = InputSideEffect(gender_question, random_gender)
mock_input.side_effect = side_effect.func
# run the func.
profile_populator()
result_json = mock_json.dump.call_args[0][0]
assert result_json['va_gender'] == 'female'
assert invalid_input_message in mock_stdout.getvalue()
def test_snap(self, mock_log, mock_etree, mock_dev):
prs = Parser()
test_file = os.path.join(os.path.dirname(__file__),
'configs', 'delta.yml')
test_file = open(test_file, 'r')
test_file = yaml.load(test_file, Loader=yaml.FullLoader)
dev = jnpr.junos.device.Device(
host="1.1.1.1",
user="user",
passwd="xyz")
dev.open()
m_op = mock_open()
with patch('jnpr.jsnapy.snap.open', m_op, create=True) as m_open:
prs.generate_reply(
test_file,
dev,
"1.1.1.1_snap_mock",
"1.1.1.1",
self.db)
self.assertEqual(prs.command_list, ['show chassis fpc'])
self.assertEqual(prs.rpc_list, [])
self.assertEqual(prs.test_included, ['check_chassis_fpc'])
dev.close()
def test_prediag_is_an_instance_true_xen(self):
responses.add(responses.GET, "http://169.254.169.254/latest/dynamic/instance-identity/document", status=200)
open_mock = mock.mock_open(read_data="ec2SomeUUIDWouldNormallyGoHere\n")
# mock_open does not have support for iteration so it must be added manually
# readline() until a blank line is reached (the sentinel)
def iter_func(self):
return iter(self.readline, "")
open_mock.return_value.__iter__ = iter_func
def py3_next_func(self):
return next(iter(self.readline, ""))
if sys.hexversion >= 0x3000000:
open_mock.return_value.__next__ = py3_next_func
with mock.patch("ec2rlcore.prediag.open", open_mock):
self.assertTrue(ec2rlcore.prediag.is_an_instance())
self.assertTrue(open_mock.called)
def test_explicit_mock(self):
mock = MagicMock()
mock_open(mock)
with patch('%s.open' % __name__, mock, create=True) as patched:
self.assertIs(patched, mock)
open('foo')
mock.assert_called_once_with('foo')
def test_encrypt_works(self):
url = '{0}/agent/encrypt'.format(self.conn.host)
HTTPretty.register_uri(HTTPretty.POST,
url, status=204)
self.assertEqual(self.agent.encrypted, False)
# This is some vicious mocking. In order:
# 1. Mock the built-in open so we read in a mock PEM
# 2. Mock PyCrypto's importKey so we avoid reading from /dev/urandom
# 3. Mock PyCrypto's Cipher generator so we avoid randgen
# 4. Mock PyCrypto's Cipher.encrypt since its already a mock
# 5. Ensure the call can complete and the agent is encrypted
m = mock.mock_open(read_data=mock_rsa.public_key())
open_fn_name = ('builtins.open' if sys.version_info[0] == 3 else
'__builtin__.open')
with mock.patch(open_fn_name, m, create=True):
with mock.patch('Crypto.PublicKey.RSA.importKey') as key:
key.return_value = 'woot'
with mock.patch('Crypto.Cipher.PKCS1_v1_5.new') as cipher:
cipher.return_value = mock.MagicMock()
cipher.return_value.encrypt.return_value = b'awesome'
self.agent.encrypt('sweet_tacos')
self.assertEqual(self.agent.encrypted, True)
def test_app(self, *args):
with mock.patch("builtins.open", mock.mock_open(read_data=json.dumps(crudcast_config))):
app = CrudcastApp(__name__, config_file='file')
self.assertEqual(app.swagger_config['swagger'], '2.0')
with mock.patch('flask_swagger_ui.get_swaggerui_blueprint'):
app.get_swagger_ui_view()
self.assertTrue(dhcp_handler.routes is None)
self.assertTrue(dhcp_handler.gateway is None)
# execute
routing_table = "\
Iface Destination Gateway Flags RefCnt Use Metric " \
"Mask MTU Window IRTT \n\
eth0 00000000 10813FA8 0003 0 0 5 " \
"00000000 0 0 0 \n\
eth0 00345B0A 00000000 0001 0 0 5 " \
"00000000 0 0 0 \n\
lo 00000000 01345B0A 0003 0 0 1 " \
"00FCFFFF 0 0 0 \n"
with patch("os.path.exists", return_value=True):
mo = mock.mock_open(read_data=routing_table)
with patch(open_patch(), mo):
self.assertTrue(dhcp_handler.wireserver_route_exists)
# test
self.assertTrue(dhcp_handler.endpoint is not None)
self.assertTrue(dhcp_handler.routes is None)
self.assertTrue(dhcp_handler.gateway is None)
def test_create_invalid_spec(self, mock_open, mock_print):
mock_open.side_effect = mock.mock_open(read_data="[]")
self.assertEqual(
1, self.env.create(self.api, "n", "d", spec="spec.yml"))
mock_print.assert_has_calls([
mock.call("Env spec has wrong format:"),
mock.call("[]"),
mock.call(mock.ANY)
])