summaryrefslogtreecommitdiff
path: root/tests/integration/test_stats.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/integration/test_stats.py')
-rw-r--r--tests/integration/test_stats.py146
1 files changed, 146 insertions, 0 deletions
diff --git a/tests/integration/test_stats.py b/tests/integration/test_stats.py
new file mode 100644
index 0000000..46ca39b
--- /dev/null
+++ b/tests/integration/test_stats.py
@@ -0,0 +1,146 @@
1"""The /stats endpoint.
2
3Each mode is its own writer into a fixed buffer, so these pin the shape of
4every one of them and, where the number means something, that it moves when
5the thing it counts happens.
6"""
7
8import re
9import unittest
10import urllib.error
11import xml.etree.ElementTree
12
13from harness import TrackerTestCase
14
15# The modes a default build answers. "tpbs" and "statedump" are full scrapes
16# and belong to test_fullscrape; "woodpeckers" sits behind a
17# feature flag this build does not set.
18MRTG_MODES = ["peer", "conn", "scrp", "udp4", "tcp4", "torr", "fscr", "completed", "syncs"]
19TEXT_MODES = ["herr", "top10", "top100", "renew"]
20
21
22class StatsShape(TrackerTestCase):
23 def test_every_mode_answers_something(self):
24 for mode in MRTG_MODES + TEXT_MODES + ["everything"]:
25 self.assertTrue(self.tracker.stats(mode), "mode %s answered empty" % mode)
26
27 def test_an_mrtg_mode_opens_with_two_numbers(self):
28 """MRTG reads the first two lines as counters and the rest as labels,
29 so the order matters to something outside this project."""
30 for mode in MRTG_MODES:
31 lines = self.tracker.stats(mode).splitlines()
32 self.assertGreaterEqual(len(lines), 2, "mode %s" % mode)
33 for line in lines[:2]:
34 self.assertRegex(line, rb"^\d+$", "mode %s line %r" % (mode, line))
35
36 def test_the_busy_mode_is_gone(self):
37 """It was listed in the mode table and implemented nowhere, so it only
38 ever answered 500. Removed rather than written: picking poisoning
39 networks out stopped being meaningful once consumer lines became bot
40 hosts and v6 made the address space too large to watch."""
41 with self.assertRaises(urllib.error.HTTPError) as caught:
42 self.tracker.stats("busy")
43 self.assertEqual(caught.exception.code, 400, "an unknown mode, like any other")
44
45 def test_an_unknown_mode_is_refused(self):
46 with self.assertRaises(urllib.error.HTTPError) as caught:
47 self.tracker.stats("nosuchmode")
48 self.assertEqual(caught.exception.code, 400)
49
50 def test_everything_is_well_formed_xml(self):
51 root = xml.etree.ElementTree.fromstring(self.tracker.stats("everything").decode())
52 self.assertEqual(root.tag, "stats")
53 self.assertIsNotNone(root.find("torrents/count_mutex"))
54 self.assertIsNotNone(root.find("connections/tcp/announce"))
55
56 def test_the_version_mode_is_gone(self):
57 """Which build a tracker is and what went into it is of use to whoever
58 attacks it and to nobody else on the far end of a socket. It is
59 reported by -v, to whoever already has the binary."""
60 with self.assertRaises(urllib.error.HTTPError) as caught:
61 self.tracker.stats("version")
62 self.assertEqual(caught.exception.code, 400, "an unknown mode, like any other")
63
64 def test_everything_does_not_name_the_build(self):
65 self.assertNotIn(b"opentracker 1", self.tracker.stats("everything"))
66
67 def test_everything_does_not_name_the_tracker_id(self):
68 """The id a tracker recognises its own live sync packets by. Knowing
69 it is of no use to a client and of some use to whoever would like a
70 tracker to discard what it is told."""
71 self.assertNotIn(b"tracker_id", self.tracker.stats("everything"))
72
73
74class StatsCount(TrackerTestCase):
75 def test_the_torrent_count_follows_the_torrents(self):
76 before = self.tracker.torrent_count()
77 self.tracker.announce(self.hash_for("statA"), port=6001, left=0)
78 self.assertEqual(self.tracker.torrent_count(), before + 1)
79 self.tracker.announce(self.hash_for("statB"), port=6002, left=0)
80 self.assertEqual(self.tracker.torrent_count(), before + 2)
81
82 def test_the_peer_count_separates_seeders_from_the_rest(self):
83 self.tracker.announce(self.hash_for("statpeers"), port=6001, left=0)
84 self.tracker.announce(self.hash_for("statpeers"), port=6002, left=4096)
85 peers, seeds = self.tracker.stats("peer").splitlines()[:2]
86 self.assertEqual(int(peers), 2)
87 self.assertEqual(int(seeds), 1)
88
89 def test_the_completed_count_follows_the_completed_event(self):
90 before = int(self.tracker.stats("completed").splitlines()[0])
91 self.tracker.announce(self.hash_for("statdone"), port=6001, left=0, event="completed")
92 self.assertEqual(int(self.tracker.stats("completed").splitlines()[0]), before + 1)
93
94 def test_a_bad_request_is_counted_as_an_http_error(self):
95 before = self.tracker.stats("herr")
96 try:
97 self.tracker.stats("nosuchmode")
98 except urllib.error.HTTPError:
99 pass
100 after = self.tracker.stats("herr")
101 self.assertNotEqual(before, after, "no error counter moved")
102
103 def test_the_tcp_counter_follows_tcp_requests(self):
104 before = int(self.tracker.stats("tcp4").splitlines()[0])
105 for _ in range(3):
106 self.tracker.announce(self.hash_for("stattcp"), port=6001, left=0)
107 self.assertGreaterEqual(int(self.tracker.stats("tcp4").splitlines()[0]), before + 3)
108
109 def test_the_udp_counter_follows_udp_requests(self):
110 before = int(self.tracker.stats("udp4").splitlines()[0])
111 sock, connection_id = self.tracker.udp_connect()
112 try:
113 self.tracker.udp_announce(sock, connection_id, self.hash_for("statudp"), left=0)
114 finally:
115 sock.close()
116 self.assertGreater(int(self.tracker.stats("udp4").splitlines()[0]), before)
117
118 def test_the_scrape_counter_follows_scrapes(self):
119 before = int(self.tracker.stats("scrp").splitlines()[0])
120 self.tracker.scrape(self.hash_for("statscrape"))
121 self.assertEqual(int(self.tracker.stats("scrp").splitlines()[0]), before + 1)
122
123
124class StatsTop(TrackerTestCase):
125 def test_the_top_list_ranks_by_peer_count(self):
126 busy = self.hash_for("topbusy")
127 quiet = self.hash_for("topquiet")
128 for port in range(6001, 6006):
129 self.tracker.announce(busy, port=port, left=0)
130 self.tracker.announce(quiet, port=6100, left=0)
131
132 body = self.tracker.stats("top10")
133 self.assertIn(b"Top 10 torrents by peers", body)
134 ranked = re.findall(rb"\t(\d+)\t([0-9A-F]{40})", body.split(b"by seeds")[0])
135 self.assertEqual(ranked[0][1], busy.hex().upper().encode(), "the busy torrent ranks first")
136 self.assertEqual(int(ranked[0][0]), 5)
137
138 def test_the_top_list_has_three_sections(self):
139 self.tracker.announce(self.hash_for("topsections"), port=6001, left=0)
140 body = self.tracker.stats("top100")
141 for heading in (b"by peers", b"by seeds", b"by leechers"):
142 self.assertIn(heading, body)
143
144
145if __name__ == "__main__":
146 unittest.main()