-
Notifications
You must be signed in to change notification settings - Fork 679
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chaos Monkey now messes up with network (#1514)
* Chaos Monkey now messes up with network * Fixing a typo * Adding missing shell command * Marking `local_network` monkey as existing * Adding one of the new tests that was previously missing
- Loading branch information
1 parent
864d6c9
commit f3c7238
Showing
5 changed files
with
171 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,6 @@ tmp/ | |
|
||
# Logs | ||
*.log | ||
|
||
# Vim tmp files | ||
*.swp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import subprocess, sys | ||
|
||
def _run_process(cmd): | ||
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
out, err = process.communicate() | ||
return (process.returncode, out, err) | ||
|
||
def init_network_pillager(): | ||
_run_process(["mkdir", "/sys/fs/cgroup/net_cls/block"]) | ||
try: | ||
with open("/sys/fs/cgroup/net_cls/block/net_cls.classid", 'w') as f: | ||
f.write("42") | ||
except IOError as e: | ||
if e[0] == 13: | ||
print("Failed to modify `/sys/fs/cgroup/net_cls/block/net_cls.classid`.") | ||
print("Make sure the current user has access to it, e.g. by changing the owner:") | ||
print("") | ||
print(" chown <group>.<user> /sys/fs/cgroup/net_cls/block/net_cls.classid") | ||
print("") | ||
sys.exit(1) | ||
_run_process(["iptables", "-A", "OUTPUT", "-m", "cgroup", "--cgroup", "42", "-j", "DROP"]) | ||
|
||
def stop_network(pid): | ||
with open('/sys/fs/cgroup/net_cls/block/tasks', 'w') as f: | ||
f.write(str(pid)) | ||
|
||
def resume_network(pid): | ||
with open('/sys/fs/cgroup/net_cls/tasks', 'w') as f: | ||
f.write(str(pid)) | ||
|
||
if __name__ == "__main__": | ||
import time | ||
init_network_pillager() | ||
handle = subprocess.Popen(["ping", "8.8.8.8"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
print(handle.pid) | ||
time.sleep(3) | ||
stop_network(handle.pid) | ||
time.sleep(3) | ||
resume_network(handle.pid) | ||
time.sleep(3) | ||
handle.kill() | ||
out, err = handle.communicate() | ||
print("STDOUT (expect ~6 entries if all goes well):") | ||
print(out) | ||
print("STDERR (expect ~3 entries if all goes well):") | ||
print(err) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import sys, random, time, base58, requests | ||
|
||
sys.path.append('lib') | ||
|
||
from stress import stress_process, doit, monkey_staking, get_validator_ids, get_the_guy_to_mess_up_with, get_recent_hash, sign_payment_tx, expect_network_issues | ||
from network import stop_network, resume_network, init_network_pillager | ||
|
||
TIMEOUT = 300 | ||
|
||
@stress_process | ||
def monkey_transactions_noval(stopped, error, nodes, nonces): | ||
while stopped.value == 0: | ||
validator_ids = get_validator_ids(nodes) | ||
|
||
from_ = random.randint(0, len(nodes) - 1) | ||
to = random.randint(0, len(nodes) - 1) | ||
while from_ == to: | ||
to = random.randint(0, len(nodes) - 1) | ||
amt = random.randint(0, 100) | ||
nonce_val, nonce_lock = nonces[from_] | ||
|
||
hash_, _ = get_recent_hash(nodes[-1]) | ||
|
||
with nonce_lock: | ||
tx = sign_payment_tx(nodes[from_].signer_key, 'test%s' % to, amt, nonce_val.value, base58.b58decode(hash_.encode('utf8'))) | ||
for validator_id in validator_ids: | ||
try: | ||
tx_hash = nodes[validator_id].send_tx(tx)['result'] | ||
except requests.exceptions.ReadTimeout: | ||
pass | ||
|
||
nonce_val.value = nonce_val.value + 1 | ||
|
||
time.sleep(0.1) | ||
|
||
@stress_process | ||
def monkey_network_hammering(stopped, error, nodes, nonces): | ||
s = [False for x in nodes] | ||
while stopped.value == 0: | ||
node_idx = random.randint(0, len(nodes) - 2) | ||
pid = nodes[node_idx].handle.pid | ||
if s[node_idx]: | ||
print("Resuming network for process %s" % pid) | ||
resume_network(pid) | ||
else: | ||
print("Stopping network for process %s" % pid) | ||
stop_network(pid) | ||
s[node_idx] = not s[node_idx] | ||
|
||
time.sleep(0.5) | ||
for i, x in enumerate(s): | ||
if x: | ||
pid = nodes[i].handle.pid | ||
print("Resuming network for process %s" % pid) | ||
resume_network(pid) | ||
|
||
|
||
expect_network_issues() | ||
init_network_pillager() | ||
doit(3, 3, 3, 0, [monkey_network_hammering, monkey_transactions_noval, monkey_staking], TIMEOUT) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters