summaryrefslogtreecommitdiffstats
path: root/clustercontroller-standalone/src/test/java/com/yahoo
diff options
context:
space:
mode:
Diffstat (limited to 'clustercontroller-standalone/src/test/java/com/yahoo')
-rw-r--r--clustercontroller-standalone/src/test/java/com/yahoo/vespa/clustercontroller/standalone/ClusterControllerConfigFetcherTest.java52
-rw-r--r--clustercontroller-standalone/src/test/java/com/yahoo/vespa/clustercontroller/standalone/ClusterControllerTest.java96
-rw-r--r--clustercontroller-standalone/src/test/java/com/yahoo/vespa/clustercontroller/standalone/StandAloneClusterControllerTest.java103
3 files changed, 251 insertions, 0 deletions
diff --git a/clustercontroller-standalone/src/test/java/com/yahoo/vespa/clustercontroller/standalone/ClusterControllerConfigFetcherTest.java b/clustercontroller-standalone/src/test/java/com/yahoo/vespa/clustercontroller/standalone/ClusterControllerConfigFetcherTest.java
new file mode 100644
index 00000000000..32096953b76
--- /dev/null
+++ b/clustercontroller-standalone/src/test/java/com/yahoo/vespa/clustercontroller/standalone/ClusterControllerConfigFetcherTest.java
@@ -0,0 +1,52 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.vespa.clustercontroller.standalone;
+
+public class ClusterControllerConfigFetcherTest extends ClusterControllerTest {
+ public void testSimple() throws Exception {
+ setFleetControllerConfigProperty();
+ setSlobrokConfigProperty();
+ addFleetControllerConfig(2, 1);
+ addSlobrokConfig();
+ addDistributionConfig();
+ addZookeepersConfig();
+ ClusterControllerConfigFetcher configFetcher = new ClusterControllerConfigFetcher();
+ configFetcher.getOptions();
+ configFetcher.updated(100);
+ assertEquals(1, configFetcher.getGeneration());
+ configFetcher.close();
+ }
+
+ public void testInitialConfigFailure() throws Exception {
+ setFleetControllerConfigProperty();
+ setSlobrokConfigProperty();
+ addFleetControllerConfig(2, 1);
+ addSlobrokConfig();
+ addDistributionConfig();
+ addZookeepersConfig();
+ try{
+ ClusterControllerConfigFetcher configFetcher = new ClusterControllerConfigFetcher() {
+ boolean configReady() {
+ return false;
+ }
+ };
+ fail("Control should not reach here");
+ } catch (IllegalStateException e) {
+ assertEquals("Initial configuration failed.", e.getMessage());
+ }
+ }
+
+ public void testConfigUpdate() throws Exception {
+ setFleetControllerConfigProperty();
+ setSlobrokConfigProperty();
+ addFleetControllerConfig(2, 1);
+ addSlobrokConfig();
+ addDistributionConfig();
+ addZookeepersConfig();
+ ClusterControllerConfigFetcher configFetcher = new ClusterControllerConfigFetcher() {
+ boolean configUpdated(long millis) {
+ return true;
+ }
+ };
+ configFetcher.updated(1000);
+ }
+}
diff --git a/clustercontroller-standalone/src/test/java/com/yahoo/vespa/clustercontroller/standalone/ClusterControllerTest.java b/clustercontroller-standalone/src/test/java/com/yahoo/vespa/clustercontroller/standalone/ClusterControllerTest.java
new file mode 100644
index 00000000000..9b274861e0c
--- /dev/null
+++ b/clustercontroller-standalone/src/test/java/com/yahoo/vespa/clustercontroller/standalone/ClusterControllerTest.java
@@ -0,0 +1,96 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.vespa.clustercontroller.standalone;
+
+import com.yahoo.vdslib.distribution.Distribution;
+import junit.framework.TestCase;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.Map;
+import java.util.TreeMap;
+
+public abstract class ClusterControllerTest extends TestCase {
+ private final Map<String, String> overriddenProperties = new TreeMap<>();
+ private File tempDirectory;
+
+ protected void setProperty(String p, String val) {
+ if (!overriddenProperties.containsKey(p)) {
+ overriddenProperties.put(p, System.getProperty(p));
+ }
+ System.setProperty(p, val);
+ }
+
+ protected File createTemporaryDirectory() throws IOException {
+ File f = File.createTempFile("clustercontroller", "configtest");
+ if (f.exists()) f.delete();
+ f.mkdirs();
+ return f;
+ }
+
+ @Override
+ public void setUp() throws Exception {
+ tempDirectory = createTemporaryDirectory();
+ }
+
+ @Override
+ public void tearDown() {
+ for (Map.Entry<String, String> e : overriddenProperties.entrySet()) {
+ if (e.getValue() == null) {
+ System.clearProperty(e.getKey());
+ } else {
+ System.setProperty(e.getKey(), e.getValue());
+ }
+ }
+ overriddenProperties.clear();
+ if (tempDirectory != null) {
+ for (File f : tempDirectory.listFiles()) {
+ f.delete();
+ }
+ tempDirectory.delete();
+ tempDirectory = null;
+ }
+ }
+
+ protected void writeConfig(String config, String value) throws IOException {
+ File f = new File(tempDirectory, config + ".cfg");
+ FileWriter fw = new FileWriter(f);
+ fw.write(value);
+ fw.close();
+ }
+
+ protected void setFleetControllerConfigProperty() {
+ setProperty("config.id", "dir:" + tempDirectory.toString());
+ }
+
+ protected void addFleetControllerConfig(int stateGatherCount, int fleetcontrollers) throws IOException {
+ writeConfig("fleetcontroller", "cluster_name \"storage\"\n" +
+ "index 0\n" +
+ "state_gather_count " + stateGatherCount + "\n" +
+ "fleet_controller_count " + fleetcontrollers + "\n" +
+ "zookeeper_server \"\"");
+ }
+ protected void setSlobrokConfigProperty() {
+ setProperty("slobrok.config.id", "dir:" + tempDirectory.toString());
+ }
+ protected void addSlobrokConfig() throws IOException {
+ writeConfig("slobroks", "cluster_name \"storage\"\n" +
+ "index 0\n" +
+ "zookeeper_server \"\"");
+ }
+ protected void addDistributionConfig() throws IOException {
+ writeConfig("stor-distribution", Distribution.getDefaultDistributionConfig(2, 10));
+ }
+ protected void addZookeepersConfig() throws IOException {
+ writeConfig("zookeepers", "zookeeperserverlist \"\"");
+ }
+
+ protected void setupConfig() throws Exception {
+ setFleetControllerConfigProperty();
+ setSlobrokConfigProperty();
+ addFleetControllerConfig(2, 1);
+ addSlobrokConfig();
+ addDistributionConfig();
+ addZookeepersConfig();
+ }
+}
diff --git a/clustercontroller-standalone/src/test/java/com/yahoo/vespa/clustercontroller/standalone/StandAloneClusterControllerTest.java b/clustercontroller-standalone/src/test/java/com/yahoo/vespa/clustercontroller/standalone/StandAloneClusterControllerTest.java
new file mode 100644
index 00000000000..6f673185a5d
--- /dev/null
+++ b/clustercontroller-standalone/src/test/java/com/yahoo/vespa/clustercontroller/standalone/StandAloneClusterControllerTest.java
@@ -0,0 +1,103 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.vespa.clustercontroller.standalone;
+
+public class StandAloneClusterControllerTest extends ClusterControllerTest {
+
+ abstract class Runner implements Runnable {
+ boolean completed = false;
+ Exception exception = null;
+
+ public void run() {
+ try{
+ attemptRun();
+ completed = true;
+ } catch (Exception e) {
+ exception = e;
+ }
+ }
+
+ public abstract void attemptRun() throws Exception;
+ }
+
+ public void testSimpleStartStop() throws Exception {
+ setupConfig();
+ ClusterControllerConfigFetcher configFetcher = new ClusterControllerConfigFetcher();
+ StandAloneClusterController controller = new StandAloneClusterController(configFetcher);
+ assertEquals("Fleetcontroller 0 of cluster storage", controller.getName());
+ controller.start();
+ controller.stop();
+ }
+
+ public void testShortRun() throws Exception {
+ setupConfig();
+ ClusterControllerConfigFetcher configFetcher = new ClusterControllerConfigFetcher() {
+ int counter = 0;
+ @Override
+ public boolean updated(long timeoutMillis) throws Exception {
+ return (++counter % 2 == 0);
+ }
+ @Override
+ public void close() {
+ throw new RuntimeException("Failed to stop");
+ }
+ };
+ final StandAloneClusterController controller = new StandAloneClusterController(configFetcher);
+ assertEquals("Fleetcontroller 0 of cluster storage", controller.getName());
+ controller.start();
+ Runner r = new Runner() {
+ @Override
+ public void attemptRun() throws Exception {
+ controller.run();
+ }
+ };
+ Thread t = new Thread(r);
+ t.start();
+ for (int i=0; i<100; ++i) {
+ try{ Thread.sleep(1); } catch (InterruptedException e) {}
+ synchronized (controller) {
+ controller.notifyAll();
+ }
+ }
+ StandAloneClusterController.ShutdownHook hook = new StandAloneClusterController.ShutdownHook(controller);
+ hook.start();
+ hook.join();
+ t.join();
+ assertEquals(true, r.completed);
+ assertNull(r.exception);
+ }
+
+ public void testFailStart() throws Exception {
+ setupConfig();
+ ClusterControllerConfigFetcher configFetcher = new ClusterControllerConfigFetcher();
+ StandAloneClusterController controller = new StandAloneClusterController(configFetcher) {
+ @Override
+ public void start() throws Exception {
+ throw new RuntimeException("Foo");
+ }
+ };
+ StandAloneClusterController.runApplication(controller);
+ }
+
+ public void testFailRun() throws Exception {
+ setupConfig();
+ ClusterControllerConfigFetcher configFetcher = new ClusterControllerConfigFetcher();
+ StandAloneClusterController controller = new StandAloneClusterController(configFetcher) {
+ @Override
+ public void run() throws Exception {
+ throw new RuntimeException("Foo");
+ }
+ };
+ StandAloneClusterController.runApplication(controller);
+ }
+
+ public void testCallMainToGetCoverage() throws Exception {
+ tearDown();
+ setProperty("config.id", "file:non-existing");
+ try{
+ StandAloneClusterController.main(new String[0]);
+ fail("Control should not get here");
+ } catch (IllegalArgumentException e) {
+ }
+ }
+
+}