From d2193f988c076bffe81ad9391cd9437968468209 Mon Sep 17 00:00:00 2001 From: IgorPerikov Date: Thu, 8 Mar 2018 22:07:35 +0300 Subject: [PATCH 1/5] build: add testcontainers-java dependency --- jetcd-core/pom.xml | 5 +++++ pom.xml | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/jetcd-core/pom.xml b/jetcd-core/pom.xml index d610191fc..2e9195b53 100644 --- a/jetcd-core/pom.xml +++ b/jetcd-core/pom.xml @@ -98,6 +98,11 @@ ${os.detected.classifier} test + + org.testcontainers + testcontainers + test + diff --git a/pom.xml b/pom.xml index 0ec7930b3..7f8225c08 100644 --- a/pom.xml +++ b/pom.xml @@ -121,6 +121,7 @@ 3.9.0 4.12 2.0.7.Final + 1.5.1 @@ -263,6 +264,12 @@ ${netty-tcnative.version} test + + org.testcontainers + testcontainers + ${testcontainers.version} + test + From 24714d4713ec5ed953909b96b786b932aadcbbf2 Mon Sep 17 00:00:00 2001 From: IgorPerikov Date: Thu, 8 Mar 2018 22:08:40 +0300 Subject: [PATCH 2/5] test: introduce EtcdCluster abstraction use testcontainers-java for cleaner test environment Fixes #106 --- .../jetcd/internal/impl/TestConstants.java | 12 +-- .../infrastructure/ClusterFactory.java | 41 +++++++++ .../internal/infrastructure/EtcdCluster.java | 33 +++++++ .../infrastructure/SingleNodeEtcdCluster.java | 74 +++++++++++++++ .../SingleNodeSslEtcdCluster.java | 79 ++++++++++++++++ .../infrastructure/ThreeNodeEtcdCluster.java | 92 +++++++++++++++++++ 6 files changed, 322 insertions(+), 9 deletions(-) create mode 100644 jetcd-core/src/test/java/com/coreos/jetcd/internal/infrastructure/ClusterFactory.java create mode 100644 jetcd-core/src/test/java/com/coreos/jetcd/internal/infrastructure/EtcdCluster.java create mode 100644 jetcd-core/src/test/java/com/coreos/jetcd/internal/infrastructure/SingleNodeEtcdCluster.java create mode 100644 jetcd-core/src/test/java/com/coreos/jetcd/internal/infrastructure/SingleNodeSslEtcdCluster.java create mode 100644 jetcd-core/src/test/java/com/coreos/jetcd/internal/infrastructure/ThreeNodeEtcdCluster.java diff --git a/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/TestConstants.java b/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/TestConstants.java index 2addba1bb..646842cf8 100644 --- a/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/TestConstants.java +++ b/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/TestConstants.java @@ -19,18 +19,12 @@ * Test constants, contain the cluster info. */ public class TestConstants { + public static final String ETCD_DOCKER_IMAGE_NAME = "gcr.io/etcd-development/etcd:v3.3"; - public static final String[] endpoints = System.getProperty( - "etcd.endpoints", - "http://localhost:12379,http://localhost:22379,http://localhost:32379").split(","); - - public static final String[] peerUrls = System.getProperty( - "etcd.peerurls", - "http://localhost:12380,http://localhost:22380,http://localhost:32380").split(","); + public static final int ETCD_CLIENT_PORT = 2379; + public static final int ETCD_PEER_PORT = 2380; public static final String DEFAULT_SSL_AUTHORITY = "etcd-ssl"; - public static final String DEFAULT_SSL_ENDPOINTS = "https://127.0.0.1:42379"; - public static final String DEFAULT_SSL_CA_PATH = "/ssl/cert/ca.pem"; } diff --git a/jetcd-core/src/test/java/com/coreos/jetcd/internal/infrastructure/ClusterFactory.java b/jetcd-core/src/test/java/com/coreos/jetcd/internal/infrastructure/ClusterFactory.java new file mode 100644 index 000000000..de1274d64 --- /dev/null +++ b/jetcd-core/src/test/java/com/coreos/jetcd/internal/infrastructure/ClusterFactory.java @@ -0,0 +1,41 @@ +/** + * Copyright 2017 The jetcd authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.coreos.jetcd.internal.infrastructure; + +import org.testcontainers.containers.Network; + +import javax.annotation.Nonnull; + +public class ClusterFactory { + @Nonnull + public static EtcdCluster buildThreeNodeCluster(@Nonnull String clusterName) { + return new ThreeNodeEtcdCluster(buildNetwork(clusterName)); + } + + @Nonnull + public static EtcdCluster buildSingleNodeCluster(@Nonnull String clusterName) { + return new SingleNodeEtcdCluster(buildNetwork(clusterName)); + } + + @Nonnull + public static EtcdCluster buildSingleNodeClusterWithSsl(@Nonnull String clusterName) { + return new SingleNodeSslEtcdCluster(buildNetwork(clusterName)); + } + + private static Network buildNetwork(String clusterName) { + return Network.builder().id(clusterName).build(); + } +} diff --git a/jetcd-core/src/test/java/com/coreos/jetcd/internal/infrastructure/EtcdCluster.java b/jetcd-core/src/test/java/com/coreos/jetcd/internal/infrastructure/EtcdCluster.java new file mode 100644 index 000000000..49da20a7e --- /dev/null +++ b/jetcd-core/src/test/java/com/coreos/jetcd/internal/infrastructure/EtcdCluster.java @@ -0,0 +1,33 @@ +/** + * Copyright 2017 The jetcd authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.coreos.jetcd.internal.infrastructure; + +import com.coreos.jetcd.Client; + +import javax.annotation.Nonnull; +import java.io.Closeable; +import java.util.List; + +public interface EtcdCluster extends Closeable { + @Nonnull + Client getClient(); + + @Nonnull + List getClientEndpoints(); + + @Nonnull + List getPeerEndpoints(); +} diff --git a/jetcd-core/src/test/java/com/coreos/jetcd/internal/infrastructure/SingleNodeEtcdCluster.java b/jetcd-core/src/test/java/com/coreos/jetcd/internal/infrastructure/SingleNodeEtcdCluster.java new file mode 100644 index 000000000..1fa115b30 --- /dev/null +++ b/jetcd-core/src/test/java/com/coreos/jetcd/internal/infrastructure/SingleNodeEtcdCluster.java @@ -0,0 +1,74 @@ +/** + * Copyright 2017 The jetcd authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.coreos.jetcd.internal.infrastructure; + +import com.coreos.jetcd.Client; +import com.coreos.jetcd.internal.impl.TestUtil; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.Network; + +import javax.annotation.Nonnull; +import java.util.Collections; +import java.util.List; + +import static com.coreos.jetcd.internal.impl.TestConstants.*; + +public class SingleNodeEtcdCluster implements EtcdCluster { + private final GenericContainer nodeContainer; + private final List clientEndpoints; + private final List peerEndpoints; + + SingleNodeEtcdCluster(Network network) { + nodeContainer = new GenericContainer(ETCD_DOCKER_IMAGE_NAME) + .withExposedPorts(ETCD_CLIENT_PORT, ETCD_PEER_PORT) + .withNetwork(network) + .withNetworkAliases("etcd") + .withCommand( + "etcd " + + "-name etcd " + + "-advertise-client-urls http://0.0.0.0:2379 " + + "-listen-client-urls http://0.0.0.0:2379 " + + "-initial-advertise-peer-urls http://etcd:2380 " + + "-listen-peer-urls http://0.0.0.0:2380" + ); + nodeContainer.start(); + clientEndpoints = TestUtil.buildClientEndpoints(nodeContainer); + peerEndpoints = TestUtil.buildPeerEndpoints(nodeContainer); + } + + @Nonnull + @Override + public Client getClient() { + return Client.builder().endpoints(clientEndpoints).build(); + } + + @Nonnull + @Override + public List getClientEndpoints() { + return Collections.unmodifiableList(clientEndpoints); + } + + @Nonnull + @Override + public List getPeerEndpoints() { + return Collections.unmodifiableList(peerEndpoints); + } + + @Override + public void close() { + nodeContainer.stop(); + } +} diff --git a/jetcd-core/src/test/java/com/coreos/jetcd/internal/infrastructure/SingleNodeSslEtcdCluster.java b/jetcd-core/src/test/java/com/coreos/jetcd/internal/infrastructure/SingleNodeSslEtcdCluster.java new file mode 100644 index 000000000..8ba70d718 --- /dev/null +++ b/jetcd-core/src/test/java/com/coreos/jetcd/internal/infrastructure/SingleNodeSslEtcdCluster.java @@ -0,0 +1,79 @@ +/** + * Copyright 2017 The jetcd authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.coreos.jetcd.internal.infrastructure; + +import com.coreos.jetcd.Client; +import com.coreos.jetcd.internal.impl.TestUtil; +import org.assertj.core.util.Lists; +import org.testcontainers.containers.BindMode; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.Network; + +import javax.annotation.Nonnull; +import java.util.Collections; +import java.util.List; + +import static com.coreos.jetcd.internal.impl.TestConstants.ETCD_CLIENT_PORT; +import static com.coreos.jetcd.internal.impl.TestConstants.ETCD_DOCKER_IMAGE_NAME; + +public class SingleNodeSslEtcdCluster implements EtcdCluster { + private final GenericContainer nodeContainer; + private final List clientEndpoints; + private final List peerEndpoints; + + SingleNodeSslEtcdCluster(Network network) { + nodeContainer = new GenericContainer(ETCD_DOCKER_IMAGE_NAME) + .withExposedPorts(ETCD_CLIENT_PORT) + .withClasspathResourceMapping("ssl/cert/server.pem", "/etc/ssl/etcd/server.pem", BindMode.READ_ONLY) + .withClasspathResourceMapping("ssl/cert/server-key.pem", "/etc/ssl/etcd/server-key.pem", BindMode.READ_ONLY) + .withNetwork(network) + .withNetworkAliases("etcd-ssl") + .withCommand( + "etcd " + + "--name etcd-ssl " + + "--cert-file=/etc/ssl/etcd/server.pem " + + "--key-file=/etc/ssl/etcd/server-key.pem " + + "--advertise-client-urls https://0.0.0.0:2379 " + + "--listen-client-urls https://0.0.0.0:2379" + ); + nodeContainer.start(); + clientEndpoints = Lists.newArrayList(TestUtil.buildEndpoint(nodeContainer, "https", ETCD_CLIENT_PORT)); + peerEndpoints = TestUtil.buildPeerEndpoints(nodeContainer); + } + + @Nonnull + @Override + public Client getClient() { + return Client.builder().endpoints(clientEndpoints).build(); + } + + @Nonnull + @Override + public List getClientEndpoints() { + return Collections.unmodifiableList(clientEndpoints); + } + + @Nonnull + @Override + public List getPeerEndpoints() { + return Collections.unmodifiableList(peerEndpoints); + } + + @Override + public void close() { + nodeContainer.stop(); + } +} diff --git a/jetcd-core/src/test/java/com/coreos/jetcd/internal/infrastructure/ThreeNodeEtcdCluster.java b/jetcd-core/src/test/java/com/coreos/jetcd/internal/infrastructure/ThreeNodeEtcdCluster.java new file mode 100644 index 000000000..749b4754f --- /dev/null +++ b/jetcd-core/src/test/java/com/coreos/jetcd/internal/infrastructure/ThreeNodeEtcdCluster.java @@ -0,0 +1,92 @@ +/** + * Copyright 2017 The jetcd authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.coreos.jetcd.internal.infrastructure; + +import com.coreos.jetcd.Client; +import com.coreos.jetcd.internal.impl.TestUtil; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.Network; + +import javax.annotation.Nonnull; +import java.util.Collections; +import java.util.List; + +import static com.coreos.jetcd.internal.impl.TestConstants.*; + +public class ThreeNodeEtcdCluster implements EtcdCluster { + private final GenericContainer firstNodeContainer; + private final GenericContainer secondNodeContainer; + private final GenericContainer thirdNodeContainer; + + private final List clientEndpoints; + private final List peerEndpoints; + + public ThreeNodeEtcdCluster(Network network) { + firstNodeContainer = createClusterNode(network, 1); + secondNodeContainer = createClusterNode(network, 2); + thirdNodeContainer = createClusterNode(network, 3); + + firstNodeContainer.start(); + secondNodeContainer.start(); + thirdNodeContainer.start(); + + clientEndpoints = TestUtil.buildClientEndpoints(firstNodeContainer, secondNodeContainer, thirdNodeContainer); + peerEndpoints = TestUtil.buildPeerEndpoints(firstNodeContainer, secondNodeContainer, thirdNodeContainer); + } + + @Nonnull + @Override + public Client getClient() { + return Client.builder().endpoints(clientEndpoints).build(); + } + + @Nonnull + @Override + public List getClientEndpoints() { + return Collections.unmodifiableList(clientEndpoints); + } + + @Nonnull + @Override + public List getPeerEndpoints() { + return Collections.unmodifiableList(peerEndpoints); + } + + @Override + public void close() { + firstNodeContainer.stop(); + secondNodeContainer.stop(); + thirdNodeContainer.stop(); + } + + private static GenericContainer createClusterNode(Network network, int nodeNumber) { + return new GenericContainer(ETCD_DOCKER_IMAGE_NAME) + .withExposedPorts(ETCD_CLIENT_PORT, ETCD_PEER_PORT) + .withNetwork(network) + .withNetworkAliases("etcd" + nodeNumber) + .withCommand( + "etcd " + + "-name etcd" + nodeNumber + " " + + "-advertise-client-urls http://0.0.0.0:2379 " + + "-listen-client-urls http://0.0.0.0:2379 " + + "-initial-advertise-peer-urls http://etcd" + nodeNumber + ":2380 " + + "-listen-peer-urls http://0.0.0.0:2380 " + + "-initial-cluster-token etcd-cluster " + + "-initial-cluster etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380 " + + "-initial-cluster-state new" + ); + } +} From 360a96460af8ec5f18e1e643497a8fd2a7978361 Mon Sep 17 00:00:00 2001 From: IgorPerikov Date: Thu, 8 Mar 2018 22:09:41 +0300 Subject: [PATCH 3/5] test: rewrite tests using EtcdCluster Fixes #106 --- .../jetcd/internal/impl/AuthClientTest.java | 29 +++++++++---- .../internal/impl/ClusterClientTest.java | 41 +++++++++++++++---- .../coreos/jetcd/internal/impl/KVTest.java | 17 ++++++-- .../coreos/jetcd/internal/impl/LeaseTest.java | 16 +++++--- .../jetcd/internal/impl/LoadBalancerTest.java | 34 ++++++++++++--- .../coreos/jetcd/internal/impl/LockTest.java | 20 +++++---- .../jetcd/internal/impl/MaintenanceTest.java | 31 +++++++++----- .../coreos/jetcd/internal/impl/SslTest.java | 19 +++++++-- .../coreos/jetcd/internal/impl/TestUtil.java | 31 ++++++++++++++ .../coreos/jetcd/internal/impl/WatchTest.java | 37 +++++++++-------- .../test/resources/simplelogger.properties | 12 +++--- 11 files changed, 212 insertions(+), 75 deletions(-) diff --git a/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/AuthClientTest.java b/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/AuthClientTest.java index c8fb5ab05..df0b8c4ff 100755 --- a/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/AuthClientTest.java +++ b/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/AuthClientTest.java @@ -15,9 +15,6 @@ */ package com.coreos.jetcd.internal.impl; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; - import com.coreos.jetcd.Auth; import com.coreos.jetcd.Client; import com.coreos.jetcd.KV; @@ -26,15 +23,24 @@ import com.coreos.jetcd.auth.Permission; import com.coreos.jetcd.auth.Permission.Type; import com.coreos.jetcd.data.ByteSequence; -import java.util.List; -import java.util.concurrent.ExecutionException; +import com.coreos.jetcd.internal.infrastructure.ClusterFactory; +import com.coreos.jetcd.internal.infrastructure.EtcdCluster; +import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; +import java.io.IOException; +import java.util.List; +import java.util.concurrent.ExecutionException; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + /** * test etcd auth */ public class AuthClientTest { + private static final EtcdCluster CLUSTER = ClusterFactory.buildSingleNodeCluster("auth-etcd"); private ByteSequence rootRolekeyRangeBegin = ByteSequence.fromString("root"); private ByteSequence rootkeyRangeEnd = ByteSequence.fromString("root1"); @@ -65,14 +71,16 @@ public class AuthClientTest { private Auth authDisabledAuthClient; private KV authDisabledKVClient; + private List endpoints; /** * Build etcd client to create role, permission */ @BeforeTest public void setupEnv() { + endpoints = CLUSTER.getClientEndpoints(); Client client = Client.builder() - .endpoints("http://localhost:12379") + .endpoints(endpoints) .build(); this.authDisabledKVClient = client.getKVClient(); @@ -165,11 +173,11 @@ public void testEnableAuth() throws ExecutionException, InterruptedException { @Test(dependsOnMethods = "testEnableAuth", groups = "authEnable", priority = 1) public void setupAuthClient() { this.userClient = Client.builder() - .endpoints("http://localhost:12379") + .endpoints(endpoints) .user(user) .password(userNewPass).build(); this.rootClient = Client.builder() - .endpoints("http://localhost:12379") + .endpoints(endpoints) .user(root) .password(rootPass).build(); } @@ -261,4 +269,9 @@ public void delRole() throws ExecutionException, InterruptedException { this.authDisabledAuthClient.roleDelete(rootRole).get(); this.authDisabledAuthClient.roleDelete(userRole).get(); } + + @AfterTest + public void tearDown() throws IOException { + CLUSTER.close(); + } } diff --git a/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/ClusterClientTest.java b/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/ClusterClientTest.java index 7efc502f0..dfcc0b01c 100644 --- a/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/ClusterClientTest.java +++ b/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/ClusterClientTest.java @@ -20,29 +20,48 @@ import com.coreos.jetcd.cluster.Member; import com.coreos.jetcd.cluster.MemberAddResponse; import com.coreos.jetcd.cluster.MemberListResponse; +import com.coreos.jetcd.internal.infrastructure.ClusterFactory; +import com.coreos.jetcd.internal.infrastructure.EtcdCluster; +import org.testng.annotations.AfterTest; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.Test; +import org.testng.asserts.Assertion; + +import java.io.IOException; import java.util.Arrays; +import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import org.testng.annotations.Test; -import org.testng.asserts.Assertion; /** * test etcd cluster client */ public class ClusterClientTest { + private static final EtcdCluster CLUSTER = ClusterFactory.buildThreeNodeCluster("CLUSTER-etcd"); private Assertion assertion = new Assertion(); private Member addedMember; + private List endpoints; + private List peerUrls; + /** * test list cluster function */ + + @BeforeTest + public void setUp() throws InterruptedException { + endpoints = CLUSTER.getClientEndpoints(); + peerUrls = CLUSTER.getPeerEndpoints(); + TimeUnit.SECONDS.sleep(5); + } + @Test public void testListCluster() throws ExecutionException, InterruptedException { - Client client = Client.builder().endpoints(TestConstants.endpoints).build(); + Client client = Client.builder().endpoints(endpoints).build(); Cluster clusterClient = client.getClusterClient(); MemberListResponse response = clusterClient.listMember().get(); assertion @@ -56,14 +75,13 @@ public void testListCluster() public void testAddMember() throws ExecutionException, InterruptedException, TimeoutException { Client client = Client.builder() - .endpoints(Arrays.copyOfRange(TestConstants.endpoints, 0, 2)) + .endpoints(endpoints.subList(0, 2)) .build(); Cluster clusterClient = client.getClusterClient(); MemberListResponse response = clusterClient.listMember().get(); assertion.assertEquals(response.getMembers().size(), 3); - CompletableFuture responseListenableFuture = clusterClient - .addMember(Arrays.asList(Arrays.copyOfRange(TestConstants.peerUrls, 2, 3))); + CompletableFuture responseListenableFuture = clusterClient.addMember(peerUrls.subList(2, 3)); MemberAddResponse addResponse = responseListenableFuture.get(5, TimeUnit.SECONDS); addedMember = addResponse.getMember(); assertion.assertNotNull(addedMember, "added member: " + addedMember.getId()); @@ -78,12 +96,12 @@ public void testUpdateMember() { Throwable throwable = null; try { Client client = Client.builder() - .endpoints(Arrays.copyOfRange(TestConstants.endpoints, 1, 3)) + .endpoints(endpoints.subList(1, 3)) .build(); Cluster clusterClient = client.getClusterClient(); MemberListResponse response = clusterClient.listMember().get(); - String[] newPeerUrl = new String[]{"http://localhost:12380"}; + String[] newPeerUrl = peerUrls.subList(0, 1).toArray(new String[]{}); clusterClient.updateMember(response.getMembers().get(0).getId(), Arrays.asList(newPeerUrl)) .get(); } catch (Exception e) { @@ -100,7 +118,7 @@ public void testUpdateMember() { public void testDeleteMember() throws ExecutionException, InterruptedException { Client client = Client.builder() - .endpoints(Arrays.copyOfRange(TestConstants.endpoints, 0, 2)) + .endpoints(endpoints.subList(0, 2)) .build(); Cluster clusterClient = client.getClusterClient(); @@ -109,4 +127,9 @@ public void testDeleteMember() assertion.assertEquals(newCount, 3, "delete added member(" + addedMember.getId() + "), and left " + newCount + " members"); } + + @AfterTest + public void tearDown() throws IOException { + CLUSTER.close(); + } } diff --git a/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/KVTest.java b/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/KVTest.java index 51e9181df..840c683cf 100644 --- a/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/KVTest.java +++ b/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/KVTest.java @@ -19,6 +19,8 @@ import com.coreos.jetcd.KV; import com.coreos.jetcd.Txn; import com.coreos.jetcd.data.ByteSequence; +import com.coreos.jetcd.internal.infrastructure.ClusterFactory; +import com.coreos.jetcd.internal.infrastructure.EtcdCluster; import com.coreos.jetcd.kv.DeleteResponse; import com.coreos.jetcd.kv.GetResponse; import com.coreos.jetcd.kv.PutResponse; @@ -30,17 +32,21 @@ import com.coreos.jetcd.options.GetOption.SortOrder; import com.coreos.jetcd.options.GetOption.SortTarget; import com.coreos.jetcd.options.PutOption; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; +import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import org.testng.asserts.Assertion; +import java.io.IOException; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + /** * KV service test cases. */ public class KVTest { + private static final EtcdCluster CLUSTER = ClusterFactory.buildThreeNodeCluster("kv-etcd"); private KV kvClient; private Assertion test; @@ -55,7 +61,7 @@ public class KVTest { @BeforeTest public void setUp() throws Exception { test = new Assertion(); - Client client = Client.builder().endpoints(TestConstants.endpoints).build(); + Client client = CLUSTER.getClient(); kvClient = client.getKVClient(); } @@ -221,4 +227,9 @@ public void testNestedTxn() throws Exception { test.assertEquals(getResp2.getKvs().size(), 1); test.assertEquals(getResp2.getKvs().get(0).getValue().toStringUtf8(), oneTwoThree.toStringUtf8()); } + + @AfterTest + public void tearDown() throws IOException { + CLUSTER.close(); + } } diff --git a/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/LeaseTest.java b/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/LeaseTest.java index fa3de7478..94b9d0805 100644 --- a/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/LeaseTest.java +++ b/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/LeaseTest.java @@ -15,28 +15,33 @@ */ package com.coreos.jetcd.internal.impl; -import static org.assertj.core.api.Assertions.assertThat; - import com.coreos.jetcd.Client; import com.coreos.jetcd.KV; import com.coreos.jetcd.Lease; import com.coreos.jetcd.Lease.KeepAliveListener; import com.coreos.jetcd.data.ByteSequence; +import com.coreos.jetcd.internal.infrastructure.ClusterFactory; +import com.coreos.jetcd.internal.infrastructure.EtcdCluster; import com.coreos.jetcd.kv.PutResponse; import com.coreos.jetcd.lease.LeaseKeepAliveResponse; import com.coreos.jetcd.lease.LeaseTimeToLiveResponse; import com.coreos.jetcd.options.LeaseOption; import com.coreos.jetcd.options.PutOption; -import java.util.concurrent.ExecutionException; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import org.testng.asserts.Assertion; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +import static org.assertj.core.api.Assertions.assertThat; + /** * KV service test cases. */ public class LeaseTest { + private static final EtcdCluster CLUSTER = ClusterFactory.buildThreeNodeCluster("lease-etcd"); private KV kvClient; private Client client; @@ -50,14 +55,15 @@ public class LeaseTest { @BeforeClass public void setUp() throws Exception { test = new Assertion(); - client = Client.builder().endpoints(TestConstants.endpoints).build(); + client = Client.builder().endpoints(CLUSTER.getClientEndpoints()).build(); kvClient = client.getKVClient(); leaseClient = client.getLeaseClient(); } @AfterClass - public void tearDown() { + public void tearDown() throws IOException { this.client.close(); + CLUSTER.close(); } @Test diff --git a/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/LoadBalancerTest.java b/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/LoadBalancerTest.java index a03afc258..a1a4a64b9 100644 --- a/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/LoadBalancerTest.java +++ b/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/LoadBalancerTest.java @@ -15,28 +15,45 @@ */ package com.coreos.jetcd.internal.impl; -import static org.assertj.core.api.Assertions.assertThat; - import com.coreos.jetcd.Client; import com.coreos.jetcd.KV; +import com.coreos.jetcd.internal.infrastructure.ClusterFactory; +import com.coreos.jetcd.internal.infrastructure.EtcdCluster; import com.coreos.jetcd.kv.PutResponse; import io.grpc.PickFirstBalancerFactory; import io.grpc.util.RoundRobinLoadBalancerFactory; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.Timeout; +import java.io.IOException; +import java.util.List; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; + /** * KV service test cases. */ public class LoadBalancerTest { + private static final EtcdCluster CLUSTER = ClusterFactory.buildThreeNodeCluster("load-balancer-etcd"); + + private static List endpoints; + + @BeforeClass + public static void setUp() { + endpoints = CLUSTER.getClientEndpoints(); + } + @Rule public Timeout timeout = Timeout.seconds(10); @Test public void testPickFirstBalancerFactory() throws Exception { try (Client client = Client.builder() - .endpoints(TestConstants.endpoints) + .endpoints(endpoints) .loadBalancerFactory(PickFirstBalancerFactory.getInstance()) .build(); @@ -44,7 +61,7 @@ public void testPickFirstBalancerFactory() throws Exception { PutResponse response; long lastMemberId = 0; - for (int i = 0; i < TestConstants.endpoints.length * 2; i++) { + for (int i = 0; i < endpoints.stream().collect(Collectors.joining(",")).length() * 2; i++) { response = kv.put(TestUtil.randomByteSequence(), TestUtil.randomByteSequence()).get(); if (i == 0) { @@ -60,7 +77,7 @@ public void testPickFirstBalancerFactory() throws Exception { public void testRoundRobinLoadBalancerFactory() throws Exception { try (Client client = Client.builder() - .endpoints(TestConstants.endpoints) + .endpoints(endpoints) .loadBalancerFactory(RoundRobinLoadBalancerFactory.getInstance()) .build(); KV kv = client.getKVClient()) { @@ -68,7 +85,7 @@ public void testRoundRobinLoadBalancerFactory() throws Exception { long lastMemberId = 0; long differences = 0; - for (int i = 0; i < TestConstants.endpoints.length; i++) { + for (int i = 0; i < endpoints.stream().collect(Collectors.joining(",")).length(); i++) { response = kv.put(TestUtil.randomByteSequence(), TestUtil.randomByteSequence()).get(); if (i > 0 && lastMemberId != response.getHeader().getMemberId()) { @@ -81,4 +98,9 @@ public void testRoundRobinLoadBalancerFactory() throws Exception { assertThat(differences).isNotEqualTo(lastMemberId); } } + + @AfterClass + public static void tearDown() throws IOException { + CLUSTER.close(); + } } diff --git a/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/LockTest.java b/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/LockTest.java index 1c996edfa..9b7f0e450 100644 --- a/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/LockTest.java +++ b/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/LockTest.java @@ -19,22 +19,24 @@ import com.coreos.jetcd.Lease; import com.coreos.jetcd.Lock; import com.coreos.jetcd.data.ByteSequence; +import com.coreos.jetcd.internal.infrastructure.ClusterFactory; +import com.coreos.jetcd.internal.infrastructure.EtcdCluster; import com.coreos.jetcd.lease.LeaseGrantResponse; import com.coreos.jetcd.lock.LockResponse; +import org.testng.annotations.*; +import org.testng.asserts.Assertion; + +import java.io.IOException; import java.util.HashSet; import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.BeforeTest; -import org.testng.annotations.Test; -import org.testng.asserts.Assertion; /** * Lock service test cases. */ public class LockTest { + private static final EtcdCluster CLUSTER = ClusterFactory.buildThreeNodeCluster("lock-etcd"); private Lock lockClient; private Lease leaseClient; @@ -46,7 +48,7 @@ public class LockTest { @BeforeTest public void setUp() throws Exception { test = new Assertion(); - Client client = Client.builder().endpoints(TestConstants.endpoints).build(); + Client client = Client.builder().endpoints(CLUSTER.getClientEndpoints()).build(); lockClient = client.getLockClient(); leaseClient = client.getLeaseClient(); } @@ -57,7 +59,7 @@ public void setUpEach() throws Exception { } @AfterMethod - public void tearDown() throws Exception { + public void tearDownEach() throws Exception { for (ByteSequence lockKey : locksToRelease) { lockClient.unlock(lockKey).get(); } @@ -130,4 +132,8 @@ private long grantLease(long ttl) throws Exception { return response.getID(); } + @AfterTest + public void tearDown() throws IOException { + CLUSTER.close(); + } } diff --git a/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/MaintenanceTest.java b/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/MaintenanceTest.java index 214a7adcf..7cde6ca7a 100644 --- a/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/MaintenanceTest.java +++ b/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/MaintenanceTest.java @@ -16,35 +16,39 @@ package com.coreos.jetcd.internal.impl; import com.coreos.jetcd.Client; -import com.coreos.jetcd.ClientBuilder; import com.coreos.jetcd.Maintenance; import com.coreos.jetcd.Maintenance.Snapshot; -import com.coreos.jetcd.data.ByteSequence; +import com.coreos.jetcd.internal.infrastructure.ClusterFactory; +import com.coreos.jetcd.internal.infrastructure.EtcdCluster; import com.coreos.jetcd.maintenance.StatusResponse; +import org.testng.annotations.AfterTest; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.testng.asserts.Assertion; + import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.concurrent.ExecutionException; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; -import org.testng.asserts.Assertion; /** * Maintenance test. */ public class MaintenanceTest { + private static final EtcdCluster CLUSTER = ClusterFactory.buildThreeNodeCluster("maintenance-etcd"); private Client client; private Maintenance maintenance; private final Assertion test = new Assertion(); + private List endpoints; @BeforeClass public void setup() { - this.client = Client.builder().endpoints(TestConstants.endpoints).build(); + endpoints = CLUSTER.getClientEndpoints(); + this.client = Client.builder().endpoints(endpoints).build(); this.maintenance = client.getMaintenanceClient(); } @@ -54,7 +58,7 @@ public void setup() { */ @Test public void testStatusMember() throws ExecutionException, InterruptedException { - StatusResponse statusResponse = maintenance.statusMember(TestConstants.endpoints[0]).get(); + StatusResponse statusResponse = maintenance.statusMember(endpoints.get(0)).get(); test.assertTrue(statusResponse.getDbSize() > 0); } @@ -75,7 +79,7 @@ public void testNnapshot() throws IOException { @Test public void testHashKV() throws ExecutionException, InterruptedException { - maintenance.hashKV(TestConstants.endpoints[0], 0).get(); + maintenance.hashKV(endpoints.get(0), 0).get(); } /** @@ -93,14 +97,14 @@ public void testAlarmList() throws ExecutionException, InterruptedException { */ @Test public void testDefragment() throws ExecutionException, InterruptedException { - maintenance.defragmentMember(TestConstants.endpoints[0]).get(); + maintenance.defragmentMember(endpoints.get(0)).get(); } @Test public void testMoveLeader() throws ExecutionException, InterruptedException { String leaderEndpoint = null; List followers = new ArrayList<>(); - for(String ep : TestConstants.endpoints){ + for(String ep : endpoints){ StatusResponse statusResponse = maintenance.statusMember(ep).get(); long memberId = statusResponse.getHeader().getMemberId(); if (memberId == statusResponse.getLeader()) { @@ -117,4 +121,9 @@ public void testMoveLeader() throws ExecutionException, InterruptedException { client.getMaintenanceClient().moveLeader(followers.get(0)).get(); } } + + @AfterTest + public void tearDown() throws IOException { + CLUSTER.close(); + } } diff --git a/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/SslTest.java b/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/SslTest.java index 32ea64525..d62648f56 100644 --- a/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/SslTest.java +++ b/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/SslTest.java @@ -15,19 +15,25 @@ */ package com.coreos.jetcd.internal.impl; -import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; - import com.coreos.jetcd.Client; import com.coreos.jetcd.KV; import com.coreos.jetcd.data.ByteSequence; +import com.coreos.jetcd.internal.infrastructure.ClusterFactory; +import com.coreos.jetcd.internal.infrastructure.EtcdCluster; import io.grpc.netty.GrpcSslContexts; +import org.junit.AfterClass; +import org.junit.Test; + import java.io.File; import java.io.FileInputStream; +import java.io.IOException; import java.io.InputStream; import java.util.Objects; -import org.junit.Test; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; public class SslTest { + private final static EtcdCluster CLUSTER = ClusterFactory.buildSingleNodeClusterWithSsl("ssl-etcd"); @Test(timeout = 5000) public void testSimpleSllSetup() throws Exception { @@ -35,7 +41,7 @@ public void testSimpleSllSetup() throws Exception { final ByteSequence val = ByteSequence.fromString(TestUtil.randomString()); final String capath = System.getProperty("ssl.cert.capath"); final String authority = System.getProperty("ssl.cert.authority", TestConstants.DEFAULT_SSL_AUTHORITY); - final String endpoints = System.getProperty("ssl.cert.endpoints", TestConstants.DEFAULT_SSL_ENDPOINTS); + final String endpoints = System.getProperty("ssl.cert.endpoints", CLUSTER.getClientEndpoints().get(0)); try (InputStream is = Objects.nonNull(capath) ? new FileInputStream(new File(capath)) @@ -59,5 +65,10 @@ public void testSimpleSllSetup() throws Exception { client.close(); } } + + @AfterClass + public static void tearDown() throws IOException { + CLUSTER.close(); + } } diff --git a/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/TestUtil.java b/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/TestUtil.java index c673ac339..7ec5ac64a 100644 --- a/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/TestUtil.java +++ b/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/TestUtil.java @@ -16,8 +16,13 @@ package com.coreos.jetcd.internal.impl; import com.coreos.jetcd.data.ByteSequence; +import org.testcontainers.containers.GenericContainer; + import java.io.IOException; import java.net.ServerSocket; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; public class TestUtil { @@ -34,4 +39,30 @@ public static int findNextAvailablePort() throws IOException { return socket.getLocalPort(); } } + + public static String buildClientEndpoint(GenericContainer container) { + return buildEndpoint(container, "http", TestConstants.ETCD_CLIENT_PORT); + } + + public static List buildClientEndpoints(GenericContainer... etcdContainers) { + return Arrays.stream(etcdContainers) + .map(TestUtil::buildClientEndpoint) + .collect(Collectors.toList()); + } + + public static String buildPeerEndpoint(GenericContainer container) { + return buildEndpoint(container, "http", TestConstants.ETCD_PEER_PORT); + } + + public static List buildPeerEndpoints(GenericContainer... etcdContainers) { + return Arrays.stream(etcdContainers) + .map(TestUtil::buildPeerEndpoint) + .collect(Collectors.toList()); + } + + public static String buildEndpoint(GenericContainer container, String scheme, int port) { + String nodeAddress = container.getContainerIpAddress(); + Integer nodePort = container.getMappedPort(port); + return scheme + "://" + nodeAddress + ":" + nodePort; + } } diff --git a/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/WatchTest.java b/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/WatchTest.java index cb0b51f5d..bcea98be1 100644 --- a/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/WatchTest.java +++ b/jetcd-core/src/test/java/com/coreos/jetcd/internal/impl/WatchTest.java @@ -16,46 +16,52 @@ package com.coreos.jetcd.internal.impl; -import static org.assertj.core.api.Assertions.assertThat; - import com.coreos.jetcd.Client; import com.coreos.jetcd.KV; import com.coreos.jetcd.Watch; import com.coreos.jetcd.Watch.Watcher; import com.coreos.jetcd.data.ByteSequence; +import com.coreos.jetcd.internal.infrastructure.ClusterFactory; +import com.coreos.jetcd.internal.infrastructure.EtcdCluster; import com.coreos.jetcd.watch.WatchEvent; import com.coreos.jetcd.watch.WatchEvent.EventType; import com.coreos.jetcd.watch.WatchResponse; -import java.util.Arrays; -import java.util.concurrent.ExecutionException; -import org.junit.After; -import org.junit.Before; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.Timeout; +import java.io.IOException; +import java.util.Arrays; +import java.util.concurrent.ExecutionException; + +import static org.assertj.core.api.Assertions.assertThat; + /** * watch test case. */ public class WatchTest { + private static final EtcdCluster CLUSTER = ClusterFactory.buildThreeNodeCluster("watch-etcd"); - private Client client; - private Watch watchClient; - private KV kvClient; + private static Client client; + private static Watch watchClient; + private static KV kvClient; @Rule public Timeout timeout = Timeout.seconds(10); - @Before - public void setUp() { - client = Client.builder().endpoints(TestConstants.endpoints).build(); + @BeforeClass + public static void setUp() { + client = Client.builder().endpoints(CLUSTER.getClientEndpoints()).build(); watchClient = client.getWatchClient(); kvClient = client.getKVClient(); } - @After - public void tearDown() { + @AfterClass + public static void tearDown() throws IOException { client.close(); + CLUSTER.close(); } @Test @@ -87,6 +93,3 @@ public void testWatchOnDelete() throws ExecutionException, InterruptedException } } } - - - diff --git a/jetcd-core/src/test/resources/simplelogger.properties b/jetcd-core/src/test/resources/simplelogger.properties index 5cc515ee9..73d0e41a4 100644 --- a/jetcd-core/src/test/resources/simplelogger.properties +++ b/jetcd-core/src/test/resources/simplelogger.properties @@ -14,8 +14,10 @@ # limitations under the License. # -org.slf4j.simpleLogger.defaultLogLevel = debug -org.slf4j.simpleLogger.showThreadName = true -org.slf4j.simpleLogger.levelInBrackets = true -org.slf4j.simpleLogger.log.io.grpc = info -org.slf4j.simpleLogger.log.io.netty = info \ No newline at end of file +org.slf4j.simpleLogger.defaultLogLevel = debug +org.slf4j.simpleLogger.showThreadName = true +org.slf4j.simpleLogger.levelInBrackets = true +org.slf4j.simpleLogger.log.io.grpc = info +org.slf4j.simpleLogger.log.io.netty = info +org.slf4j.simpleLogger.log.com.github.dockerjava = warn +org.slf4j.simpleLogger.log.org.testcontainers.shaded = warn From 44cf0279dfa2eeca8b4e2cf98a68aabe49a2e9ca Mon Sep 17 00:00:00 2001 From: IgorPerikov Date: Thu, 8 Mar 2018 22:13:27 +0300 Subject: [PATCH 4/5] travis_ci: remove unnecessary script from ci pipeline run_etcd_docker no longer needed, since testcontainers used for testing Fixes #106 --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 01cc688c7..943865a9e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,7 +15,6 @@ before_install: - sdk install java 8u144-zulu script: - - ./etc/scripts/run_etcd_docker.sh - sdk use java 8u144-zulu - java -version - ./mvnw clean install From 1645678c3b2ad1a671dd25b72748f578e16f19b1 Mon Sep 17 00:00:00 2001 From: IgorPerikov Date: Thu, 8 Mar 2018 22:13:44 +0300 Subject: [PATCH 5/5] etc: remove unused script and update readme Fixes #106 --- README.md | 10 +-- etc/scripts/run_etcd_docker.sh | 111 --------------------------------- 2 files changed, 2 insertions(+), 119 deletions(-) delete mode 100755 etc/scripts/run_etcd_docker.sh diff --git a/README.md b/README.md index 1cb79033e..e15e2cb67 100644 --- a/README.md +++ b/README.md @@ -77,16 +77,10 @@ The current major version is zero (0.y.z). Anything may change at any time. The ## Running tests -The project is to be tested against a three node `etcd` setup, launched by the [scripts/run_etcd_docker.sh](etc/scripts/run_etcd_docker.sh) shell script: - -``` -./etc/scripts/run_etcd_docker.sh -``` - +The project is to be tested against a three node `etcd` setup, which automatically launched via Testcontainers framework. +For more info and prerequisites visit [official website](https://www.testcontainers.org/) It should work on either macOS or Linux. -Note: Make sure you don't have a default `etcd` running on your system! The script uses the default port `2379` for the first node, which fails to launch if that port is already taken. - ```sh $ mvn test ... diff --git a/etc/scripts/run_etcd_docker.sh b/etc/scripts/run_etcd_docker.sh deleted file mode 100755 index 5d67e7212..000000000 --- a/etc/scripts/run_etcd_docker.sh +++ /dev/null @@ -1,111 +0,0 @@ -#!/bin/bash -# -# Copyright 2017 The jetcd authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -ETCD_VERSION="v3.3" - -export SCRIPT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -export ROOT=$SCRIPT_PATH/../../ -export CERT_HOME=$ROOT/jetcd-core/src/test/resources/ssl/cert - -docker network ls | grep etcd 2>&1 > /dev/null -if [ $? -ne 0 ]; then - docker network create etcd -fi - -# Pull docker -docker pull gcr.io/etcd-development/etcd:${ETCD_VERSION} - -docker run \ - --detach \ - --rm \ - --name etcd1 \ - --network etcd \ - --publish 12379:2379 \ - gcr.io/etcd-development/etcd:${ETCD_VERSION} \ - etcd \ - -name etcd1 \ - -advertise-client-urls "http://127.0.0.1:2379" \ - -listen-client-urls "http://0.0.0.0:2379" \ - -initial-advertise-peer-urls http://etcd1:2380 \ - -listen-peer-urls http://0.0.0.0:2380 \ - -initial-cluster-token etcd-cluster \ - -initial-cluster etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380 \ - -initial-cluster-state new - -docker run \ - --detach \ - --rm \ - --name etcd2 \ - --network etcd \ - --publish 22379:2379 \ - gcr.io/etcd-development/etcd:${ETCD_VERSION} \ - etcd \ - -name etcd2 \ - -advertise-client-urls "http://127.0.0.1:2379" \ - -listen-client-urls "http://0.0.0.0:2379" \ - -initial-advertise-peer-urls http://etcd2:2380 \ - -listen-peer-urls http://0.0.0.0:2380 \ - -initial-cluster-token etcd-cluster \ - -initial-cluster etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380 \ - -initial-cluster-state new - - -docker run \ - --detach \ - --rm \ - --name etcd3 \ - --network etcd \ - --publish 32379:2379 \ - gcr.io/etcd-development/etcd:${ETCD_VERSION} \ - etcd \ - -name etcd3 \ - -advertise-client-urls "http://127.0.0.1:2379" \ - -listen-client-urls "http://0.0.0.0:2379" \ - -initial-advertise-peer-urls http://etcd3:2380 \ - -listen-peer-urls http://0.0.0.0:2380 \ - -initial-cluster-token etcd-cluster \ - -initial-cluster etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380 \ - -initial-cluster-state new - -docker run \ - --detach \ - --rm \ - --name etcd-ssl \ - --hostname etcd-ssl \ - --network etcd \ - --volume $CERT_HOME:/etc/ssl/etcd:Z \ - --publish 42379:2379 \ - gcr.io/etcd-development/etcd:${ETCD_VERSION} \ - etcd \ - --name etcd-ssl \ - --cert-file=/etc/ssl/etcd/server.pem \ - --key-file=/etc/ssl/etcd/server-key.pem \ - --advertise-client-urls "https://127.0.0.1:2379" \ - --listen-client-urls "https://0.0.0.0:2379" - -docker run \ - --detach \ - --rm \ - --name etcd-proxy \ - --network etcd \ - --publish 2379:2379 \ - gcr.io/etcd-development/etcd:${ETCD_VERSION} \ - etcd \ - grpc-proxy \ - start \ - --endpoints=etcd1:2379,etcd2:2379,etcd3:2379 \ - --listen-addr=0.0.0.0:2379