Best Python code snippet using lisa_python
test_extra.py
Source:test_extra.py
...11 alice_init_balance = accounts[0].balance()12 bob_init_balance = accounts[1].balance()13 # Creating channel14 chan_address = alice.establish_channel(15 bob.get_eth_address(), bob.get_ip_address(), 10 * ONE_ETH)16 chan = Channel.at(chan_address)17 assert chan.balance() == 10 * ONE_ETH18 # Alice sends money thrice19 hist = len(history)20 send_valid_balance_on_own_channel(network, bob.get_ip_address(), alice, chan_address, ONE_ETH)21 assert hist == len(history)22 # BOB CLOSING UNILATERALLY23 bob.close_channel(chan_address)24 # waiting25 chain.mine(APPEAL_PERIOD)26 assert Channel.at(chan_address).balance() == 10 * ONE_ETH27 # Bob Withdraws28 bob.withdraw_funds(chan_address)29 assert Channel.at(chan_address).balance() == 9 * ONE_ETH30 # Alice Withdraws31 alice.withdraw_funds(chan_address)32 assert Channel.at(chan_address).balance() == 033 assert alice_init_balance == accounts[0].balance() + 1*ONE_ETH34 assert bob_init_balance == accounts[1].balance() - 1*ONE_ETH35def test_transfer_money_without_reducing_own_balance(alice: LightningNode, bob: LightningNode, network: Network) -> None:36 alice_init_balance = accounts[0].balance()37 bob_init_balance = accounts[1].balance()38 # Creating channel39 chan_address = alice.establish_channel(40 bob.get_eth_address(), bob.get_ip_address(), 10 * ONE_ETH)41 chan = Channel.at(chan_address)42 assert chan.balance() == 10 * ONE_ETH43 # Alice sends money44 send_without_reducing_own_balance_on_own_channel(network, bob.get_ip_address(), alice, chan_address, ONE_ETH)45 46 alice.close_channel(chan_address)47 bob.appeal_closed_chan(chan_address)48 # Waiting49 chain.mine(APPEAL_PERIOD+2)50 # Bob Withdraws51 bob.withdraw_funds(chan_address)52 # Alice Withdraws53 alice.withdraw_funds(chan_address)54 assert chan.balance() == 055 assert alice_init_balance == accounts[0].balance()56 assert bob_init_balance == accounts[1].balance()57def test_transfer_money_with_bad_sig(alice: LightningNode, bob: LightningNode, network: Network) -> None:58 alice_init_balance = accounts[0].balance()59 bob_init_balance = accounts[1].balance()60 # Creating channel61 chan_address = alice.establish_channel(62 bob.get_eth_address(), bob.get_ip_address(), 10 * ONE_ETH)63 chan = Channel.at(chan_address)64 assert chan.balance() == 10 * ONE_ETH65 # Alice sends money66 alice.send(chan_address, 1*ONE_ETH)67 send_with_bad_sig_on_own_channel(network, bob.get_ip_address(), alice, chan_address, ONE_ETH)68 alice.close_channel(chan_address)69 bob.appeal_closed_chan(chan_address)70 # Waiting71 chain.mine(APPEAL_PERIOD+2)72 # Bob Withdraws73 bob.withdraw_funds(chan_address)74 # Alice Withdraws75 alice.withdraw_funds(chan_address)76 assert chan.balance() == 077 assert alice_init_balance == accounts[0].balance() + 1*ONE_ETH78 assert bob_init_balance == accounts[1].balance() - 1*ONE_ETH79def test_transfer_money_bad_serial(alice: LightningNode, bob: LightningNode, network: Network) -> None:80 alice_init_balance = accounts[0].balance()81 bob_init_balance = accounts[1].balance()82 # Creating channel83 chan_address = alice.establish_channel(84 bob.get_eth_address(), bob.get_ip_address(), 10 * ONE_ETH)85 chan = Channel.at(chan_address)86 assert chan.balance() == 10 * ONE_ETH87 # Alice sends money88 alice.send(chan_address, 1*ONE_ETH)89 send_bad_serial_on_own_channel(network, bob.get_ip_address(), alice, chan_address, ONE_ETH)90 alice.close_channel(chan_address)91 bob.appeal_closed_chan(chan_address)92 # Waiting93 chain.mine(APPEAL_PERIOD+2)94 # Bob Withdraws95 bob.withdraw_funds(chan_address)96 # Alice Withdraws97 alice.withdraw_funds(chan_address)98 assert chan.balance() == 099 assert alice_init_balance == accounts[0].balance() + 1*ONE_ETH100 assert bob_init_balance == accounts[1].balance() - 1*ONE_ETH101def test_close_channel_with_bad_balance(alice: LightningNode, bob: LightningNode, network: Network):102 alice_init_balance = accounts[0].balance()103 bob_init_balance = accounts[1].balance()104 # Creating channel105 chan_address = alice.establish_channel(106 bob.get_eth_address(), bob.get_ip_address(), 10 * ONE_ETH)107 chan = Channel.at(chan_address)108 assert chan.balance() == 10 * ONE_ETH109 with pytest.raises(VirtualMachineError):110 # Alice tries to close channel with bad balance (but signed properly)111 alice.close_channel(chan_address, sign(ChannelStateMessage(chan_address, alice_init_balance, alice_init_balance, 1), bob.get_eth_address()))112def test_give_wrong_ack_own_channel(alice: LightningNode, bob: LightningNode, network: Network):113 alice_init_balance = accounts[0].balance()114 bob_init_balance = accounts[1].balance()115 # Creating channel116 chan_address = alice.establish_channel(117 bob.get_eth_address(), bob.get_ip_address(), 10 * ONE_ETH)118 chan = Channel.at(chan_address)119 assert chan.balance() == 10 * ONE_ETH120 # Alice sends money121 old_state = alice.get_current_channel_state(chan_address)122 alice.send(chan_address, 1*ONE_ETH)123 alice.send(chan_address, 1*ONE_ETH)124 alice.send(chan_address, 1*ONE_ETH)125 with pytest.MonkeyPatch.context() as m:126 m.setattr(LightningNode, 'receive_funds', get_malicious_node_recieve_funds_wrong_amount_own_channel(bob.get_ip_address(), network))127 bob.send(chan_address, 1*ONE_ETH)128 129 # now bob's latest confirmation is invalid, so alice can try close with old state and bob's appeal may fail.130 alice.close_channel(chan_address, old_state)131 bob.appeal_closed_chan(chan_address)132 # Waiting133 chain.mine(APPEAL_PERIOD+2)134 # Bob Withdraws135 bob.withdraw_funds(chan_address)136 # Alice Withdraws137 alice.withdraw_funds(chan_address)138 assert chan.balance() == 0139 assert alice_init_balance == accounts[0].balance() + 3*ONE_ETH140 assert bob_init_balance == accounts[1].balance() - 3*ONE_ETH141def test_give_invalid_ack_own_channel(alice: LightningNode, bob: LightningNode, network: Network):142 alice_init_balance = accounts[0].balance()143 bob_init_balance = accounts[1].balance()144 # Creating channel145 chan_address = alice.establish_channel(146 bob.get_eth_address(), bob.get_ip_address(), 10 * ONE_ETH)147 chan = Channel.at(chan_address)148 assert chan.balance() == 10 * ONE_ETH149 # Alice sends money150 old_state = alice.get_current_channel_state(chan_address)151 alice.send(chan_address, 1*ONE_ETH)152 alice.send(chan_address, 1*ONE_ETH)153 alice.send(chan_address, 1*ONE_ETH)154 with pytest.MonkeyPatch.context() as m:155 m.setattr(LightningNode, 'receive_funds', get_malicious_node_recieve_funds_invalid_amount_own_channel(bob.get_ip_address(), network))156 bob.send(chan_address, 1*ONE_ETH)157 158 # now bob's latest confirmation is invalid, so alice can try close with old state and bob's appeal may fail.159 alice.close_channel(chan_address, old_state)160 bob.appeal_closed_chan(chan_address)161 # Waiting162 chain.mine(APPEAL_PERIOD+2)163 # Bob Withdraws164 bob.withdraw_funds(chan_address)165 # Alice Withdraws166 alice.withdraw_funds(chan_address)167 assert chan.balance() == 0168 assert alice_init_balance == accounts[0].balance() + 3*ONE_ETH169 assert bob_init_balance == accounts[1].balance() - 3*ONE_ETH170def test_notify_channel_bad_appeal_length(alice: LightningNode, bob: LightningNode, network: Network):171 network.stop() 172 # Creating channel173 with pytest.MonkeyPatch.context() as m:174 m.setattr(scripts.node, 'APPEAL_PERIOD', 1)175 chan_address = alice.establish_channel(176 bob.get_eth_address(), bob.get_ip_address(), 10 * ONE_ETH)177 network.resume()178 m.setattr(scripts.node, 'APPEAL_PERIOD', 5)179 bob.notify_of_channel(chan_address, alice.get_ip_address())180 assert chan_address not in bob.get_list_of_channels()181def test_notify_bob_of_channel_he_established(alice: LightningNode, bob: LightningNode, network: Network):182 network.stop()183 chan_address = bob.establish_channel(184 alice.get_eth_address(), alice.get_ip_address(), 10 * ONE_ETH)185 assert chan_address in bob.get_list_of_channels()186 network.resume()187 # Try to trick bob into thinking he's not the one who established the channel188 bob.notify_of_channel(chan_address, alice.get_ip_address())189 assert chan_address in bob.get_list_of_channels()190 # Now try to trick bob into thinking he's recieving money when in fact he is sending.191 bob.receive_funds(sign(ChannelStateMessage(chan_address, 5*ONE_ETH, 5 * ONE_ETH, 1), alice.get_eth_address()))192 193 # make sure bob didn't save that state message194 assert bob.get_current_channel_state(chan_address).serial_number == 0195def test_close_channel_with_modified_serial(alice: LightningNode, bob: LightningNode, network: Network):196 alice_init_balance = accounts[0].balance()197 bob_init_balance = accounts[1].balance()198 # Creating channel199 chan_address = alice.establish_channel(200 bob.get_eth_address(), bob.get_ip_address(), 10 * ONE_ETH)201 chan = Channel.at(chan_address)202 assert chan.balance() == 10 * ONE_ETH203 alice.send(chan_address, 1*ONE_ETH)204 alice.send(chan_address, 1*ONE_ETH)205 alice.send(chan_address, 1*ONE_ETH)206 state = alice.get_current_channel_state(chan_address)207 with pytest.raises(VirtualMachineError):208 # Alice tries to close channel with modified serial (improperly signed)...
test_resource_utils.py
Source:test_resource_utils.py
...38 self.assertEqual(mac_v4, '00:00:5E:00:01:99')39 # Check VRID type validation40 self.assertRaises(ValueError, resource_utils.get_vrrp_mac, None, True)41 self.assertRaises(ValueError, resource_utils.get_vrrp_mac, '11', True)42 def test_get_ip_address(self):43 vip = {44 'ipv4': {'address': '1.1.1.1'},45 'ipv6': {'virtual_ip': u'1234:5678:90ab:cdef::1'}46 }47 # Check get IPv4 address48 self.assertEqual(vip['ipv4']['address'],49 resource_utils.get_ip_address(vip, True, 'vip'))50 # Check get IPv6 address51 self.assertEqual(vip['ipv6']['virtual_ip'],52 resource_utils.get_ip_address(vip, False, 'vip'))53 # Check ip validation54 vip['ipv4']['address'] = 155 vip['ipv6']['virtual_ip'] = None56 self.assertRaises(ValueError, resource_utils.get_ip_address,57 vip, True, 'vip')58 self.assertRaises(ValueError, resource_utils.get_ip_address,59 vip, False, 'vip')60 vip['ipv4'] = None61 vip['ipv6'] = None62 self.assertRaises(ValueError, resource_utils.get_ip_address,63 vip, True, 'vip')64 self.assertRaises(ValueError, resource_utils.get_ip_address,65 vip, False, 'vip')66 vip = {}...
main.py
Source:main.py
...28 with the required parameters29 """30 excatch.socket.AF_INET = 'AF_INET'31 excatch.socket.SOCK_DGRAM = 'SOCK_DGRAM'32 excatch.Hook().get_ip_address('test')33 excatch.socket.mockCheckCall(34 1, 'socket', 'AF_INET', 'SOCK_DGRAM'35 )36 def testSocketResponse(self):37 excatch.Hook().get_ip_address('test')38 excatch.fcntl.ioctl.assert_called_with(39 excatch.socket.socket().fileno(),40 0x8915,41 excatch.struct.pack()42 )43 excatch.struct.pack.mockCheckCall()44 excatch.socket.inet_ntoa.assert_called_with(45 [20, 21, 22, 23]46 )47class AddressTest(TestCase):48 def testGetIPAddressWorking(self):49 old = excatch.Hook.get_ip_address50 excatch.Hook.get_ip_address = mock.Mock()51 excatch.Hook.get_ip_address.return_value = 'TEST'...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!