|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.yarn.server.nodemanager.containermanager.linux.resources; |
| 19 | + |
| 20 | +import org.apache.hadoop.conf.Configuration; |
| 21 | +import org.apache.hadoop.yarn.api.records.ContainerId; |
| 22 | +import org.apache.hadoop.yarn.api.records.ExecutionType; |
| 23 | +import org.apache.hadoop.yarn.api.records.Resource; |
| 24 | +import org.apache.hadoop.yarn.conf.YarnConfiguration; |
| 25 | +import org.apache.hadoop.yarn.security.ContainerTokenIdentifier; |
| 26 | +import org.apache.hadoop.yarn.server.nodemanager.Context; |
| 27 | +import org.apache.hadoop.yarn.server.nodemanager.containermanager.container.Container; |
| 28 | +import org.apache.hadoop.yarn.server.nodemanager.containermanager.container.ResourceMappings; |
| 29 | +import org.apache.hadoop.yarn.server.nodemanager.containermanager.container.ResourceMappings.AssignedResources; |
| 30 | +import org.apache.hadoop.yarn.server.nodemanager.containermanager.linux.privileged.PrivilegedOperation; |
| 31 | +import org.apache.hadoop.yarn.server.nodemanager.containermanager.linux.resources.ResourceHandlerException; |
| 32 | +import org.apache.hadoop.yarn.server.nodemanager.containermanager.linux.resources.numa.NumaResourceAllocation; |
| 33 | +import org.apache.hadoop.yarn.server.nodemanager.containermanager.linux.resources.numa.NumaResourceHandlerImpl; |
| 34 | +import org.apache.hadoop.yarn.server.nodemanager.recovery.NMStateStoreService; |
| 35 | +import org.junit.Assert; |
| 36 | +import org.junit.Before; |
| 37 | +import org.junit.Test; |
| 38 | + |
| 39 | +import java.io.IOException; |
| 40 | +import java.util.Arrays; |
| 41 | +import java.util.List; |
| 42 | +import java.util.concurrent.ConcurrentHashMap; |
| 43 | + |
| 44 | +import static org.junit.Assert.assertEquals; |
| 45 | +import static org.junit.Assert.assertNull; |
| 46 | +import static org.mockito.ArgumentMatchers.any; |
| 47 | +import static org.mockito.Mockito.*; |
| 48 | +import static org.mockito.Mockito.times; |
| 49 | + |
| 50 | +/** |
| 51 | + * Test class for CGroupsPidsResourceHandlerImpl. |
| 52 | + * |
| 53 | + */ |
| 54 | +public class TestPidsResourceHandlerImpl { |
| 55 | + |
| 56 | + private CGroupsPidsResourceHandlerImpl pidsResourceHandler; |
| 57 | + private Container mockContainer; |
| 58 | + private CGroupsHandler mockCGroupsHandler; |
| 59 | + |
| 60 | + @Before |
| 61 | + public void setUp() throws IOException, ResourceHandlerException { |
| 62 | + mockCGroupsHandler = mock(CGroupsHandler.class); |
| 63 | + when(mockCGroupsHandler.getPathForCGroup(any(), any())).thenReturn("."); |
| 64 | + pidsResourceHandler = |
| 65 | + new CGroupsPidsResourceHandlerImpl(mockCGroupsHandler); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testBootstrap() throws Exception { |
| 70 | + Configuration conf = new YarnConfiguration(); |
| 71 | + List<PrivilegedOperation> ret = |
| 72 | + pidsResourceHandler.bootstrap(conf); |
| 73 | + verify(mockCGroupsHandler, times(1)) |
| 74 | + .initializeCGroupController(CGroupsHandler.CGroupController.PIDS); |
| 75 | + Assert.assertNull(ret); |
| 76 | + Assert.assertEquals("Default process number incorrect", 10000, |
| 77 | + pidsResourceHandler.getProcessMaxCount()); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testProcessNumbers() throws Exception { |
| 82 | + Configuration conf = new YarnConfiguration(); |
| 83 | + conf.setInt(YarnConfiguration.NM_CONTAINER_PROCESS_MAX_LIMIT_NUM, -1); |
| 84 | + try { |
| 85 | + pidsResourceHandler.bootstrap(conf); |
| 86 | + Assert.fail("Negative values for process number should not be allowed."); |
| 87 | + } catch (ResourceHandlerException re) { |
| 88 | + // do nothing |
| 89 | + } |
| 90 | + |
| 91 | + conf.setInt(YarnConfiguration.NM_CONTAINER_PROCESS_MAX_LIMIT_NUM, 1000); |
| 92 | + pidsResourceHandler.bootstrap(conf); |
| 93 | + Assert.assertEquals("process number value incorrect", 1000, |
| 94 | + pidsResourceHandler.getProcessMaxCount()); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testPreStart() throws Exception { |
| 99 | + Configuration conf = new Configuration(); |
| 100 | + pidsResourceHandler.bootstrap(conf); |
| 101 | + String id = "container_01_01"; |
| 102 | + String path = "test-path/" + id; |
| 103 | + ContainerId mockContainerId = mock(ContainerId.class); |
| 104 | + when(mockContainerId.toString()).thenReturn(id); |
| 105 | + Container mockContainer = mock(Container.class); |
| 106 | + when(mockContainer.getContainerId()).thenReturn(mockContainerId); |
| 107 | + when(mockCGroupsHandler |
| 108 | + .getPathForCGroupTasks(CGroupsHandler.CGroupController.PIDS, id)) |
| 109 | + .thenReturn(path); |
| 110 | + int maxProcess = 1024; |
| 111 | + List<PrivilegedOperation> ret = |
| 112 | + pidsResourceHandler.preStart(mockContainer); |
| 113 | + verify(mockCGroupsHandler, times(1)) |
| 114 | + .createCGroup(CGroupsHandler.CGroupController.PIDS, id); |
| 115 | + verify(mockCGroupsHandler, times(1)) |
| 116 | + .updateCGroupParam(CGroupsHandler.CGroupController.PIDS, id, |
| 117 | + CGroupsHandler.CGROUP_PIDS_MAX, String.valueOf(maxProcess)); |
| 118 | + Assert.assertNotNull(ret); |
| 119 | + Assert.assertEquals(1, ret.size()); |
| 120 | + PrivilegedOperation op = ret.get(0); |
| 121 | + Assert.assertEquals(PrivilegedOperation.OperationType.ADD_PID_TO_CGROUP, |
| 122 | + op.getOperationType()); |
| 123 | + List<String> args = op.getArguments(); |
| 124 | + Assert.assertEquals(1, args.size()); |
| 125 | + Assert.assertEquals(PrivilegedOperation.CGROUP_ARG_PREFIX + path, |
| 126 | + args.get(0)); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void testReacquireContainer() throws Exception { |
| 131 | + ContainerId containerIdMock = mock(ContainerId.class); |
| 132 | + Assert.assertNull( |
| 133 | + pidsResourceHandler.reacquireContainer(containerIdMock)); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void testPostComplete() throws Exception { |
| 138 | + String id = "container_01_01"; |
| 139 | + ContainerId mockContainerId = mock(ContainerId.class); |
| 140 | + when(mockContainerId.toString()).thenReturn(id); |
| 141 | + Assert |
| 142 | + .assertNull(pidsResourceHandler.postComplete(mockContainerId)); |
| 143 | + verify(mockCGroupsHandler, times(1)) |
| 144 | + .deleteCGroup(CGroupsHandler.CGroupController.PIDS, id); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void testTeardown() throws Exception { |
| 149 | + Assert.assertNull(pidsResourceHandler.teardown()); |
| 150 | + } |
| 151 | +} |
0 commit comments