Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Improve integration test framework #295

Merged
merged 5 commits into from
Mar 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
...
Expand Down
111 changes: 0 additions & 111 deletions etc/scripts/run_etcd_docker.sh

This file was deleted.

5 changes: 5 additions & 0 deletions jetcd-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@
<classifier>${os.detected.classifier}</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -65,14 +71,16 @@ public class AuthClientTest {
private Auth authDisabledAuthClient;
private KV authDisabledKVClient;

private List<String> 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();
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> endpoints;
private List<String> 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
Expand All @@ -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<MemberAddResponse> responseListenableFuture = clusterClient
.addMember(Arrays.asList(Arrays.copyOfRange(TestConstants.peerUrls, 2, 3)));
CompletableFuture<MemberAddResponse> responseListenableFuture = clusterClient.addMember(peerUrls.subList(2, 3));
MemberAddResponse addResponse = responseListenableFuture.get(5, TimeUnit.SECONDS);
addedMember = addResponse.getMember();
assertion.assertNotNull(addedMember, "added member: " + addedMember.getId());
Expand All @@ -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) {
Expand All @@ -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();
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();
}

Expand Down Expand Up @@ -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();
}
}
Loading