| 
									
										
										
										
											2019-11-06 13:56:16 +01:00
										 |  |  | import ./make-test-python.nix ({ pkgs, ... }: { | 
					
						
							| 
									
										
										
										
											2019-10-09 13:32:03 +02:00
										 |  |  |   name = "caddy"; | 
					
						
							|  |  |  |   meta = with pkgs.stdenv.lib.maintainers; { | 
					
						
							| 
									
										
										
										
											2020-05-08 09:39:24 +00:00
										 |  |  |     maintainers = [ xfix filalex77 ]; | 
					
						
							| 
									
										
										
										
											2019-10-09 13:32:03 +02:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   nodes = { | 
					
						
							|  |  |  |     webserver = { pkgs, lib, ... }: { | 
					
						
							|  |  |  |       services.caddy.enable = true; | 
					
						
							|  |  |  |       services.caddy.config = ''
 | 
					
						
							|  |  |  |         http://localhost { | 
					
						
							| 
									
										
										
										
											2020-05-08 09:39:24 +00:00
										 |  |  |           encode gzip | 
					
						
							| 
									
										
										
										
											2019-10-09 13:32:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-07 09:42:00 +02:00
										 |  |  |           file_server | 
					
						
							| 
									
										
										
										
											2020-05-08 09:39:24 +00:00
										 |  |  |           root * ${ | 
					
						
							| 
									
										
										
										
											2019-10-09 13:32:03 +02:00
										 |  |  |             pkgs.runCommand "testdir" {} ''
 | 
					
						
							|  |  |  |               mkdir "$out" | 
					
						
							|  |  |  |               echo hello world > "$out/example.html" | 
					
						
							|  |  |  |             ''
 | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-05 17:07:20 -05:00
										 |  |  |       specialisation.etag.configuration = { | 
					
						
							|  |  |  |         services.caddy.config = lib.mkForce ''
 | 
					
						
							|  |  |  |           http://localhost { | 
					
						
							| 
									
										
										
										
											2020-05-08 09:39:24 +00:00
										 |  |  |             encode gzip | 
					
						
							| 
									
										
										
										
											2019-10-09 13:32:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-07 09:42:00 +02:00
										 |  |  |             file_server | 
					
						
							| 
									
										
										
										
											2020-05-08 09:39:24 +00:00
										 |  |  |             root * ${ | 
					
						
							| 
									
										
										
										
											2020-03-05 17:07:20 -05:00
										 |  |  |               pkgs.runCommand "testdir2" {} ''
 | 
					
						
							|  |  |  |                 mkdir "$out" | 
					
						
							|  |  |  |                 echo changed > "$out/example.html" | 
					
						
							|  |  |  |               ''
 | 
					
						
							| 
									
										
										
										
											2019-10-09 13:32:03 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-03-05 17:07:20 -05:00
										 |  |  |           } | 
					
						
							|  |  |  |         '';
 | 
					
						
							|  |  |  |       }; | 
					
						
							| 
									
										
										
										
											2019-10-09 13:32:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-05 17:07:20 -05:00
										 |  |  |       specialisation.config-reload.configuration = { | 
					
						
							|  |  |  |         services.caddy.config = ''
 | 
					
						
							|  |  |  |           http://localhost:8080 { | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         '';
 | 
					
						
							|  |  |  |       }; | 
					
						
							| 
									
										
										
										
											2019-10-09 13:32:03 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   testScript = { nodes, ... }: let | 
					
						
							| 
									
										
										
										
											2020-03-05 17:07:20 -05:00
										 |  |  |     etagSystem = "${nodes.webserver.config.system.build.toplevel}/specialisation/etag"; | 
					
						
							|  |  |  |     justReloadSystem = "${nodes.webserver.config.system.build.toplevel}/specialisation/config-reload"; | 
					
						
							| 
									
										
										
										
											2019-10-09 13:32:03 +02:00
										 |  |  |   in ''
 | 
					
						
							| 
									
										
										
										
											2019-11-06 13:56:16 +01:00
										 |  |  |     url = "http://localhost/example.html" | 
					
						
							|  |  |  |     webserver.wait_for_unit("caddy") | 
					
						
							|  |  |  |     webserver.wait_for_open_port("80") | 
					
						
							| 
									
										
										
										
											2019-10-09 13:32:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-06 13:56:16 +01:00
										 |  |  |     def check_etag(url): | 
					
						
							|  |  |  |         etag = webserver.succeed( | 
					
						
							| 
									
										
										
										
											2020-09-16 09:03:04 -07:00
										 |  |  |             "curl --fail -v '{}' 2>&1 | sed -n -e \"s/^< [Ee][Tt][Aa][Gg]: *//p\"".format( | 
					
						
							|  |  |  |                 url | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2019-11-06 13:56:16 +01:00
										 |  |  |         ) | 
					
						
							|  |  |  |         etag = etag.replace("\r\n", " ") | 
					
						
							|  |  |  |         http_code = webserver.succeed( | 
					
						
							| 
									
										
										
										
											2020-09-16 09:03:04 -07:00
										 |  |  |             "curl --fail --silent --show-error -o /dev/null -w \"%{{http_code}}\" --head -H 'If-None-Match: {}' {}".format( | 
					
						
							| 
									
										
										
										
											2020-09-07 09:42:00 +02:00
										 |  |  |                 etag, url | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2019-11-06 13:56:16 +01:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-09-07 09:42:00 +02:00
										 |  |  |         assert int(http_code) == 304, "HTTP code is {}, expected 304".format(http_code) | 
					
						
							| 
									
										
										
										
											2019-11-06 13:56:16 +01:00
										 |  |  |         return etag | 
					
						
							| 
									
										
										
										
											2019-10-09 13:32:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-06 13:56:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     with subtest("check ETag if serving Nix store paths"): | 
					
						
							|  |  |  |         old_etag = check_etag(url) | 
					
						
							|  |  |  |         webserver.succeed( | 
					
						
							|  |  |  |             "${etagSystem}/bin/switch-to-configuration test >&2" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         webserver.sleep(1) | 
					
						
							|  |  |  |         new_etag = check_etag(url) | 
					
						
							|  |  |  |         assert old_etag != new_etag, "Old ETag {} is the same as {}".format( | 
					
						
							|  |  |  |             old_etag, new_etag | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-03-05 17:07:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-06 13:56:16 +01:00
										 |  |  |     with subtest("config is reloaded on nixos-rebuild switch"): | 
					
						
							|  |  |  |         webserver.succeed( | 
					
						
							|  |  |  |             "${justReloadSystem}/bin/switch-to-configuration test >&2" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         webserver.wait_for_open_port("8080") | 
					
						
							| 
									
										
										
										
											2019-10-09 13:32:03 +02:00
										 |  |  |   '';
 | 
					
						
							|  |  |  | }) |