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
|
2024-12-17 13:01:41 +00:00
|
|
|
import ./codexprocess
|
|
|
|
import ./nodeprocess
|
2023-03-08 15:04:54 +00:00
|
|
|
|
|
|
|
ethersuite "Node block expiration tests":
|
2024-12-17 13:01:41 +00:00
|
|
|
var node: CodexProcess
|
2023-03-08 15:04:54 +00:00
|
|
|
var baseurl: string
|
|
|
|
|
|
|
|
let dataDir = getTempDir() / "Codex1"
|
|
|
|
let content = "test file content"
|
|
|
|
|
|
|
|
setup:
|
|
|
|
baseurl = "http://localhost:8080/api/codex/v1"
|
|
|
|
|
|
|
|
teardown:
|
2024-12-17 13:01:41 +00:00
|
|
|
await node.stop()
|
2023-03-08 15:04:54 +00:00
|
|
|
|
|
|
|
dataDir.removeDir()
|
|
|
|
|
2024-12-17 13:01:41 +00:00
|
|
|
proc startTestNode(blockTtlSeconds: int) {.async.} =
|
|
|
|
node = await CodexProcess.startNode(@[
|
2023-03-08 15:04:54 +00:00
|
|
|
"--api-port=8080",
|
|
|
|
"--data-dir=" & dataDir,
|
2024-12-18 09:31:18 +00:00
|
|
|
"--nat=none",
|
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-port=8090",
|
|
|
|
"--block-ttl=" & $blockTtlSeconds,
|
2023-03-27 13:47:25 +00:00
|
|
|
"--block-mi=1",
|
2024-12-17 13:01:41 +00:00
|
|
|
"--block-mn=10"],
|
|
|
|
false,
|
|
|
|
"cli-test-node"
|
|
|
|
)
|
|
|
|
await 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":
|
2024-12-17 13:01:41 +00:00
|
|
|
await 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":
|
2024-12-17 13:01:41 +00:00
|
|
|
await 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
|