1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.apache.zookeeper.server.embedded;
19 
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22 import java.io.IOException;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.util.Properties;
26 import org.apache.zookeeper.PortAssignment;
27 import org.apache.zookeeper.test.ClientBase;
28 import org.junit.jupiter.api.AfterAll;
29 import org.junit.jupiter.api.BeforeAll;
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.api.io.TempDir;
32 
33 public class ZookeeperServerClusterTest {
34 
35     @BeforeAll
setUpEnvironment()36     public static void setUpEnvironment() {
37         System.setProperty("zookeeper.admin.enableServer", "false");
38         System.setProperty("zookeeper.4lw.commands.whitelist", "*");
39     }
40 
41     @AfterAll
cleanUpEnvironment()42     public static void cleanUpEnvironment() throws InterruptedException, IOException {
43         System.clearProperty("zookeeper.admin.enableServer");
44         System.clearProperty("zookeeper.4lw.commands.whitelist");
45     }
46 
47     @TempDir
48     public Path baseDir;
49 
50     @Test
testStart()51     public void testStart() throws Exception {
52         Path baseDir1 = baseDir.resolve("server1");
53         Path baseDir2 = baseDir.resolve("server2");
54         Path baseDir3 = baseDir.resolve("server3");
55 
56         int clientport1 = PortAssignment.unique();
57         int clientport2 = PortAssignment.unique();
58         int clientport3 = PortAssignment.unique();
59 
60         int port4 = PortAssignment.unique();
61         int port5 = PortAssignment.unique();
62         int port6 = PortAssignment.unique();
63 
64         int port7 = PortAssignment.unique();
65         int port8 = PortAssignment.unique();
66         int port9 = PortAssignment.unique();
67 
68         Properties config = new Properties();
69         config.put("host", "localhost");
70         config.put("ticktime", "10");
71         config.put("initLimit", "4000");
72         config.put("syncLimit", "5");
73         config.put("server.1", "localhost:" + port4 + ":" + port7);
74         config.put("server.2", "localhost:" + port5 + ":" + port8);
75         config.put("server.3", "localhost:" + port6 + ":" + port9);
76 
77 
78         final Properties configZookeeper1 = new Properties();
79         configZookeeper1.putAll(config);
80         configZookeeper1.put("clientPort", clientport1 + "");
81 
82         final Properties configZookeeper2 = new Properties();
83         configZookeeper2.putAll(config);
84         configZookeeper2.put("clientPort", clientport2 + "");
85 
86         final Properties configZookeeper3 =  new Properties();
87         configZookeeper3.putAll(config);
88         configZookeeper3.put("clientPort", clientport3 + "");
89 
90         Files.createDirectories(baseDir1.resolve("data"));
91         Files.write(baseDir1.resolve("data").resolve("myid"), "1".getBytes("ASCII"));
92         Files.createDirectories(baseDir2.resolve("data"));
93         Files.write(baseDir2.resolve("data").resolve("myid"), "2".getBytes("ASCII"));
94         Files.createDirectories(baseDir3.resolve("data"));
95         Files.write(baseDir3.resolve("data").resolve("myid"), "3".getBytes("ASCII"));
96 
97         try (ZooKeeperServerEmbedded zkServer1 = ZooKeeperServerEmbedded.builder().configuration(configZookeeper1).baseDir(baseDir1).exitHandler(ExitHandler.LOG_ONLY).build();
98                 ZooKeeperServerEmbedded zkServer2 = ZooKeeperServerEmbedded.builder().configuration(configZookeeper2).baseDir(baseDir2).exitHandler(ExitHandler.LOG_ONLY).build();
99                 ZooKeeperServerEmbedded zkServer3 = ZooKeeperServerEmbedded.builder().configuration(configZookeeper3).baseDir(baseDir3).exitHandler(ExitHandler.LOG_ONLY).build();) {
100             zkServer1.start();
101             zkServer2.start();
102             zkServer3.start();
103 
104             assertTrue(ClientBase.waitForServerUp("localhost:" + clientport1, 60000));
105             assertTrue(ClientBase.waitForServerUp("localhost:" + clientport2, 60000));
106             assertTrue(ClientBase.waitForServerUp("localhost:" + clientport3, 60000));
107             for (int i = 0; i < 100; i++) {
108                 ZookeeperServeInfo.ServerInfo status = ZookeeperServeInfo.getStatus("ReplicatedServer*");
109                 System.out.println("status:" + status);
110                 if (status.isLeader() && !status.isStandaloneMode() && status.getPeers().size() == 3) {
111                     break;
112                 }
113                 Thread.sleep(100);
114             }
115             ZookeeperServeInfo.ServerInfo status = ZookeeperServeInfo.getStatus("ReplicatedServer*");
116             assertTrue(status.isLeader());
117             assertTrue(!status.isStandaloneMode());
118             assertEquals(3, status.getPeers().size());
119 
120         }
121     }
122 
123 }
124