Skip to content

Commit 7d956f7

Browse files
committed
Added new test and cleaned test_tree_building
1 parent 7b2c372 commit 7d956f7

File tree

3 files changed

+307
-5
lines changed

3 files changed

+307
-5
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* This file contains random elements from different nix configurations and my own hand to ensure all elements of the program are sufficiently covered in unit tests */
2+
3+
# To test multiline headers
4+
{ lib,
5+
...
6+
}:
7+
8+
{
9+
# from hardware_configuration.nix bundled in a install of Nix-OS
10+
11+
networking.useDHCP = lib.mkDefault true;
12+
# networking.interfaces.ens33.useDHCP = lib.mkDefault true;
13+
14+
nixpkgs.hostPlatform = lib.mkDefault "x86_64-linux";
15+
16+
# To test lib.mkForce, taken from https://search.nixos.org/options?channel=24.11&show=boot.supportedFilesystems&from=0&size=50&sort=relevance&type=packages&query=lib.mkForce
17+
18+
boot.supportedFilesystems = {
19+
btrfs = true;
20+
zfs = lib.mkForce false;
21+
};
22+
23+
# Testing integers
24+
25+
services.i2pd.bandwidth = 32;
26+
services.tigerbeetle.clusterId = 15;
27+
28+
}
29+

tests/test_comment_collecting.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,11 @@ def test_pms_example_config():
2626

2727
comment_handler = CommentHandling(Path("./tests/example_configurations/pms_example_config.nix"))
2828
assert "{6: [('# Edit this configuration file to define what should be installed on', True), ('# your system. Help is available in the configuration.nix(5) man page', True), ('# and in the NixOS manual (accessible by running `nixos-help`).', True)], 11: [('# Include the results of the hardware scan.\\n', False)], 16: [('# Use the systemd-boot EFI boot loader.', True)], 22: [('#boot.zfs.extraPools = [ \"zfstest\" ];', True)], 57: [('# badblocks\\n', False)], 97: [('#defaultSession = \"xfce+bspwm\";', True)], 110: [('# make shares visible for windows 10 clients\\n', False)]}".replace("\"", '"') == str(comment_handler.get_comments_for_attaching())
29+
30+
def test_random():
31+
"""
32+
Checks if all comments are collected correctly in random.nix configuration
33+
"""
34+
35+
comment_handler = CommentHandling(Path("./tests/example_configurations/random.nix"))
36+
assert "{3: [('# To test multiline headers', True)], 9: [('# from hardware_configuration.nix bundled in a install of Nix-OS', True)], 12: [('# networking.interfaces.ens33.useDHCP = lib.mkDefault true;', True)], 16: [('# To test lib.mkForce, taken from https://search.nixos.org/options?channel=24.11&show=boot.supportedFilesystems&from=0&size=50&sort=relevance&type=packages&query=lib.mkForce', True)], 23: [('# Testing integers', True)]}".replace("\"", '"') == str(comment_handler.get_comments_for_attaching())

tests/test_tree_building.py

Lines changed: 270 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,267 @@
55
from nix_tree.tree import DecomposerTree, ConnectorNode, VariableNode, Node
66
from nix_tree.decomposer import Decomposer
77

