tests and xml generation work (for real this guy trust)
Some checks failed
Run tests / run_tests (push) Failing after 3s
Some checks failed
Run tests / run_tests (push) Failing after 3s
This commit is contained in:
parent
b7fdfe57e6
commit
fa91487a1f
|
@ -15,9 +15,9 @@
|
|||
parseTag = str: depth: xml: (builtins.concatStringsSep "" [
|
||||
str
|
||||
"${indent depth}<${xml.tag}${
|
||||
if (builtins.hasAttr "content" xml) && ((builtins.isString xml.content) || (builtins.isList xml.content) && ((builtins.length xml.content) > 0))
|
||||
then ">"
|
||||
else " "
|
||||
if !(builtins.hasAttr "content" xml) || (((builtins.isString xml.content) && xml.content == "") || ((builtins.isList xml.content) && ((builtins.length xml.content) == 0)))
|
||||
then ""
|
||||
else ">"
|
||||
}"
|
||||
|
||||
(
|
||||
|
@ -31,9 +31,9 @@
|
|||
)
|
||||
|
||||
(
|
||||
if (builtins.hasAttr "content" xml) && ((builtins.isString xml.content) || (builtins.isList xml.content) && ((builtins.length xml.content) > 0))
|
||||
then ((toXmlRecursive' "\n" (depth + 1) xml.content) + "</${xml.tag}>")
|
||||
else "/>"
|
||||
if !(builtins.hasAttr "content" xml) || (((builtins.isString xml.content) && xml.content == "") || ((builtins.isList xml.content) && ((builtins.length xml.content) == 0)))
|
||||
then " />"
|
||||
else ((toXmlRecursive' "\n" (depth + 1) xml.content) + "</${xml.tag}>")
|
||||
)
|
||||
]);
|
||||
in
|
||||
|
@ -41,10 +41,12 @@
|
|||
then "${parseTag str depth xml}\n${indent (depth - 1)}"
|
||||
else if (builtins.isList xml)
|
||||
then "\n${(builtins.concatStringsSep "" (builtins.map (x: (toXmlRecursive' "" depth x)) xml))}${indent (depth - 1)}"
|
||||
else if ((builtins.isBool xml) || (builtins.isInt xml) || (builtins.isNull xml) || (builtins.isFloat xml))
|
||||
else if ((builtins.isInt xml) || (builtins.isNull xml) || (builtins.isFloat xml))
|
||||
then (builtins.toString xml)
|
||||
else if (builtins.isString xml)
|
||||
then xml
|
||||
else if (builtins.isBool xml)then
|
||||
if xml then "true" else "false"
|
||||
else throw "Cannot convert a ${builtins.typeOf xml} to XML. ${toString (builtins.trace xml xml)}";
|
||||
in
|
||||
toXMLRecursive;
|
||||
|
|
|
@ -7,11 +7,11 @@
|
|||
with lib; let
|
||||
cfg = config.services.declarative-jellyfin;
|
||||
toXml' = (import ../lib {nixpkgs = pkgs;}).toXMLGeneric;
|
||||
isStrList = x: builtins.all (x: builtins.isString x) x;
|
||||
isStrList = x: all (x: isString x) x;
|
||||
prepass = x:
|
||||
if (builtins.isAttrs x)
|
||||
if (isAttrs x)
|
||||
then
|
||||
if !(builtins.hasAttr "tag" x)
|
||||
if !(hasAttr "tag" x)
|
||||
then
|
||||
attrsets.mapAttrsToList
|
||||
(tag: value: {
|
||||
|
@ -19,23 +19,23 @@ with lib; let
|
|||
content = prepass value;
|
||||
})
|
||||
x
|
||||
else if (builtins.hasAttr "content" x)
|
||||
else if (hasAttr "content" x)
|
||||
then {
|
||||
tag = x.tag;
|
||||
content = prepass x.content;
|
||||
}
|
||||
else x
|
||||
else if (builtins.isList x)
|
||||
else if (isList x)
|
||||
then
|
||||
if (isStrList x)
|
||||
then
|
||||
(builtins.map
|
||||
(map
|
||||
(content: {
|
||||
tag = "string";
|
||||
inherit content;
|
||||
})
|
||||
x)
|
||||
else builtins.map prepass x
|
||||
else map prepass x
|
||||
else x;
|
||||
|
||||
toXml = tag: x: (toXml' {
|
||||
|
@ -59,8 +59,8 @@ in {
|
|||
(
|
||||
let
|
||||
commands =
|
||||
builtins.concatStringsSep "\n"
|
||||
(builtins.map
|
||||
concatStringsSep "\n"
|
||||
(map
|
||||
(x: "cp -s \"${pkgs.writeText x.file (toXml x.name x.content)}\" \"/var/lib/jellyfin/config/${x.file}\"")
|
||||
[
|
||||
{
|
||||
|
|
215
tests/xml.nix
215
tests/xml.nix
|
@ -1,48 +1,153 @@
|
|||
{pkgs ? import <nixpkgs> {}, ...}: let
|
||||
{ pkgs ? import <nixpkgs> { }, ... }:
|
||||
let
|
||||
name = "networking";
|
||||
in {
|
||||
in
|
||||
{
|
||||
inherit name;
|
||||
test = pkgs.nixosTest {
|
||||
inherit name;
|
||||
nodes = {
|
||||
machine = {
|
||||
config,
|
||||
pkgs,
|
||||
...
|
||||
}: {
|
||||
imports = [
|
||||
../modules/default.nix
|
||||
];
|
||||
machine =
|
||||
{ config
|
||||
, pkgs
|
||||
, ...
|
||||
}: {
|
||||
imports = [
|
||||
../modules/default.nix
|
||||
];
|
||||
|
||||
assertions = let
|
||||
toXml = (import ../lib {nixpkgs = pkgs;}).toXMLGeneric;
|
||||
in [
|
||||
(
|
||||
# assertions = let
|
||||
# toXml = (import ../lib {nixpkgs = pkgs;}).toXMLGeneric;
|
||||
# in [
|
||||
# (
|
||||
# let
|
||||
# xml =
|
||||
# toXml {tag = "test";};
|
||||
# expected = ''
|
||||
# <?xml version='1.0' encoding='utf-8'?>
|
||||
# <test />
|
||||
# '';
|
||||
# in {
|
||||
# assertion = xml == expected;
|
||||
# message = "Generated XML is incorrect!\nExpected \n\n${expected}\n\n but got \n\n${xml}\n";
|
||||
# }
|
||||
# )
|
||||
# ];
|
||||
|
||||
assertions =
|
||||
let
|
||||
xml =
|
||||
toXml {tag = "test";};
|
||||
expected = ''
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<test />
|
||||
'';
|
||||
in {
|
||||
assertion = xml == expected;
|
||||
message = "Generated XML is incorrect!\nExpected \n\n${expected}\n\n but got \n\n${xml}\n";
|
||||
}
|
||||
)
|
||||
];
|
||||
|
||||
virtualisation.memorySize = 1024 * 2;
|
||||
|
||||
services.declarative-jellyfin = {
|
||||
enable = true;
|
||||
network = {
|
||||
PublishedServerUriBySubnet = [
|
||||
"all=https://test.test.test"
|
||||
genTest = name: expected: got: {
|
||||
assertion = expected == got;
|
||||
message = "[Test: ${name}] Generated XML is incorrect!\nExpected \n\n${expected}\n but got \n\n${got}";
|
||||
};
|
||||
toXml = (import ../lib { nixpkgs = pkgs; }).toXMLGeneric;
|
||||
in
|
||||
[
|
||||
(genTest "Single tag"
|
||||
''
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<test />
|
||||
''
|
||||
(toXml { tag = "test"; })
|
||||
)
|
||||
(genTest "Single inner tag"
|
||||
''
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<test>
|
||||
<inner />
|
||||
</test>
|
||||
''
|
||||
(toXml {
|
||||
tag = "test";
|
||||
content = { tag = "inner"; };
|
||||
})
|
||||
)
|
||||
(genTest "Tag with string"
|
||||
''
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<test>stringstringstring</test>
|
||||
''
|
||||
(toXml {
|
||||
tag = "test";
|
||||
content = "stringstringstring";
|
||||
})
|
||||
)
|
||||
(genTest "Empty string"
|
||||
''
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<test />
|
||||
''
|
||||
(toXml {
|
||||
tag = "test";
|
||||
content = "";
|
||||
})
|
||||
)
|
||||
(genTest "List of tags"
|
||||
''
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<test>
|
||||
<tag0 />
|
||||
<tag1 />
|
||||
<tag2 />
|
||||
<tag3 />
|
||||
<tag4 />
|
||||
<tag5 />
|
||||
<tag6 />
|
||||
<tag7 />
|
||||
<tag8 />
|
||||
</test>
|
||||
''
|
||||
(toXml {
|
||||
tag = "test";
|
||||
content = builtins.genList (x: { tag = "tag${toString x}"; }) 9;
|
||||
})
|
||||
)
|
||||
(genTest "Empty list"
|
||||
''
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<test />
|
||||
''
|
||||
(toXml {
|
||||
tag = "test";
|
||||
content = [ ];
|
||||
})
|
||||
)
|
||||
(genTest "bool value true"
|
||||
''
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<test>true</test>
|
||||
''
|
||||
(toXml {
|
||||
tag = "test";
|
||||
content = true;
|
||||
})
|
||||
)
|
||||
(genTest "bool value false"
|
||||
''
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<test>false</test>
|
||||
''
|
||||
(toXml {
|
||||
tag = "test";
|
||||
content = false;
|
||||
})
|
||||
)
|
||||
];
|
||||
|
||||
virtualisation.memorySize = 1024;
|
||||
|
||||
services.declarative-jellyfin = {
|
||||
enable = true;
|
||||
network = {
|
||||
PublishedServerUriBySubnet = [
|
||||
"all=https://test.test.test"
|
||||
];
|
||||
EnableHttps = true;
|
||||
RequireHttps = true;
|
||||
CertificatePath = "/path/to/cert";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
testScript = ''
|
||||
|
@ -50,22 +155,40 @@ in {
|
|||
|
||||
machine.wait_for_unit("multi-user.target");
|
||||
|
||||
with subtest("Networking"):
|
||||
with subtest("network.xml"):
|
||||
# stupid fucking hack because you cant open files in python for some reason
|
||||
xml = machine.succeed("cat /var/lib/jellyfin/config/network.xml")
|
||||
tree = ET.ElementTree(ET.fromstring(xml))
|
||||
root = tree.getroot()
|
||||
for child in root:
|
||||
if child.tag == "PublishedServerUriBySubnet":
|
||||
try:
|
||||
if child[0].text == "all=https://test.test.test":
|
||||
|
||||
with subtest("PublishedServerUriBySubnet"):
|
||||
for child in root:
|
||||
if child.tag == "PublishedServerUriBySubnet":
|
||||
try:
|
||||
if child[0].text == "all=https://test.test.test":
|
||||
break
|
||||
except:
|
||||
print("An error occured when trying to parse xml")
|
||||
print(xml)
|
||||
assert False, "Exception occured, check output above"
|
||||
else:
|
||||
assert False, "The shit was not found. Full XML: " + xml
|
||||
|
||||
with subtest("EnableHttps"):
|
||||
for child in root:
|
||||
if child.tag == "EnableHttps":
|
||||
if child.text == "true":
|
||||
break
|
||||
except:
|
||||
print("An error occured when trying to parse xml")
|
||||
print(xml)
|
||||
assert False, "Exception occured, check output above"
|
||||
else:
|
||||
assert False, "The shit was not found"
|
||||
else:
|
||||
assert False, "The shit was not found. Full XML: " + xml
|
||||
|
||||
with subtest("RequireHttps"):
|
||||
for child in root:
|
||||
if child.tag == "RequireHttps":
|
||||
if child.text == "true":
|
||||
break
|
||||
else:
|
||||
assert False, "The shit was not found. Full XML: " + xml
|
||||
|
||||
machine.shutdown()
|
||||
'';
|
||||
|
|
Loading…
Reference in a new issue