@Override public void writeClusterTemplate(ClusterTemplate clusterTemplate) throws IOException, IllegalAccessException { int version = getVersion(EntityType.CLUSTER_TEMPLATE, clusterTemplate.getName()); clusterTemplate.setVersion(version); writeEntity(EntityType.CLUSTER_TEMPLATE, clusterTemplate.getName(), version, serialize(clusterTemplate, ClusterTemplate.class)); clusterTemplate.setVersion(Constants.DEFAULT_VERSION); }
@Test public void testClusterCreateLeaseDuration() throws Exception { // Reactor template has forever initial lease duration verifyInitialLeaseDuration(200, HttpResponseStatus.OK, 200, reactorTemplate.getName()); verifyInitialLeaseDuration(0, HttpResponseStatus.OK, 0, reactorTemplate.getName()); verifyInitialLeaseDuration(0, HttpResponseStatus.OK, -1, reactorTemplate.getName()); // Small template has 10000 initial lease duration verifyInitialLeaseDuration(10000, HttpResponseStatus.BAD_REQUEST, 20000, smallTemplate.getName()); verifyInitialLeaseDuration(10000, HttpResponseStatus.OK, 10000, smallTemplate.getName()); verifyInitialLeaseDuration(500, HttpResponseStatus.OK, 500, smallTemplate.getName()); verifyInitialLeaseDuration(10000, HttpResponseStatus.BAD_REQUEST, 0, smallTemplate.getName()); verifyInitialLeaseDuration(10000, HttpResponseStatus.OK, -1, smallTemplate.getName()); }
@Test public void testTemplatesDoNotOverlapAcrossTenants() throws Exception { ClusterTemplate entity1 = Entities.ClusterTemplateExample.HDFS; ClusterTemplate entity2 = Entities.ClusterTemplateExample.REACTOR; EntityStoreView account1View = entityStoreService.getView(tenant1Admin); EntityStoreView account2View = entityStoreService.getView(tenant2Admin); account1View.writeClusterTemplate(entity1); Assert.assertNull(account2View.getClusterTemplate(entity1.getName())); Assert.assertEquals(entity1, account1View.getClusterTemplate(entity1.getName())); account1View.deleteClusterTemplate(entity1.getName()); account2View.writeClusterTemplate(entity1); Assert.assertNull(account1View.getClusterTemplate(entity1.getName())); Assert.assertEquals(entity1, account2View.getClusterTemplate(entity1.getName())); account2View.writeClusterTemplate(entity2); Assert.assertTrue(account1View.getAllClusterTemplates().isEmpty()); Assert.assertEquals(ImmutableSet.<ClusterTemplate>of(entity1, entity2), ImmutableSet.copyOf(account2View.getAllClusterTemplates())); }
@Test public void test_13_deleteTemplate() throws Exception { int beforePartialsCount = entityStoreView.getAllPartialTemplates().size(); int beforeClusterTemplatesCount = entityStoreView.getAllClusterTemplates().size(); entityStoreView.deleteClusterTemplate(insecureTemplate.getName()); entityStoreView.deletePartialTemplate(sensuPartial.getName()); int afterPartialsCount = entityStoreView.getAllPartialTemplates().size(); int afterClusterTemplatesCount = entityStoreView.getAllClusterTemplates().size(); Assert.assertEquals(afterPartialsCount, beforePartialsCount - 1); Assert.assertEquals(afterClusterTemplatesCount, beforeClusterTemplatesCount - 1); } }
@Test public void testGetAllClusterTemplates() throws Exception { EntityStoreView entityStore = entityStoreService.getView(tenant1Admin); Assert.assertEquals(0, entityStore.getAllClusterTemplates().size()); ClusterTemplate c1 = Entities.ClusterTemplateExample.HDFS; ClusterTemplate c2 = Entities.ClusterTemplateExample.REACTOR; List<ClusterTemplate> clusterTemplates = ImmutableList.of(c1, c2); for (ClusterTemplate clusterTemplate : clusterTemplates) { entityStore.writeClusterTemplate(clusterTemplate); } Collection<ClusterTemplate> result = entityStore.getAllClusterTemplates(); Assert.assertEquals("cluster templates written and fetched are not equal in size", clusterTemplates.size(), result.size()); Assert.assertTrue("not all written cluster templates were found in the results", result.containsAll(clusterTemplates)); clusterTemplates = ImmutableList.of(c1); entityStore.deleteClusterTemplate(c2.getName()); result = entityStore.getAllClusterTemplates(); Assert.assertEquals("cluster templates written and fetched are not equal in size", clusterTemplates.size(), result.size()); Assert.assertTrue("not all written cluster templates were found in the results", result.containsAll(clusterTemplates)); }
@Test(expected = MissingEntityException.class) public void testInvalidTemplateClusterCreate() throws Exception { String name = "clusty"; Map<String, Object> providerFields = ImmutableMap.<String, Object>of("keyname", "ec2", "key", "keycontents"); ClusterCreateRequest createRequest = ClusterCreateRequest.builder() .setName(name) .setClusterTemplateName("not" + basicTemplate.getName()) .setNumMachines(1) .setProviderFields(providerFields) .build(); clusterService.requestClusterCreate(createRequest, account); }
@Test public void testEndToEnd() throws Exception { ClusterCreateRequest request = ClusterCreateRequest.builder() .setName("mycluster") .setClusterTemplateName(reactorTemplate.getName()) .setNumMachines(5) .setInitialLeaseDuration(0L) .build(); Map<String, Node> nodes = solver.solveClusterNodes( getBaseBuilder().setClusterTemplate(reactorTemplate).setProvider(provider).build(), request); Multiset<Set<String>> serviceSetCounts = HashMultiset.create(); for (Node node : nodes.values()) { Set<String> serviceNames = Sets.newHashSet( Iterables.transform(node.getServices(), new Function<Service, String>() { @Override public String apply(Service input) { return input.getName(); } }) ); serviceSetCounts.add(serviceNames); } Assert.assertEquals(1, serviceSetCounts.count(ImmutableSet.of("namenode", "resourcemanager", "hbasemaster"))); Assert.assertEquals(3, serviceSetCounts.count(ImmutableSet.of("datanode", "nodemanager", "regionserver"))); Assert.assertEquals(1, serviceSetCounts.count(ImmutableSet.of("reactor", "zookeeper"))); }
@Test public void testMaxClusterSize() throws Exception { Configuration conf = Configuration.create(); int maxClusterSize = conf.getInt(Constants.MAX_CLUSTER_SIZE); ClusterCreateRequest clusterCreateRequest = ClusterCreateRequest.builder() .setName("cluster") .setClusterTemplateName(smallTemplate.getName()) .setNumMachines(maxClusterSize + 1) .build(); assertResponseStatus(doPostExternalAPI("/clusters", gson.toJson(clusterCreateRequest), USER1_HEADERS), HttpResponseStatus.BAD_REQUEST); }
@Test(expected = MissingEntityException.class) public void testInvalidProviderClusterCreate() throws Exception { String name = "clusty"; Map<String, Object> providerFields = ImmutableMap.<String, Object>of("keyname", "ec2", "key", "keycontents"); ClusterCreateRequest createRequest = ClusterCreateRequest.builder() .setName(name) .setClusterTemplateName(basicTemplate.getName()) .setProviderName("not" + provider.getName()) .setNumMachines(1) .setProviderFields(providerFields) .build(); clusterService.requestClusterCreate(createRequest, account); }
@Test public void testGetClusterNotOwnedByUserReturns404() throws Exception { ClusterCreateRequest clusterCreateRequest = ClusterCreateRequest.builder() .setName("cluster1") .setClusterTemplateName(reactorTemplate.getName()) .setNumMachines(5) .build(); HttpResponse response = doPostExternalAPI("/clusters", gson.toJson(clusterCreateRequest), USER1_HEADERS); assertResponseStatus(response, HttpResponseStatus.OK); String clusterId = getIdFromResponse(response); assertResponseStatus(doGetExternalAPI("/clusters/" + clusterId, USER2_HEADERS), HttpResponseStatus.NOT_FOUND); }
@Test public void testAdminCanGetClustersOwnedByOthers() throws Exception { ClusterCreateRequest clusterCreateRequest = ClusterCreateRequest.builder() .setName("cluster1") .setClusterTemplateName(reactorTemplate.getName()) .setNumMachines(5) .build(); HttpResponse response = doPostExternalAPI("/clusters", gson.toJson(clusterCreateRequest), USER1_HEADERS); assertResponseStatus(response, HttpResponseStatus.OK); String clusterId = getIdFromResponse(response); assertResponseStatus(doGetExternalAPI("/clusters/" + clusterId, ADMIN_HEADERS), HttpResponseStatus.OK); }
@Test public void testDeleteOnClusterNotOwnedByUserReturns404() throws Exception { ClusterCreateRequest clusterCreateRequest = ClusterCreateRequest.builder() .setName("cluster1") .setClusterTemplateName(reactorTemplate.getName()) .setNumMachines(5) .build(); HttpResponse response = doPostExternalAPI("/clusters", gson.toJson(clusterCreateRequest), USER1_HEADERS); assertResponseStatus(response, HttpResponseStatus.OK); String clusterId = getIdFromResponse(response); assertResponseStatus(doDeleteExternalAPI("/clusters/" + clusterId, USER2_HEADERS), HttpResponseStatus.NOT_FOUND); }
@Test public void testGetStoreDeleteClusterTemplate() throws Exception { EntityStoreView entityStore = entityStoreService.getView(tenant1Admin); ClusterTemplate clusterTemplate = Entities.ClusterTemplateExample.REACTOR; String clusterTemplateName = clusterTemplate.getName(); Assert.assertNull(entityStore.getClusterTemplate(clusterTemplateName)); // write should work entityStore.writeClusterTemplate(clusterTemplate); ClusterTemplate result = entityStore.getClusterTemplate(clusterTemplateName); Assert.assertEquals(clusterTemplate, result); // bump version should work entityStore.writeClusterTemplate(clusterTemplate); entityStore.writeClusterTemplate(clusterTemplate); Collection<ClusterTemplate> clusterTemplates = entityStore.getAllClusterTemplates(); Assert.assertEquals(1, clusterTemplates.size()); Set<Integer> versions = Sets.newHashSet(); for (ClusterTemplate clusterTemplate1 : clusterTemplates) { versions.add(clusterTemplate1.getVersion()); } Assert.assertEquals(Sets.newHashSet(3), versions); // delete should work entityStore.deleteClusterTemplate(clusterTemplateName); Assert.assertNull(entityStore.getClusterTemplate(clusterTemplateName)); }
@Test public void testAddCluster() throws Exception { String clusterName = "my-cluster"; String tenantId = USER1_ACCOUNT.getTenantId(); ClusterCreateRequest clusterCreateRequest = ClusterCreateRequest.builder() .setName(clusterName) .setClusterTemplateName(reactorTemplate.getName()) .setNumMachines(5) .build(); HttpResponse response = doPostExternalAPI("/clusters", gson.toJson(clusterCreateRequest), USER1_HEADERS); assertResponseStatus(response, HttpResponseStatus.OK); String clusterId = getIdFromResponse(response); // check there was an element added to the cluster queue for creating this cluster Element element = solverQueues.take(tenantId, "0"); Assert.assertEquals(clusterId, element.getId()); ClusterCreateRequest expected = ClusterCreateRequest.builder() .setName(clusterName) .setClusterTemplateName(reactorTemplate.getName()) .setNumMachines(5) .setInitialLeaseDuration(-1L) .build(); SolverRequest expectedSolverRequest = new SolverRequest(SolverRequest.Type.CREATE_CLUSTER, gson.toJson(expected)); Assert.assertEquals(expectedSolverRequest, gson.fromJson(element.getValue(), SolverRequest.class)); }
@Test public void test_07_not_persisted_resolveTemplate() throws Exception { InputStream notPersistedIn = classLoader.getResourceAsStream("partials/cdap-not-persisted.json"); ClusterTemplate notPersisted = gson.fromJson(IOUtils.toString(notPersistedIn), ClusterTemplate.class); ClusterTemplate rt = clusterService.resolveTemplate(account, notPersisted); Assert.assertEquals(notPersisted.getIncludes(), rt.getIncludes()); Assert.assertEquals(notPersisted.getParent(), rt.getParent()); Assert.assertEquals(notPersisted.getName(), rt.getName()); Assert.assertEquals(notPersisted.getDescription(), rt.getDescription()); Assert.assertEquals(sensuPartial.getClusterDefaults().getServices(), rt.getClusterDefaults().getServices()); Assert.assertEquals(sensuPartial.getClusterDefaults().getConfig(), rt.getClusterDefaults().getConfig()); Assert.assertEquals(sensuPartial.getCompatibilities().getServices(), rt.getCompatibilities().getServices()); }
@Test public void testInvalidNumMachines() throws Exception { // when its below the min ClusterCreateRequest clusterCreateRequest =ClusterCreateRequest.builder() .setName("my-cluster") .setClusterTemplateName(reactorTemplate.getName()) .setProviderName(reactorTemplate.getClusterDefaults().getProvider()) .setNumMachines(1) .build(); assertResponseStatus(doPostExternalAPI("/clusters", gson.toJson(clusterCreateRequest), USER1_HEADERS), HttpResponseStatus.BAD_REQUEST); // when its above the max clusterCreateRequest = ClusterCreateRequest.builder() .setName("my-cluster") .setClusterTemplateName(reactorTemplate.getName()) .setProviderName(reactorTemplate.getClusterDefaults().getProvider()) .setNumMachines(500) .build(); assertResponseStatus(doPostExternalAPI("/clusters", gson.toJson(clusterCreateRequest), USER1_HEADERS), HttpResponseStatus.BAD_REQUEST); }
@Test(expected = IllegalArgumentException.class) public void testDisallowedServicesThrowsException() throws Exception { ClusterCreateRequest request = ClusterCreateRequest.builder() .setName("mycluster") .setClusterTemplateName(reactorTemplate.getName()) .setNumMachines(5) .setProviderName("joyent") .setServiceNames(ImmutableSet.of("namenode", "datanode", "mysql", "httpd")) .setInitialLeaseDuration(-1L) .build(); solver.solveClusterNodes( getBaseBuilder().setClusterTemplate(reactorTemplate).setProvider(Entities.ProviderExample.JOYENT).build(), request); }
@Test(expected = IllegalArgumentException.class) public void testMissingServiceDependenciesThrowsException() throws Exception { ClusterCreateRequest request = ClusterCreateRequest.builder() .setName("mycluster") .setClusterTemplateName(reactorTemplate.getName()) .setNumMachines(5) .setProviderName("joyent") .setServiceNames(ImmutableSet.of("reactor", "datanode")) .setInitialLeaseDuration(-1L) .build(); solver.solveClusterNodes( getBaseBuilder().setClusterTemplate(reactorTemplate).setProvider(Entities.ProviderExample.JOYENT).build(), request); }
@Test(expected = IllegalArgumentException.class) public void testNoAvailableHardwareTypesThrowsException() throws Exception { ClusterCreateRequest request = ClusterCreateRequest.builder() .setName("mycluster") .setClusterTemplateName(reactorTemplate.getName()) .setNumMachines(5) .setProviderName("rackspace") .setServiceNames(ImmutableSet.of("namenode", "datanode")) .setInitialLeaseDuration(-1L) .build(); solver.solveClusterNodes( getBaseBuilder().setClusterTemplate(reactorTemplate).setProvider(Entities.ProviderExample.RACKSPACE).build(), request); }
private ClusterTemplate copyWithNewCompatibilities(ClusterTemplate template, Compatibilities newCompatibilities) { return ClusterTemplate.builder() .setName(template.getName()) .setDescription(template.getDescription()) .setClusterDefaults(template.getClusterDefaults()) .setCompatibilities(newCompatibilities) .setConstraints(template.getConstraints()) .setAdministration(template.getAdministration()) .build(); }