2022-01-30 14:13:37 +01:00
|
|
|
import random
|
|
|
|
import tempfile
|
|
|
|
import tarfile
|
|
|
|
from pathlib import Path
|
|
|
|
import uuid
|
|
|
|
import argparse
|
|
|
|
import asyncio
|
|
|
|
import aiohttp
|
2022-01-30 18:02:45 +01:00
|
|
|
import sys
|
2022-01-30 14:13:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
# A list of words the program can choose from
|
|
|
|
WORDS = ["alpha", "bravo", "charlie", "delta", "echo", "foxtrot", "golf",
|
|
|
|
"hotel", "india", "juliet", "kilo", "lima", "mike", "november",
|
|
|
|
"oscar", "papa", "quebec", "romeo", "sierra", "tango", "uniform",
|
|
|
|
"victor", "whiskey", "xray", "yankee", "zulu"]
|
|
|
|
SEED = 2022
|
|
|
|
|
|
|
|
|
2022-01-30 18:02:45 +01:00
|
|
|
def random_words(words, min_len, max_len=None):
|
2022-01-30 14:13:37 +01:00
|
|
|
"""
|
|
|
|
Returns a random list of words, with a length randomly choosen between
|
|
|
|
min_len and max_len. If max_len is None, it is equal to the length of
|
2022-01-30 18:02:45 +01:00
|
|
|
words.
|
2022-01-30 14:13:37 +01:00
|
|
|
"""
|
|
|
|
if max_len is None:
|
2022-01-30 18:02:45 +01:00
|
|
|
max_len = len(words)
|
2022-01-30 14:13:37 +01:00
|
|
|
|
|
|
|
k = random.randint(min_len, max_len)
|
|
|
|
|
2022-01-30 18:02:45 +01:00
|
|
|
return random.choices(words, k=k)
|
2022-01-30 14:13:37 +01:00
|
|
|
|
2022-01-30 18:02:45 +01:00
|
|
|
def random_lists(words, n, min_len, max_len=None):
|
|
|
|
return [random_words(words, min_len, max_len) for _ in range(n)]
|
2022-01-30 14:13:37 +01:00
|
|
|
|
2022-01-30 18:02:45 +01:00
|
|
|
def create_random_pkginfo(words, name_min_len, name_max_len):
|
2022-01-30 14:13:37 +01:00
|
|
|
"""
|
|
|
|
Generates a random .PKGINFO
|
|
|
|
"""
|
2022-01-30 18:02:45 +01:00
|
|
|
name = "-".join(random_words(words, name_min_len, name_max_len))
|
2022-01-30 14:13:37 +01:00
|
|
|
ver = "0.1.0" # doesn't matter what it is anyways
|
|
|
|
|
|
|
|
# TODO add random dependencies (all types)
|
|
|
|
|
|
|
|
data = {
|
|
|
|
"pkgname": name,
|
|
|
|
"pkgbase": name,
|
|
|
|
"pkgver": ver,
|
|
|
|
"arch": "x86_64"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "\n".join(f"{key} = {value}" for key, value in data.items())
|
|
|
|
|
2022-01-30 18:02:45 +01:00
|
|
|
def create_random_package(tmpdir, words, pkg_name_min_len, pkg_name_max_len, min_files, max_files, min_filename_len, max_filename_len):
|
2022-01-30 14:13:37 +01:00
|
|
|
"""
|
|
|
|
Creates a random, but valid Arch package, using the provided tmpdir. Output
|
|
|
|
is the path to the created package tarball.
|
|
|
|
"""
|
|
|
|
|
|
|
|
sub_path = tmpdir / uuid.uuid4().hex
|
|
|
|
sub_path.mkdir()
|
|
|
|
|
|
|
|
tar_path = sub_path / "archive.pkg.tar.gz"
|
|
|
|
|
|
|
|
def remove_prefix(tar_info):
|
|
|
|
tar_info.name = tar_info.name[len(str(sub_path)):]
|
|
|
|
|
|
|
|
return tar_info
|
|
|
|
|
2022-01-31 22:59:12 +01:00
|
|
|
with tarfile.open(tar_path, "w:gz") as tar:
|
2022-01-30 14:13:37 +01:00
|
|
|
# Add random .PKGINFO file
|
|
|
|
pkginfo_file = sub_path / ".PKGINFO"
|
2022-01-30 18:02:45 +01:00
|
|
|
pkginfo_file.write_text(create_random_pkginfo(words, pkg_name_min_len, pkg_name_max_len))
|
2022-01-30 14:13:37 +01:00
|
|
|
tar.add(pkginfo_file, filter=remove_prefix)
|
|
|
|
|
|
|
|
# Create random files
|
2022-01-30 18:02:45 +01:00
|
|
|
file_count = random.randint(min_files, max_files)
|
|
|
|
|
|
|
|
for words in random_lists(words, file_count, min_filename_len, max_filename_len):
|
2022-01-30 14:13:37 +01:00
|
|
|
path = sub_path / 'usr' / ('/'.join(words) + ".txt")
|
|
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
path.write_text(' '.join(words))
|
|
|
|
|
|
|
|
tar.add(path, filter=remove_prefix)
|
|
|
|
|
|
|
|
return tar_path
|
|
|
|
|
|
|
|
|
2022-01-30 18:02:45 +01:00
|
|
|
async def check_output(r):
|
|
|
|
good = {"File already exists.", "Package added successfully."}
|
|
|
|
txt = await r.text()
|
|
|
|
|
|
|
|
return (txt in good, txt)
|
|
|
|
|
2022-01-30 14:13:37 +01:00
|
|
|
|
2022-01-30 18:02:45 +01:00
|
|
|
async def upload_random_package(tar_path, sem):
|
2022-01-30 14:13:37 +01:00
|
|
|
async with sem:
|
|
|
|
with open(tar_path, 'rb') as f:
|
|
|
|
async with aiohttp.ClientSession() as s:
|
|
|
|
async with s.post("http://localhost:8000/publish", data=f.read(), headers={"x-api-key": "test"}) as r:
|
2022-01-30 18:02:45 +01:00
|
|
|
return await check_output(r)
|
2022-01-30 14:13:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
parser = argparse.ArgumentParser(description="Test vieter by uploading random package files.")
|
|
|
|
|
|
|
|
parser.add_argument("count", help="How many packages to upload.", default=1, type=int)
|
|
|
|
parser.add_argument("-p", "--parallel", help="How many uploads to run in parallel.", default=1, type=int)
|
|
|
|
parser.add_argument("-s", "--seed", help="Seed for the randomizer.", default=SEED, type=int)
|
2022-01-30 18:02:45 +01:00
|
|
|
parser.add_argument("--min-files", help="Minimum amount of files to add to an archive.", default=5, type=int)
|
|
|
|
parser.add_argument("--max-files", help="Max amount of files to add to an archive.", default=10, type=int)
|
|
|
|
parser.add_argument("--min-filename-length", help="Minimum amount of words to use for generating filenames.", default=1, type=int)
|
|
|
|
parser.add_argument("--max-filename-length", help="Max amount of words to use for generating filenames.", default=5, type=int)
|
|
|
|
parser.add_argument("--min-pkg-name-length", help="Minimum amount of words to use for creating package name.", default=1, type=int)
|
|
|
|
parser.add_argument("--max-pkg-name-length", help="Max amount of words to use for creating package name.", default=3, type=int)
|
|
|
|
parser.add_argument("--words", help="Words to use for randomizing.", default=WORDS, type=lambda s: s.split(','))
|
|
|
|
# parser.add_argument("--words", help="Words to use for randomizing.", default=WORDS, type=)
|
2022-01-30 14:13:37 +01:00
|
|
|
# parser.add_argument("-d", "--dir", help="Directory to create ")
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
sem = asyncio.BoundedSemaphore(args.parallel)
|
|
|
|
random.seed(args.seed)
|
|
|
|
|
2022-01-30 18:02:45 +01:00
|
|
|
|
2022-01-30 14:13:37 +01:00
|
|
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
|
|
|
tmpdir = Path(tmpdirname)
|
|
|
|
|
2022-01-30 18:02:45 +01:00
|
|
|
# We generate the tars in advance because they're not async anyways
|
|
|
|
print("Generating tarballs...")
|
|
|
|
tars = {
|
|
|
|
create_random_package(tmpdir, args.words, args.min_pkg_name_length, args.max_pkg_name_length, args.min_files, args.max_files, args.min_filename_length, args.max_filename_length)
|
|
|
|
for _ in range(args.count)
|
|
|
|
}
|
|
|
|
|
|
|
|
print("Sending requests...")
|
|
|
|
res = await asyncio.gather(*(upload_random_package(tar, sem) for tar in tars))
|
|
|
|
|
|
|
|
# Generate status report
|
|
|
|
if any(not x[0] for x in res):
|
|
|
|
sys.exit(1)
|
2022-01-30 14:13:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
asyncio.run(main())
|