2023-03-08 15:04:54 +00:00
|
|
|
import std/os
|
|
|
|
import std/httpclient
|
2023-11-09 08:47:09 +00:00
|
|
|
import std/strutils
|
2023-08-01 23:47:57 +00:00
|
|
|
from std/net import TimeoutError
|
2023-03-08 15:04:54 +00:00
|
|
|
|
|
|
|
import pkg/chronos
|
|
|
|
import ../ethertest
|
|
|
|
import ./nodes
|
|
|
|
|
|
|
|
ethersuite "Node block expiration tests":
|
|
|
|
var node: NodeProcess
|
|
|
|
var baseurl: string
|
|
|
|
|
|
|
|
let dataDir = getTempDir() / "Codex1"
|
|
|
|
let content = "test file content"
|
|
|
|
|
|
|
|
setup:
|
|
|
|
baseurl = "http://localhost:8080/api/codex/v1"
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
node.stop()
|
|
|
|
|
|
|
|
dataDir.removeDir()
|
|
|
|
|
|
|
|
proc startTestNode(blockTtlSeconds: int) =
|
|
|
|
node = startNode([
|
|
|
|
"--api-port=8080",
|
|
|
|
"--data-dir=" & dataDir,
|
|
|
|
"--nat=127.0.0.1",
|
2023-10-24 14:52:06 +00:00
|
|
|
"--listen-addrs=/ip4/127.0.0.1/tcp/0",
|
2023-03-08 15:04:54 +00:00
|
|
|
"--disc-ip=127.0.0.1",
|
|
|
|
"--disc-port=8090",
|
|
|
|
"--block-ttl=" & $blockTtlSeconds,
|
2023-03-27 13:47:25 +00:00
|
|
|
"--block-mi=1",
|
2023-03-08 15:04:54 +00:00
|
|
|
"--block-mn=10"
|
|
|
|
], debug = false)
|
2023-09-13 14:17:56 +00:00
|
|
|
node.waitUntilStarted()
|
2023-03-08 15:04:54 +00:00
|
|
|
|
|
|
|
proc uploadTestFile(): string =
|
|
|
|
let client = newHttpClient()
|
2023-11-09 08:47:09 +00:00
|
|
|
let uploadUrl = baseurl & "/data"
|
2023-03-08 15:04:54 +00:00
|
|
|
let uploadResponse = client.post(uploadUrl, content)
|
|
|
|
check uploadResponse.status == "200 OK"
|
|
|
|
client.close()
|
|
|
|
uploadResponse.body
|
|
|
|
|
2023-11-21 00:14:06 +00:00
|
|
|
proc downloadTestFile(contentId: string, local = false): Response =
|
2023-03-08 15:04:54 +00:00
|
|
|
let client = newHttpClient(timeout=3000)
|
2023-11-21 00:14:06 +00:00
|
|
|
let downloadUrl = baseurl & "/data/" &
|
2024-10-17 16:54:28 +00:00
|
|
|
contentId & (if local: "" else: "/network/stream")
|
2023-11-21 00:14:06 +00:00
|
|
|
|
2023-03-08 15:04:54 +00:00
|
|
|
let content = client.get(downloadUrl)
|
|
|
|
client.close()
|
|
|
|
content
|
|
|
|
|
2023-11-09 08:47:09 +00:00
|
|
|
proc hasFile(contentId: string): bool =
|
|
|
|
let client = newHttpClient(timeout=3000)
|
2023-11-21 00:14:06 +00:00
|
|
|
let dataLocalUrl = baseurl & "/data/" & contentId
|
2023-11-09 08:47:09 +00:00
|
|
|
let content = client.get(dataLocalUrl)
|
|
|
|
client.close()
|
2023-11-21 00:14:06 +00:00
|
|
|
content.code == Http200
|
2023-11-09 08:47:09 +00:00
|
|
|
|
2023-03-08 15:04:54 +00:00
|
|
|
test "node retains not-expired file":
|
2023-03-27 13:47:25 +00:00
|
|
|
startTestNode(blockTtlSeconds = 10)
|
2023-03-08 15:04:54 +00:00
|
|
|
|
|
|
|
let contentId = uploadTestFile()
|
|
|
|
|
2023-03-27 13:47:25 +00:00
|
|
|
await sleepAsync(2.seconds)
|
2023-03-08 15:04:54 +00:00
|
|
|
|
2023-11-21 00:14:06 +00:00
|
|
|
let response = downloadTestFile(contentId, local = true)
|
2023-03-08 15:04:54 +00:00
|
|
|
check:
|
2023-11-09 08:47:09 +00:00
|
|
|
hasFile(contentId)
|
2023-03-08 15:04:54 +00:00
|
|
|
response.status == "200 OK"
|
|
|
|
response.body == content
|
|
|
|
|
|
|
|
test "node deletes expired file":
|
2023-03-27 13:47:25 +00:00
|
|
|
startTestNode(blockTtlSeconds = 1)
|
2023-03-08 15:04:54 +00:00
|
|
|
|
|
|
|
let contentId = uploadTestFile()
|
|
|
|
|
2023-09-04 09:12:14 +00:00
|
|
|
await sleepAsync(3.seconds)
|
2023-03-08 15:04:54 +00:00
|
|
|
|
2023-11-09 08:47:09 +00:00
|
|
|
check:
|
|
|
|
not hasFile(contentId)
|
2023-11-21 00:14:06 +00:00
|
|
|
downloadTestFile(contentId, local = true).code == Http404
|