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+ * http://www.apache.org/licenses/LICENSE-2.0
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package org .apache .hadoop .yarn .server .resourcemanager .scheduler .capacity ;
18+
19+ import org .apache .hadoop .conf .Configuration ;
20+ import org .apache .hadoop .security .authorize .AccessControlList ;
21+ import org .apache .hadoop .util .Sets ;
22+ import org .apache .hadoop .yarn .api .records .QueueACL ;
23+ import org .junit .Test ;
24+
25+ import java .util .Set ;
26+
27+ import static org .apache .hadoop .yarn .server .resourcemanager .scheduler .capacity .CapacitySchedulerConfiguration .ROOT ;
28+ import static org .apache .hadoop .yarn .server .resourcemanager .scheduler .capacity .CapacitySchedulerConfiguration .getQueuePrefix ;
29+ import static org .junit .Assert .assertEquals ;
30+ import static org .junit .Assert .assertFalse ;
31+ import static org .junit .Assert .assertTrue ;
32+
33+ public class TestCapacitySchedulerConfiguration {
34+
35+ private static final String ROOT_TEST = ROOT + ".test" ;
36+ private static final String EMPTY_ACL = "" ;
37+ private static final String SPACE_ACL = " " ;
38+ private static final String USER1 = "user1" ;
39+ private static final String USER2 = "user2" ;
40+ private static final String GROUP1 = "group1" ;
41+ private static final String GROUP2 = "group2" ;
42+ public static final String ONE_USER_ONE_GROUP_ACL = USER1 + " " + GROUP1 ;
43+ public static final String TWO_USERS_TWO_GROUPS_ACL =
44+ USER1 + "," + USER2 + " " + GROUP1 + ", " + GROUP2 ;
45+
46+ private CapacitySchedulerConfiguration createDefaultCsConf () {
47+ return new CapacitySchedulerConfiguration (new Configuration (false ), false );
48+ }
49+
50+ private AccessControlList getSubmitAcl (CapacitySchedulerConfiguration csConf , String queue ) {
51+ return csConf .getAcl (queue , QueueACL .SUBMIT_APPLICATIONS );
52+ }
53+
54+ private void setSubmitAppsConfig (CapacitySchedulerConfiguration csConf , String queue ,
55+ String value ) {
56+ csConf .set (getSubmitAppsConfigKey (queue ), value );
57+ }
58+
59+ private String getSubmitAppsConfigKey (String queue ) {
60+ return getQueuePrefix (queue ) + "acl_submit_applications" ;
61+ }
62+
63+ private void testWithGivenAclNoOneHasAccess (String queue , String aclValue ) {
64+ testWithGivenAclNoOneHasAccessInternal (queue , queue , aclValue );
65+ }
66+
67+ private void testWithGivenAclNoOneHasAccess (String queueToSet , String queueToVerify ,
68+ String aclValue ) {
69+ testWithGivenAclNoOneHasAccessInternal (queueToSet , queueToVerify , aclValue );
70+ }
71+
72+ private void testWithGivenAclNoOneHasAccessInternal (String queueToSet , String queueToVerify ,
73+ String aclValue ) {
74+ CapacitySchedulerConfiguration csConf = createDefaultCsConf ();
75+ setSubmitAppsConfig (csConf , queueToSet , aclValue );
76+ AccessControlList acl = getSubmitAcl (csConf , queueToVerify );
77+ assertTrue (acl .getUsers ().isEmpty ());
78+ assertTrue (acl .getGroups ().isEmpty ());
79+ assertFalse (acl .isAllAllowed ());
80+ }
81+
82+ private void testWithGivenAclCorrectUserAndGroupHasAccess (String queue , String aclValue ,
83+ Set <String > expectedUsers , Set <String > expectedGroups ) {
84+ testWithGivenAclCorrectUserAndGroupHasAccessInternal (queue , queue , aclValue , expectedUsers ,
85+ expectedGroups );
86+ }
87+
88+ private void testWithGivenAclCorrectUserAndGroupHasAccessInternal (String queueToSet ,
89+ String queueToVerify , String aclValue , Set <String > expectedUsers ,
90+ Set <String > expectedGroups ) {
91+ CapacitySchedulerConfiguration csConf = createDefaultCsConf ();
92+ setSubmitAppsConfig (csConf , queueToSet , aclValue );
93+ AccessControlList acl = getSubmitAcl (csConf , queueToVerify );
94+ assertFalse (acl .getUsers ().isEmpty ());
95+ assertFalse (acl .getGroups ().isEmpty ());
96+ assertEquals (expectedUsers , acl .getUsers ());
97+ assertEquals (expectedGroups , acl .getGroups ());
98+ assertFalse (acl .isAllAllowed ());
99+ }
100+
101+ @ Test
102+ public void testDefaultSubmitACLForRootAllAllowed () {
103+ CapacitySchedulerConfiguration csConf = createDefaultCsConf ();
104+ AccessControlList acl = getSubmitAcl (csConf , ROOT );
105+ assertTrue (acl .getUsers ().isEmpty ());
106+ assertTrue (acl .getGroups ().isEmpty ());
107+ assertTrue (acl .isAllAllowed ());
108+ }
109+
110+ @ Test
111+ public void testDefaultSubmitACLForRootChildNoneAllowed () {
112+ CapacitySchedulerConfiguration csConf = createDefaultCsConf ();
113+ AccessControlList acl = getSubmitAcl (csConf , ROOT_TEST );
114+ assertTrue (acl .getUsers ().isEmpty ());
115+ assertTrue (acl .getGroups ().isEmpty ());
116+ assertFalse (acl .isAllAllowed ());
117+ }
118+
119+ @ Test
120+ public void testSpecifiedEmptySubmitACLForRoot () {
121+ testWithGivenAclNoOneHasAccess (ROOT , EMPTY_ACL );
122+ }
123+
124+ @ Test
125+ public void testSpecifiedEmptySubmitACLForRootIsNotInherited () {
126+ testWithGivenAclNoOneHasAccess (ROOT , ROOT_TEST , EMPTY_ACL );
127+ }
128+
129+ @ Test
130+ public void testSpecifiedSpaceSubmitACLForRoot () {
131+ testWithGivenAclNoOneHasAccess (ROOT , SPACE_ACL );
132+ }
133+
134+ @ Test
135+ public void testSpecifiedSpaceSubmitACLForRootIsNotInherited () {
136+ testWithGivenAclNoOneHasAccess (ROOT , ROOT_TEST , SPACE_ACL );
137+ }
138+
139+ @ Test
140+ public void testSpecifiedSubmitACLForRoot () {
141+ Set <String > expectedUsers = Sets .newHashSet (USER1 );
142+ Set <String > expectedGroups = Sets .newHashSet (GROUP1 );
143+ testWithGivenAclCorrectUserAndGroupHasAccess (ROOT , ONE_USER_ONE_GROUP_ACL , expectedUsers ,
144+ expectedGroups );
145+ }
146+
147+ @ Test
148+ public void testSpecifiedSubmitACLForRootIsNotInherited () {
149+ testWithGivenAclNoOneHasAccess (ROOT , ROOT_TEST , ONE_USER_ONE_GROUP_ACL );
150+ }
151+
152+ @ Test
153+ public void testSpecifiedSubmitACLTwoUsersTwoGroupsForRoot () {
154+ Set <String > expectedUsers = Sets .newHashSet (USER1 , USER2 );
155+ Set <String > expectedGroups = Sets .newHashSet (GROUP1 , GROUP2 );
156+ testWithGivenAclCorrectUserAndGroupHasAccess (ROOT , TWO_USERS_TWO_GROUPS_ACL , expectedUsers ,
157+ expectedGroups );
158+ }
159+
160+ }
0 commit comments