| 
									
										
										
										
											2019-11-20 18:33:36 +01:00
										 |  |  | import ./make-test-python.nix ({ pkgs, ... }: | 
					
						
							| 
									
										
										
										
											2019-04-24 16:23:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | let | 
					
						
							|  |  |  |   port = 1888; | 
					
						
							|  |  |  |   username = "mqtt"; | 
					
						
							|  |  |  |   password = "VERY_secret"; | 
					
						
							|  |  |  |   topic = "test/foo"; | 
					
						
							| 
									
										
										
										
											2019-08-13 21:52:01 +00:00
										 |  |  | in { | 
					
						
							| 
									
										
										
										
											2019-04-24 16:23:13 +08:00
										 |  |  |   name = "mosquitto"; | 
					
						
							|  |  |  |   meta = with pkgs.stdenv.lib; { | 
					
						
							|  |  |  |     maintainers = with maintainers; [ peterhoeg ]; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   nodes = let | 
					
						
							|  |  |  |     client = { pkgs, ... }: { | 
					
						
							|  |  |  |       environment.systemPackages = with pkgs; [ mosquitto ]; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   in { | 
					
						
							|  |  |  |     server = { pkgs, ... }: { | 
					
						
							|  |  |  |       networking.firewall.allowedTCPPorts = [ port ]; | 
					
						
							|  |  |  |       services.mosquitto = { | 
					
						
							|  |  |  |         inherit port; | 
					
						
							|  |  |  |         enable = true; | 
					
						
							|  |  |  |         host = "0.0.0.0"; | 
					
						
							|  |  |  |         checkPasswords = true; | 
					
						
							| 
									
										
										
										
											2019-08-13 21:52:01 +00:00
										 |  |  |         users.${username} = { | 
					
						
							| 
									
										
										
										
											2019-04-24 16:23:13 +08:00
										 |  |  |           inherit password; | 
					
						
							|  |  |  |           acl = [ | 
					
						
							|  |  |  |             "topic readwrite ${topic}" | 
					
						
							|  |  |  |           ]; | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     client1 = client; | 
					
						
							|  |  |  |     client2 = client; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   testScript = let | 
					
						
							|  |  |  |     file = "/tmp/msg"; | 
					
						
							|  |  |  |   in ''
 | 
					
						
							| 
									
										
										
										
											2019-11-20 18:58:24 +01:00
										 |  |  |     def mosquitto_cmd(binary): | 
					
						
							|  |  |  |         return ( | 
					
						
							|  |  |  |             "${pkgs.mosquitto}/bin/mosquitto_{} " | 
					
						
							|  |  |  |             "-V mqttv311 " | 
					
						
							|  |  |  |             "-h server " | 
					
						
							|  |  |  |             "-p ${toString port} " | 
					
						
							|  |  |  |             "-u ${username} " | 
					
						
							|  |  |  |             "-P '${password}' " | 
					
						
							|  |  |  |             "-t ${topic}" | 
					
						
							|  |  |  |         ).format(binary) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def publish(args): | 
					
						
							|  |  |  |         return "{} {}".format(mosquitto_cmd("pub"), args) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def subscribe(args): | 
					
						
							|  |  |  |         return "({} -C 1 {} | tee ${file} &)".format(mosquitto_cmd("sub"), args) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-20 18:33:36 +01:00
										 |  |  |     start_all() | 
					
						
							|  |  |  |     server.wait_for_unit("mosquitto.service") | 
					
						
							| 
									
										
										
										
											2019-06-24 13:59:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-20 18:33:36 +01:00
										 |  |  |     for machine in server, client1, client2: | 
					
						
							|  |  |  |         machine.fail("test -f ${file}") | 
					
						
							| 
									
										
										
										
											2019-06-24 13:59:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # QoS = 0, so only one subscribers should get it | 
					
						
							| 
									
										
										
										
											2019-11-20 18:58:24 +01:00
										 |  |  |     server.execute(subscribe("-q 0")) | 
					
						
							| 
									
										
										
										
											2019-06-24 13:59:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # we need to give the subscribers some time to connect | 
					
						
							| 
									
										
										
										
											2019-11-20 18:33:36 +01:00
										 |  |  |     client2.execute("sleep 5") | 
					
						
							| 
									
										
										
										
											2019-11-20 18:58:24 +01:00
										 |  |  |     client2.succeed(publish("-m FOO -q 0")) | 
					
						
							| 
									
										
										
										
											2019-06-24 13:59:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-20 18:33:36 +01:00
										 |  |  |     server.wait_until_succeeds("grep -q FOO ${file}") | 
					
						
							|  |  |  |     server.execute("rm ${file}") | 
					
						
							| 
									
										
										
										
											2019-06-24 13:59:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # QoS = 1, so both subscribers should get it | 
					
						
							| 
									
										
										
										
											2019-11-20 18:58:24 +01:00
										 |  |  |     server.execute(subscribe("-q 1")) | 
					
						
							|  |  |  |     client1.execute(subscribe("-q 1")) | 
					
						
							| 
									
										
										
										
											2019-04-24 16:23:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-24 13:59:53 +08:00
										 |  |  |     # we need to give the subscribers some time to connect | 
					
						
							| 
									
										
										
										
											2019-11-20 18:33:36 +01:00
										 |  |  |     client2.execute("sleep 5") | 
					
						
							| 
									
										
										
										
											2019-11-20 18:58:24 +01:00
										 |  |  |     client2.succeed(publish("-m BAR -q 1")) | 
					
						
							| 
									
										
										
										
											2019-04-24 16:23:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-20 18:58:24 +01:00
										 |  |  |     for machine in server, client1: | 
					
						
							|  |  |  |         machine.wait_until_succeeds("grep -q BAR ${file}") | 
					
						
							|  |  |  |         machine.execute("rm ${file}") | 
					
						
							| 
									
										
										
										
											2019-04-24 16:23:13 +08:00
										 |  |  |   '';
 | 
					
						
							|  |  |  | }) |