diff --git a/lib/default.nix b/lib/default.nix index 732733c..759cc8d 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -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) + "") - 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) + "") ) ]); 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; diff --git a/modules/config.nix b/modules/config.nix index a39998b..0164ac2 100644 --- a/modules/config.nix +++ b/modules/config.nix @@ -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}\"") [ { diff --git a/tests/xml.nix b/tests/xml.nix index 057ef8f..9b5c6ce 100644 --- a/tests/xml.nix +++ b/tests/xml.nix @@ -1,48 +1,153 @@ -{pkgs ? import {}, ...}: let +{ pkgs ? import { }, ... }: +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 = '' + # + # + # ''; + # 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 = '' - - - ''; - 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" + '' + + + '' + (toXml { tag = "test"; }) + ) + (genTest "Single inner tag" + '' + + + + + '' + (toXml { + tag = "test"; + content = { tag = "inner"; }; + }) + ) + (genTest "Tag with string" + '' + + stringstringstring + '' + (toXml { + tag = "test"; + content = "stringstringstring"; + }) + ) + (genTest "Empty string" + '' + + + '' + (toXml { + tag = "test"; + content = ""; + }) + ) + (genTest "List of tags" + '' + + + + + + + + + + + + + '' + (toXml { + tag = "test"; + content = builtins.genList (x: { tag = "tag${toString x}"; }) 9; + }) + ) + (genTest "Empty list" + '' + + + '' + (toXml { + tag = "test"; + content = [ ]; + }) + ) + (genTest "bool value true" + '' + + true + '' + (toXml { + tag = "test"; + content = true; + }) + ) + (genTest "bool value false" + '' + + false + '' + (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() '';