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 
19 package org.apache.hadoop.yarn.server.timeline.webapp;
20 
21 import java.io.File;
22 import java.util.EnumSet;
23 
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.hadoop.fs.FileUtil;
26 import org.apache.hadoop.security.ssl.KeyStoreTestUtil;
27 import org.apache.hadoop.yarn.api.records.timeline.TimelineEntity;
28 import org.apache.hadoop.yarn.api.records.timeline.TimelineEvent;
29 import org.apache.hadoop.yarn.api.records.timeline.TimelinePutResponse;
30 import org.apache.hadoop.yarn.client.api.impl.TimelineClientImpl;
31 import org.apache.hadoop.yarn.conf.YarnConfiguration;
32 import org.apache.hadoop.yarn.server.applicationhistoryservice.ApplicationHistoryServer;
33 import org.apache.hadoop.yarn.server.applicationhistoryservice.webapp.AHSWebApp;
34 import org.apache.hadoop.yarn.server.timeline.MemoryTimelineStore;
35 import org.apache.hadoop.yarn.server.timeline.TimelineReader.Field;
36 import org.apache.hadoop.yarn.server.timeline.TimelineStore;
37 import org.junit.AfterClass;
38 import org.junit.Assert;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 
42 import com.sun.jersey.api.client.ClientResponse;
43 
44 public class TestTimelineWebServicesWithSSL {
45 
46   private static final String BASEDIR =
47       System.getProperty("test.build.dir", "target/test-dir") + "/"
48           + TestTimelineWebServicesWithSSL.class.getSimpleName();
49 
50   private static String keystoresDir;
51   private static String sslConfDir;
52   private static ApplicationHistoryServer timelineServer;
53   private static TimelineStore store;
54   private static Configuration conf;
55 
56   @BeforeClass
setupServer()57   public static void setupServer() throws Exception {
58     conf = new YarnConfiguration();
59     conf.setBoolean(YarnConfiguration.TIMELINE_SERVICE_ENABLED, true);
60     conf.setClass(YarnConfiguration.TIMELINE_SERVICE_STORE,
61         MemoryTimelineStore.class, TimelineStore.class);
62     conf.set(YarnConfiguration.YARN_HTTP_POLICY_KEY, "HTTPS_ONLY");
63 
64     File base = new File(BASEDIR);
65     FileUtil.fullyDelete(base);
66     base.mkdirs();
67     keystoresDir = new File(BASEDIR).getAbsolutePath();
68     sslConfDir =
69         KeyStoreTestUtil.getClasspathDir(TestTimelineWebServicesWithSSL.class);
70 
71     KeyStoreTestUtil.setupSSLConfig(keystoresDir, sslConfDir, conf, false);
72     conf.addResource("ssl-server.xml");
73     conf.addResource("ssl-client.xml");
74 
75     timelineServer = new ApplicationHistoryServer();
76     timelineServer.init(conf);
77     timelineServer.start();
78     store = timelineServer.getTimelineStore();
79   }
80 
81   @AfterClass
tearDownServer()82   public static void tearDownServer() throws Exception {
83     if (timelineServer != null) {
84       timelineServer.stop();
85     }
86   }
87 
88   @Test
testPutEntities()89   public void testPutEntities() throws Exception {
90     TestTimelineClient client = new TestTimelineClient();
91     try {
92       client.init(conf);
93       client.start();
94       TimelineEntity expectedEntity = new TimelineEntity();
95       expectedEntity.setEntityType("test entity type");
96       expectedEntity.setEntityId("test entity id");
97       expectedEntity.setDomainId("test domain id");
98       TimelineEvent event = new TimelineEvent();
99       event.setEventType("test event type");
100       event.setTimestamp(0L);
101       expectedEntity.addEvent(event);
102 
103       TimelinePutResponse response = client.putEntities(expectedEntity);
104       Assert.assertEquals(0, response.getErrors().size());
105       Assert.assertTrue(client.resp.toString().contains("https"));
106 
107       TimelineEntity actualEntity = store.getEntity(
108           expectedEntity.getEntityId(), expectedEntity.getEntityType(),
109           EnumSet.allOf(Field.class));
110       Assert.assertNotNull(actualEntity);
111       Assert.assertEquals(
112           expectedEntity.getEntityId(), actualEntity.getEntityId());
113       Assert.assertEquals(
114           expectedEntity.getEntityType(), actualEntity.getEntityType());
115     } finally {
116       client.stop();
117       client.close();
118     }
119   }
120 
121   private static class TestTimelineClient extends TimelineClientImpl {
122 
123     private ClientResponse resp;
124 
125     @Override
doPostingObject(Object obj, String path)126     public ClientResponse doPostingObject(Object obj, String path) {
127       resp = super.doPostingObject(obj, path);
128       return resp;
129     }
130 
131   }
132 
133 }
134