1# Run:
2# nix-instantiate --eval nixos/modules/system/service/portable/test.nix
3let
4 lib = import ../../../../../lib;
5
6 inherit (lib) mkOption types;
7
8 portable-lib = import ./lib.nix { inherit lib; };
9
10 configured = portable-lib.configure {
11 serviceManagerPkgs = throw "do not use pkgs in this test";
12 extraRootModules = [ ];
13 extraRootSpecialArgs = { };
14 };
15
16 dummyPkg =
17 name:
18 derivation {
19 system = "dummy";
20 name = name;
21 builder = "/bin/false";
22 };
23
24 exampleConfig = {
25 _file = "${__curPos.file}:${toString __curPos.line}";
26 services = {
27 service1 = {
28 process = {
29 argv = [
30 "/usr/bin/echo" # *giggles*
31 "hello"
32 ];
33 };
34 assertions = [
35 {
36 assertion = false;
37 message = "you can't enable this for that reason";
38 }
39 ];
40 warnings = [
41 "The `foo' service is deprecated and will go away soon!"
42 ];
43 };
44 service2 = {
45 process = {
46 # No meta.mainProgram, because it's supposedly an executable script _file_,
47 # not a directory with a bin directory containing the main program.
48 argv = [
49 (dummyPkg "cowsay.sh")
50 "world"
51 ];
52 };
53 };
54 service3 = {
55 process = {
56 argv = [ "/bin/false" ];
57 };
58 services.exclacow = {
59 process = {
60 argv = [
61 (lib.getExe (
62 dummyPkg "cowsay-ng"
63 // {
64 meta.mainProgram = "cowsay";
65 }
66 ))
67 "!"
68 ];
69 };
70 assertions = [
71 {
72 assertion = false;
73 message = "you can't enable this for such reason";
74 }
75 ];
76 warnings = [
77 "The `bar' service is deprecated and will go away soon!"
78 ];
79 };
80 };
81 };
82 };
83
84 exampleEval = lib.evalModules {
85 modules = [
86 {
87 options.services = mkOption {
88 type = types.attrsOf configured.serviceSubmodule;
89 };
90 }
91 exampleConfig
92 ];
93 };
94
95 filterEval =
96 config:
97 lib.optionalAttrs (config ? process) {
98 inherit (config) assertions warnings process;
99 }
100 // {
101 services = lib.mapAttrs (k: filterEval) config.services;
102 };
103
104 test =
105 assert
106 filterEval exampleEval.config == {
107 services = {
108 service1 = {
109 process = {
110 argv = [
111 "/usr/bin/echo"
112 "hello"
113 ];
114 };
115 services = { };
116 assertions = [
117 {
118 assertion = false;
119 message = "you can't enable this for that reason";
120 }
121 ];
122 warnings = [
123 "The `foo' service is deprecated and will go away soon!"
124 ];
125 };
126 service2 = {
127 process = {
128 argv = [
129 "${dummyPkg "cowsay.sh"}"
130 "world"
131 ];
132 };
133 services = { };
134 assertions = [ ];
135 warnings = [ ];
136 };
137 service3 = {
138 process = {
139 argv = [ "/bin/false" ];
140 };
141 services.exclacow = {
142 process = {
143 argv = [
144 "${dummyPkg "cowsay-ng"}/bin/cowsay"
145 "!"
146 ];
147 };
148 services = { };
149 assertions = [
150 {
151 assertion = false;
152 message = "you can't enable this for such reason";
153 }
154 ];
155 warnings = [ "The `bar' service is deprecated and will go away soon!" ];
156 };
157 assertions = [ ];
158 warnings = [ ];
159 };
160 };
161 };
162
163 assert
164 portable-lib.getWarnings [ "service1" ] exampleEval.config.services.service1 == [
165 "in service1: The `foo' service is deprecated and will go away soon!"
166 ];
167
168 assert
169 portable-lib.getAssertions [ "service1" ] exampleEval.config.services.service1 == [
170 {
171 message = "in service1: you can't enable this for that reason";
172 assertion = false;
173 }
174 ];
175
176 assert
177 portable-lib.getWarnings [ "service3" ] exampleEval.config.services.service3 == [
178 "in service3.services.exclacow: The `bar' service is deprecated and will go away soon!"
179 ];
180 assert
181 portable-lib.getAssertions [ "service3" ] exampleEval.config.services.service3 == [
182 {
183 message = "in service3.services.exclacow: you can't enable this for such reason";
184 assertion = false;
185 }
186 ];
187
188 "ok";
189
190in
191test