Skip to content

Commit

Permalink
[#eng-2514] move integration tests into separate file, factor out com…
Browse files Browse the repository at this point in the history
…monality into testutil
  • Loading branch information
dakrone committed Jun 1, 2012
1 parent 8c80562 commit f5c8e98
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 128 deletions.
@@ -0,0 +1,82 @@
package com.sonian.elasticsearch;

import com.sonian.elasticsearch.equilibrium.ClusterEqualizerService;
import com.sonian.elasticsearch.tests.AbstractJettyHttpServerTests;
import org.testng.annotations.AfterTest;

import static org.hamcrest.MatcherAssert.assertThat;

/**
* @author dakrone
*/
public class DiskShardAllocatorIntegrationTests extends AbstractJettyHttpServerTests {
TestUtils tu = new TestUtils();

@AfterTest
public void cleanUp() {
closeAllNodes();
}

// These tests are commented out because I might revisit them as
// integration tests one day

//@Test
public void testEnoughDiskForShard() {
tu.startNode("1");

// Mock out disk usage so it returns 50% free

// create index
tu.createIndex("1", "i1", 2, 0);

// assert that cluster is green and shards are assigned
assertThat("cluster is green", tu.isGreen("1"));

// Mock out disk usage so it returns 10% free

// create index
tu.createIndex("1", "i2", 2, 0);

// assert that cluster is red and shards are unassigned
assertThat("cluster is red", tu.isRed("1"));

tu.deleteIndex("1", "i1");
tu.deleteIndex("1", "i2");
}

//@Test
public void rebalanceTest() {
tu.startNode("1");
tu.startNode("2");
tu.startNode("3");

// Mock out disk usage so it returns > 20% free

// create three shards, one shard should go to each machine
tu.createIndex("1", "i1", 1, 0);
tu.createIndex("2", "i2", 1, 0);
tu.createIndex("3", "i3", 1, 0);

// mock i1's shard so it's large 100mb
// mock i2's shard so it's medium 50mb
// mock i3's shard so it's small 1mb

// kick off rebalancing
ClusterEqualizerService ce = instance("1", ClusterEqualizerService.class);
assertThat("kick off rebalancing", ce.equalize());

// assert that the cluster is green immediately after a rebalance
assertThat("cluster is green", tu.isGreen("1"));

// wait for rebalancing to finish

// assert that the location of i1's shard i3's shard have been swapped

// assert that the cluster is still green
assertThat("cluster is green", tu.isGreen("1"));

tu.deleteIndex("1", "i1");
tu.deleteIndex("1", "i2");
tu.deleteIndex("1", "i3");
}
}
140 changes: 12 additions & 128 deletions src/test/java/com/sonian/elasticsearch/DiskShardsAllocatorTests.java
Expand Up @@ -32,60 +32,7 @@
* @author dakrone
*/
public class DiskShardsAllocatorTests extends AbstractJettyHttpServerTests {


// Helpers for tests
public void createIndex(String id, String name, int numberOfShards, int numberOfRelicas) {
Client c = client(id);
c.admin().indices().prepareCreate(name)
.setSettings(ImmutableSettings.settingsBuilder()
.put("number_of_shards", numberOfShards)
.put("number_of_replicas", numberOfRelicas))
.execute().actionGet();
}

protected void deleteIndex(String id, String name) {
try {
client(id).admin().indices().prepareDelete(name).execute().actionGet();
} catch (Exception e) {
// ignore
}
}

public ClusterHealthStatus getStatus(String id) {
Client c = client(id);
ClusterHealthResponse healthResponse = c.admin().cluster().prepareHealth().setTimeout("2s").execute().actionGet();
return healthResponse.status();
}

public boolean isGreen (String id) {
return ClusterHealthStatus.GREEN == getStatus(id);
}

public boolean isYellow (String id) {
return ClusterHealthStatus.YELLOW == getStatus(id);
}

public boolean isRed (String id) {
return ClusterHealthStatus.RED == getStatus(id);
}

public FsStats makeFakeFsStats(long total, long avail) {
FsStats fs = createMock(FsStats.class);
FsStats.Info[] infos = new FsStats.Info[1];

FsStats.Info fsInfo1 = createMock(FsStats.Info.class);
expect(fsInfo1.total()).andStubReturn(new ByteSizeValue(total));
expect(fsInfo1.available()).andStubReturn(new ByteSizeValue(avail));

infos[0] = fsInfo1;
expect(fs.iterator()).andStubReturn(Iterators.forArray(infos));

replay(fs, fsInfo1);

return fs;
}

TestUtils tu = new TestUtils();

// Testing functions
@AfterTest
Expand All @@ -94,14 +41,14 @@ public void cleanUp() {
}

@Test void injectedDiskShardAllocator() {
startNode("1");
ShardsAllocator sa = instance("1", ShardsAllocator.class);
tu.startNode("1");
ShardsAllocator sa = tu.instance("1", ShardsAllocator.class);
assertThat("DiskShardsAllocator was injected", sa instanceof DiskShardsAllocator);
}

@Test
public void unitTestNodeFsStats() {
NodeInfoHelper helper = instance("1", NodeInfoHelper.class);
NodeInfoHelper helper = tu.instance("1", NodeInfoHelper.class);
DiskShardsAllocator dsa = new DiskShardsAllocator(ImmutableSettings.settingsBuilder().build(), helper);
NodesStatsResponse resp = helper.nodeFsStats();

Expand All @@ -115,12 +62,12 @@ public void unitTestNodeFsStats() {

@Test
public void unitTestNodeShardStats() {
startNode("1");
tu.startNode("1");

createIndex("1", "i1", 2, 0);
createIndex("1", "i2", 3, 0);
tu.createIndex("1", "i1", 2, 0);
tu.createIndex("1", "i2", 3, 0);

NodeInfoHelper helper = instance("1", NodeInfoHelper.class);
NodeInfoHelper helper = tu.instance("1", NodeInfoHelper.class);
HashMap<ShardId, Long> shardSizes = helper.nodeShardStats();

assertThat("there are sizes for all shards", shardSizes.size() == 5);
Expand All @@ -130,8 +77,8 @@ public void unitTestNodeShardStats() {
assertThat("each shard has a positive size", size > 0.0);
}

deleteIndex("1", "i1");
deleteIndex("1", "i2");
tu.deleteIndex("1", "i1");
tu.deleteIndex("1", "i2");
}

@Test
Expand All @@ -145,8 +92,8 @@ public void unitTestEnoughDiskForShard() {
new HashMap<String, String>());
RoutingNode node = new RoutingNode("node1", dn);

FsStats fakeSmallFs = makeFakeFsStats(1000, 10);
FsStats fakeLargeFs = makeFakeFsStats(1000, 999);
FsStats fakeSmallFs = tu.makeFakeFsStats(1000, 10);
FsStats fakeLargeFs = tu.makeFakeFsStats(1000, 999);

HashMap<String, NodeStats> fakeSmallStats = new HashMap<String, NodeStats>();
fakeSmallStats.put("node1", new NodeStats(dn, 0, "hostname", null,
Expand All @@ -172,67 +119,4 @@ public void unitTestEnoughDiskForShard() {

}


// These tests are commented out because I might revisit them as
// integration tests one day

//@Test
public void testEnoughDiskForShard() {
startNode("1");

// Mock out disk usage so it returns 50% free

// create index
createIndex("1", "i1", 2, 0);

// assert that cluster is green and shards are assigned
assertThat("cluster is green", isGreen("1"));

// Mock out disk usage so it returns 10% free

// create index
createIndex("1", "i2", 2, 0);

// assert that cluster is red and shards are unassigned
assertThat("cluster is red", isRed("1"));

deleteIndex("1", "i1");
deleteIndex("1", "i2");
}

//@Test
public void rebalanceTest() {
startNode("1");
startNode("2");
startNode("3");

// Mock out disk usage so it returns > 20% free

// create three shards, one shard should go to each machine
createIndex("1", "i1", 1, 0);
createIndex("2", "i2", 1, 0);
createIndex("3", "i3", 1, 0);

// mock i1's shard so it's large 100mb
// mock i2's shard so it's medium 50mb
// mock i3's shard so it's small 1mb

// kick off rebalancing
ClusterEqualizerService ce = instance("1", ClusterEqualizerService.class);
assertThat("kick off rebalancing", ce.equalize());

// assert that the cluster is green immediately after a rebalance
assertThat("cluster is green", isGreen("1"));

// wait for rebalancing to finish

// assert that the location of i1's shard i3's shard have been swapped

// assert that the cluster is still green
assertThat("cluster is green", isGreen("1"));

deleteIndex("1", "i1");
deleteIndex("1", "i2");
deleteIndex("1", "i3");
}
}
79 changes: 79 additions & 0 deletions src/test/java/com/sonian/elasticsearch/TestUtils.java
@@ -0,0 +1,79 @@
package com.sonian.elasticsearch;

import com.sonian.elasticsearch.tests.AbstractJettyHttpServerTests;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.collect.Iterators;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.monitor.fs.FsStats;

import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;

/**
* @author dakrone
*/
public class TestUtils extends AbstractJettyHttpServerTests {

public TestUtils() {}

// Helpers for tests

public void start(String id) {
startNode(id);
}

public void createIndex(String id, String name, int numberOfShards, int numberOfRelicas) {
Client c = client(id);
c.admin().indices().prepareCreate(name)
.setSettings(ImmutableSettings.settingsBuilder()
.put("number_of_shards", numberOfShards)
.put("number_of_replicas", numberOfRelicas))
.execute().actionGet();
}

protected void deleteIndex(String id, String name) {
try {
client(id).admin().indices().prepareDelete(name).execute().actionGet();
} catch (Exception e) {
// ignore
}
}

public ClusterHealthStatus getStatus(String id) {
Client c = client(id);
ClusterHealthResponse healthResponse = c.admin().cluster().prepareHealth().setTimeout("2s").execute().actionGet();
return healthResponse.status();
}

public boolean isGreen (String id) {
return ClusterHealthStatus.GREEN == getStatus(id);
}

public boolean isYellow (String id) {
return ClusterHealthStatus.YELLOW == getStatus(id);
}

public boolean isRed (String id) {
return ClusterHealthStatus.RED == getStatus(id);
}

public FsStats makeFakeFsStats(long total, long avail) {
FsStats fs = createMock(FsStats.class);
FsStats.Info[] infos = new FsStats.Info[1];

FsStats.Info fsInfo1 = createMock(FsStats.Info.class);
expect(fsInfo1.total()).andStubReturn(new ByteSizeValue(total));
expect(fsInfo1.available()).andStubReturn(new ByteSizeValue(avail));

infos[0] = fsInfo1;
expect(fs.iterator()).andStubReturn(Iterators.forArray(infos));

replay(fs, fsInfo1);

return fs;
}
}

0 comments on commit f5c8e98

Please sign in to comment.