8-
YASU_TREE = """{}{ |--headers=[ config, pkgs, ... ]}{ |--imports=[ ./hardware-configuration.nix ]}{ boot}{ loader}{ systemd-boot}{ |--enable=true}{ efi}{ |--canTouchEfiVariables=true}{ networking}{ |--hostName='nixos'}{ |--defaultGateway='10.11.12.1'}{ |--nameservers=[ '10.11.12.1' ]}{ firewall}{ |--allowedTCPPorts=[ 3389 ]}{ time}{ |--timeZone='Japan'}{ virtualisation}{ virtualbox}{ host}{ |--enable=true}{ hardware}{ bluetooth}{ |--enable=true}{ config}{ General}{ |--Enable='Source,Sink,Media,Socket'}{ pulseaudio}{ |--enable=true}{ |--extraModules=[ pkgs.pulseaudio-modules-bt ]}{ |--package=pkgs.pulseaudioFull}{ |--support32Bit=true}{ |--extraConfig='' load-module module-bluetooth-policy auto_switch = 2 ''}{ opengl}{ |--driSupport32Bit=true}{ services}{ blueman}{ |--enable=true}{ cron}{ |--enable=true}{ |--systemCronJobs=[ '@reboot root ${pkgs.ethtool}/sbin/ethtool -s enp4s0 wol g' ]}{ openssh}{ |--enable=true}{ |--passwordAuthentication=false}{ |--challengeResponseAuthentication=false}{ |--extraConfig='UseDNS yes'}{ vsftpd}{ |--enable=true}{ |--localUsers=true}{ |--writeEnable=true}{ |--extraConfig='' pasv_enable = YES connect_from_port_20 = YES pasv_min_port = 4242 pasv_max_port = 4243 ''}{ apcupsd}{ |--enable=true}{ |--configText='' UPSCABLE smart UPSTYPE apcsmart DEVICE /dev/ttyS0 ''}{ postfix}{ |--enable=true}{ |--setSendmail=true}{ xserver}{ |--enable=true}{ |--layout='us'}{ displayManager}{ gdm}{ |--enable=true}{ desktopManager}{ gnome3}{ |--enable=true}{ |--videoDrivers=[ 'nvidia' ]}{ fail2ban}{ |--enable=true}{ netdata}{ |--enable=true}{ xrdp}{ |--enable=true}{ |--defaultWindowManager='${pkgs.icewm}/bin/icewm'}{ vnstat}{ |--enable=true}{ sound}{ |--enable=true}{ programs}{ mosh}{ |--enable=true}{ gnupg}{ agent}{ |--enable=true}{ systemd}{ targets}{ sleep}{ |--enable=false}{ suspend}{ |--enable=false}{ hibernate}{ |--enable=false}{ hybrid-sleep}{ |--enable=false}{ users}{ extraUsers}{ yasu}{ |--home='/home/yasu'}{ |--isNormalUser=true}{ |--uid=1000}{ |--extraGroups=[ 'wheel' ]}{ nixpkgs}{ config}{ |--allowUnfree=true}{ powerManagement}{ |--enable=true}{ i18n}{ inputMethod}{ |--enabled='ibus'}{ ibus}{ |--engines=[ (pkgs.ibus-engines).mozc ]}{ fonts}{ |--fonts=[ (pkgs).carlito (pkgs).dejavu_fonts (pkgs).ipafont (pkgs).kochi-substitute (pkgs).source-code-pro (pkgs).ttf_bitstream_vera ]}{ fontconfig}{ defaultFonts}{ |--monospace=[ 'DejaVu Sans Mono' 'IPAGothic' ]}{ |--sansSerif=[ 'DejaVu Sans' 'IPAPGothic' ]}{ |--serif=[ 'DejaVu Serif' 'IPAPMincho' ]}"""
8+
YASU_TREE = """
9+
|--headers=[ config, pkgs, ... ]
10+
|--imports=[ ./hardware-configuration.nix ]
11+
boot
12+
loader
13+
systemd-boot
14+
|--enable=true
15+
efi
16+
|--canTouchEfiVariables=true
17+
networking
18+
|--hostName='nixos'
19+
|--defaultGateway='10.11.12.1'
20+
|--nameservers=[ '10.11.12.1' ]
21+
firewall
22+
|--allowedTCPPorts=[ 3389 ]
23+
time
24+
|--timeZone='Japan'
25+
virtualisation
26+
virtualbox
27+
host
28+
|--enable=true
29+
hardware
30+
bluetooth
31+
|--enable=true
32+
config
33+
General
34+
|--Enable='Source,Sink,Media,Socket'
35+
pulseaudio
36+
|--enable=true
37+
|--extraModules=[ pkgs.pulseaudio-modules-bt ]
38+
|--package=pkgs.pulseaudioFull
39+
|--support32Bit=true
40+
|--extraConfig='' load-module module-bluetooth-policy auto_switch = 2 ''
41+
opengl
42+
|--driSupport32Bit=true
43+
services
44+
blueman
45+
|--enable=true
46+
cron
47+
|--enable=true
48+
|--systemCronJobs=[ '@reboot root ${pkgs.ethtool}/sbin/ethtool -s enp4s0 wol g' ]
49+
openssh
50+
|--enable=true
51+
|--passwordAuthentication=false
52+
|--challengeResponseAuthentication=false
53+
|--extraConfig='UseDNS yes'
54+
vsftpd
55+
|--enable=true
56+
|--localUsers=true
57+
|--writeEnable=true
58+
|--extraConfig='' pasv_enable = YES connect_from_port_20 = YES pasv_min_port = 4242 pasv_max_port = 4243 ''
59+
apcupsd
60+
|--enable=true
61+
|--configText='' UPSCABLE smart UPSTYPE apcsmart DEVICE /dev/ttyS0 ''
62+
postfix
63+
|--enable=true
64+
|--setSendmail=true
65+
xserver
66+
|--enable=true
67+
|--layout='us'
68+
displayManager
69+
gdm
70+
|--enable=true
71+
desktopManager
72+
gnome3
73+
|--enable=true
74+
|--videoDrivers=[ 'nvidia' ]
75+
fail2ban
76+
|--enable=true
77+
netdata
78+
|--enable=true
79+
xrdp
80+
|--enable=true
81+
|--defaultWindowManager='${pkgs.icewm}/bin/icewm'
82+
vnstat
83+
|--enable=true
84+
sound
85+
|--enable=true
86+
programs
87+
mosh
88+
|--enable=true
89+
gnupg
90+
agent
91+
|--enable=true
92+
systemd
93+
targets
94+
sleep
95+
|--enable=false
96+
suspend
97+
|--enable=false
98+
hibernate
99+
|--enable=false
100+
hybrid-sleep
101+
|--enable=false
102+
users
103+
extraUsers
104+
yasu
105+
|--home='/home/yasu'
106+
|--isNormalUser=true
107+
|--uid=1000
108+
|--extraGroups=[ 'wheel' ]
109+
nixpkgs
110+
config
111+
|--allowUnfree=true
112+
powerManagement
113+
|--enable=true
114+
i18n
115+
inputMethod
116+
|--enabled='ibus'
117+
ibus
118+
|--engines=[ (pkgs.ibus-engines).mozc ]
119+
fonts
120+
|--fonts=[ (pkgs).carlito (pkgs).dejavu_fonts (pkgs).ipafont (pkgs).kochi-substitute (pkgs).source-code-pro (pkgs).ttf_bitstream_vera ]
121+
fontconfig
122+
defaultFonts
123+
|--monospace=[ 'DejaVu Sans Mono' 'IPAGothic' ]
124+
|--sansSerif=[ 'DejaVu Sans' 'IPAPGothic' ]
125+
|--serif=[ 'DejaVu Serif' 'IPAPMincho' ]
126+
"""
9127

