List of usage examples for org.apache.commons.configuration Configuration addProperty
void addProperty(String key, Object value);
From source file:org.apache.whirr.actions.BootstrapClusterActionTest.java
@SuppressWarnings("unchecked") @Test/* w ww .ja v a 2 s .c o m*/ public void testDoActionRetriesSucceed() throws Exception { CompositeConfiguration config = new CompositeConfiguration(); if (System.getProperty("config") != null) { config.addConfiguration(new PropertiesConfiguration(System.getProperty("config"))); } Configuration conf = new PropertiesConfiguration(); conf.addProperty("whirr.service-name", "test-service"); conf.addProperty("whirr.cluster-name", "test-cluster"); conf.addProperty("whirr.instance-templates", "1 hadoop-namenode+hadoop-jobtracker,4 hadoop-datanode+hadoop-tasktracker"); conf.addProperty("whirr.instance-templates-max-percent-failures", "60 hadoop-datanode+hadoop-tasktracker"); conf.addProperty("whirr.provider", "ec2"); config.addConfiguration(conf); ClusterSpec clusterSpec = ClusterSpec.withTemporaryKeys(conf); Set<String> jtnn = new HashSet<String>(); jtnn.add("hadoop-jobtracker"); jtnn.add("hadoop-namenode"); Set<String> dntt = new HashSet<String>(); dntt.add("hadoop-datanode"); dntt.add("hadoop-tasktracker"); TestNodeStarterFactory nodeStarterFactory = null; ClusterActionHandler handler = mock(ClusterActionHandler.class); LoadingCache<String, ClusterActionHandler> handlerMap = convertMapToLoadingCache( ImmutableMap.<String, ClusterActionHandler>builder().put("hadoop-jobtracker", handler) .put("hadoop-namenode", handler).put("hadoop-datanode", handler) .put("hadoop-tasktracker", handler).build()); Function<ClusterSpec, ComputeServiceContext> getCompute = mock(Function.class); ComputeServiceContext serviceContext = mock(ComputeServiceContext.class); ComputeService computeService = mock(ComputeService.class); TemplateBuilder templateBuilder = mock(TemplateBuilder.class); Template template = mock(Template.class); when(getCompute.apply(clusterSpec)).thenReturn(serviceContext); when(serviceContext.getComputeService()).thenReturn(computeService); when(computeService.templateBuilder()).thenReturn(templateBuilder); when(templateBuilder.options((TemplateOptions) any())).thenReturn(templateBuilder); when(templateBuilder.build()).thenReturn(template); // here is a scenario when jt+nn fails once, then the retry is successful // and from the dn+tt one node fails, then the retry is successful Map<Set<String>, Stack<Integer>> reaction = Maps.newHashMap(); Stack<Integer> jtnnStack = new Stack<Integer>(); jtnnStack.push(1); // then ok jtnnStack.push(0); // initially fail reaction.put(jtnn, jtnnStack); Stack<Integer> ddttStack = new Stack<Integer>(); ddttStack.push(3); // 3 from 4, just enough reaction.put(dntt, ddttStack); nodeStarterFactory = new TestNodeStarterFactory(reaction); BootstrapClusterAction bootstrapper = new BootstrapClusterAction(getCompute, handlerMap, nodeStarterFactory); bootstrapper.execute(clusterSpec, null); if (nodeStarterFactory != null) { nodeStarterFactory.validateCompletion(); } }
From source file:org.apache.whirr.actions.BootstrapClusterActionTest.java
@SuppressWarnings("unchecked") @Test(expected = IOException.class) public void testDoActionRetriesExceeds() throws Exception { CompositeConfiguration config = new CompositeConfiguration(); if (System.getProperty("config") != null) { config.addConfiguration(new PropertiesConfiguration(System.getProperty("config"))); }//from w w w . j a v a2 s . c o m Configuration conf = new PropertiesConfiguration(); conf.addProperty("whirr.service-name", "test-service"); conf.addProperty("whirr.cluster-name", "test-cluster"); conf.addProperty("whirr.instance-templates", "1 hadoop-namenode+hadoop-jobtracker,4 hadoop-datanode+hadoop-tasktracker"); conf.addProperty("whirr.instance-templates-max-percent-failures", "60 hadoop-datanode+hadoop-tasktracker"); conf.addProperty("whirr.provider", "ec2"); config.addConfiguration(conf); ClusterSpec clusterSpec = ClusterSpec.withTemporaryKeys(conf); Set<String> jtnn = new HashSet<String>(); jtnn.add("hadoop-jobtracker"); jtnn.add("hadoop-namenode"); Set<String> dntt = new HashSet<String>(); dntt.add("hadoop-datanode"); dntt.add("hadoop-tasktracker"); TestNodeStarterFactory nodeStarterFactory = null; ClusterActionHandler handler = mock(ClusterActionHandler.class); LoadingCache<String, ClusterActionHandler> handlerMap = convertMapToLoadingCache( ImmutableMap.<String, ClusterActionHandler>builder().put("hadoop-jobtracker", handler) .put("hadoop-namenode", handler).put("hadoop-datanode", handler) .put("hadoop-tasktracker", handler).build()); Function<ClusterSpec, ComputeServiceContext> getCompute = mock(Function.class); ComputeServiceContext serviceContext = mock(ComputeServiceContext.class); ComputeService computeService = mock(ComputeService.class); TemplateBuilder templateBuilder = mock(TemplateBuilder.class); Template template = mock(Template.class); when(getCompute.apply(clusterSpec)).thenReturn(serviceContext); when(serviceContext.getComputeService()).thenReturn(computeService); when(computeService.templateBuilder()).thenReturn(templateBuilder); when(templateBuilder.options((TemplateOptions) any())).thenReturn(templateBuilder); when(templateBuilder.build()).thenReturn(template); // here is a scenario when jt+nn does not fail // but the dn+tt one node fails 3, then in the retry fails 2 // at the end result, the cluster will fail, throwing IOException Map<Set<String>, Stack<Integer>> reaction = Maps.newHashMap(); Stack<Integer> jtnnStack = new Stack<Integer>(); jtnnStack.push(new Integer(1)); reaction.put(jtnn, jtnnStack); Stack<Integer> ddttStack = new Stack<Integer>(); ddttStack.push(new Integer(1)); // 1 from 4, retryRequired ddttStack.push(new Integer(1)); // 1 from 4, still retryRequired reaction.put(dntt, ddttStack); nodeStarterFactory = new TestNodeStarterFactory(reaction); BootstrapClusterAction bootstrapper = new BootstrapClusterAction(getCompute, handlerMap, nodeStarterFactory); bootstrapper.execute(clusterSpec, null); // this should file with too many retries if (nodeStarterFactory != null) { nodeStarterFactory.validateCompletion(); } }
From source file:org.apache.whirr.actions.BootstrapClusterActionTest.java
@Test @SuppressWarnings("unchecked") public void testSubroleInvoked() throws Exception { CompositeConfiguration config = new CompositeConfiguration(); if (System.getProperty("config") != null) { config.addConfiguration(new PropertiesConfiguration(System.getProperty("config"))); }//from w ww.j a v a 2s. c o m Configuration conf = new PropertiesConfiguration(); conf.addProperty("whirr.service-name", "test-service"); conf.addProperty("whirr.cluster-name", "test-cluster"); conf.addProperty("whirr.instance-templates", "1 puppet:module::manifest+something-else"); conf.addProperty("whirr.provider", "ec2"); config.addConfiguration(conf); ClusterSpec clusterSpec = ClusterSpec.withTemporaryKeys(conf); Set<String> nn = new HashSet<String>(); nn.add("puppet:module::manifest"); nn.add("something-else"); TestNodeStarterFactory nodeStarterFactory = null; ClusterActionHandlerFactory puppetHandlerFactory = mock(ClusterActionHandlerFactory.class); ClusterActionHandler handler = mock(ClusterActionHandler.class); when(puppetHandlerFactory.getRolePrefix()).thenReturn("puppet:"); when(puppetHandlerFactory.create("module::manifest")).thenReturn(handler); when(handler.getRole()).thenReturn("something-else"); LoadingCache<String, ClusterActionHandler> handlerMap = new HandlerMapFactory() .create(ImmutableSet.of(puppetHandlerFactory), ImmutableSet.of(handler)); Function<ClusterSpec, ComputeServiceContext> getCompute = mock(Function.class); ComputeServiceContext serviceContext = mock(ComputeServiceContext.class); ComputeService computeService = mock(ComputeService.class); TemplateBuilder templateBuilder = mock(TemplateBuilder.class); Template template = mock(Template.class); when(getCompute.apply(clusterSpec)).thenReturn(serviceContext); when(serviceContext.getComputeService()).thenReturn(computeService); when(computeService.templateBuilder()).thenReturn(templateBuilder); when(templateBuilder.options((TemplateOptions) any())).thenReturn(templateBuilder); when(templateBuilder.build()).thenReturn(template); Map<Set<String>, Stack<Integer>> reaction = Maps.newHashMap(); Stack<Integer> nnStack = new Stack<Integer>(); nnStack.push(new Integer(1)); reaction.put(nn, nnStack); nodeStarterFactory = new TestNodeStarterFactory(reaction); BootstrapClusterAction bootstrapper = new BootstrapClusterAction(getCompute, handlerMap, nodeStarterFactory); bootstrapper.execute(clusterSpec, null); if (nodeStarterFactory != null) { nodeStarterFactory.validateCompletion(); } }
From source file:org.apache.whirr.actions.BootstrapClusterActionTest.java
@SuppressWarnings("unchecked") @Test(expected = IllegalArgumentException.class) /** test is the same as previous (SubroleInvoked) except it knows puppet, not puppet:, as the role; * the colon in the role def'n is the indication it accepts subroles, * so this should throw IllegalArgument when we refer to puppet:module... *///from www . ja v a 2 s . com public void testSubroleNotSupported() throws Exception { CompositeConfiguration config = new CompositeConfiguration(); if (System.getProperty("config") != null) { config.addConfiguration(new PropertiesConfiguration(System.getProperty("config"))); } Configuration conf = new PropertiesConfiguration(); conf.addProperty("whirr.service-name", "test-service"); conf.addProperty("whirr.cluster-name", "test-cluster"); conf.addProperty("whirr.instance-templates", "1 puppet:module::manifest+something-else"); conf.addProperty("whirr.provider", "ec2"); config.addConfiguration(conf); ClusterSpec clusterSpec = ClusterSpec.withTemporaryKeys(conf); Set<String> nn = new HashSet<String>(); nn.add("puppet:module::manifest"); nn.add("something-else"); TestNodeStarterFactory nodeStarterFactory = null; ClusterActionHandlerFactory puppetHandlerFactory = mock(ClusterActionHandlerFactory.class); ClusterActionHandler handler = mock(ClusterActionHandler.class); when(puppetHandlerFactory.getRolePrefix()).thenReturn("puppet"); when(handler.getRole()).thenReturn("something-else"); LoadingCache<String, ClusterActionHandler> handlerMap = new HandlerMapFactory() .create(ImmutableSet.of(puppetHandlerFactory), ImmutableSet.of(handler)); Function<ClusterSpec, ComputeServiceContext> getCompute = mock(Function.class); ComputeServiceContext serviceContext = mock(ComputeServiceContext.class); ComputeService computeService = mock(ComputeService.class); TemplateBuilder templateBuilder = mock(TemplateBuilder.class); Template template = mock(Template.class); when(getCompute.apply(clusterSpec)).thenReturn(serviceContext); when(serviceContext.getComputeService()).thenReturn(computeService); when(computeService.templateBuilder()).thenReturn(templateBuilder); when(templateBuilder.options((TemplateOptions) any())).thenReturn(templateBuilder); when(templateBuilder.build()).thenReturn(template); Map<Set<String>, Stack<Integer>> reaction = Maps.newHashMap(); Stack<Integer> nnStack = new Stack<Integer>(); nnStack.push(1); reaction.put(nn, nnStack); nodeStarterFactory = new TestNodeStarterFactory(reaction); BootstrapClusterAction bootstrapper = new BootstrapClusterAction(getCompute, handlerMap, nodeStarterFactory); bootstrapper.execute(clusterSpec, null); if (nodeStarterFactory != null) { nodeStarterFactory.validateCompletion(); } }
From source file:org.apache.whirr.cli.command.DestroyClusterCommandTest.java
@Test public void testAllOptions() throws Exception { ClusterControllerFactory factory = mock(ClusterControllerFactory.class); ClusterController controller = mock(ClusterController.class); when(factory.create((String) any())).thenReturn(controller); DestroyClusterCommand command = new DestroyClusterCommand(factory); Map<String, File> keys = KeyPair.generateTemporaryFiles(); int rc = command.run(null, out, null, Lists.newArrayList("--instance-templates", "1 noop", "--service-name", "test-service", "--cluster-name", "test-cluster", "--provider", "rackspace", "--identity", "myusername", "--credential", "mypassword", "--private-key-file", keys.get("private").getAbsolutePath(), "--version", "version-string")); assertThat(rc, is(0));/* w w w . ja va 2 s . c o m*/ Configuration conf = new PropertiesConfiguration(); conf.addProperty("whirr.version", "version-string"); conf.addProperty("whirr.instance-templates", "1 noop"); ClusterSpec expectedClusterSpec = ClusterSpec.withTemporaryKeys(conf); expectedClusterSpec.setServiceName("test-service"); expectedClusterSpec.setProvider("rackspace"); expectedClusterSpec.setIdentity("myusername"); expectedClusterSpec.setCredential("mypassword"); expectedClusterSpec.setClusterName("test-cluster"); expectedClusterSpec.setPrivateKey(keys.get("private")); expectedClusterSpec.setPublicKey(keys.get("public")); verify(factory).create("test-service"); verify(controller).destroyCluster(expectedClusterSpec); }
From source file:org.apache.whirr.cli.command.LaunchClusterCommandTest.java
@Test public void testAllOptions() throws Exception { ClusterControllerFactory factory = mock(ClusterControllerFactory.class); ClusterController controller = mock(ClusterController.class); Cluster cluster = mock(Cluster.class); when(factory.create((String) any())).thenReturn(controller); when(controller.launchCluster((ClusterSpec) any())).thenReturn(cluster); LaunchClusterCommand command = new LaunchClusterCommand(factory); Map<String, File> keys = KeyPair.generateTemporaryFiles(); int rc = command.run(null, out, null, Lists.newArrayList("--service-name", "test-service", "--cluster-name", "test-cluster", "--instance-templates", "1 role1+role2,2 role3", "--provider", "rackspace", "--identity", "myusername", "--credential", "mypassword", "--private-key-file", keys.get("private").getAbsolutePath(), "--version", "version-string")); assertThat(rc, is(0));//from w w w .ja v a 2s . co m Configuration conf = new PropertiesConfiguration(); conf.addProperty("whirr.version", "version-string"); ClusterSpec expectedClusterSpec = ClusterSpec.withTemporaryKeys(conf); expectedClusterSpec.setInstanceTemplates( Lists.newArrayList(InstanceTemplate.builder().numberOfInstance(1).roles("role1", "role2").build(), InstanceTemplate.builder().numberOfInstance(2).roles("role3").build())); expectedClusterSpec.setServiceName("test-service"); expectedClusterSpec.setProvider("rackspace"); expectedClusterSpec.setIdentity("myusername"); expectedClusterSpec.setCredential("mypassword"); expectedClusterSpec.setClusterName("test-cluster"); expectedClusterSpec.setPrivateKey(keys.get("private")); expectedClusterSpec.setPublicKey(keys.get("public")); verify(factory).create("test-service"); verify(controller).launchCluster(expectedClusterSpec); assertThat(outBytes.toString(), containsString("Started cluster of 0 instances")); }
From source file:org.apache.whirr.cli.command.LaunchClusterCommandTest.java
@Test public void testMaxPercentFailure() throws Exception { ClusterControllerFactory factory = mock(ClusterControllerFactory.class); ClusterController controller = mock(ClusterController.class); Cluster cluster = mock(Cluster.class); when(factory.create((String) any())).thenReturn(controller); when(controller.launchCluster((ClusterSpec) any())).thenReturn(cluster); LaunchClusterCommand command = new LaunchClusterCommand(factory); Map<String, File> keys = KeyPair.generateTemporaryFiles(); int rc = command.run(null, out, null, Lists.newArrayList("--service-name", "hadoop", "--cluster-name", "test-cluster", "--instance-templates", "1 hadoop-namenode+hadoop-jobtracker,3 hadoop-datanode+hadoop-tasktracker", "--instance-templates-max-percent-failures", "60 hadoop-datanode+hadoop-tasktracker", "--provider", "aws-ec2", "--identity", "myusername", "--credential", "mypassword", "--private-key-file", keys.get("private").getAbsolutePath(), "--version", "version-string")); assertThat(rc, is(0));//from w ww . jav a 2s .c o m Configuration conf = new PropertiesConfiguration(); conf.addProperty("whirr.version", "version-string"); conf.addProperty("whirr.instance-templates-max-percent-failure", "60 hadoop-datanode+hadoop-tasktracker"); ClusterSpec expectedClusterSpec = ClusterSpec.withTemporaryKeys(conf); expectedClusterSpec.setInstanceTemplates(Lists.newArrayList( InstanceTemplate.builder().numberOfInstance(1).minNumberOfInstances(1) .roles("hadoop-namenode", "hadoop-jobtracker").build(), InstanceTemplate.builder().numberOfInstance(3).minNumberOfInstances(2) .roles("hadoop-datanode", "hadoop-tasktracker").build())); expectedClusterSpec.setServiceName("hadoop"); expectedClusterSpec.setProvider("aws-ec2"); expectedClusterSpec.setIdentity("myusername"); expectedClusterSpec.setCredential("mypassword"); expectedClusterSpec.setClusterName("test-cluster"); expectedClusterSpec.setPrivateKey(keys.get("private")); expectedClusterSpec.setPublicKey(keys.get("public")); verify(factory).create("hadoop"); verify(controller).launchCluster(expectedClusterSpec); assertThat(outBytes.toString(), containsString("Started cluster of 0 instances")); }
From source file:org.apache.whirr.ClusterSpec.java
@VisibleForTesting public static ClusterSpec withTemporaryKeys(Configuration conf) throws ConfigurationException, JSchException, IOException { if (!conf.containsKey(Property.PRIVATE_KEY_FILE.getConfigName())) { Map<String, File> keys = org.apache.whirr.util.KeyPair.generateTemporaryFiles(); LoggerFactory.getLogger(ClusterSpec.class).debug("ssh keys: " + keys.toString()); conf.addProperty(Property.PRIVATE_KEY_FILE.getConfigName(), keys.get("private").getAbsolutePath()); conf.addProperty(Property.PUBLIC_KEY_FILE.getConfigName(), keys.get("public").getAbsolutePath()); }/*from w ww . j a v a 2s. co m*/ return new ClusterSpec(conf); }
From source file:org.apache.whirr.ClusterSpecTest.java
@Test(expected = IllegalArgumentException.class) public void testIllegalArgumentExceptionOnInstancesTemplates() throws Exception { Configuration conf = new PropertiesConfiguration(); conf.addProperty("whirr.instance-templates", "1 hadoop-namenode+hadoop-jobtracker,3 hadoop-datanode+hadoop-tasktracker"); conf.addProperty("whirr.instance-templates-max-percent-failures", "60 % hadoop-datanode+hadoop-tasktracker"); ClusterSpec expectedClusterSpec = ClusterSpec.withNoDefaults(conf); List<InstanceTemplate> templates = expectedClusterSpec.getInstanceTemplates(); InstanceTemplate t1 = templates.get(0); assertThat(t1.getMinNumberOfInstances(), is(1)); InstanceTemplate t2 = templates.get(1); assertThat(t2.getMinNumberOfInstances(), is(2)); }
From source file:org.apache.whirr.ClusterSpecTest.java
@Test(expected = NumberFormatException.class) public void testNumberFormatExceptionOnInstancesTemplates() throws Exception { Configuration conf = new PropertiesConfiguration(); conf.addProperty("whirr.instance-templates", "1 hadoop-namenode+hadoop-jobtracker,3 hadoop-datanode+hadoop-tasktracker"); conf.addProperty("whirr.instance-templates-max-percent-failures", "60% hadoop-datanode+hadoop-tasktracker"); ClusterSpec expectedClusterSpec = ClusterSpec.withNoDefaults(conf); List<InstanceTemplate> templates = expectedClusterSpec.getInstanceTemplates(); InstanceTemplate t1 = templates.get(0); assertThat(t1.getMinNumberOfInstances(), is(1)); InstanceTemplate t2 = templates.get(1); assertThat(t2.getMinNumberOfInstances(), is(2)); }