@Test public void testGetNonexistantClusterReturns404() throws Exception { assertResponseStatus(doGetExternalAPI("/clusters/567", USER1_HEADERS), HttpResponseStatus.NOT_FOUND); }
@Test public void testGetServicesFromNonexistantClusterReturns404() throws Exception { assertResponseStatus(doGetExternalAPI("/clusters/567/services", USER1_HEADERS), HttpResponseStatus.NOT_FOUND); }
@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); }
protected static void assertStatus(String clusterId, Cluster.Status status, ClusterJob.Status actionStatus, ClusterAction action, int totalSteps, int completeSteps, Header[] userHeaders) throws Exception { HttpResponse response = doGetExternalAPI(String.format("/clusters/%s/status", clusterId), userHeaders); assertResponseStatus(response, HttpResponseStatus.OK); InputStreamReader reader = new InputStreamReader(response.getEntity().getContent(), Charsets.UTF_8); ClusterStatusResponse statusResponse = gson.fromJson(reader, ClusterStatusResponse.class); Assert.assertEquals(clusterId, statusResponse.getClusterid()); Assert.assertEquals(totalSteps, statusResponse.getStepstotal()); Assert.assertEquals(completeSteps, statusResponse.getStepscompleted()); Assert.assertEquals(actionStatus, statusResponse.getActionstatus()); Assert.assertEquals(action, statusResponse.getAction()); Assert.assertEquals(status, statusResponse.getStatus()); }
@Test public void testAddServicesOnNonexistantClusterReturns404() throws Exception { Cluster cluster = Cluster.builder() .setID("123") .setAccount(USER1_ACCOUNT) .setName("test-cluster") .setClusterTemplate(Entities.ClusterTemplateExample.HDFS) .setServices(ImmutableSet.<String>of("namenode", "datanode")) .setStatus(Cluster.Status.ACTIVE) .build(); clusterStoreService.getView(cluster.getAccount()).writeCluster(cluster); AddServicesRequest body = new AddServicesRequest(null, ImmutableSet.of("resourcemanager", "nodemanager")); assertResponseStatus(doPostExternalAPI("/clusters/1123/services", gson.toJson(body), USER1_HEADERS), HttpResponseStatus.NOT_FOUND); assertResponseStatus(doPostExternalAPI("/clusters/123/services", gson.toJson(body), USER2_HEADERS), HttpResponseStatus.NOT_FOUND); }
@Test public void testAddInvalidServicesReturns400() throws Exception { Cluster cluster = Cluster.builder() .setID("123") .setAccount(USER1_ACCOUNT) .setName("test-cluster") .setClusterTemplate(Entities.ClusterTemplateExample.HDFS) .setServices(ImmutableSet.<String>of("namenode", "datanode")) .setStatus(Cluster.Status.ACTIVE) .build(); clusterStoreService.getView(cluster.getAccount()).writeCluster(cluster); // can't add nodemanager without resourcemanager AddServicesRequest body = new AddServicesRequest(null, ImmutableSet.of("nodemanager")); assertResponseStatus(doPostExternalAPI("/clusters/123/services", gson.toJson(body), USER1_HEADERS), HttpResponseStatus.BAD_REQUEST); // can't add nonexistant service body = new AddServicesRequest(null, ImmutableSet.of("fakeservice")); assertResponseStatus(doPostExternalAPI("/clusters/123/services", gson.toJson(body), USER1_HEADERS), HttpResponseStatus.BAD_REQUEST); }
@Test public void testServiceActionsOnNonexistantClusterServiceReturn404() throws Exception { Cluster cluster = Cluster.builder() .setID("123") .setAccount(USER1_ACCOUNT) .setName("test-cluster") .setClusterTemplate(Entities.ClusterTemplateExample.HDFS) .setStatus(Cluster.Status.ACTIVE) .build(); clusterStoreService.getView(cluster.getAccount()).writeCluster(cluster); assertResponseStatus(doPostExternalAPI("/clusters/123/services/fake/stop", "", USER1_HEADERS), HttpResponseStatus.NOT_FOUND); assertResponseStatus(doPostExternalAPI("/clusters/123/services/fake/start", "", USER1_HEADERS), HttpResponseStatus.NOT_FOUND); assertResponseStatus(doPostExternalAPI("/clusters/123/services/fake/restart", "", USER1_HEADERS), HttpResponseStatus.NOT_FOUND); }
@Test public void testInvalidClusterConfigRequests() throws Exception { Cluster cluster = Cluster.builder() .setID("123") .setAccount(USER1_ACCOUNT) .setName("get-config-test") .setClusterTemplate(Entities.ClusterTemplateExample.HDFS) .build(); clusterStoreService.getView(cluster.getAccount()).writeCluster(cluster); String requestStr = gson.toJson(new ClusterConfigureRequest(null, new JsonObject(), false)); assertResponseStatus(doPutExternalAPI("/clusters/" + cluster.getId() + "/config", "{}", USER1_HEADERS), HttpResponseStatus.BAD_REQUEST); assertResponseStatus(doPutExternalAPI("/clusters/" + cluster.getId() + "9/config", requestStr, USER1_HEADERS), HttpResponseStatus.NOT_FOUND); assertResponseStatus(doPutExternalAPI("/clusters/" + cluster.getId() + "/config", requestStr, USER2_HEADERS), HttpResponseStatus.NOT_FOUND); cluster.setStatus(Cluster.Status.INCOMPLETE); clusterStoreService.getView(cluster.getAccount()).writeCluster(cluster); assertResponseStatus(doPutExternalAPI("/clusters/" + cluster.getId() + "/config", requestStr, USER1_HEADERS), HttpResponseStatus.CONFLICT); }
@Test public void testAddServicesCanOnlyRunOnActiveCluster() throws Exception { Cluster cluster = Cluster.builder() .setID("123") .setAccount(USER1_ACCOUNT) .setName("test-cluster") .setClusterTemplate(Entities.ClusterTemplateExample.HDFS) .setServices(ImmutableSet.<String>of("namenode", "datanode")) .build(); Set<Cluster.Status> badStatuses = ImmutableSet.of( Cluster.Status.INCOMPLETE, Cluster.Status.PENDING, Cluster.Status.TERMINATED, Cluster.Status.INCONSISTENT); AddServicesRequest body = new AddServicesRequest(null, ImmutableSet.of("resourcemanager", "nodemanager")); for (Cluster.Status status : badStatuses) { cluster.setStatus(status); clusterStoreService.getView(cluster.getAccount()).writeCluster(cluster); assertResponseStatus(doPostExternalAPI("/clusters/123/services", gson.toJson(body), USER1_HEADERS), HttpResponseStatus.CONFLICT); } }
@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 public void testPutClusterConfigCanRunOnInconsistentClusters() throws Exception { Cluster cluster = Cluster.builder() .setID("123") .setAccount(USER1_ACCOUNT) .setName("get-config-test") .setProvider(Entities.ProviderExample.JOYENT) .setClusterTemplate(Entities.ClusterTemplateExample.HDFS) .build(); cluster.setStatus(Cluster.Status.INCONSISTENT); clusterStoreService.getView(cluster.getAccount()).writeCluster(cluster); String requestStr = gson.toJson(new ClusterConfigureRequest(null, new JsonObject(), false)); assertResponseStatus(doPutExternalAPI("/clusters/" + cluster.getId() + "/config", requestStr, USER1_HEADERS), HttpResponseStatus.OK); }
@Test public void testInvalidGetClusterConfigRequests() throws Exception { Cluster cluster = Cluster.builder() .setID("123") .setAccount(USER1_ACCOUNT) .setName("get-config-test") .setClusterTemplate(Entities.ClusterTemplateExample.HDFS) .build(); clusterStoreService.getView(cluster.getAccount()).writeCluster(cluster); assertResponseStatus(doGetExternalAPI("/clusters/" + cluster.getId() + "9/config", USER1_HEADERS), HttpResponseStatus.NOT_FOUND); assertResponseStatus(doGetExternalAPI("/clusters/" + cluster.getId() + "/config", USER2_HEADERS), HttpResponseStatus.NOT_FOUND); }
@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 public void testAddClusterWithOptionalArgs() throws Exception { String clusterName = "my-cluster"; String tenantId = USER1_ACCOUNT.getTenantId(); ClusterCreateRequest clusterCreateRequest = ClusterCreateRequest.builder() .setName(clusterName) .setClusterTemplateName(reactorTemplate.getName()) .setNumMachines(5) .setProviderName("joyent") .setServiceNames(ImmutableSet.of("namenode", "datanode")) .setHardwareTypeName("large") .setImageTypeName("centos6") .setInitialLeaseDuration(-1L) .build(); HttpResponse response = doPostExternalAPI("/clusters", gson.toJson(clusterCreateRequest), USER1_HEADERS); assertResponseStatus(response, HttpResponseStatus.OK); // check there was an element added to the cluster queue for creating this cluster Element element = solverQueues.take(tenantId, "0"); SolverRequest request = gson.fromJson(element.getValue(), SolverRequest.class); ClusterCreateRequest createRequest = gson.fromJson(request.getJsonRequest(), ClusterCreateRequest.class); Assert.assertEquals("joyent", createRequest.getProvider()); Assert.assertEquals("centos6", createRequest.getImageType()); Assert.assertEquals("large", createRequest.getHardwareType()); Assert.assertEquals(ImmutableSet.of("namenode", "datanode"), createRequest.getServices()); }
@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 testAdminCanDeleteClustersOwnedByOthers() throws Exception { String clusterId = "2"; Cluster cluster = Entities.ClusterExample.createCluster(); cluster.setStatus(Cluster.Status.ACTIVE); ClusterJob clusterJob = new ClusterJob(new JobId(clusterId, 1), ClusterAction.CLUSTER_DELETE); clusterJob.setJobStatus(ClusterJob.Status.COMPLETE); cluster.setLatestJobId(clusterJob.getJobId()); clusterStoreService.getView(cluster.getAccount()).writeCluster(cluster); clusterStore.writeClusterJob(clusterJob); assertResponseStatus(doDeleteExternalAPI("/clusters/" + clusterId, ADMIN_HEADERS), HttpResponseStatus.OK); }
private void verifyInitialLeaseDuration(long expectedExpireTime, HttpResponseStatus expectedStatus, long requestedLeaseDuration, String clusterTemplate) throws Exception { ClusterCreateRequest clusterCreateRequest = ClusterCreateRequest.builder() .setName("test-lease") .setClusterTemplateName(clusterTemplate) .setNumMachines(4) .setInitialLeaseDuration(requestedLeaseDuration) .build(); HttpResponse response = doPostExternalAPI("/clusters", gson.toJson(clusterCreateRequest), USER1_HEADERS); assertResponseStatus(response, expectedStatus); if (expectedStatus == HttpResponseStatus.BAD_REQUEST) { return; } solverScheduler.run(); String clusterId = getIdFromResponse(response); Cluster cluster = clusterStoreService.getView(USER1_ACCOUNT).getCluster(clusterId); Assert.assertEquals(Cluster.Status.PENDING, cluster.getStatus()); if (expectedExpireTime == 0) { Assert.assertEquals(expectedExpireTime, cluster.getExpireTime()); } else { Assert.assertEquals(expectedExpireTime, cluster.getExpireTime() == 0 ? 0 : cluster.getExpireTime() - cluster.getCreateTime()); } }
@Test public void testClusterTemplateSyncOnlyAllowedOnActiveClusters() throws Exception { Cluster cluster = Entities.ClusterExample.createCluster(); Account clusterAccount = cluster.getAccount(); clusterStoreService.getView(clusterAccount).writeCluster(cluster); entityStoreService.getView(Entities.ADMIN_ACCOUNT).writeClusterTemplate(cluster.getClusterTemplate()); String path = "/clusters/" + cluster.getId() + "/clustertemplate/sync"; // test cluster in bad state return 409 entityStoreService.getView(Entities.ADMIN_ACCOUNT).writeClusterTemplate(cluster.getClusterTemplate()); for (Cluster.Status status : Cluster.Status.values()) { if (status != Cluster.Status.ACTIVE) { cluster.setStatus(status); clusterStoreService.getView(clusterAccount).writeCluster(cluster); assertResponseStatus(doPostExternalAPI(path, "", USER1_HEADERS), HttpResponseStatus.CONFLICT); } } }