Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#eng-2514] move integration tests into separate file, factor out com…
…monality into testutil
- Loading branch information
Showing
3 changed files
with
173 additions
and
128 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
src/test/java/com/sonian/elasticsearch/DiskShardAllocatorIntegrationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |