| 
									
										
										
										
											2019-11-06 16:00:36 +01:00
										 |  |  | import ./make-test-python.nix ({ pkgs, lib, ... }: | 
					
						
							| 
									
										
										
										
											2016-07-22 13:29:35 -04:00
										 |  |  | let | 
					
						
							| 
									
										
										
										
											2017-11-07 14:11:56 +01:00
										 |  |  |   # Change this to test a different version of Cassandra: | 
					
						
							|  |  |  |   testPackage = pkgs.cassandra; | 
					
						
							| 
									
										
										
										
											2019-04-23 05:02:30 +02:00
										 |  |  |   clusterName = "NixOS Automated-Test Cluster"; | 
					
						
							| 
									
										
										
										
											2019-04-23 04:59:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-26 01:13:26 +02:00
										 |  |  |   testRemoteAuth = lib.versionAtLeast testPackage.version "3.11"; | 
					
						
							| 
									
										
										
										
											2019-04-23 05:05:39 +02:00
										 |  |  |   jmxRoles = [{ username = "me"; password = "password"; }]; | 
					
						
							| 
									
										
										
										
											2019-04-26 01:13:26 +02:00
										 |  |  |   jmxRolesFile = ./cassandra-jmx-roles; | 
					
						
							| 
									
										
										
										
											2019-04-23 05:05:39 +02:00
										 |  |  |   jmxAuthArgs = "-u ${(builtins.elemAt jmxRoles 0).username} -pw ${(builtins.elemAt jmxRoles 0).password}"; | 
					
						
							| 
									
										
										
										
											2019-07-31 00:37:27 +02:00
										 |  |  |   jmxPort = 7200;  # Non-standard port so it doesn't accidentally work | 
					
						
							| 
									
										
										
										
											2019-11-06 16:00:36 +01:00
										 |  |  |   jmxPortStr = toString jmxPort; | 
					
						
							| 
									
										
										
										
											2019-04-23 05:05:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-06 16:00:36 +01:00
										 |  |  |   # Would usually be assigned to 512M. | 
					
						
							|  |  |  |   # Set it to a different value, so that we can check whether our config | 
					
						
							|  |  |  |   # actually changes it. | 
					
						
							| 
									
										
										
										
											2019-04-23 05:05:06 +02:00
										 |  |  |   numMaxHeapSize = "400"; | 
					
						
							|  |  |  |   getHeapLimitCommand = ''
 | 
					
						
							| 
									
										
										
										
											2019-11-06 16:00:36 +01:00
										 |  |  |     nodetool info -p ${jmxPortStr} | grep "^Heap Memory" | awk '{print $NF}' | 
					
						
							| 
									
										
										
										
											2019-04-23 05:05:06 +02:00
										 |  |  |   '';
 | 
					
						
							| 
									
										
										
										
											2019-11-06 16:00:36 +01:00
										 |  |  |   checkHeapLimitCommand = pkgs.writeShellScript "check-heap-limit.sh" ''
 | 
					
						
							| 
									
										
										
										
											2019-04-23 05:05:06 +02:00
										 |  |  |     [ 1 -eq "$(echo "$(${getHeapLimitCommand}) < ${numMaxHeapSize}" | ${pkgs.bc}/bin/bc)" ] | 
					
						
							|  |  |  |   '';
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-23 04:59:53 +02:00
										 |  |  |   cassandraCfg = ipAddress: | 
					
						
							| 
									
										
										
										
											2017-11-07 14:11:56 +01:00
										 |  |  |     { enable = true; | 
					
						
							| 
									
										
										
										
											2019-04-23 05:02:30 +02:00
										 |  |  |       inherit clusterName; | 
					
						
							| 
									
										
										
										
											2019-04-23 04:59:53 +02:00
										 |  |  |       listenAddress = ipAddress; | 
					
						
							|  |  |  |       rpcAddress = ipAddress; | 
					
						
							| 
									
										
										
										
											2019-04-23 05:03:07 +02:00
										 |  |  |       seedAddresses = [ "192.168.1.1" ]; | 
					
						
							| 
									
										
										
										
											2017-11-07 14:11:56 +01:00
										 |  |  |       package = testPackage; | 
					
						
							| 
									
										
										
										
											2019-04-23 05:05:06 +02:00
										 |  |  |       maxHeapSize = "${numMaxHeapSize}M"; | 
					
						
							|  |  |  |       heapNewSize = "100M"; | 
					
						
							| 
									
										
										
										
											2019-07-31 00:37:27 +02:00
										 |  |  |       inherit jmxPort; | 
					
						
							| 
									
										
										
										
											2017-11-07 14:11:56 +01:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2019-07-31 00:37:27 +02:00
										 |  |  |   nodeCfg = ipAddress: extra: {pkgs, config, ...}: rec { | 
					
						
							|  |  |  |     environment.systemPackages = [ testPackage ]; | 
					
						
							|  |  |  |     networking = { | 
					
						
							|  |  |  |       firewall.allowedTCPPorts = [ 7000 9042 services.cassandra.jmxPort ]; | 
					
						
							|  |  |  |       useDHCP = false; | 
					
						
							|  |  |  |       interfaces.eth1.ipv4.addresses = pkgs.lib.mkOverride 0 [ | 
					
						
							|  |  |  |         { address = ipAddress; prefixLength = 24; } | 
					
						
							|  |  |  |       ]; | 
					
						
							| 
									
										
										
										
											2016-07-22 13:29:35 -04:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2019-07-31 00:37:27 +02:00
										 |  |  |     services.cassandra = cassandraCfg ipAddress // extra; | 
					
						
							|  |  |  |     virtualisation.memorySize = 1024; | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2016-07-22 13:29:35 -04:00
										 |  |  | in | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-11-06 16:00:36 +01:00
										 |  |  |   name = "cassandra"; | 
					
						
							|  |  |  |   meta = { | 
					
						
							|  |  |  |     maintainers = with lib.maintainers; [ johnazoidberg ]; | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2016-07-22 13:29:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   nodes = { | 
					
						
							| 
									
										
										
										
											2019-04-09 04:02:49 +02:00
										 |  |  |     cass0 = nodeCfg "192.168.1.1" {}; | 
					
						
							| 
									
										
										
										
											2019-04-26 01:13:26 +02:00
										 |  |  |     cass1 = nodeCfg "192.168.1.2" (lib.optionalAttrs testRemoteAuth { inherit jmxRoles; remoteJmx = true; }); | 
					
						
							| 
									
										
										
										
											2019-04-09 04:02:49 +02:00
										 |  |  |     cass2 = nodeCfg "192.168.1.3" { jvmOpts = [ "-Dcassandra.replace_address=cass1" ]; }; | 
					
						
							| 
									
										
										
										
											2016-07-22 13:29:35 -04:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-06 16:00:36 +01:00
										 |  |  |   testScript = ''
 | 
					
						
							| 
									
										
										
										
											2019-04-23 04:59:53 +02:00
										 |  |  |     # Check configuration | 
					
						
							| 
									
										
										
										
											2019-11-06 16:00:36 +01:00
										 |  |  |     with subtest("Timers exist"): | 
					
						
							|  |  |  |         cass0.succeed("systemctl list-timers | grep cassandra-full-repair.timer") | 
					
						
							|  |  |  |         cass0.succeed("systemctl list-timers | grep cassandra-incremental-repair.timer") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with subtest("Can connect via cqlsh"): | 
					
						
							|  |  |  |         cass0.wait_for_unit("cassandra.service") | 
					
						
							|  |  |  |         cass0.wait_until_succeeds("nc -z cass0 9042") | 
					
						
							|  |  |  |         cass0.succeed("echo 'show version;' | cqlsh cass0") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with subtest("Nodetool is operational"): | 
					
						
							|  |  |  |         cass0.wait_for_unit("cassandra.service") | 
					
						
							|  |  |  |         cass0.wait_until_succeeds("nc -z localhost ${jmxPortStr}") | 
					
						
							|  |  |  |         cass0.succeed("nodetool status -p ${jmxPortStr} --resolve-ip | egrep '^UN[[:space:]]+cass0'") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with subtest("Cluster name was set"): | 
					
						
							|  |  |  |         cass0.wait_for_unit("cassandra.service") | 
					
						
							|  |  |  |         cass0.wait_until_succeeds("nc -z localhost ${jmxPortStr}") | 
					
						
							|  |  |  |         cass0.wait_until_succeeds( | 
					
						
							|  |  |  |             "nodetool describecluster -p ${jmxPortStr} | grep 'Name: ${clusterName}'" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with subtest("Heap limit set correctly"): | 
					
						
							|  |  |  |         # Nodetool takes a while until it can display info | 
					
						
							|  |  |  |         cass0.wait_until_succeeds("nodetool info -p ${jmxPortStr}") | 
					
						
							|  |  |  |         cass0.succeed("${checkHeapLimitCommand}") | 
					
						
							| 
									
										
										
										
											2019-04-23 04:59:53 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Check cluster interaction | 
					
						
							| 
									
										
										
										
											2019-11-06 16:00:36 +01:00
										 |  |  |     with subtest("Bring up cluster"): | 
					
						
							|  |  |  |         cass1.wait_for_unit("cassandra.service") | 
					
						
							|  |  |  |         cass1.wait_until_succeeds( | 
					
						
							|  |  |  |             "nodetool -p ${jmxPortStr} ${jmxAuthArgs} status | egrep -c '^UN' | grep 2" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         cass0.succeed("nodetool status -p ${jmxPortStr} --resolve-ip | egrep '^UN[[:space:]]+cass1'") | 
					
						
							| 
									
										
										
										
											2019-04-26 01:13:26 +02:00
										 |  |  |   '' + lib.optionalString testRemoteAuth '' | 
					
						
							| 
									
										
										
										
											2019-11-06 16:00:36 +01:00
										 |  |  |     with subtest("Remote authenticated jmx"): | 
					
						
							|  |  |  |         # Doesn't work if not enabled | 
					
						
							|  |  |  |         cass0.wait_until_succeeds("nc -z localhost ${jmxPortStr}") | 
					
						
							|  |  |  |         cass1.fail("nc -z 192.168.1.1 ${jmxPortStr}") | 
					
						
							|  |  |  |         cass1.fail("nodetool -p ${jmxPortStr} -h 192.168.1.1 status") | 
					
						
							| 
									
										
										
										
											2019-04-23 05:05:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-06 16:00:36 +01:00
										 |  |  |         # Works if enabled | 
					
						
							|  |  |  |         cass1.wait_until_succeeds("nc -z localhost ${jmxPortStr}") | 
					
						
							|  |  |  |         cass0.succeed("nodetool -p ${jmxPortStr} -h 192.168.1.2 ${jmxAuthArgs} status") | 
					
						
							| 
									
										
										
										
											2019-04-26 01:13:26 +02:00
										 |  |  |   '' + '' | 
					
						
							| 
									
										
										
										
											2019-11-06 16:00:36 +01:00
										 |  |  |     with subtest("Break and fix node"): | 
					
						
							|  |  |  |         cass1.block() | 
					
						
							|  |  |  |         cass0.wait_until_succeeds( | 
					
						
							|  |  |  |             "nodetool status -p ${jmxPortStr} --resolve-ip | egrep -c '^DN[[:space:]]+cass1'" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         cass0.succeed("nodetool status -p ${jmxPortStr} | egrep -c '^UN'  | grep 1") | 
					
						
							|  |  |  |         cass1.unblock() | 
					
						
							|  |  |  |         cass1.wait_until_succeeds( | 
					
						
							|  |  |  |             "nodetool -p ${jmxPortStr} ${jmxAuthArgs} status | egrep -c '^UN'  | grep 2" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         cass0.succeed("nodetool status -p ${jmxPortStr} | egrep -c '^UN'  | grep 2") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with subtest("Replace crashed node"): | 
					
						
							|  |  |  |         cass1.block()  # .crash() waits until it's fully shutdown | 
					
						
							|  |  |  |         cass2.start() | 
					
						
							|  |  |  |         cass0.wait_until_fails( | 
					
						
							|  |  |  |             "nodetool status -p ${jmxPortStr} --resolve-ip | egrep '^UN[[:space:]]+cass1'" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         cass2.wait_for_unit("cassandra.service") | 
					
						
							|  |  |  |         cass0.wait_until_succeeds( | 
					
						
							|  |  |  |             "nodetool status -p ${jmxPortStr} --resolve-ip | egrep '^UN[[:space:]]+cass2'" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2016-07-22 13:29:35 -04:00
										 |  |  |   '';
 | 
					
						
							|  |  |  | }) |