Ejemplo n.º 1
0
def test_tickets_extend_no_ticket_id(fake_time):
    ticket = testutils.create_ticket()
    auth.add(ticket)
    prev_ticket = auth.get(ticket["uuid"]).info()
    body = json.dumps({"timeout": 300})
    res = http.unix_request(config.tickets.socket, "PATCH", "/tickets/", body)
    cur_ticket = auth.get(ticket["uuid"]).info()
    assert res.status == 400
    assert cur_ticket == prev_ticket
Ejemplo n.º 2
0
def test_tickets_extend_no_ticket_id(fake_time):
    ticket = testutils.create_ticket()
    auth.add(ticket)
    prev_ticket = auth.get(ticket["uuid"]).info()
    body = json.dumps({"timeout": 300})
    res = http.unix_request(config.tickets.socket, "PATCH", "/tickets/", body)
    cur_ticket = auth.get(ticket["uuid"]).info()
    assert res.status == 400
    assert cur_ticket == prev_ticket
Ejemplo n.º 3
0
def test_tickets_idle_time_inactive(fake_time):
    ticket = testutils.create_ticket()
    auth.add(ticket)

    # Ticket idle time starts with ticket is added.
    assert auth.get(ticket["uuid"]).idle_time == 0

    # Simulate time passing without any request.
    fake_time.now += 200
    assert auth.get(ticket["uuid"]).idle_time == 200
Ejemplo n.º 4
0
def test_tickets_extend_invalid_json(fake_time):
    ticket = testutils.create_ticket()
    auth.add(ticket)
    prev_ticket = auth.get(ticket["uuid"]).info()
    res = http.unix_request(
        config.tickets.socket, "PATCH", "/tickets/%(uuid)s" % ticket,
        "{invalid}")
    cur_ticket = auth.get(ticket["uuid"]).info()
    assert res.status == 400
    assert cur_ticket == prev_ticket
Ejemplo n.º 5
0
def test_tickets_extend_invalid_json(fake_time):
    ticket = testutils.create_ticket()
    auth.add(ticket)
    prev_ticket = auth.get(ticket["uuid"]).info()
    res = http.unix_request(
        config.tickets.socket, "PATCH", "/tickets/%(uuid)s" % ticket,
        "{invalid}")
    cur_ticket = auth.get(ticket["uuid"]).info()
    assert res.status == 400
    assert cur_ticket == prev_ticket
Ejemplo n.º 6
0
def test_tickets_idle_time_inactive(fake_time):
    ticket = testutils.create_ticket()
    auth.add(ticket)

    # Ticket idle time starts with ticket is added.
    assert auth.get(ticket["uuid"]).idle_time == 0

    # Simulate time passing without any request.
    fake_time.now += 200
    assert auth.get(ticket["uuid"]).idle_time == 200
Ejemplo n.º 7
0
def test_images_options_ticket_expired(fake_time):
    ticket = testutils.create_ticket(timeout=300)
    auth.add(ticket)
    server_ticket = auth.get(ticket["uuid"]).info()
    assert server_ticket["expires"] == 300

    # Make the ticket expire
    fake_time.now += 300
    res = http.options("/images/" + ticket["uuid"])
    assert res.status == 403

    server_ticket = auth.get(ticket["uuid"]).info()
    assert server_ticket["expires"] == 300
Ejemplo n.º 8
0
def test_tickets_extend_expired_ticket(fake_time):
    ticket = testutils.create_ticket()
    auth.add(ticket)
    # Make the ticket expire.
    fake_time.now += 500
    server_ticket = auth.get(ticket["uuid"]).info()
    # Extend the expired ticket.
    body = json.dumps({"timeout": 300})
    res = http.unix_request(
        config.tickets.socket, "PATCH", "/tickets/%(uuid)s" % ticket, body)
    assert res.status == 200
    server_ticket = auth.get(ticket["uuid"]).info()
    assert server_ticket["expires"] == 800
Ejemplo n.º 9
0
def test_tickets_extend_expired_ticket(fake_time):
    ticket = testutils.create_ticket()
    auth.add(ticket)
    # Make the ticket expire.
    fake_time.now += 500
    server_ticket = auth.get(ticket["uuid"]).info()
    # Extend the expired ticket.
    body = json.dumps({"timeout": 300})
    res = http.unix_request(
        config.tickets.socket, "PATCH", "/tickets/%(uuid)s" % ticket, body)
    assert res.status == 200
    server_ticket = auth.get(ticket["uuid"]).info()
    assert server_ticket["expires"] == 800
