Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions config/integration.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# overrides for tests/integration.sh, sourced after config/default.env
# separate ports so a test run never clashes with a dev primary on 45000
PRIMARY_PORT=46000
NUM_SECONDARYS=2
SPAWN_SECONDARY=1
STOP_GRACE_PERIOD_SECS=1
STALE_MS=3000
LOG_DIR=logs/integration
TASK_TYPES=sumtil,sleep,fail,hold
RESULTS_DIR=/tmp/integrationtesting

# integration specs kill secondaries on purpose and expect them to stay closed
RESPAWN_MAX=0

USE_CGROUP=0
22 changes: 22 additions & 0 deletions tests/LICENSE.qspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2013 Daniel Nugent

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
22 changes: 22 additions & 0 deletions tests/LICENSE.qutil
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2009 Daniel Nugent

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
194 changes: 194 additions & 0 deletions tests/bootstrap.q
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
.utl.LOADED:()
.utl.LOADING:`symbol$()
.utl.PKGSLOADED:enlist[""]!enlist[`:]
.utl.FILELOADING:`
.utl.PKGLOADING:""
.utl.DEBUG:0b
.utl.PKGSTRUCTURE:`
.utl.LOADSTRUCTURE:`
.utl.LOADERROR:()

.utl.baseLoadV:{[x;v;allowReload];
oldStructure:.utl.LOADSTRUCTURE;
.utl.LOADSTRUCTURE:.utl.PKGSTRUCTURE;
pkgInfo: .utl.requireVH.getPackageInfo[x;v;allowReload];
.utl.LOADSTRUCTURE:oldStructure;
/ The convention used for packages will be to start by using an init file
file: $[not 11h ~ type key pkgInfo[`file];pkgInfo[`file];` sv pkgInfo[`file],`init.q];
if[not count key file; '"File '",(1 _ string file),"' not found"];
oldFileLoading: .utl.FILELOADING;
oldPkgLoading: .utl.PKGLOADING;
/ Let the name of the file being loaded and the "package" if available be accessed
`.utl.PKGLOADING set .utl.requireVH.getPackageLoading[x;pkgInfo];
`.utl.FILELOADING set .utl.realPath file;
result:1b;
/ The require function prevents files from being loaded that have already been loaded
/ Files should NEVER be loaded recursively
if[(allowReload or not file in .utl.LOADED) and not file in .utl.LOADING;
.[`.utl.LOADING;();union;file];
result:.utl.requireVH.loadFile file;
.[`.utl.LOADING;();except;file];
if[1b ~ result;.[`.utl.LOADED;();union;file]];
];
if[not 1b ~ result;
.utl.LOADERROR,:file;
];
`.utl.FILELOADING set oldFileLoading;
`.utl.PKGLOADING set oldPkgLoading;
$[1b ~ result;
1b;
result like "*.utl.LOADERROR*";
'result;
'"Error while loading. Please see .utl.LOADERROR: ",result
];
}

.utl.loadV:.utl.baseLoadV[;;1b]
.utl.load:.utl.baseLoadV[;"";1b]
.utl.requireV:.utl.baseLoadV[;;0b]
.utl.require:.utl.baseLoadV[;"";0b]

// Convenience funciton for loading files inside of pakcages
.utl.pkg:{.utl.load $[10h ~ type .utl.PKGLOADING;.utl.PKGLOADING,"/",x;.utl.PKGLOADING,x]}

/Get the real path of a filehandle cross platform (hopefully)
.utl.realPath:{
rPath:{[absm;p] $[p like absm;p;` sv (hsym `$system "cd"), (`$1 _ string p)]};
$["w" ~ (string .z.o) 0;
rPath[":[A-z]:*";x];
rPath[":/*";x]
]
}

.utl.requireVH:((),`)!(),(::)
.utl.requireVH.loadFile:{[file];
f:1 _ string file;
if[0i ~ system "e";
:@[{system "l ", x;1b};1 _ string file;(::)];
];
system "l ",f;
1b
}

.utl.requireVH.getPackageInfo:{[loadArg;v;allowReload];
$[-11h ~ type loadArg;
`file`package!2#$[null[.utl.LOADSTRUCTURE] or loadArg like "*.q";loadArg;` sv loadArg,.utl.LOADSTRUCTURE];
/ If a generic list is passed, we treat it as a symbol and characters, using the symbol as a package name
/ This is useful to allow requires that reference relative paths
0h ~ type loadArg;
`file`package!(` sv loadArg[0],`$"/" vs 1 _ loadArg;loadArg[0]);
.utl.requireVH.findV[loadArg;v;allowReload]
]
}

