@@ -77,8 +77,8 @@ static Service *service_for_path(Manager *m, Path *path, const char *service_nam
7777 return SERVICE (service_unit );
7878}
7979
80- static void _check_states (unsigned line ,
81- Manager * m , Path * path , Service * service , PathState path_state , ServiceState service_state ) {
80+ static int _check_states (unsigned line ,
81+ Manager * m , Path * path , Service * service , PathState path_state , ServiceState service_state ) {
8282 assert_se (m );
8383 assert_se (service );
8484
@@ -102,11 +102,24 @@ static void _check_states(unsigned line,
102102 service_state_to_string (service -> state ),
103103 service_result_to_string (service -> result ));
104104
105+ if (service -> state == SERVICE_FAILED &&
106+ service -> main_exec_status .status == EXIT_CGROUP )
107+ /* On a general purpose system we may fail to start the service for reasons which are
108+ * not under our control: permission limits, resource exhaustion, etc. Let's skip the
109+ * test in those cases. */
110+ return log_notice_errno (SYNTHETIC_ERRNO (ECANCELED ),
111+ "Failed to start service %s, aborting test: %s/%s" ,
112+ UNIT (service )-> id ,
113+ service_state_to_string (service -> state ),
114+ service_result_to_string (service -> result ));
115+
105116 if (n >= end ) {
106117 log_error ("Test timeout when testing %s" , UNIT (path )-> id );
107118 exit (EXIT_FAILURE );
108119 }
109120 }
121+
122+ return 0 ;
110123}
111124#define check_states (...) _check_states(__LINE__, __VA_ARGS__)
112125
@@ -124,18 +137,22 @@ static void test_path_exists(Manager *m) {
124137 service = service_for_path (m , path , NULL );
125138
126139 assert_se (unit_start (unit ) >= 0 );
127- check_states (m , path , service , PATH_WAITING , SERVICE_DEAD );
140+ if (check_states (m , path , service , PATH_WAITING , SERVICE_DEAD ) < 0 )
141+ return ;
128142
129143 assert_se (touch (test_path ) >= 0 );
130- check_states (m , path , service , PATH_RUNNING , SERVICE_RUNNING );
144+ if (check_states (m , path , service , PATH_RUNNING , SERVICE_RUNNING ) < 0 )
145+ return ;
131146
132147 /* Service restarts if file still exists */
133148 assert_se (unit_stop (UNIT (service )) >= 0 );
134- check_states (m , path , service , PATH_RUNNING , SERVICE_RUNNING );
149+ if (check_states (m , path , service , PATH_RUNNING , SERVICE_RUNNING ) < 0 )
150+ return ;
135151
136152 assert_se (rm_rf (test_path , REMOVE_ROOT |REMOVE_PHYSICAL ) == 0 );
137153 assert_se (unit_stop (UNIT (service )) >= 0 );
138- check_states (m , path , service , PATH_WAITING , SERVICE_DEAD );
154+ if (check_states (m , path , service , PATH_WAITING , SERVICE_DEAD ) < 0 )
155+ return ;
139156
140157 assert_se (unit_stop (unit ) >= 0 );
141158}
@@ -154,18 +171,22 @@ static void test_path_existsglob(Manager *m) {
154171 service = service_for_path (m , path , NULL );
155172
156173 assert_se (unit_start (unit ) >= 0 );
157- check_states (m , path , service , PATH_WAITING , SERVICE_DEAD );
174+ if (check_states (m , path , service , PATH_WAITING , SERVICE_DEAD ) < 0 )
175+ return ;
158176
159177 assert_se (touch (test_path ) >= 0 );
160- check_states (m , path , service , PATH_RUNNING , SERVICE_RUNNING );
178+ if (check_states (m , path , service , PATH_RUNNING , SERVICE_RUNNING ) < 0 )
179+ return ;
161180
162181 /* Service restarts if file still exists */
163182 assert_se (unit_stop (UNIT (service )) >= 0 );
164- check_states (m , path , service , PATH_RUNNING , SERVICE_RUNNING );
183+ if (check_states (m , path , service , PATH_RUNNING , SERVICE_RUNNING ) < 0 )
184+ return ;
165185
166186 assert_se (rm_rf (test_path , REMOVE_ROOT |REMOVE_PHYSICAL ) == 0 );
167187 assert_se (unit_stop (UNIT (service )) >= 0 );
168- check_states (m , path , service , PATH_WAITING , SERVICE_DEAD );
188+ if (check_states (m , path , service , PATH_WAITING , SERVICE_DEAD ) < 0 )
189+ return ;
169190
170191 assert_se (unit_stop (unit ) >= 0 );
171192}
@@ -185,23 +206,28 @@ static void test_path_changed(Manager *m) {
185206 service = service_for_path (m , path , NULL );
186207
187208 assert_se (unit_start (unit ) >= 0 );
188- check_states (m , path , service , PATH_WAITING , SERVICE_DEAD );
209+ if (check_states (m , path , service , PATH_WAITING , SERVICE_DEAD ) < 0 )
210+ return ;
189211
190212 assert_se (touch (test_path ) >= 0 );
191- check_states (m , path , service , PATH_RUNNING , SERVICE_RUNNING );
213+ if (check_states (m , path , service , PATH_RUNNING , SERVICE_RUNNING ) < 0 )
214+ return ;
192215
193216 /* Service does not restart if file still exists */
194217 assert_se (unit_stop (UNIT (service )) >= 0 );
195- check_states (m , path , service , PATH_WAITING , SERVICE_DEAD );
218+ if (check_states (m , path , service , PATH_WAITING , SERVICE_DEAD ) < 0 )
219+ return ;
196220
197221 f = fopen (test_path , "w" );
198222 assert_se (f );
199223 fclose (f );
200224
201- check_states (m , path , service , PATH_RUNNING , SERVICE_RUNNING );
225+ if (check_states (m , path , service , PATH_RUNNING , SERVICE_RUNNING ) < 0 )
226+ return ;
202227
203228 assert_se (unit_stop (UNIT (service )) >= 0 );
204- check_states (m , path , service , PATH_WAITING , SERVICE_DEAD );
229+ if (check_states (m , path , service , PATH_WAITING , SERVICE_DEAD ) < 0 )
230+ return ;
205231
206232 (void ) rm_rf (test_path , REMOVE_ROOT |REMOVE_PHYSICAL );
207233 assert_se (unit_stop (unit ) >= 0 );
@@ -222,23 +248,28 @@ static void test_path_modified(Manager *m) {
222248 service = service_for_path (m , path , NULL );
223249
224250 assert_se (unit_start (unit ) >= 0 );
225- check_states (m , path , service , PATH_WAITING , SERVICE_DEAD );
251+ if (check_states (m , path , service , PATH_WAITING , SERVICE_DEAD ) < 0 )
252+ return ;
226253
227254 assert_se (touch (test_path ) >= 0 );
228- check_states (m , path , service , PATH_RUNNING , SERVICE_RUNNING );
255+ if (check_states (m , path , service , PATH_RUNNING , SERVICE_RUNNING ) < 0 )
256+ return ;
229257
230258 /* Service does not restart if file still exists */
231259 assert_se (unit_stop (UNIT (service )) >= 0 );
232- check_states (m , path , service , PATH_WAITING , SERVICE_DEAD );
260+ if (check_states (m , path , service , PATH_WAITING , SERVICE_DEAD ) < 0 )
261+ return ;
233262
234263 f = fopen (test_path , "w" );
235264 assert_se (f );
236265 fputs ("test" , f );
237266
238- check_states (m , path , service , PATH_RUNNING , SERVICE_RUNNING );
267+ if (check_states (m , path , service , PATH_RUNNING , SERVICE_RUNNING ) < 0 )
268+ return ;
239269
240270 assert_se (unit_stop (UNIT (service )) >= 0 );
241- check_states (m , path , service , PATH_WAITING , SERVICE_DEAD );
271+ if (check_states (m , path , service , PATH_WAITING , SERVICE_DEAD ) < 0 )
272+ return ;
242273
243274 (void ) rm_rf (test_path , REMOVE_ROOT |REMOVE_PHYSICAL );
244275 assert_se (unit_stop (unit ) >= 0 );
@@ -258,14 +289,17 @@ static void test_path_unit(Manager *m) {
258289 service = service_for_path (m , path , "path-mycustomunit.service" );
259290
260291 assert_se (unit_start (unit ) >= 0 );
261- check_states (m , path , service , PATH_WAITING , SERVICE_DEAD );
292+ if (check_states (m , path , service , PATH_WAITING , SERVICE_DEAD ) < 0 )
293+ return ;
262294
263295 assert_se (touch (test_path ) >= 0 );
264- check_states (m , path , service , PATH_RUNNING , SERVICE_RUNNING );
296+ if (check_states (m , path , service , PATH_RUNNING , SERVICE_RUNNING ) < 0 )
297+ return ;
265298
266299 assert_se (rm_rf (test_path , REMOVE_ROOT |REMOVE_PHYSICAL ) == 0 );
267300 assert_se (unit_stop (UNIT (service )) >= 0 );
268- check_states (m , path , service , PATH_WAITING , SERVICE_DEAD );
301+ if (check_states (m , path , service , PATH_WAITING , SERVICE_DEAD ) < 0 )
302+ return ;
269303
270304 assert_se (unit_stop (unit ) >= 0 );
271305}
@@ -286,22 +320,26 @@ static void test_path_directorynotempty(Manager *m) {
286320 assert_se (access (test_path , F_OK ) < 0 );
287321
288322 assert_se (unit_start (unit ) >= 0 );
289- check_states (m , path , service , PATH_WAITING , SERVICE_DEAD );
323+ if (check_states (m , path , service , PATH_WAITING , SERVICE_DEAD ) < 0 )
324+ return ;
290325
291326 /* MakeDirectory default to no */
292327 assert_se (access (test_path , F_OK ) < 0 );
293328
294329 assert_se (mkdir_p (test_path , 0755 ) >= 0 );
295330 assert_se (touch (strjoina (test_path , "test_file" )) >= 0 );
296- check_states (m , path , service , PATH_RUNNING , SERVICE_RUNNING );
331+ if (check_states (m , path , service , PATH_RUNNING , SERVICE_RUNNING ) < 0 )
332+ return ;
297333
298334 /* Service restarts if directory is still not empty */
299335 assert_se (unit_stop (UNIT (service )) >= 0 );
300- check_states (m , path , service , PATH_RUNNING , SERVICE_RUNNING );
336+ if (check_states (m , path , service , PATH_RUNNING , SERVICE_RUNNING ) < 0 )
337+ return ;
301338
302339 assert_se (rm_rf (test_path , REMOVE_ROOT |REMOVE_PHYSICAL ) == 0 );
303340 assert_se (unit_stop (UNIT (service )) >= 0 );
304- check_states (m , path , service , PATH_WAITING , SERVICE_DEAD );
341+ if (check_states (m , path , service , PATH_WAITING , SERVICE_DEAD ) < 0 )
342+ return ;
305343
306344 assert_se (unit_stop (unit ) >= 0 );
307345}
0 commit comments