Ejemplo n.º 10
0
def test_images_options_ticket_expired(fake_time):
    ticket = testutils.create_ticket(timeout=300)
    auth.add(ticket)
    server_ticket = auth.get(ticket["uuid"]).info()
    assert server_ticket["expires"] == 300

    # Make the ticket expire
    fake_time.now += 300
    res = http.options("/images/" + ticket["uuid"])
    assert res.status == 403

    server_ticket = auth.get(ticket["uuid"]).info()
    assert server_ticket["expires"] == 300
Ejemplo n.º 11
0
def test_tickets_idle_time_options(fake_time):
    ticket = testutils.create_ticket(url="file:///no/such/file")
    auth.add(ticket)

    # Request must reset idle time.
    fake_time.now += 200
    http.options("/images/" + ticket["uuid"])
    assert auth.get(ticket["uuid"]).idle_time == 0
Ejemplo n.º 12
0
def test_images_options_extends_ticket(fake_time):
    ticket = testutils.create_ticket()
    auth.add(ticket)
    server_ticket = auth.get(ticket["uuid"]).info()
    assert server_ticket["expires"] == 300

    fake_time.now += 200
    res = http.options("/images/" + ticket["uuid"])
    assert res.status == 200

    res.read()

    # Yield to server thread - will close the opreration and extend the ticket.
    time.sleep(0.1)

    server_ticket = auth.get(ticket["uuid"]).info()
    assert server_ticket["expires"] == 500
Ejemplo n.º 13
0
def test_images_options_extends_ticket(fake_time):
    ticket = testutils.create_ticket()
    auth.add(ticket)
    server_ticket = auth.get(ticket["uuid"]).info()
    assert server_ticket["expires"] == 300

    fake_time.now += 200
    res = http.options("/images/" + ticket["uuid"])
    assert res.status == 200

    res.read()

    # Yield to server thread - will close the opreration and extend the ticket.
    time.sleep(0.1)

    server_ticket = auth.get(ticket["uuid"]).info()
    assert server_ticket["expires"] == 500
Ejemplo n.º 14
0
def test_tickets_idle_time_options(fake_time):
    ticket = testutils.create_ticket(url="file:///no/such/file")
    auth.add(ticket)

    # Request must reset idle time.
    fake_time.now += 200
    http.options("/images/" + ticket["uuid"])
    assert auth.get(ticket["uuid"]).idle_time == 0
Ejemplo n.º 15
0
def test_tickets_idle_time_get(fake_time, tmpdir):
    image = testutils.create_tempfile(tmpdir, "image", b"a" * 8192)
    ticket = testutils.create_ticket(url="file://" + str(image))
    auth.add(ticket)

    # Request must reset idle time.
    fake_time.now += 200
    http.get("/images/" + ticket["uuid"])
    assert auth.get(ticket["uuid"]).idle_time == 0
Ejemplo n.º 16
0
def test_images_upload_extends_ticket(tmpdir, fake_time):
    image = testutils.create_tempfile(tmpdir, "image", b"before")
    ticket = testutils.create_ticket(url="file://" + str(image))
    auth.add(ticket)
    server_ticket = auth.get(ticket["uuid"]).info()
    assert server_ticket["expires"] == 300

    fake_time.now += 200
    res = http.put("/images/" + ticket["uuid"], "")
    assert res.status == 200

    res.read()

    # Yield to server thread - will close the opreration and extend the ticket.
    time.sleep(0.1)

    server_ticket = auth.get(ticket["uuid"]).info()
    assert server_ticket["expires"] == 500
Ejemplo n.º 17
0
def test_images_upload_extends_ticket(tmpdir, fake_time):
    image = testutils.create_tempfile(tmpdir, "image", "before")
    ticket = testutils.create_ticket(url="file://" + str(image))
    auth.add(ticket)
    server_ticket = auth.get(ticket["uuid"]).info()
    assert server_ticket["expires"] == 300

    fake_time.now += 200
    res = http.put("/images/" + ticket["uuid"], "")
    assert res.status == 200

    res.read()

    # Yield to server thread - will close the opreration and extend the ticket.
    time.sleep(0.1)

    server_ticket = auth.get(ticket["uuid"]).info()
    assert server_ticket["expires"] == 500
Ejemplo n.º 18
0
def test_tickets_idle_time_get(fake_time, tmpdir):
    image = testutils.create_tempfile(tmpdir, "image", "a" * 8192)
    ticket = testutils.create_ticket(url="file://" + str(image))
    auth.add(ticket)

    # Request must reset idle time.
    fake_time.now += 200
    http.get("/images/" + ticket["uuid"])
    assert auth.get(ticket["uuid"]).idle_time == 0
