CGroupsBlkioResourceHandlerImpl implements DiskResourceHandler { @Override public List<PrivilegedOperation> preStart(Container container) throws ResourceHandlerException { String cgroupId = container.getContainerId().toString(); cGroupsHandler .createCGroup(CGroupsHandler.CGroupController.BLKIO, cgroupId); try { cGroupsHandler.updateCGroupParam(CGroupsHandler.CGroupController.BLKIO, cgroupId, CGroupsHandler.CGROUP_PARAM_BLKIO_WEIGHT, DEFAULT_WEIGHT); } catch (ResourceHandlerException re) { cGroupsHandler.deleteCGroup(CGroupsHandler.CGroupController.BLKIO, cgroupId); LOG.warn("Could not update cgroup for container", re); throw re; } List<PrivilegedOperation> ret = new ArrayList<>(); ret.add(new PrivilegedOperation( PrivilegedOperation.OperationType.ADD_PID_TO_CGROUP, PrivilegedOperation.CGROUP_ARG_PREFIX + cGroupsHandler.getPathForCGroupTasks( CGroupsHandler.CGroupController.BLKIO, cgroupId))); return ret; } CGroupsBlkioResourceHandlerImpl(CGroupsHandler cGroupsHandler); @Override List<PrivilegedOperation> bootstrap(Configuration configuration); @Override List<PrivilegedOperation> preStart(Container container); @Override List<PrivilegedOperation> reacquireContainer(ContainerId containerId); @Override List<PrivilegedOperation> postComplete(ContainerId containerId); @Override List<PrivilegedOperation> teardown();  }
@Test public void testPreStart() throws Exception { String id = "container_01_01"; String path = "test-path/" + id; ContainerId mockContainerId = mock(ContainerId.class); when(mockContainerId.toString()).thenReturn(id); Container mockContainer = mock(Container.class); when(mockContainer.getContainerId()).thenReturn(mockContainerId); when( mockCGroupsHandler.getPathForCGroupTasks( CGroupsHandler.CGroupController.BLKIO, id)).thenReturn(path); List<PrivilegedOperation> ret = cGroupsBlkioResourceHandlerImpl.preStart(mockContainer); verify(mockCGroupsHandler, times(1)).createCGroup( CGroupsHandler.CGroupController.BLKIO, id); verify(mockCGroupsHandler, times(1)).updateCGroupParam( CGroupsHandler.CGroupController.BLKIO, id, CGroupsHandler.CGROUP_PARAM_BLKIO_WEIGHT, CGroupsBlkioResourceHandlerImpl.DEFAULT_WEIGHT); Assert.assertNotNull(ret); Assert.assertEquals(1, ret.size()); PrivilegedOperation op = ret.get(0); Assert.assertEquals(PrivilegedOperation.OperationType.ADD_PID_TO_CGROUP, op.getOperationType()); List<String> args = op.getArguments(); Assert.assertEquals(1, args.size()); Assert.assertEquals(PrivilegedOperation.CGROUP_ARG_PREFIX + path, args.get(0)); }