@Test public void testCallNonexistentResourceReturns404() throws Exception { List<String> getPaths = ImmutableList.of( // test nonexistent plugin "/plugins/providertypes/nonexistent/keys", "/plugins/automatortypes/nonexistent/cookbooks", // test nonexistent plugin resource type "/plugins/providertypes/joyent/cookbooks", "/plugins/automatortypes/chef-solo/keys" ); for (String getPath : getPaths) { assertResponseStatus(doGetExternalAPI(getPath, ADMIN_HEADERS), HttpResponseStatus.NOT_FOUND); } }
private void assertSendContents(String contents, ResourceType type, String name) throws Exception { String path = getNamePath(type, name); HttpResponse response = doPostExternalAPI(path, contents, ADMIN_HEADERS); assertResponseStatus(response, HttpResponseStatus.OK); Reader reader = new InputStreamReader(response.getEntity().getContent()); ResourceMeta responseMeta = gson.fromJson(reader, ResourceMeta.class); Assert.assertEquals(name, responseMeta.getName()); Assert.assertEquals(ResourceStatus.INACTIVE, responseMeta.getStatus()); }
@Test public void testNonAdminGetsForbidden() throws Exception { ResourceType type1 = new ResourceType(PluginType.PROVIDER, "joyent", "keys"); ResourceType type2 = new ResourceType(PluginType.AUTOMATOR, "shell", "script"); ResourceMeta meta = new ResourceMeta("name", 1); assertResponseStatus(doPostExternalAPI(getNamePath(type1, "name"), "contents", USER1_HEADERS), HttpResponseStatus.FORBIDDEN); assertResponseStatus(doPostExternalAPI(getNamePath(type2, "name"), "contents", USER1_HEADERS), HttpResponseStatus.FORBIDDEN); assertResponseStatus(doDeleteExternalAPI(getVersionedPath(type1, meta), USER1_HEADERS), HttpResponseStatus.FORBIDDEN); assertResponseStatus(doDeleteExternalAPI(getVersionedPath(type2, meta), USER1_HEADERS), HttpResponseStatus.FORBIDDEN); assertResponseStatus(doGetExternalAPI(getNamePath(type1, "name"), USER1_HEADERS), HttpResponseStatus.FORBIDDEN); assertResponseStatus(doGetExternalAPI(getNamePath(type2, "name"), USER1_HEADERS), HttpResponseStatus.FORBIDDEN); assertResponseStatus(doGetExternalAPI(getTypePath(type1), USER1_HEADERS), HttpResponseStatus.FORBIDDEN); assertResponseStatus(doGetExternalAPI(getTypePath(type2), USER1_HEADERS), HttpResponseStatus.FORBIDDEN); }
private void testGetAndDelete(ResourceType type) throws Exception { String contents = "some contents"; ResourceMeta meta1 = new ResourceMeta("name1", 1, ResourceStatus.INACTIVE); ResourceMeta meta2 = new ResourceMeta("name1", 2, ResourceStatus.INACTIVE); ResourceMeta meta3 = new ResourceMeta("name2", 1, ResourceStatus.INACTIVE); ResourceMeta meta4 = new ResourceMeta("name3", 1, ResourceStatus.INACTIVE); assertSendContents(contents, type, meta1.getName()); assertSendContents(contents, type, meta2.getName()); assertSendContents(contents, type, meta3.getName()); assertSendContents(contents, type, meta4.getName()); HttpResponse response = doGetExternalAPI(getTypePath(type), ADMIN_HEADERS); assertResponseStatus(response, HttpResponseStatus.OK); Assert.assertEquals( ImmutableMap.<String, Set<ResourceMeta>>of( "name1", ImmutableSet.<ResourceMeta>of(meta1, meta2), "name2", ImmutableSet.<ResourceMeta>of(meta3), "name3", ImmutableSet.<ResourceMeta>of(meta4)), bodyToMetaMap(response) ); // delete one assertResponseStatus(doDeleteExternalAPI(getVersionedPath(type, meta3), ADMIN_HEADERS), HttpResponseStatus.OK); response = doGetExternalAPI(getTypePath(type), ADMIN_HEADERS); assertResponseStatus(response, HttpResponseStatus.OK); Assert.assertEquals( ImmutableMap.<String, Set<ResourceMeta>>of( "name1", ImmutableSet.<ResourceMeta>of(meta1, meta2), "name3", ImmutableSet.<ResourceMeta>of(meta4)), bodyToMetaMap(response) ); }
private void testPutAndGet(PluginType type, String pluginName, String resourceType) throws Exception { String contents = RandomStringUtils.randomAlphanumeric(8 * Constants.PLUGIN_RESOURCE_CHUNK_SIZE); ResourceType pluginResourceType = new ResourceType(type, pluginName, resourceType); ResourceMeta meta = new ResourceMeta("hello", 1, ResourceStatus.INACTIVE); assertSendContents(contents, type, pluginName, resourceType, "hello"); // get metadata HttpResponse response = doGetExternalAPI(getNamePath(pluginResourceType, meta.getName()), ADMIN_HEADERS); assertResponseStatus(response, HttpResponseStatus.OK); Assert.assertEquals(ImmutableSet.of(meta), bodyToMetaSet(response)); // get actual contents String typeStr = type.name().toLowerCase() + "types"; String path = Joiner.on('/').join( "/tenants", ADMIN_ACCOUNT.getTenantId(), typeStr, pluginName, resourceType, meta.getName(), "versions", meta.getVersion() ); response = doGetInternalAPI(path); assertResponseStatus(response, HttpResponseStatus.OK); Assert.assertEquals(contents, bodyToString(response)); }
assertResponseStatus(doPostExternalAPI(getVersionedPath(cookbooks, "hadoop", 2) + "/stage", "", ADMIN_HEADERS), HttpResponseStatus.OK); assertResponseStatus(doPostExternalAPI(getVersionedPath(cookbooks, "mysql", 1) + "/stage", "", ADMIN_HEADERS), HttpResponseStatus.OK); assertResponseStatus(doPostExternalAPI(getVersionedPath(keys, "dev", 2) + "/stage", "", ADMIN_HEADERS), HttpResponseStatus.OK); assertResponseStatus(doPostExternalAPI("/plugins/sync", "", ADMIN_HEADERS), HttpResponseStatus.OK); assertResponseStatus(response, HttpResponseStatus.OK); Map<String, Set<ResourceMeta>> actual = bodyToMetaMap(response); Map<String, Set<ResourceMeta>> expected = ImmutableMap.<String, Set<ResourceMeta>>of( assertResponseStatus(response, HttpResponseStatus.OK); actual = bodyToMetaMap(response); expected = ImmutableMap.<String, Set<ResourceMeta>>of( assertResponseStatus(doPostExternalAPI(getVersionedPath(cookbooks, "hadoop", 3) + "/stage", "", ADMIN_HEADERS), HttpResponseStatus.OK); assertResponseStatus(doPostExternalAPI(getVersionedPath(cookbooks, "mysql", 1) + "/recall", "", ADMIN_HEADERS), HttpResponseStatus.OK); response = doGetExternalAPI(getTypePath(cookbooks), ADMIN_HEADERS); assertResponseStatus(response, HttpResponseStatus.OK); actual = bodyToMetaMap(response); assertResponseStatus(doPostExternalAPI("/plugins/sync", "", ADMIN_HEADERS), HttpResponseStatus.OK);
@Test public void testStageRecallOnNonexistentReturns404() throws Exception { ResourceType cookbooks = new ResourceType(PluginType.AUTOMATOR, "chef-solo", "cookbooks"); ResourceType keys = new ResourceType(PluginType.PROVIDER, "joyent", "keys"); assertSendContents("hadoop contents 1", cookbooks, "hadoop"); assertSendContents("dev keys 1", keys, "dev"); List<String> paths = ImmutableList.of( getVersionedPath(cookbooks, "hadoop", 2) + "/stage", getVersionedPath(cookbooks, "hadoop", 2) + "/recall", getVersionedPath(keys, "dev", 2) + "/stage", getVersionedPath(keys, "dev", 2) + "/recall" ); for (String path : paths) { assertResponseStatus(doPostExternalAPI(path, "", ADMIN_HEADERS), HttpResponseStatus.NOT_FOUND); } }
assertResponseStatus(doPostExternalAPI(getVersionedPath(pluginResourceType, meta2) + "/stage", "", ADMIN_HEADERS), HttpResponseStatus.OK); assertResponseStatus(response, HttpResponseStatus.OK); Assert.assertEquals(Sets.newHashSet(meta1, meta2), bodyToMetaSet(response)); assertResponseStatus(response, HttpResponseStatus.OK); Assert.assertEquals( ImmutableMap.<String, Set<ResourceMeta>>of("name", ImmutableSet.<ResourceMeta>of(meta2)), assertResponseStatus(response, HttpResponseStatus.OK); Assert.assertEquals(Sets.newHashSet(meta2), bodyToMetaSet(response)); assertResponseStatus(doPostExternalAPI(getVersionedPath(pluginResourceType, meta1) + "/stage", "", ADMIN_HEADERS), HttpResponseStatus.OK); assertResponseStatus(response, HttpResponseStatus.OK); Assert.assertEquals(Sets.newHashSet(meta1, meta2), bodyToMetaSet(response)); assertResponseStatus(response, HttpResponseStatus.OK); Assert.assertEquals( ImmutableMap.<String, Set<ResourceMeta>>of("name", ImmutableSet.<ResourceMeta>of(meta1)), assertResponseStatus(response, HttpResponseStatus.OK); Assert.assertEquals(Sets.newHashSet(meta1), bodyToMetaSet(response)); assertResponseStatus(doPostExternalAPI(getVersionedPath(pluginResourceType, meta1) + "/recall", "", ADMIN_HEADERS), HttpResponseStatus.OK);