Configurator { void writeConfigToDisk(Optional<TransportSecurityOptions> transportSecurityOptions) { configFilePath.toFile().getParentFile().mkdirs(); try { writeZooKeeperConfigFile(zookeeperServerConfig, transportSecurityOptions); writeMyIdFile(zookeeperServerConfig); transportSecurityOptions.ifPresent(this::writeJksKeystore); } catch (IOException e) { throw new RuntimeException("Error writing zookeeper config", e); } } Configurator(ZookeeperServerConfig zookeeperServerConfig);   }
@Test public void config_is_written_correctly_when_one_server() throws IOException { ZookeeperServerConfig.Builder builder = createConfigBuilderForSingleHost(cfgFile, idFile, jksKeyStoreFile); new Configurator(builder.build()).writeConfigToDisk(Optional.empty()); validateConfigFileSingleHost(cfgFile); validateIdFile(idFile, ""); }
@Test public void config_is_written_correctly_when_multiple_servers() throws IOException { ZookeeperServerConfig.Builder builder = new ZookeeperServerConfig.Builder(); builder.zooKeeperConfigFile(cfgFile.getAbsolutePath()); builder.server(newServer(0, "foo", 123, 321)); builder.server(newServer(1, "bar", 234, 432)); builder.server(newServer(2, "baz", 345, 543)); builder.myidFile(idFile.getAbsolutePath()); builder.myid(1); new Configurator(builder.build()).writeConfigToDisk(Optional.empty()); validateConfigFileMultipleHosts(cfgFile); validateIdFile(idFile, "1\n"); }
@Test public void config_is_written_correctly_with_tls_for_quorum_communication_port_unification() throws IOException { ZookeeperServerConfig.Builder builder = createConfigBuilderForSingleHost(cfgFile, idFile, jksKeyStoreFile); builder.tlsForQuorumCommunication(TlsForQuorumCommunication.PORT_UNIFICATION); builder.tlsForClientServerCommunication(TlsForClientServerCommunication.Enum.PORT_UNIFICATION); Optional<TransportSecurityOptions> transportSecurityOptions = createTransportSecurityOptions(); new Configurator(builder.build()).writeConfigToDisk(transportSecurityOptions); validateConfigFilePortUnification(cfgFile, jksKeyStoreFile, transportSecurityOptions.get().getCaCertificatesFile().get().toFile()); validateThatJksKeyStoreFileExists(jksKeyStoreFile); }
@Test public void config_is_written_correctly_with_tls_for_quorum_communication_tls_with_port_unification() throws IOException { ZookeeperServerConfig.Builder builder = createConfigBuilderForSingleHost(cfgFile, idFile, jksKeyStoreFile); builder.tlsForQuorumCommunication(TlsForQuorumCommunication.TLS_WITH_PORT_UNIFICATION); builder.tlsForClientServerCommunication(TlsForClientServerCommunication.Enum.TLS_WITH_PORT_UNIFICATION); Optional<TransportSecurityOptions> transportSecurityOptions = createTransportSecurityOptions(); new Configurator(builder.build()).writeConfigToDisk(transportSecurityOptions); validateConfigFileTlsWithPortUnification(cfgFile, jksKeyStoreFile, transportSecurityOptions.get().getCaCertificatesFile().get().toFile()); validateThatJksKeyStoreFileExists(jksKeyStoreFile); }
@Test public void config_is_written_correctly_with_tls_for_quorum_communication_tls_only() throws IOException { ZookeeperServerConfig.Builder builder = createConfigBuilderForSingleHost(cfgFile, idFile, jksKeyStoreFile); builder.tlsForQuorumCommunication(TlsForQuorumCommunication.TLS_ONLY); builder.tlsForClientServerCommunication(TlsForClientServerCommunication.Enum.TLS_ONLY); Optional<TransportSecurityOptions> transportSecurityOptions = createTransportSecurityOptions(); new Configurator(builder.build()).writeConfigToDisk(transportSecurityOptions); validateConfigFileTlsOnly(cfgFile, jksKeyStoreFile, transportSecurityOptions.get().getCaCertificatesFile().get().toFile()); validateThatJksKeyStoreFileExists(jksKeyStoreFile); }
@Test(expected = RuntimeException.class) public void require_that_this_id_must_be_present_amongst_servers() { ZookeeperServerConfig.Builder builder = new ZookeeperServerConfig.Builder(); builder.zooKeeperConfigFile(cfgFile.getAbsolutePath()); builder.server(newServer(1, "bar", 234, 432)); builder.server(newServer(2, "baz", 345, 543)); builder.myid(0); new Configurator(builder.build()).writeConfigToDisk(Optional.empty()); }
@Test public void juteMaxBufferCanBeSet() throws IOException { ZookeeperServerConfig.Builder builder = new ZookeeperServerConfig.Builder(); builder.myid(0); File idFile = folder.newFile(); File cfgFile = folder.newFile(); builder.server(new ZookeeperServerConfig.Server.Builder().id(0).hostname("testhost")); builder.zooKeeperConfigFile(cfgFile.getAbsolutePath()); builder.myidFile(idFile.getAbsolutePath()); new Configurator(builder.build()).writeConfigToDisk(Optional.empty()); assertEquals("" + new ZookeeperServerConfig(builder).juteMaxBuffer(), System.getProperty(ZOOKEEPER_JUTE_MAX_BUFFER)); final int max_buffer = 1; builder.juteMaxBuffer(max_buffer); new Configurator(builder.build()).writeConfigToDisk(Optional.empty()); assertEquals("" + max_buffer, System.getProperty(ZOOKEEPER_JUTE_MAX_BUFFER)); }