Ejemplo n.º 19
0
def test_images_flush_extends_ticket(tmpdir, fake_time):
    data = "x" * 512
    image = testutils.create_tempfile(tmpdir, "image", data)
    ticket = testutils.create_ticket(url="file://" + str(image))
    auth.add(ticket)
    server_ticket = auth.get(ticket["uuid"]).info()
    assert server_ticket["expires"] == 300

    fake_time.now += 200
    body = json.dumps({"op": "flush"}).encode("ascii")
    res = http.patch("/images/" + ticket["uuid"], body)
    assert res.status == 200

    res.read()

    # Yield to server thread - will close the opreration and extend the ticket.
    time.sleep(0.1)

    server_ticket = auth.get(ticket["uuid"]).info()
    assert server_ticket["expires"] == 500
Ejemplo n.º 20
0
def test_tickets_idle_time_patch(fake_time, tmpdir, msg):
    image = testutils.create_tempfile(tmpdir, "image", "a" * 8192)
    ticket = testutils.create_ticket(url="file://" + str(image))
    auth.add(ticket)

    # Request must reset idle time.
    fake_time.now += 200
    body = json.dumps(msg).encode('ascii')
    http.patch("/images/" + ticket["uuid"], body,
               headers={"content-type": "application/json"})
    assert auth.get(ticket["uuid"]).idle_time == 0
Ejemplo n.º 21
0
def test_tickets_idle_time_patch(fake_time, tmpdir, msg):
    image = testutils.create_tempfile(tmpdir, "image", b"a" * 8192)
    ticket = testutils.create_ticket(url="file://" + str(image))
    auth.add(ticket)

    # Request must reset idle time.
    fake_time.now += 200
    body = json.dumps(msg).encode('ascii')
    http.patch("/images/" + ticket["uuid"], body,
               headers={"content-type": "application/json"})
    assert auth.get(ticket["uuid"]).idle_time == 0
Ejemplo n.º 22
0
def test_images_flush_extends_ticket(tmpdir, fake_time):
    data = b"x" * 512
    image = testutils.create_tempfile(tmpdir, "image", data)
    ticket = testutils.create_ticket(url="file://" + str(image))
    auth.add(ticket)
    server_ticket = auth.get(ticket["uuid"]).info()
    assert server_ticket["expires"] == 300

    fake_time.now += 200
    body = json.dumps({"op": "flush"}).encode("ascii")
    res = http.patch("/images/" + ticket["uuid"], body)
    assert res.status == 200

    res.read()

    # Yield to server thread - will close the opreration and extend the ticket.
    time.sleep(0.1)

    server_ticket = auth.get(ticket["uuid"]).info()
    assert server_ticket["expires"] == 500
Ejemplo n.º 23
0
def test_tickets_put(fake_time):
    ticket = testutils.create_ticket(sparse=False)
    body = json.dumps(ticket)
    res = http.unix_request(
        config.tickets.socket, "PUT", "/tickets/%(uuid)s" % ticket, body)
    # Server adds expires key
    ticket["expires"] = int(util.monotonic_time()) + ticket["timeout"]
    ticket["active"] = False
    ticket["idle_time"] = 0
    server_ticket = auth.get(ticket["uuid"]).info()
    assert res.status == 200
    assert res.getheader("content-length") == "0"
    assert server_ticket == ticket
Ejemplo n.º 24
0
def test_tickets_put(fake_time):
    ticket = testutils.create_ticket(sparse=False)
    body = json.dumps(ticket)
    res = http.unix_request(
        config.tickets.socket, "PUT", "/tickets/%(uuid)s" % ticket, body)
    # Server adds expires key
    ticket["expires"] = int(util.monotonic_time()) + ticket["timeout"]
    ticket["active"] = False
    ticket["idle_time"] = 0
    server_ticket = auth.get(ticket["uuid"]).info()
    assert res.status == 200
    assert res.getheader("content-length") == "0"
    assert server_ticket == ticket
Ejemplo n.º 25
0
def test_tickets_extend(fake_time):
    ticket = testutils.create_ticket(sparse=False)
    auth.add(ticket)
    patch = {"timeout": 300}
    body = json.dumps(patch)
    fake_time.now += 240
    res = http.unix_request(
        config.tickets.socket, "PATCH", "/tickets/%(uuid)s" % ticket, body)
    ticket["expires"] = int(fake_time.now + ticket["timeout"])
    ticket["active"] = False
    ticket["idle_time"] = 240
    server_ticket = auth.get(ticket["uuid"]).info()
    assert res.status == 200
    assert res.getheader("content-length") == "0"
    assert server_ticket == ticket
