source: trunk/src/allmydata/test/web/test_status.py

Last change on this file was 1cfe843d, checked in by Alexandre Detiste <alexandre.detiste@…>, at 2024-02-22T23:40:25Z

more python2 removal

  • Property mode set to 100644
File size: 6.4 KB
Line 
1"""
2Tests for ```allmydata.web.status```.
3
4Ported to Python 3.
5"""
6
7from bs4 import BeautifulSoup
8from twisted.web.template import flattenString
9
10from allmydata.web.status import (
11    Status,
12    StatusElement,
13)
14
15from zope.interface import implementer
16
17from allmydata.interfaces import IDownloadResults
18from allmydata.web.status import DownloadStatusElement
19from allmydata.immutable.downloader.status import DownloadStatus
20
21from .common import (
22    assert_soup_has_favicon,
23    assert_soup_has_tag_with_content,
24)
25from ..common import TrialTestCase
26
27from .test_web import FakeHistory
28
29# Test that status.StatusElement can render HTML.
30class StatusTests(TrialTestCase):
31
32    def _render_status_page(self, active, recent):
33        elem = StatusElement(active, recent)
34        d = flattenString(None, elem)
35        return self.successResultOf(d)
36
37    def test_status_page(self):
38        status = Status(FakeHistory())
39        doc = self._render_status_page(
40            status._get_active_operations(),
41            status._get_recent_operations()
42        )
43        soup = BeautifulSoup(doc, 'html5lib')
44
45        assert_soup_has_favicon(self, soup)
46
47        assert_soup_has_tag_with_content(
48            self, soup, u"title",
49            u"Tahoe-LAFS - Recent and Active Operations"
50        )
51
52        assert_soup_has_tag_with_content(
53            self, soup, u"h2",
54            u"Active Operations:"
55        )
56
57        assert_soup_has_tag_with_content(
58            self, soup, u"td",
59            u"retrieve"
60        )
61
62        assert_soup_has_tag_with_content(
63            self, soup, u"td",
64            u"publish"
65        )
66
67        assert_soup_has_tag_with_content(
68            self, soup, u"td",
69            u"download"
70        )
71
72        assert_soup_has_tag_with_content(
73            self, soup, u"td",
74            u"upload"
75        )
76
77        assert_soup_has_tag_with_content(
78            self, soup, u"h2",
79            "Recent Operations:"
80        )
81
82
83@implementer(IDownloadResults)
84class FakeDownloadResults(object):
85
86    def __init__(self,
87                 file_size=0,
88                 servers_used=None,
89                 server_problems=None,
90                 servermap=None,
91                 timings=None):
92        """
93        See IDownloadResults for parameters.
94        """
95        self.file_size = file_size
96        self.servers_used = servers_used
97        self.server_problems = server_problems
98        self.servermap = servermap
99        self.timings = timings
100
101
102class FakeDownloadStatus(DownloadStatus):
103
104    def __init__(self,
105                 storage_index = None,
106                 file_size = 0,
107                 servers_used = None,
108                 server_problems = None,
109                 servermap = None,
110                 timings = None):
111        """
112        See IDownloadStatus and IDownloadResults for parameters.
113        """
114        super(FakeDownloadStatus, self).__init__(storage_index, file_size)
115
116        self.servers_used = servers_used
117        self.server_problems = server_problems
118        self.servermap = servermap
119        self.timings = timings
120
121    def get_results(self):
122        return FakeDownloadResults(self.size,
123                                   self.servers_used,
124                                   self.server_problems,
125                                   self.servermap,
126                                   self.timings)
127
128
129class DownloadStatusElementTests(TrialTestCase):
130    """
131    Tests for ```allmydata.web.status.DownloadStatusElement```.
132    """
133
134    def _render_download_status_element(self, status):
135        """
136        :param IDownloadStatus status:
137        :return: HTML string rendered by DownloadStatusElement
138        """
139        elem = DownloadStatusElement(status)
140        d = flattenString(None, elem)
141        return self.successResultOf(d)
142
143    def test_download_status_element(self):
144        """
145        See if we can render the page almost fully.
146        """
147        status = FakeDownloadStatus(
148            b"si-1", 123,
149            [b"s-1", b"s-2", b"s-3"],
150            {b"s-1": "unknown problem"},
151            {b"s-1": [1], b"s-2": [1,2], b"s-3": [2,3]},
152            {"fetch_per_server":
153             {b"s-1": [1], b"s-2": [2,3], b"s-3": [3,2]}}
154        )
155
156        result = self._render_download_status_element(status)
157        soup = BeautifulSoup(result, 'html5lib')
158
159        assert_soup_has_favicon(self, soup)
160
161        assert_soup_has_tag_with_content(
162            self, soup, u"title", u"Tahoe-LAFS - File Download Status"
163        )
164
165        assert_soup_has_tag_with_content(
166            self, soup, u"li", u"File Size: 123 bytes"
167        )
168        assert_soup_has_tag_with_content(
169            self, soup, u"li", u"Progress: 0.0%"
170        )
171
172        assert_soup_has_tag_with_content(
173            self, soup, u"li", u"Servers Used: [omwtc], [omwte], [omwtg]"
174        )
175
176        assert_soup_has_tag_with_content(
177            self, soup, u"li", u"Server Problems:"
178        )
179
180        assert_soup_has_tag_with_content(
181            self, soup, u"li", u"[omwtc]: unknown problem"
182        )
183
184        assert_soup_has_tag_with_content(self, soup, u"li", u"Servermap:")
185
186        assert_soup_has_tag_with_content(
187            self, soup, u"li", u"[omwtc] has share: #1"
188        )
189
190        assert_soup_has_tag_with_content(
191            self, soup, u"li", u"[omwte] has shares: #1,#2"
192        )
193
194        assert_soup_has_tag_with_content(
195            self, soup, u"li", u"[omwtg] has shares: #2,#3"
196        )
197
198        assert_soup_has_tag_with_content(
199            self, soup, u"li", u"Per-Server Segment Fetch Response Times:"
200        )
201
202        assert_soup_has_tag_with_content(
203            self, soup, u"li", u"[omwtc]: 1.00s"
204        )
205
206        assert_soup_has_tag_with_content(
207            self, soup, u"li", u"[omwte]: 2.00s, 3.00s"
208        )
209
210        assert_soup_has_tag_with_content(
211            self, soup, u"li", u"[omwtg]: 3.00s, 2.00s"
212        )
213
214    def test_download_status_element_partial(self):
215        """
216        See if we can render the page with incomplete download status.
217        """
218        status = FakeDownloadStatus()
219        result = self._render_download_status_element(status)
220        soup = BeautifulSoup(result, 'html5lib')
221
222        assert_soup_has_tag_with_content(
223            self, soup, u"li", u"Servermap: None"
224        )
225
226        assert_soup_has_tag_with_content(
227            self, soup, u"li", u"File Size: 0 bytes"
228        )
229
230        assert_soup_has_tag_with_content(
231            self, soup, u"li", u"Total: None (None)"
232        )
Note: See TracBrowser for help on using the repository browser.