10-
SHORTENED_DEFAULT_TREE = """{}{ |--headers=[ config, pkgs, ... ]}{ |--imports=[ ./hardware-configuration.nix ]}{ boot}{ loader}{ grub}{ |--enable=true}{ |--device='/dev/sda'}{ |--useOSProber=true}{ networking}{ |--hostName='nixos'}{ networkmanager}{ |--enable=true}{ time}{ |--timeZone='Europe/London'}{ i18n}{ |--defaultLocale='en_GB.UTF-8'}{ services}{ xserver}{ |--enable=true}{ programs}{ firefox}{ |--enable=true}{ nixpkgs}{ config}{ |--allowUnfree=true}{ environment}{ |--systemPackages=[ (pkgs).vim (pkgs).git ]}{ system}{ |--stateVersion='23.11'}"""
128+
SHORTENED_DEFAULT_TREE = """
129+
|--headers=[ config, pkgs, ... ]
130+
|--imports=[ ./hardware-configuration.nix ]
131+
boot
132+
loader
133+
grub
134+
|--enable=true
135+
|--device='/dev/sda'
136+
|--useOSProber=true
137+
networking
138+
|--hostName='nixos'
139+
networkmanager
140+
|--enable=true
141+
time
142+
|--timeZone='Europe/London'
143+
i18n
144+
|--defaultLocale='en_GB.UTF-8'
145+
services
146+
xserver
147+
|--enable=true
148+
programs
149+
firefox
150+
|--enable=true
151+
nixpkgs
152+
config
153+
|--allowUnfree=true
154+
environment
155+
|--systemPackages=[ (pkgs).vim (pkgs).git ]
156+
system
157+
|--stateVersion='23.11'
158+
"""
11159