.utl.requireVH.getPackageLoading:{[loadArg;pkgInfo];
/ Package specified with a file handle using init.q extension
$[(-11h ~ type loadArg) and pkgInfo[`file] like "*init.q";
first ` vs pkgInfo[`file];
/ Package specified with a file handle and string component using directory path
(0h ~ type loadArg) and 11h ~ type key pkgInfo `file;
pkgInfo[`file];
/ All other .q files
pkgInfo[`file] like "*.q";
.utl.PKGLOADING;
/ The rest (new packages defined by loading argument)
pkgInfo[`package]
]
}

.utl.requireVH.packagesLoaded:{[package;v;allowReload];
/ If the the thing attempting to be loaded isn't a pure package, we're not going to try restricing it based on package id
if[("" ~ v) and (package like "*-*") and package like "*[.0-9]";
l: {$[1 < count x;("-" sv -1 _ x;-1#x);x]} "-" vs raze string package;
v: raze "=",last l;
package: first l];
if[not package in key .utl.PKGSLOADED;:`];
$[.utl.requireVH.makeFilter[v] file:.utl.PKGSLOADED package;
file;
allowReload;
`;
'"Error loading package '", package, "' with version '", v, "', there was a conflicting package version already loaded at ", 1 _ string file
]
}

.utl.requireVH.allPackages:{raze {(` sv x,) each key x} each .utl.QPATH}
.utl.requireVH.findV:{[x;v;allowReload];
/ The paths passed to require should be the same across platforms
packageName: first pathComponents: "/" vs x;
if[not null path:.utl.requireVH.packagesLoaded[packageName;v;allowReload];
:.utl.requireVH.foundDict[path;pathComponents]
];
matchingPackages:.utl.requireVH.getMatchingPackages[allPackages:.utl.requireVH.allPackages[];packageName;v];
allMatchingPackages:.utl.requireVH.getMatchingPackages[allPackages:.utl.requireVH.allPackages[];packageName;""];
highestMatch:.utl.requireVH.highestPackage matchingPackages;
exactPackage: $[count matchingPackages;
first matchingPackages where matchingPackages like "*",packageName;
first allMatchingPackages where allMatchingPackages like "*",packageName
];
/ If a Version String has been provided (not "") we want to use the most recent matching
/ package and *then* fall back to an exact package name match.
path: $[(v ~ "") and not null exactPackage;exactPackage;
not null highestMatch;highestMatch;
not null exactPackage;exactPackage;
'"A matching package was not found for '",x,"' with version string '",v,"'. Paths searched:\n\t", "\n\t" sv string .utl.QPATH];
rval:.utl.requireVH.foundDict[path;pathComponents];
/ Keep track of loaded packages to prevent mismatching requires
.utl.PKGSLOADED,:enlist["-" sv {$[(last[x] like "*[.0-9]") and 1 < count x;-1 _ x;x]} "-" vs packageName]!enlist[path];
//.utl.PKGSLOADED,:enlist[first "-" vs packageName]!enlist[path];
rval
}

.utl.requireVH.getMatchingPackages:{[allPackages;packageName;v];
matchingPackages: allPackages where ('[last;vs[`]] each allPackages) like packageName,"*"; / Only consider the last part of the available paths as package names
matchingPackages where .utl.requireVH.makeFilter[v] each matchingPackages
}

.utl.requireVH.highestPackage:{[matchingPackages];
first matchingPackages idesc (),.utl.requireVH.numVNStr .utl.requireVH.VNStrPath each matchingPackages
}

/ Package numbers are only considered at the top level, special logic to support
/ sub-package elements (packages and files within a versioned package) is needed
.utl.requireVH.foundDict:{[path;pathComponents];
subPackagePath: $[1 < count pathComponents;`$ 1 _ string ` sv `$@[1 _ pathComponents;0;":",];()];
file: ` sv path,$[null .utl.LOADSTRUCTURE;();.utl.LOADSTRUCTURE],subPackagePath;
package: 1 _ string ` sv (hsym last ` vs path),subPackagePath;
`file`package!(file;package)
}

/ Compare two Version Number Strings given an operator
/ It's much easier to do the partial application of the arguments for our purposes
/ if the second arg (the one on the right hand side) is passed in first
.utl.requireVH.cmpVNStr:{[op;vsn2;vsn1];
if[not count vsn1;:0b];
if[not count vsn2;:1b];
op . .utl.requireVH.numVNStr (vsn1;vsn2)
}

/ Create ordinal numbers from a list of Version Number Strings (Because a Version Number String could ostensibly have infinite depth, we need to work on them in groupings)
.utl.requireVH.numVNStr:{[vns0];
if[not count vns0;:0];
vns:"I"$"." vs' (),/:vns0;
s: max count each vns;
ns:{@[x#0;til count y;:;y]}[s] each vns; / Fill out each list with 0's
mvn:max each flip ns;
exponents: reverse sums reverse ?[(1 _ mvn) > 0;1+`int$log[1 _ mvn] div log[10];1],0;
sum each ns *\: `long$10 xexp' exponents / Get the total version numbers
}

/ Get a Version Number String from the last element of a path
.utl.requireVH.VNStrPath:{`char$raze last "-" vs string last ` vs x}

/ Create a pair of operator and Version Number String
.utl.requireVH.parseVStr:{
v: trim each "," vs x;
pairs: {(0,0^1 + last x ss "[<>=]") _ x} each v; / Split the version specifiers into operator and number.
.[pairs;(::;0);:;(("";(),"=";(),"<";(),">";">=";"<=";"<>")!(=;=;<;>;>=;<=;<>)) pairs[;0]] / Create the comparators
}

/ Creates a Filter function based on a Version String
/ Individual comparison functions are created from each of the comma separated version string elements
.utl.requireVH.makeFilter:{[vstr];
if[vstr ~ "";:{1b}];
{all x @\: .utl.requireVH.VNStrPath y}[.utl.requireVH.cmpVNStr ./: .utl.requireVH.parseVStr vstr] / Partially apply the version string elements to get a list of comparators
}
45 changes: 45 additions & 0 deletions tests/helpers/it.q
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Claude
/ helpers for the integration specs: a client connection to the primary and a few readers
/ not a spec itself: it lives outside the folders qspec is pointed at

.it.ep:`$"::",getenv`PRIMARY_PORT;
.it.n:"J"$getenv`NUM_SECONDARYS;
.it.h:0Ni;

/ a handle to the primary, opened on first use (retries for up to 10s)
.it.connect:{
if[not null .it.h;:.it.h];
n:0;
while[n<50;
h:@[hopen;(.it.ep;1000);{[e] 0Ni}];
if[not null h;.it.h:h;:h];
system "sleep 0.2";
n+:1];
'"could not connect to the primary at ",string .it.ep};

/ run q on the primary and return the result
.it.q:{[s] .it.connect[] s};

/ the primary's secondaries table, unkeyed
.it.sec:{.it.q "0!secondaries"};

/ how many secondaries are free right now
.it.nfree:{t:.it.sec[]; sum t[`status]=`free};

/ status of secondarys id, secid
.it.status:{[secid] t:.it.sec[]; first exec status from t where id=secid};

/ is this pid still running?
.it.alive:{[p] not ()~key hsym `$"/proc/",string p};

/ call f[] every 0.2s until it returns 1b or secs seconds pass; says whether it did
.it.waitfor:{[f;secs]
n:0;
while[n<5*secs;
if[f[];:1b];
system "sleep 0.2";
n+:1];
:0b};

/ wait for every spawned secondary to register as free (up to 20s)
.it.ready:{.it.waitfor[{.it.n<=.it.nfree[]};20]};
84 changes: 84 additions & 0 deletions tests/integration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env bash
# end to end tests
# for each tests/integration/*_spec.q: start a fresh primary (which spawns its own secondaries),
# run that spec against it with qspec, then kill everything
# usage (from anywhere): tests/integration.sh run every spec
# tests/integration.sh stale run only specs whose file name contains "stale"
set -uo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT"

set -a
source config/default.env
source config/integration.env
set +a
export QHOME="$KDBX_HOME"
# export QPATH="$ROOT/tests:/opt/kdbx/mod:/home/jrutledge/x/kdbx-modules-main"

Q="$KDBX_HOME/bin/q"
FILTER="${1:-}"
mkdir -p "$LOG_DIR"
PRIMARYPID=""

# kill the test primary and its secondaries
# secondaries have no port, so they're found by the PRIMARY_PORT they inherited from the test primary;
# /proc/<pid>/environ is only readable for our own processes, and any other primary's secondaries have a
# different PRIMARY_PORT, so nothing else is touched
stopall() {
if [[ -n "$PRIMARYPID" ]]; then
kill -9 "$PRIMARYPID" 2>/dev/null || true
wait "$PRIMARYPID" 2>/dev/null || true # reap it, so bash doesn't print "Killed"
fi
local p
for p in $(pgrep -f "src/secondary.q"); do
if tr '\0' '\n' < "/proc/$p/environ" 2>/dev/null | grep -qx "PRIMARY_PORT=$PRIMARY_PORT"; then
kill -9 "$p" 2>/dev/null || true
fi
done
PRIMARYPID=""
}

# used to kill any processes incase script stops early eg. CTRL+C
trap stopall EXIT

# refuse to start if something is already on the test primary's port (secondaries don't listen)
portsfree() {
if (exec 3<>"/dev/tcp/localhost/$PRIMARY_PORT") 2>/dev/null; then
echo "port $PRIMARY_PORT is already in use; kill whatever is on it (ss -ltnp | grep $PRIMARY_PORT) and rerun" >&2
return 1
fi
}

# start the primary in the background and wait until it is listening
startprimary() {
"$Q" src/primary.q -p "$PRIMARY_PORT" < /dev/null >> "$LOG_DIR/primary.log" 2>&1 &
PRIMARYPID=$!
for _ in $(seq 50); do
if (exec 3<>"/dev/tcp/localhost/$PRIMARY_PORT") 2>/dev/null; then return 0; fi
sleep 0.2
done
echo "primary never started listening; see $LOG_DIR/primary.log" >&2
return 1
}

portsfree || exit 1

rc=0
ran=0
for spec in tests/integration/*_spec.q; do
if [[ -n "$FILTER" && "$spec" != *"$FILTER"* ]]; then continue; fi
ran=$((ran + 1))
echo
echo "== $spec"
echo "== $(date -u +%Y.%m.%dD%H:%M:%S) $spec" >> "$LOG_DIR/primary.log"
if ! startprimary; then rc=1; stopall; continue; fi
if ! EXTRA_ENV=config/integration.env tests/run.sh "$spec"; then rc=1; fi
stopall
sleep 0.5 # let the processes exit and the primary's port free up before the next spec
done

echo
if [[ $ran -eq 0 ]]; then echo "no specs matched '$FILTER'" >&2; exit 1; fi
if [[ $rc -eq 0 ]]; then echo "integration: all $ran spec files passed"; else echo "integration: failures; logs are in $LOG_DIR"; fi
exit $rc
Loading