@Test public void testProvisionContainerWhenAnotherAlreadyProvisionedAndHasAffinityRules() throws Throwable { //Create a container A which has affinity to B. Deploy A, then deploy B. B should be placed on the same host as A String secondContainerName = "second_container"; ContainerDescription desc2 = createDescriptions(secondContainerName, 2, null).get(0); ContainerDescription desc1 = createDescriptions(CONTAINER_NAME, 2, new String[] { secondContainerName + HARD.getValue() }).get(0); String hostLink1 = initialHostLinks.get(2); createContainer(desc1, hostLink1); filter = new ServiceAffinityHostFilter(host, desc2); expectedLinks = Arrays.asList(hostLink1); state.addCustomProperty(RequestUtils.CLUSTERING_OPERATION_CUSTOM_PROP, "true"); Throwable e = filter(expectedLinks); if (e != null) { fail("Container should be placed exactly on host " + hostLink1); } }
@Test public void testProvisionContainerWhenAnotherAlreadyProvisionedAndHasAntiAffinityRules() throws Throwable { //Create a container A which has anti affinity to B. Deploy A, then deploy B. B should NOT be placed on the same host as A ContainerDescription desc1 = createDescriptions(CONTAINER_NAME1, new String[] { ANTI_AFFINITY_PREFIX + CONTAINER_NAME2 }); String hostLink2 = initialHostLinks.get(1); createContainer(desc1, hostLink2); ContainerDescription desc2 = createDescriptions(CONTAINER_NAME2); filter = new ServiceAntiAffinityHostFilter(host, desc2); expectedLinks.remove(hostLink2); state.addCustomProperty(RequestUtils.CLUSTERING_OPERATION_CUSTOM_PROP, "true"); filter(expectedLinks); }
@Test public void testProvisionContainerWhenAnotherAlreadyProvisionedAndHasVolumesFromRules() throws Throwable { //Deploy containers B (volumes from) A. Then deploy A again, A should be placed on the same host as the other two ContainerDescription desc = createDescriptionsWithVolumesFrom(CONTAINER_NAME, 5, null) .get(0); ContainerState container = createContainer(desc); assertEquals(UriUtils.buildUriPath( CompositeComponentFactoryService.SELF_LINK, state.contextId), container.compositeComponentLink); String[] volumesFrom = new String[] { CONTAINER_NAME }; ContainerDescription volumesFromDesc = createDescriptionsWithVolumesFrom( "random-name36", 1, volumesFrom).get(0); createContainer(volumesFromDesc, container.parentLink); filter = new VolumesFromAffinityHostFilter(host, desc); Collection<String> expectedFilteredHostLinks = Arrays.asList(container.parentLink); state.addCustomProperty(RequestUtils.CLUSTERING_OPERATION_CUSTOM_PROP, "true"); Throwable e = filter(expectedFilteredHostLinks); if (e != null) { fail("Unexpected exception: " + e); } }
@Test public void testSelectHostExistingContainerWithLinkNoAlias() throws Throwable { // create composite component CompositeComponent component = createComponent("App-With-Links-Test"); // create 2 container descriptions with links wordpress -> mysql ContainerDescription desc1 = createDescription("mysql", null); // mysql link has no alias (only service) ContainerDescription desc2 = createDescription("wordpress", new String[] {"mysql"}); // create 2 containers in the component createContainer(component, desc1); createContainer(component, desc2); String contextId = extractId(component.documentSelfLink); state.contextId = contextId; state.addCustomProperty(RequestUtils.FIELD_NAME_CONTEXT_ID_KEY, contextId); state.addCustomProperty(RequestUtils.CLUSTERING_OPERATION_CUSTOM_PROP, "true"); // place a new container which another container has a link to it (worpress -> mysql) filter = new ClusterServiceLinkAffinityHostFilter(host, desc1); // filter selects the host where existing mysql is placed expectedLinks = Arrays.asList(initialHostLinks.get(0)); Throwable e = filter(expectedLinks); if (e != null) { fail("Unexpected exception: " + e); } }
@Test public void testSelectHostExistingContainerWithLink() throws Throwable { // create composite component CompositeComponent component = createComponent("App-With-Links-Test"); // create 2 container descriptions with links wordpress -> mysql ContainerDescription desc1 = createDescription("mysql", null); ContainerDescription desc2 = createDescription("wordpress", new String[] {"mysql:mysql"}); // create 2 containers in the component createContainer(component, desc1); createContainer(component, desc2); String contextId = extractId(component.documentSelfLink); state.contextId = contextId; state.addCustomProperty(RequestUtils.FIELD_NAME_CONTEXT_ID_KEY, contextId); state.addCustomProperty(RequestUtils.CLUSTERING_OPERATION_CUSTOM_PROP, "true"); // place a new container which another container has a link to it (worpress -> mysql) filter = new ClusterServiceLinkAffinityHostFilter(host, desc1); // filter selects the host where existing mysql is placed expectedLinks = Arrays.asList(initialHostLinks.get(0)); Throwable e = filter(expectedLinks); if (e != null) { fail("Unexpected exception: " + e); } }
@Test public void testSelectHostExistingContainerWithoutLink() throws Throwable { // create composite component CompositeComponent component = createComponent("App-With-Links-Test"); // create 2 container descriptions (no links between them) ContainerDescription desc1 = createDescription("mysql", null); ContainerDescription desc2 = createDescription("wordpress", null); // create 2 containers in the component createContainer(component, desc1); createContainer(component, desc2); String contextId = extractId(component.documentSelfLink); state.contextId = contextId; state.addCustomProperty(RequestUtils.FIELD_NAME_CONTEXT_ID_KEY, contextId); state.addCustomProperty(RequestUtils.CLUSTERING_OPERATION_CUSTOM_PROP, "true"); // place a new container (no links from or to it) filter = new ClusterServiceLinkAffinityHostFilter(host, desc1); // filter selects all the available hosts Throwable e = filter(expectedLinks); if (e != null) { fail("Unexpected exception: " + e); } }