YARN-8048. Support auto-spawning of admin configured services during bootstrap of...
[hadoop.git] / hadoop-yarn-project / hadoop-yarn / hadoop-yarn-applications / hadoop-yarn-services-api / src / test / java / org / apache / hadoop / yarn / service / client / TestSystemServiceImpl.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.hadoop.yarn.service.client;
19
20 import org.apache.hadoop.conf.Configuration;
21 import org.apache.hadoop.security.UserGroupInformation;
22 import org.apache.hadoop.yarn.api.records.ApplicationId;
23 import org.apache.hadoop.yarn.exceptions.YarnException;
24 import org.apache.hadoop.yarn.service.api.records.Service;
25 import org.apache.hadoop.yarn.service.conf.YarnServiceConf;
26 import org.junit.After;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.util.HashMap;
36 import java.util.HashSet;
37 import java.util.Iterator;
38 import java.util.Map;
39 import java.util.Set;
40
41 /**
42 * Test class for system service manager.
43 */
44 public class TestSystemServiceImpl {
45
46 private static final Logger LOG =
47 LoggerFactory.getLogger(TestSystemServiceImpl.class);
48 private SystemServiceManagerImpl systemService;
49 private Configuration conf;
50 private String resourcePath = "users";
51
52 private String[] users = new String[] {"user1", "user2"};
53 private static Map<String, Set<String>> loadedServices = new HashMap<>();
54 private static Map<String, Set<String>> submittedServices = new HashMap<>();
55
56 @Before
57 public void setup() {
58 File file = new File(
59 getClass().getClassLoader().getResource(resourcePath).getFile());
60 conf = new Configuration();
61 conf.set(YarnServiceConf.YARN_SERVICES_SYSTEM_SERVICE_DIRECTORY,
62 file.getAbsolutePath());
63 systemService = new SystemServiceManagerImpl() {
64 @Override ServiceClient getServiceClient() {
65 return new TestServiceClient();
66 }
67 };
68 systemService.init(conf); // do not call explicit start
69
70 constructUserService(users[0], "example-app1");
71 constructUserService(users[1], "example-app1", "example-app2");
72 }
73
74 @After
75 public void teadDown() {
76 systemService.stop();
77 }
78
79 @Test
80 public void testSystemServiceSubmission() throws Exception {
81 systemService.start();
82
83 /* verify for ignored sevices count */
84 Map<String, Integer> ignoredUserServices =
85 systemService.getIgnoredUserServices();
86 Assert.assertEquals(1, ignoredUserServices.size());
87 Assert.assertTrue("User user1 doesn't exist.",
88 ignoredUserServices.containsKey(users[0]));
89 int count = ignoredUserServices.get(users[0]);
90 Assert.assertEquals(1, count);
91 Assert.assertEquals(1, systemService.getSkipCounter());
92
93 Map<String, Set<Service>> userServices =
94 systemService.getSyncUserServices();
95 Assert.assertEquals(loadedServices.size(), userServices.size());
96 verifyForScannedUserServices(userServices);
97
98 verifyForLaunchedUserServices();
99
100 // 2nd time launch service to handle if service exist scenario
101 systemService.launchUserService(userServices);
102 verifyForLaunchedUserServices();
103 }
104
105 private void verifyForScannedUserServices(
106 Map<String, Set<Service>> userServices) {
107 for (String user : users) {
108 Set<Service> services = userServices.get(user);
109 Set<String> serviceNames = loadedServices.get(user);
110 Assert.assertEquals(serviceNames.size(), services.size());
111 Iterator<Service> iterator = services.iterator();
112 while (iterator.hasNext()) {
113 Service next = iterator.next();
114 Assert.assertTrue(
115 "Service name doesn't exist in expected " + "userService "
116 + serviceNames, serviceNames.contains(next.getName()));
117 }
118 }
119 }
120
121 public void constructUserService(String user, String... serviceNames) {
122 Set<String> service = loadedServices.get(user);
123 if (service == null) {
124 service = new HashSet<>();
125 for (String serviceName : serviceNames) {
126 service.add(serviceName);
127 }
128 loadedServices.put(user, service);
129 }
130 }
131
132 class TestServiceClient extends ServiceClient {
133 @Override
134 protected void serviceStart() throws Exception {
135 // do nothing
136 }
137
138 @Override
139 protected void serviceStop() throws Exception {
140 // do nothing
141 }
142
143 @Override
144 protected void serviceInit(Configuration configuration)
145 throws Exception {
146 // do nothing
147 }
148
149 @Override
150 public ApplicationId actionCreate(Service service)
151 throws YarnException, IOException {
152 String userName =
153 UserGroupInformation.getCurrentUser().getShortUserName();
154 Set<String> services = submittedServices.get(userName);
155 if (services == null) {
156 services = new HashSet<>();
157 submittedServices.put(userName, services);
158 }
159 if (services.contains(service.getName())) {
160 String message = "Failed to create service " + service.getName()
161 + ", because it already exists.";
162 throw new YarnException(message);
163 }
164 services.add(service.getName());
165 return ApplicationId.newInstance(System.currentTimeMillis(), 1);
166 }
167 }
168
169 private void verifyForLaunchedUserServices() {
170 Assert.assertEquals(loadedServices.size(), submittedServices.size());
171 for (Map.Entry<String, Set<String>> entry : submittedServices.entrySet()) {
172 String user = entry.getKey();
173 Set<String> serviceSet = entry.getValue();
174 Assert.assertTrue(loadedServices.containsKey(user));
175 Set<String> services = loadedServices.get(user);
176 Assert.assertEquals(services.size(), serviceSet.size());
177 Assert.assertTrue(services.containsAll(serviceSet));
178 }
179 }
180 }