Ejemplo n.º 26
0
def test_tickets_extend(fake_time):
    ticket = testutils.create_ticket(sparse=False)
    auth.add(ticket)
    patch = {"timeout": 300}
    body = json.dumps(patch)
    fake_time.now += 240
    res = http.unix_request(
        config.tickets.socket, "PATCH", "/tickets/%(uuid)s" % ticket, body)
    ticket["expires"] = int(fake_time.now + ticket["timeout"])
    ticket["active"] = False
    ticket["idle_time"] = 240
    server_ticket = auth.get(ticket["uuid"]).info()
    assert res.status == 200
    assert res.getheader("content-length") == "0"
    assert server_ticket == ticket
Ejemplo n.º 27
0
def test_tickets_idle_time_active(fake_time, tmpdir):
    filename = tmpdir.join("image")
    # Note: must be big enough so the request remain active.
    size = 1024**2 * 10
    with open(str(filename), 'wb') as image:
        image.truncate(size)
    ticket = testutils.create_ticket(
        url="file://" + str(filename), ops=["read"], size=size)
    auth.add(ticket)

    # Start a download, but read only 1 byte to make sure the operation becomes
    # active but do not complete.
    res = http.get("/images/" + ticket["uuid"])
    res.read(1)

    # Active ticket idle time is always 0.
    fake_time.now += 200
    assert auth.get(ticket["uuid"]).idle_time == 0
Ejemplo n.º 28
0
def test_tickets_idle_time_active(fake_time, tmpdir):
    filename = tmpdir.join("image")
    # Note: must be big enough so the request remain active.
    size = 1024**2 * 10
    with open(str(filename), 'wb') as image:
        image.truncate(size)
    ticket = testutils.create_ticket(
        url="file://" + str(filename), ops=["read"], size=size)
    auth.add(ticket)

    # Start a download, but read only 1 byte to make sure the operation becomes
    # active but do not complete.
    res = http.get("/images/" + ticket["uuid"])
    res.read(1)

    # Active ticket idle time is always 0.
    fake_time.now += 200
    assert auth.get(ticket["uuid"]).idle_time == 0
Ejemplo n.º 29
0
def test_download_progress(tmpdir, monkeypatch):
    # We need to read at least one buffer to update the transfered value.
    monkeypatch.setattr(config.daemon, "buffer_size", 1024**2)

    # And we need to request enough data so the server does not complete before
    # the client read all the data.
    size = config.daemon.buffer_size * 50

    filename = tmpdir.join("image")
    with open(str(filename), 'wb') as image:
        image.truncate(size)
    ticket = testutils.create_ticket(
        url="file://" + str(filename), ops=["read"], size=size)
    auth.add(ticket)
    ticket = auth.get(ticket["uuid"])

    # No operations
    assert not ticket.active()
    assert ticket.transferred() == 0

    res = http.get("/images/" + ticket.uuid)
    res.read(config.daemon.buffer_size)

    # The server processed at least one buffer but we need to give it time to
    # touch the ticket.
    time.sleep(0.2)

    assert ticket.active()
    assert 0 < ticket.transferred() < size

    res.read()

    # The server has sent all the chunks but we need to give it time to
    # touch the ticket.
    time.sleep(0.2)

    assert not ticket.active()
    assert ticket.transferred() == size
Ejemplo n.º 30
0
def test_download_progress(tmpdir, monkeypatch):
    # We need to read at least one buffer to update the transfered value.
    monkeypatch.setattr(config.daemon, "buffer_size", 1024**2)

    # And we need to request enough data so the server does not complete before
    # the client read all the data.
    size = config.daemon.buffer_size * 50

    filename = tmpdir.join("image")
    with open(str(filename), 'wb') as image:
        image.truncate(size)
    ticket = testutils.create_ticket(
        url="file://" + str(filename), ops=["read"], size=size)
    auth.add(ticket)
    ticket = auth.get(ticket["uuid"])

    # No operations
    assert not ticket.active()
    assert ticket.transferred() == 0

    res = http.get("/images/" + ticket.uuid)
    res.read(config.daemon.buffer_size)

    # The server processed at least one buffer but we need to give it time to
    # touch the ticket.
    time.sleep(0.2)

    assert ticket.active()
    assert 0 < ticket.transferred() < size

    res.read()

    # The server has sent all the chunks but we need to give it time to
    # touch the ticket.
    time.sleep(0.2)

    assert not ticket.active()
    assert ticket.transferred() == size