Skip to content

Instantly share code, notes, and snippets.

View cgoldberg's full-sized avatar
👋
¯\_(ツ)_/¯

Corey Goldberg cgoldberg

👋
¯\_(ツ)_/¯
View GitHub Profile
@cgoldberg
cgoldberg / got_root.py
Created November 1, 2015 00:04
check if the process is running as root (Unix/Linux)
import os
def got_root():
"""check if we are running as root."""
if os.geteuid() == 0:
return True
return False
@cgoldberg
cgoldberg / bounce_services.sh
Created October 5, 2015 16:54
bounce supervisor controlled services
#!/bin/bash
# Bounce services and print start/stop messages to console and /var/log/syslog.
logger -s Restarting all processes controlled by supervisor now...
logger -s $(/usr/bin/supervisorctl restart all)
# Scheduled by system cron to run every hour:
# 0 */1 * * * /deploy/bounce_services.sh
@cgoldberg
cgoldberg / testing_databases.md
Last active September 28, 2015 18:43
Functional Tests - Database Setup

SQLite as a test database?

One way to create isolate integration tests is to tear down and recreate database tables for every single test. This ensures every test starts with a clean and known state. However, achieving this with MySQL is an expensive (slow) operation and would add too much execution time between tests.

Tracelons uses SQLAlchemy as an ORM. Since the MySQL database is abtracted behind this layer, we could switch test database providers to SQLite. SQLite can be run in-memory, allowing fast teardowns of the test database. Coupled with fast schema creation, this enables very fast database operations during test runs.

However, there are some drawbacks to using SQLite as a replacement for MySQL during testing:

  • Some tooling in the tracelons repo is built specifically for MySQL and would require changes to work with SQLite.
  • SQLite does not have the same features, and does not enforce the same rules as MySQL. Therefore, it differs from production and could possibly allow tests to pass t
@cgoldberg
cgoldberg / recursive_file_info.py
Created June 20, 2015 10:18
walk a directory tree recursively. print total file count and size.
#!/usr/bin/env python
#
# walk a directory tree recursively.
# print total file count and size.
# Corey Goldberg, 2015
import os
start_dir = '/mnt/wd-green/Tunes'
@cgoldberg
cgoldberg / grab-sessionid.py
Created May 11, 2015 16:17
grab session id from edX cookie
def login(email, password, base_url='https://courses.edx.org'):
"""Login via HTTP and parse sessionid from the cookie."""
r = requests.get('{}/login'.format(base_url))
csrf = r.cookies['csrftoken']
payload = {'email': email, 'password': password}
cookies = {'csrftoken': csrf}
headers = {'referer': '{}/login'.format(base_url), 'X-CSRFToken': csrf}
r = requests.post('{}/user_api/v1/account/login_session/'.format(base_url),
data=payload, cookies=cookies, headers=headers)
try:
@cgoldberg
cgoldberg / mhtest.py
Created April 14, 2015 15:01
parse onload timings and weight with Cochran–Mantel–Haenszel
#!/usr/bin/env python
import collections
import re
from operator import itemgetter
import urlparse
import numpy
import sys
@cgoldberg
cgoldberg / parse-onloads.py
Created April 9, 2015 18:34
analyze top slowest pages using onload event beacon data
#!/usr/bin/env python
import collections
import re
from operator import itemgetter
import numpy
DATA_FILE = 'perflog-everything-onload.csv'
NUM_RESULTS = 50
@cgoldberg
cgoldberg / metric_to_graphite.js
Last active July 20, 2023 10:12
send metric data to hosted graphite via HTTP POST.
/* send a metric to hosted graphite via HTTP POST (async). */
function sendMetricToGraphite(metricName, value) {
var apiKey = YOUR-API-KEY;
var url = "https://" + apiKey + "@www.hostedgraphite.com/api/v1/sink";
var request = new XMLHttpRequest();
request.open("POST", url, true);
request.send(metricName + " " + value);
};
@cgoldberg
cgoldberg / testtools_add_details.py
Created July 7, 2014 16:25
testtools - adding test details (content objects) to your tests
#!/usr/bin/env python3
#
# This example adds testtools Details from a testtools TestCase.
#
# For more information about: testtools, Details, and Content Objects, visit:
# * https://testtools.readthedocs.org/en/latest/for-test-authors.html#details
from testtools import TestCase
from testtools.content import (
ContentType,
@cgoldberg
cgoldberg / subunit_save_details.py
Last active August 29, 2015 14:02
subunit - save arbitrary test result details to files
#!/usr/bin/env python3
import logging
import re
from subunit import ByteStreamToStreamResult
from testtools import StreamToExtendedDecorator, TestResult
logging.basicConfig(level='INFO')