12-
PMS_TREE = """{}{ |--headers=[ config, pkgs, ... ]}{ |--imports=[ ./hardware-configuration.nix ]}{ boot}{ loader}{ systemd-boot}{ |--enable=true}{ efi}{ |--canTouchEfiVariables=true}{ |--supportedFilesystems=[ 'zfs' ]}{ zfs}{ |--forceImportRoot=false}{ services}{ zfs}{ autoScrub}{ |--enable=true}{ openssh}{ |--enable=true}{ settings}{ |--PasswordAuthentication=false}{ |--PermitRootLogin='yes'}{ tailscale}{ |--enable=true}{ xserver}{ |--enable=true}{ displayManager}{ lightdm}{ |--enable=true}{ |--defaultSession='xfce'}{ desktopManager}{ xfce}{ |--enable=true}{ windowManager}{ bspwm}{ |--enable=true}{ samba-wsdd}{ |--enable=true}{ samba}{ |--enable=true}{ |--securityType='user'}{ |--extraConfig='' workgroup = KTZ server string = testnix netbios name = testnix security = user guest ok = yes guest account = nobody map to guest = bad user load printers = no ''}{ shares}{ zfstest}{ |--path='/mnt/zfstest'}{ |--browseable='yes'}{ |--'read only'='no'}{ |--'guest ok'='yes'}{ |--'create mask'='0644'}{ |--'directory mask'='0755'}{ |--'force user'='alex'}{ |--'force group'='users'}{ time}{ |--timeZone='America/New_York'}{ users}{ users}{ alex}{ |--isNormalUser=true}{ |--extraGroups=[ 'wheel' 'docker' ]}{ openssh}{ authorizedKeys}{ |--keyFiles=[ /etc/nixos/ssh/authorized_keys ]}{ users}{ root}{ openssh}{ authorizedKeys}{ |--keyFiles=[ /etc/nixos/ssh/authorized_keys ]}{ environment}{ |--systemPackages=[ (pkgs).docker-compose (pkgs).htop (pkgs).hddtemp (pkgs).intel-gpu-tools (pkgs).iotop (pkgs).lm_sensors (pkgs).mergerfs (pkgs).mc (pkgs).ncdu (pkgs).nmap (pkgs).nvme-cli (pkgs).sanoid (pkgs).snapraid (pkgs).tdns-cli (pkgs).tmux (pkgs).tree (pkgs).vim (pkgs).wget (pkgs).smartmontools (pkgs).e2fsprogs ]}{ networking}{ firewall}{ |--enable=false}{ |--hostName='testnix'}{ |--hostId='e5f2dc02'}{ interfaces}{ enp1s0}{ |--useDHCP=false}{ |--defaultGateway='10.42.0.254'}{ |--nameservers=[ '10.42.0.253' ]}{ virtualisation}{ docker}{ |--enable=true}{ autoPrune}{ |--enable=true}{ |--dates='weekly'}{ nix}{ settings}{ |--experimental-features=[ 'nix-command' 'flakes' ]}{ |--warn-dirty=false}{ system}{ |--copySystemConfiguration=true}{ |--stateVersion='23.05'}"""
160+
PMS_TREE = """
161+
|--headers=[ config, pkgs, ... ]
162+
|--imports=[ ./hardware-configuration.nix ]
163+
boot
164+
loader
165+
systemd-boot
166+
|--enable=true
167+
efi
168+
|--canTouchEfiVariables=true
169+
|--supportedFilesystems=[ 'zfs' ]
170+
zfs
171+
|--forceImportRoot=false
172+
services
173+
zfs
174+
autoScrub
175+
|--enable=true
176+
openssh
177+
|--enable=true
178+
settings
179+
|--PasswordAuthentication=false
180+
|--PermitRootLogin='yes'
181+
tailscale
182+
|--enable=true
183+
xserver
184+
|--enable=true
185+
displayManager
186+
lightdm
187+
|--enable=true
188+
|--defaultSession='xfce'
189+
desktopManager
190+
xfce
191+
|--enable=true
192+
windowManager
193+
bspwm
194+
|--enable=true
195+
samba-wsdd
196+
|--enable=true
197+
samba
198+
|--enable=true
199+
|--securityType='user'
200+
|--extraConfig='' workgroup = KTZ server string = testnix netbios name = testnix security = user guest ok = yes guest account = nobody map to guest = bad user load printers = no ''
201+
shares
202+
zfstest
203+
|--path='/mnt/zfstest'
204+
|--browseable='yes'
205+
|--'read only'='no'
206+
|--'guest ok'='yes'
207+
|--'create mask'='0644'
208+
|--'directory mask'='0755'
209+
|--'force user'='alex'
210+
|--'force group'='users'
211+
time
212+
|--timeZone='America/New_York'
213+
users
214+
users
215+
alex
216+
|--isNormalUser=true
217+
|--extraGroups=[ 'wheel' 'docker' ]
218+
openssh
219+
authorizedKeys
220+
|--keyFiles=[ /etc/nixos/ssh/authorized_keys ]
221+
users
222+
root
223+
openssh
224+
authorizedKeys
225+
|--keyFiles=[ /etc/nixos/ssh/authorized_keys ]
226+
environment
227+
|--systemPackages=[ (pkgs).docker-compose (pkgs).htop (pkgs).hddtemp (pkgs).intel-gpu-tools (pkgs).iotop (pkgs).lm_sensors (pkgs).mergerfs (pkgs).mc (pkgs).ncdu (pkgs).nmap (pkgs).nvme-cli (pkgs).sanoid (pkgs).snapraid (pkgs).tdns-cli (pkgs).tmux (pkgs).tree (pkgs).vim (pkgs).wget (pkgs).smartmontools (pkgs).e2fsprogs ]
228+
networking
229+
firewall
230+
|--enable=false
231+
|--hostName='testnix'
232+
|--hostId='e5f2dc02'
233+
interfaces
234+
enp1s0
235+
|--useDHCP=false
236+
|--defaultGateway='10.42.0.254'
237+
|--nameservers=[ '10.42.0.253' ]
238+
virtualisation
239+
docker
240+
|--enable=true
241+
autoPrune
242+
|--enable=true
243+
|--dates='weekly'
244+
nix
245+
settings
246+
|--experimental-features=[ 'nix-command' 'flakes' ]
247+
|--warn-dirty=false
248+
system
249+
|--copySystemConfiguration=true
250+
|--stateVersion='23.05'
251+
"""
252+
253+
RANDOM = """
254+
|--headers=[ lib, ... ]
255+
networking
256+
|--useDHCP=lib.mkDefault.true
257+
nixpkgs
258+
|--hostPlatform=lib.mkDefault.'x86_64-linux'
259+
boot
260+
supportedFilesystems
261+
|--btrfs=true
262+
|--zfs=lib.mkForce.false
263+
services
264+
i2pd
265+
|--bandwidth=32
266+
tigerbeetle
267+
|--clusterId=15
268+
"""
13269

