lxd: Increase disk size of an existing container
lxc config device override mycontainer root size=20GB
Restart the container to apply changes.
lxc config device override mycontainer root size=20GB
Restart the container to apply changes.
CPU benchmark from Dell Precision 15 5510 Core-i5 i5-6440HQ
$ sysbench cpu run --time=5
sysbench 1.0.9 (using system LuaJIT 2.1.0-beta3)
Running the test with following options:
Number of threads: 1
Initializing random number generator from current time
Prime numbers limit: 10000
Initializing worker threads...
Threads started!
CPU speed:
events per second: 1157.88
General statistics:
total time: 5.0008s
total number of events: 5792
Latency (ms):
min: 0.80
avg: 0.86
max: 3.74
95th percentile: 1.06
sum: 4998.26
Threads fairness:
events (avg/stddev): 5792.0000/0.00
execution time (avg/stddev): 4.9983/0.00
To list ip addresses of all connected to the network devices, use this command:
nmap -sn 192.168.0.0/24
Django-like testrunner for non-django projects:
#!/usr/bin/env python
import argparse
import collections
import unittest
import sys
def main():
parser = argparse.ArgumentParser(description='Django-like testrunner')
parser.add_argument('specific_test', metavar='', type=str, nargs='?', help='Specifc test')
parser.add_argument("--failfast", action="store_true")
parser.add_argument("--verbosity", type=int, default=1)
args = parser.parse_args()
loader = unittest.TestLoader()
all_tests = loader.discover('.', top_level_dir="./")
suite = unittest.TestSuite()
if args.specific_test:
def walk_tests(tests):
if isinstance(tests, collections.Iterable):
for item in tests:
walk_tests(item)
return
if tests.id().startswith(args.specific_test):
suite.addTest(tests)
elif not str(tests).startswith("test"):
sys.exit("Error in file %s" % tests)
walk_tests(all_tests)
else:
suite.addTests(all_tests)
unittest.TextTestRunner(verbosity=args.verbosity, failfast=args.failfast).run(suite)
if __name__ == "__main__":
main()
Command runuser allows to run a command as a different user. You must be root to be able to run that command:
sudo runuser -l vagrant -c "GH=1 env"
Run python script with mitmproxy:
mitmproxy --ignore ws.sitesupport.net:443 -s script.py
Script example:
from mitmproxy.script import concurrent
OLD = """var totalExpGems=0;"""
NEW = """var totalExpGems=0;debugger;"""
@concurrent
def response(flow):
if "gem_finder" in flow.request.path:
flow.response.headers["XX"] = "PATCHED"
body = flow.response.content.decode("utf-8")
if OLD in body:
flow.response.content = body.replace(OLD, NEW).encode("utf-8")
flow.response.headers["XXX"] = "PATCHED"
To use mitmproxy with Epiphany you need to install CA certificate from mitmproxy. To do that, start mitmproxy and navigate to mitm.it. Download *.pem certificate (available under Other link). Install mitmproxy certificate with command:
sudo trust anchor mitmproxy-ca-cert.pem
With this command you can also install any custom certificate to use in Epiphany.
Use this command to print size of the subfolders:
du -sh *
If you want to exclude Permission denied errors, for example:
du -sh * 2>/dev/null
When writing tests for the Django’s view functions I prefer to use RequestFactory instead of the django test client. Tests that use RequestFactory are easier to understand and they are faster. For example, you need to test a simple view function:
def incoming_queue(request):
if request.method == "POST":
pass
else:
incoming_queue_list = UsageHistory.objects.filter(company=request.user.agent.company, queued=True)
context = {
"incoming_queue_list": incoming_queue_list,
"page": "queue",
"agent_available_url": reverse("agent_available")
}
return render(request, 'agents/incoming_queue.html', context)
If you want to test the context of the rendered template, it is easy to do with django test client as it attaches it to the response. If you want to use RequestFactory for that purpose, there is no easy way to do it. It would be comfortable to have something like assertTemplateContextIn(expected_context) method, that checks that expected_context was used during the template rendering. The following code was inspired by assertTemplateUsed method:
from copy import copy
from django.db.models.query import QuerySet
from django.test.signals import template_rendered
from django.test.utils import ContextList
class TemplateContextTestCase(object):
def assertTemplateContextIn(self, expected_context=None):
"""
Asserts that the template was rendered with the context that includes
expected_context. Example:
with self.assertTemplateContextIn(expected_context):
vuew_function(self.request)
"""
return _AssertTemplateContext(self, expected_context)
class _AssertTemplateContext(object):
def __init__(self, test_case, expected_context):
self.test_case = test_case
self.expected_context = expected_context
self.context = ContextList()
def on_template_render(self, sender, signal, template, context, **kwargs):
self.context.append(copy(context))
def test(self):
missing = []
mismatched = []
for key in self.expected_context.keys():
if key in self.context:
if self.context[key] != self.expected_context[key]:
match = False
# Compare QuerySets
if isinstance(self.context[key], QuerySet) and isinstance(self.expected_context[key], QuerySet):
if self.context[key].model == self.expected_context[key].model:
if list(self.context[key].values_list("id", flat=True)) == \
list(self.expected_context[key].values_list("id", flat=True)):
match = True
if not match:
mismatched.append(key)
else:
missing.append(key)
return missing, mismatched
def __enter__(self):
template_rendered.connect(self.on_template_render)
return self
def __exit__(self, exc_type, exc_value, traceback):
template_rendered.disconnect(self.on_template_render)
if exc_type is not None:
return
missing, mismatched = self.test()
message = ""
if missing:
message = message + "Missing keys: " + ", ".join(x for x in missing) + "\n"
if mismatched:
for item in mismatched:
message = message + "Context key mismatch '%s': %s != %s \n" % (
item, self.expected_context[item], self.context[item]
)
if message:
self.test_case.fail(message)
We often pass querysets instances to the context of the rendered template. The code above checks if 2 QuerySets are the same by comparing their models and ids of the elements. TemplateContextTestCase class can be used to extend TestCase class:
from django.test import TestCase, RequestFactory
from yourapp.tests.helpers import TemplateContextTestCase
from django.core.urlresolvers import reverse
class TestIncomingQueue(TestCase, TemplateContextTestCase):
def test_context(self):
agent = AgentFactory()
self.request.user = agent.user
expected = {
"page": "queue",
"agent_available_url": reverse("agent_available"),
"incoming_queue_list": UsageHistory.objects.none()
}
with self.assertTemplateContextIn(expected):
incoming_queue(self.request)