def test_stats_from_document(self): stats = Stats.from_document(FILE_A_CONTENT) assert stats.recv_bytes == 1740360955 assert stats.sent_bytes == 328481009 with pytest.raises(DocumentParseException): Stats.from_document("not a document lol")
def test_stats_from_request(self, good_response, bad_response): with patch('requests.get', return_value=good_response): stats = Stats.from_request("http://someurl.com") assert stats.recv_bytes == 1740360955 assert stats.sent_bytes == 328481009 with patch('requests.get', return_value=bad_response): with pytest.raises(StatsLookupException): Stats.from_request("http://someurl.com") with patch('requests.get', side_effect=requests.exceptions.ConnectionError()): with pytest.raises(StatsLookupException): Stats.from_request("http://someurl.com")
def test_stats_delta_after_stat_reset(self): """Sometimes the stats on the router page will reset to zero. This tests that if a newer stats object comes in that's smaller the the last one (e.g. the stats were reset), that we should just measure from a starting point of 0 instead.""" stats_delta = Stats.delta(stats_later, stats_early) assert stats_delta.recv_bytes == stats_early.recv_bytes assert stats_delta.sent_bytes == stats_early.sent_bytes
def main(config=Config(), logger=setup_logger()): if config.is_new(): logger.error( "cwrouter is stopping because you have no config! skeleton written to %s. fill in your credentials.", config.path) config.save() return ExitStatus.NO_CONFIG config.load() stat_url = config['stats_url'] try: last_stats = config.last_stats() except EmptyStatsException: last_stats = Stats(recv_bytes=0, sent_bytes=0) try: new_stats = Stats.from_request(stats_url=stat_url) except StatsLookupException as e: logger.error(e) return ExitStatus.ERROR except DocumentParseException as e: logger.error(e) return ExitStatus.ERROR delta = Stats.delta(last_stats, new_stats) try: pm = PutMetrics.build_from_creds(config['aws_access_key_id'], config['aws_secret_access_key']) pm.put(config['namespace'], delta) except PutException as e: logger.error(e) return ExitStatus.ERROR logger.info(str(delta)) config.update_last_stats(new_stats) config.save() return ExitStatus.OK
def test_stats_delta_sequenced(self): stats_delta = Stats.delta(stats_early, stats_later) assert stats_delta.recv_bytes == 7 assert stats_delta.sent_bytes == 7
def test_recv_sent_rate(self): stats = Stats(recv_bytes=1, sent_bytes=3) assert stats.recv_sent_rate == 1.0/3
def test_total_bytes(self): stats = Stats(recv_bytes=1, sent_bytes=3) assert stats.total_bytes == 4
def test_stats_raw(self): stats = Stats(recv_bytes=1, sent_bytes=3) assert stats.recv_bytes == 1 assert stats.sent_bytes == 3
def test_update_last_stats(self): stats = Stats(recv_bytes=5, sent_bytes=7) c = Config() c.update_last_stats(stats) assert c['last_stats']['recv_bytes'] == 5 assert c['last_stats']['sent_bytes'] == 7
def test_first_run_stats(self): config = Config() # has empty last stats with pytest.raises(EmptyStatsException): Stats.delta(config.last_stats(), stats_early)
import requests from cwrouter.config import Config, ensure_config_dir_exists from cwrouter.exceptions import EmptyStatsException, StatsLookupException, DocumentParseException from cwrouter.put import PutMetrics from cwrouter.stats import Stats FIXTURES_ROOT = path.join(path.abspath(path.dirname(__file__)), 'fixtures') FILE_A_PATH = path.join(FIXTURES_ROOT, 'a.html') BAD_JSON_PATH =path.join(FIXTURES_ROOT, 'bad_json') GOOD_JSON_PATH =path.join(FIXTURES_ROOT, 'good_json') with open(FILE_A_PATH) as f: FILE_A_CONTENT = f.read() stats_early = Stats(recv_bytes=3, sent_bytes=3) stats_later = Stats(recv_bytes=10, sent_bytes=10) @pytest.fixture() def good_response(): response = Mock() response.status_code = 200 response.text = FILE_A_CONTENT return response @pytest.fixture() def bad_response(): response = Mock()
def last_stats(self): return Stats(self['last_stats']['recv_bytes'], self['last_stats']['sent_bytes'])