query-services-state for any expected response

master
Alexander Tomokhov 2023-12-12 10:12:50 +04:00
parent cd6d21b5f3
commit e6cd6dc7a9
5 changed files with 165 additions and 142 deletions

View File

@ -0,0 +1,43 @@
{
"data": {
"services": {
"allServices": [
{
"id": "bitwarden",
"isEnabled": true,
"status": "ACTIVE"
},
{
"id": "gitea",
"isEnabled": true,
"status": "ACTIVE"
},
{
"id": "email",
"isEnabled": true,
"status": "ACTIVE"
},
{
"id": "nextcloud",
"isEnabled": true,
"status": "ACTIVE"
},
{
"id": "pleroma",
"isEnabled": true,
"status": "ACTIVE"
},
{
"id": "ocserv",
"isEnabled": true,
"status": "ACTIVE"
},
{
"id": "jitsi",
"isEnabled": false,
"status": "ACTIVE"
}
]
}
}
}

View File

@ -7,21 +7,23 @@
let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
lib = nixpkgs.lib;
# Generate a user-friendly version number. TODO
version = (builtins.substring 0 8 self.lastModifiedDate) + self.shortRev;
in
{
packages.${system}.query-all-services =
pkgs.runCommandLocal "query-all-services"
makeTestProgram = { name, script, env-vars }:
pkgs.runCommandLocal name
{
nativeBuildInputs = [ pkgs.makeWrapper ];
} ''
ls -l
install -m755 ${./query-all-services.sh} \
-D $out/bin/query-all-services
patchShebangs $out/bin/query-all-services
wrapProgram $out/bin/query-all-services \
install -m755 ${script} \
-D $out/bin/${name}
patchShebangs $out/bin/${name}
wrapProgram $out/bin/${name} \
${lib.strings.concatMapStringsSep "\n"
({name, value}: "--prefix ${name} : ${value}") env-vars} \
--prefix PATH : ${with pkgs; lib.makeBinPath [
curl
diffutils
@ -32,44 +34,27 @@
gnutar
]}
'';
# packages.${system}.query-all-services =
# pkgs.writeShellApplication {
# name = "query-all-services.sh";
# checkPhase = true;
# text =./query-all-services.sh;
# runtimeInputs = with pkgs; [
# curl
# diffutils
# gzip
# jd-diff-patch
# jq
# openssh
# gnutar
# ];
# };
# packages.${system}.query-all-services =
# pkgs.resholve.writeScriptBin "query-all-services.sh" {
# inputs = with pkgs; [
# curl
# diffutils
# gzip
# jd-diff-patch
# jq
# openssh
# gnutar
# ];
# interpreter = "${pkgs.bash}/bin/bash";
# } ./query-all-services.sh;
devShells.${system}.default = pkgs.mkShell {
packages = with pkgs; [
curl
diffutils
gzip
jd-diff-patch
jq
openssh
gnutar
];
};
in
{
packages.${system} = lib.attrsets.listToAttrs (map
(a@{ name, ... }: lib.attrsets.nameValuePair name (makeTestProgram a))
[
{
name = "query-minimum-services";
script = ./query-services-state.sh;
env-vars = [{
name = "EXPECTED_RESPONSE_FILE";
value = ./minimal-services-expected-response.json;
}];
}
{
name = "query-all-services";
script = ./query-services-state.sh;
env-vars = [{
name = "EXPECTED_RESPONSE_FILE";
value = ./all-services-expected-response.json;
}];
}
]);
};
}

View File

@ -0,0 +1,43 @@
{
"data": {
"services": {
"allServices": [
{
"id": "bitwarden",
"isEnabled": false,
"status": "OFF"
},
{
"id": "gitea",
"isEnabled": false,
"status": "OFF"
},
{
"id": "email",
"isEnabled": true,
"status": "ACTIVE"
},
{
"id": "nextcloud",
"isEnabled": true,
"status": "ACTIVE"
},
{
"id": "pleroma",
"isEnabled": false,
"status": "OFF"
},
{
"id": "ocserv",
"isEnabled": false,
"status": "OFF"
},
{
"id": "jitsi",
"isEnabled": false,
"status": "OFF"
}
]
}
}
}

View File

@ -1,95 +0,0 @@
#! /usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
shopt -s inherit_errexit
: "${DOMAIN:?specify domain name}"
readonly AUTH_HEADER="Authorization: Bearer ${API_TOKEN:?not set error}"
readonly QUERY='query AllServices {
services {
allServices {
id
isEnabled
status
}
}
}'
# shellcheck disable=SC2116
# shellcheck disable=SC2086
REQUEST=$(cat << EOF
{
"query": "$(echo $QUERY)",
"variables": null,
"operationName": "AllServices"
}
EOF
)
readonly REQUEST
# TODO fix jitsi "isEnabled" expected response value
EXPECTED_RESPONSE=$(cat << EOF
{
"data": {
"services": {
"allServices": [
{
"id": "bitwarden",
"isEnabled": true,
"status": "ACTIVE"
},
{
"id": "gitea",
"isEnabled": true,
"status": "ACTIVE"
},
{
"id": "email",
"isEnabled": true,
"status": "ACTIVE"
},
{
"id": "nextcloud",
"isEnabled": true,
"status": "ACTIVE"
},
{
"id": "pleroma",
"isEnabled": true,
"status": "ACTIVE"
},
{
"id": "ocserv",
"isEnabled": true,
"status": "ACTIVE"
},
{
"id": "jitsi",
"isEnabled": false,
"status": "ACTIVE"
}
]
}
}
}
EOF
)
readonly EXPECTED_RESPONSE
received_response="$(curl --show-error -s "https://api.$DOMAIN/graphql" \
--compressed \
-H 'Content-Type: application/json' \
--data-raw "$REQUEST" -k -H "$AUTH_HEADER" | jq --sort-keys)"
jq <<<"$received_response"
if diff -w -u --color <(printf "%s\n" "$EXPECTED_RESPONSE") <(printf "%s\n" "$received_response") 2>/dev/null
then
echo -e "\e[1;32mOK"
else
echo -e "\e[1;31mFAIL: response does not match!"
jd -color <(printf "%s" "$EXPECTED_RESPONSE") <(printf "%s" "$received_response")
exit 1
fi

47
query-services-state.sh Executable file
View File

@ -0,0 +1,47 @@
#! /usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
shopt -s inherit_errexit
: "${DOMAIN:?specify domain name}"
readonly AUTH_HEADER="Authorization: Bearer ${API_TOKEN:?not set error}"
readonly QUERY='query AllServices {
services {
allServices {
id
isEnabled
status
}
}
}'
: "${EXPECTED_RESPONSE_FILE:?not set error}"
# shellcheck disable=SC2116
# shellcheck disable=SC2086
REQUEST=$(cat << EOF
{
"query": "$(echo $QUERY)",
"variables": null,
"operationName": "AllServices"
}
EOF
)
readonly REQUEST
received_response="$(curl --show-error -s "https://api.$DOMAIN/graphql" \
--compressed \
-H 'Content-Type: application/json' \
--data-raw "$REQUEST" -k -H "$AUTH_HEADER" | jq --sort-keys)"
jq <<<"$received_response"
if diff -w -u --color "$EXPECTED_RESPONSE_FILE" <(printf "%s\n" "$received_response") 2>/dev/null
then
echo -e "\e[1;32mOK"
else
echo -e "\e[1;31mFAIL: response does not match!"
jd -color "$EXPECTED_RESPONSE_FILE" <(printf "%s" "$received_response")
exit 1
fi