Exemple #1
0
def test_simple_download_fails_retried_succeeds(randbytes):
    "When a simple download's GET fails with a 500, it is retried successfully"
    attempts = 0
    data = randbytes(1024)

    class Server(httptest.Handler):
        def do_GET(self):
            nonlocal attempts
            attempts += 1
            if attempts > 2:
                self.send_response(200)
                self.send_header('content-type', 'text/plain')
                self.send_header('content-length', str(len(data)))
                self.end_headers()
                self.wfile.write(data)
            else:
                self.send_response(500)
                self.end_headers()
                self.wfile.write(b'uhoh')

    with httptest.Server(Server) as ts:
        objectService = FakeObject(ts)
        buf, content_type = download.downloadToBuf(
            name="some/object",
            objectService=objectService)

    assert attempts == 3
    assert buf == data
    assert content_type == 'text/plain'
def test_simple_download_fails():
    "When a simple download's GET fails with a 400, an exception is raised and no retries occur"
    getcount = 0

    class Server(httptest.Handler):
        def do_GET(self):
            nonlocal getcount
            getcount += 1
            self.send_response(400)
            self.end_headers()
            self.wfile.write(b'uhoh')

    with httptest.Server(Server) as ts:
        objectService = FakeObject(ts)
        with pytest.raises(requests.RequestException):
            download.downloadToBuf(name="some/object",
                                   objectService=objectService)
        assert getcount == 1
def test_simple_download_fails_retried():
    "When a simple download's GET fails with a 500, an exception is raised after five retries"
    attempts = 0

    class Server(httptest.Handler):
        def do_GET(self):
            nonlocal attempts
            attempts += 1
            self.send_response(500)
            self.end_headers()
            self.wfile.write(b'uhoh')

    with httptest.Server(Server) as ts:
        objectService = FakeObject(ts)
        with pytest.raises(requests.RequestException):
            download.downloadToBuf(name="some/object",
                                   objectService=objectService)

    assert attempts == 6  # one try plus five retries
def do_over_wire_buf_test(data, objectService):
    """We can upload a small amount of literal data"""
    name = f"taskcluster/test/client-py/{taskcluster.slugid.v4()}"
    upload.upload_from_buf(
        projectId="taskcluster",
        name=name,
        contentType="text/plain",
        contentLength=len(data),
        expires=taskcluster.fromNow('1 hour'),
        data=data,
        objectService=objectService)

    got, contentType = download.downloadToBuf(
        name=name,
        objectService=objectService)

    assert got == data
    assert contentType == 'text/plain'