mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-06-09 19:13:26 +03:00
memcachedTestHook: init
This commit is contained in:
parent
aa01fb4e17
commit
74c386d399
4 changed files with 139 additions and 0 deletions
53
doc/hooks/memcached-test-hook.section.md
Normal file
53
doc/hooks/memcached-test-hook.section.md
Normal file
|
@ -0,0 +1,53 @@
|
|||
|
||||
# `memcachedTestHook` {#sec-memcachedTestHook}
|
||||
|
||||
This hook starts a Memcached server during `checkPhase`. Example:
|
||||
|
||||
```nix
|
||||
{
|
||||
stdenv,
|
||||
memcachedTestHook,
|
||||
}:
|
||||
stdenv.mkDerivation {
|
||||
|
||||
# ...
|
||||
|
||||
nativeCheckInputs = [
|
||||
memcachedTestHook
|
||||
];
|
||||
}
|
||||
```
|
||||
|
||||
If you use a custom `checkPhase`, remember to add the `runHook` calls:
|
||||
```nix
|
||||
checkPhase ''
|
||||
runHook preCheck
|
||||
|
||||
# ... your tests
|
||||
|
||||
runHook postCheck
|
||||
''
|
||||
```
|
||||
|
||||
## Variables {#sec-memcachedTestHook-variables}
|
||||
|
||||
Bash-only variables:
|
||||
|
||||
- `memcachedTestPort`: Port to use by Memcached. Defaults to `11211`
|
||||
|
||||
Example usage:
|
||||
|
||||
```nix
|
||||
{ stdenv, memcachedTestHook }:
|
||||
stdenv.mkDerivation {
|
||||
|
||||
# ...
|
||||
|
||||
nativeCheckInputs = [
|
||||
memcachedTestHook
|
||||
];
|
||||
|
||||
preCheck = ''
|
||||
memcachedTestPort=1234
|
||||
''
|
||||
}
|
27
pkgs/by-name/me/memcachedTestHook/memcached-test-hook.sh
Normal file
27
pkgs/by-name/me/memcachedTestHook/memcached-test-hook.sh
Normal file
|
@ -0,0 +1,27 @@
|
|||
preCheckHooks+=('memcachedStart')
|
||||
postCheckHooks+=('memcachedStop')
|
||||
|
||||
|
||||
memcachedStart() {
|
||||
if [[ "${memcachedTestPort:-}" == "" ]]; then
|
||||
memcachedTestPort=11211
|
||||
fi
|
||||
|
||||
echo 'starting memcached'
|
||||
|
||||
# Note about Darwin: unless the output is redirected, the parent process becomes launchd instead of bash.
|
||||
# This would leave the memcached process running in case of a test failure (the postCheckHook would not be executed),
|
||||
# hanging the Nix build forever.
|
||||
@memcached@ -p "$memcachedTestPort" >/dev/null 2>&1 &
|
||||
MEMCACHED_PID=$!
|
||||
|
||||
echo 'waiting for memcached to be ready'
|
||||
while ! (echo 'quit' | @nc@ localhost "$memcachedTestPort") ; do
|
||||
sleep 1
|
||||
done
|
||||
}
|
||||
|
||||
memcachedStop() {
|
||||
echo 'stopping memcached'
|
||||
kill "$MEMCACHED_PID"
|
||||
}
|
18
pkgs/by-name/me/memcachedTestHook/package.nix
Normal file
18
pkgs/by-name/me/memcachedTestHook/package.nix
Normal file
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
callPackage,
|
||||
makeSetupHook,
|
||||
lib,
|
||||
memcached,
|
||||
netcat,
|
||||
}:
|
||||
|
||||
makeSetupHook {
|
||||
name = "memcached-test-hook";
|
||||
substitutions = {
|
||||
memcached = lib.getExe memcached;
|
||||
nc = lib.getExe netcat;
|
||||
};
|
||||
passthru.tests = {
|
||||
simple = callPackage ./test.nix { };
|
||||
};
|
||||
} ./memcached-test-hook.sh
|
41
pkgs/by-name/me/memcachedTestHook/test.nix
Normal file
41
pkgs/by-name/me/memcachedTestHook/test.nix
Normal file
|
@ -0,0 +1,41 @@
|
|||
{
|
||||
memcachedTestHook,
|
||||
netcat,
|
||||
stdenv,
|
||||
}:
|
||||
|
||||
stdenv.mkDerivation {
|
||||
name = "memcached-test-hook-test";
|
||||
|
||||
nativeCheckInputs = [
|
||||
memcachedTestHook
|
||||
netcat
|
||||
];
|
||||
|
||||
dontUnpack = true;
|
||||
doCheck = true;
|
||||
|
||||
preCheck = ''
|
||||
memcachedTestPort=11212
|
||||
'';
|
||||
|
||||
checkPhase = ''
|
||||
runHook preCheck
|
||||
|
||||
echo "running test"
|
||||
if echo -e "stats\nquit" | nc localhost $memcachedTestPort; then
|
||||
echo "connected to memcached"
|
||||
TEST_RAN=1
|
||||
fi
|
||||
|
||||
runHook postCheck
|
||||
'';
|
||||
|
||||
__darwinAllowLocalNetworking = true;
|
||||
|
||||
installPhase = ''
|
||||
[[ $TEST_RAN == 1 ]]
|
||||
echo "test passed"
|
||||
touch $out
|
||||
'';
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue