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_op_hosts_limit(self):
inventory = make_inventory()
state = State(inventory, Config())
connect_all(state)
# Add op to both hosts
add_op(state, server.shell, 'echo "hi"')
# Add op to just the first host
add_op(
state, server.user,
'somehost_user',
hosts=inventory['somehost'],
)
# Ensure there are two ops
self.assertEqual(len(state.get_op_order()), 2)
# Ensure somehost has two ops and anotherhost only has the one
self.assertEqual(len(state.ops[inventory.get_host('somehost')]), 2)
def test_ignore_errors_op_fail(self):
inventory = make_inventory()
state = State(inventory, Config())
connect_all(state)
add_op(state, server.shell, 'echo "hi"', ignore_errors=True)
with patch('pyinfra.api.connectors.ssh.run_shell_command') as fake_run_command:
fake_channel = FakeChannel(1)
fake_run_command.return_value = (
False,
FakeBuffer('', fake_channel),
)
# This should run OK
run_ops(state)
somehost = inventory.get_host('somehost')
# Ensure the op was added to results
def test_file_upload_op(self):
inventory = make_inventory()
state = State(inventory, Config())
connect_all(state)
with patch('pyinfra.modules.files.path.isfile', lambda *args, **kwargs: True):
# Test normal
add_op(
state, files.put,
{'First op name'},
'files/file.txt',
'/home/vagrant/file.txt',
)
# And with sudo
add_op(
state, files.put,
'files/file.txt',
'/home/vagrant/file.txt',
sudo=True,
def test_connect_with_missing_ssh_key(self):
state = State(make_inventory(hosts=(
('somehost', {'ssh_key': 'testkey'}),
)), Config())
with self.assertRaises(IOError) as e:
connect_all(state)
# Ensure pyinfra style IOError
self.assertTrue(e.exception.args[0].startswith('No such private key file:'))
def test_no_invalid_op_call(self):
inventory = make_inventory()
state = State(inventory, Config())
connect_all(state)
pseudo_state.set(state)
state.in_op = True
with self.assertRaises(PyinfraError):
server.user('someuser')
state.in_op = False
state.in_deploy = True
with self.assertRaises(PyinfraError):
server.user('someuser')
def test_op_line_numbers(self):
inventory = make_inventory()
state = State(inventory, Config())
connect_all(state)
# Add op to both hosts
add_op(state, server.shell, 'echo "hi"')
# Add op to just the second host - using the pseudo modules such that
# it replicates a deploy file.
pseudo_state.set(state)
pseudo_host.set(inventory['anotherhost'])
first_pseudo_hash = server.user('anotherhost_user').hash
first_pseudo_call_line = getframeinfo(currentframe()).lineno - 1
# Add op to just the first host - using the pseudo modules such that
# it replicates a deploy file.
pseudo_state.set(state)
pseudo_host.set(inventory['somehost'])
second_pseudo_hash = server.user('somehost_user').hash
def test_connect_all(self):
inventory = make_inventory()
state = State(inventory, Config())
connect_all(state)
self.assertEqual(len(state.active_hosts), 2)
def test_fail_percent(self):
inventory = make_inventory((
'somehost',
('thinghost', {'ssh_hostname': SSHException}),
'anotherhost',
))
state = State(inventory, Config(FAIL_PERCENT=1))
# Ensure we would fail at this point
with self.assertRaises(PyinfraError) as context:
connect_all(state)
self.assertEqual(context.exception.args[0], 'Over 1% of hosts failed (33%)')
# Ensure the other two did connect
self.assertEqual(len(state.active_hosts), 2)
# the first argument is a tuple of (list all all hosts, global/ALL data)
inventory = Inventory((hosts, {}), **groups)
# Now we create a new config (w/optional args)
config = Config(
FAIL_PERCENT=81,
CONNECT_TIMEOUT=5,
)
# Setup the pyinfra state for this deploy
state = State(inventory, config)
# Connect to all the hosts
print('Connecting...')
connect_all(state)
# Start adding operations
print('Generating operations...')
add_op(
state, server.user,
'pyinfra',
home='/home/pyinfra',
shell='/bin/bash',
sudo=True,
)
add_op(
state, server.group,
{'Ensure pyinfra2 group exists'}, # set as the first arg names the operation
'pyinfra2',
state.deploy_dir = deploy_dir
# Setup the data to be passed to config hooks
hook_data = FallbackDict(
state.inventory.get_override_data(),
state.inventory.get_group_data(inventory_group),
state.inventory.get_data(),
)
# Run the before_connect hook if provided
run_hook(state, 'before_connect', hook_data)
# Connect to all the servers
print()
print('--> Connecting to hosts...')
connect_all(state)
# Run the before_connect hook if provided
run_hook(state, 'before_facts', hook_data)
# Just getting a fact?
#
if command == 'fact':
print()
print('--> Gathering facts...')
# Print facts as we get them
state.print_fact_info = True
# Print fact output with -v
state.print_fact_output = print_output