14270
def tree_output(node: Node, append: str = "", output_string: str = "") -> str:
15271
"""
@@ -25,11 +281,11 @@ def tree_output(node: Node, append: str = "", output_string: str = "") -> str:
25281
"""
26282

27283
if isinstance(node, ConnectorNode):
28-
output_string += "{" + append + node.get_name() + "}"
284+
output_string += append + node.get_name() + "\n"
29285
for i in node.get_connected_nodes():
30286
output_string = tree_output(i, append + " ", output_string)
31287
if isinstance(node, VariableNode):
32-
output_string += "{" + append + "|--" + node.get_name().split(".")[-1] + "=" + node.get_data() + "}"
288+
output_string += append + "|--" + node.get_name().split(".")[-1] + "=" + node.get_data() + "\n"
33289
return output_string
34290

35291
def test_tree_for_yasu_example_config():
@@ -59,3 +315,12 @@ def test_tree_for_pms_example_config():
59315
tree = DecomposerTree()
60316
Decomposer(file_path=Path("./tests/example_configurations/pms_example_config.nix"), tree=tree)
61317
assert PMS_TREE == tree_output(tree.get_root())
318+
319+
def test_tree_for_random():
320+
"""
321+
Checks if the tree generator functions in decomposer and tree work as expected for the random config
322+
"""
323+
324+
tree = DecomposerTree()
325+
Decomposer(file_path=Path("./tests/example_configurations/random.nix"), tree=tree)
326+
assert RANDOM == tree_output(tree.get_root())

0 commit comments

Comments
 (0)