2 * Copyright (c) 2016, 2017 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * 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.
17 package io.fd.honeycomb.infra.distro;
19 import static org.hamcrest.CoreMatchers.containsString;
20 import static org.junit.Assert.assertThat;
21 import static org.junit.Assert.assertTrue;
23 import com.google.common.base.Charsets;
24 import com.google.common.io.ByteStreams;
25 import com.jcraft.jsch.Channel;
26 import com.jcraft.jsch.ChannelSubsystem;
27 import com.jcraft.jsch.JSch;
28 import com.jcraft.jsch.Session;
29 import com.mashape.unirest.http.HttpResponse;
30 import com.mashape.unirest.http.Unirest;
31 import io.fd.honeycomb.infra.distro.activation.ActivationModule;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.net.Socket;
35 import java.util.Properties;
36 import javax.net.ssl.SSLContext;
37 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
38 import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
39 import org.apache.http.impl.client.CloseableHttpClient;
40 import org.apache.http.impl.client.HttpClients;
41 import org.apache.http.ssl.SSLContexts;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 public class BaseMinimalDistributionTest {
49 private static final Logger LOG = LoggerFactory.getLogger(BaseMinimalDistributionTest.class);
51 private static final int HTTP_PORT = 8182;
52 private static final int HTTPS_PORT = 8444;
53 private static final String UNAME = "admin";
54 private static final String PASSWORD = "admin";
55 private static final String CERT_PASSWORD = "testing";
56 private static final int NETCONF_TCP_PORT = 7778;
57 private static final int NETCONF_SSH_PORT = 2832;
58 private static final String NETCONF_NAMESPACE = "urn:ietf:params:xml:ns:netconf:base:1.0";
59 private static final int HELLO_WAIT = 2500;
62 public void setUp() throws Exception {
63 SSLContext sslcontext = SSLContexts.custom()
64 .loadTrustMaterial(getClass().getResource("/honeycomb-keystore"),
65 CERT_PASSWORD.toCharArray(), new TrustSelfSignedStrategy())
68 SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext);
69 CloseableHttpClient httpclient = HttpClients.custom()
70 .setSSLSocketFactory(sslsf)
72 Unirest.setHttpClient(httpclient);
76 * Start base distribution and check all northbound interfaces
78 @Test(timeout = 180000)
79 public void test() throws Exception {
80 Main.init(new ActivationModule());
82 LOG.info("Testing Honeycomb base distribution");
83 LOG.info("Testing NETCONF TCP");
85 LOG.info("Testing NETCONF SSH");
87 LOG.info("Testing RESTCONF HTTP");
89 LOG.info("Testing RESTCONF HTTPS");
90 assertRestconfHttps();
93 private void assertNetconfTcp() throws Exception {
94 try (final Socket localhost = new Socket("127.0.0.1", NETCONF_TCP_PORT);
95 final InputStream inputStream = localhost.getInputStream()) {
96 // Wait until hello msg is sent from server
97 Thread.sleep(HELLO_WAIT);
98 final String helloMessage = inputStreamToString(inputStream);
100 LOG.info("NETCONF TCP sent hello: {}", helloMessage);
102 assertThat(helloMessage, containsString("hello"));
103 assertThat(helloMessage, containsString(NETCONF_NAMESPACE));
107 private byte[] readMessage(final InputStream inputStream) throws IOException {
108 final int available = inputStream.available();
109 final byte[] msg = new byte[available];
110 ByteStreams.read(inputStream, msg, 0, available);
113 private String inputStreamToString(final InputStream inputStream) throws IOException {
114 return new String(readMessage(inputStream), Charsets.UTF_8);
117 private void assertNetconfSsh() throws Exception {
118 JSch jsch = new JSch();
119 final Session session = jsch.getSession(UNAME, "127.0.0.1", NETCONF_SSH_PORT);
120 session.setPassword(PASSWORD);
121 Properties config = new Properties();
122 config.put("StrictHostKeyChecking", "no");
123 session.setConfig(config);
124 session.connect(60000);
126 Channel channel = session.openChannel("subsystem");
128 ((ChannelSubsystem) channel).setSubsystem("netconf");
129 ((ChannelSubsystem) channel).setPty(true);
130 final InputStream inputStream = channel.getInputStream();
131 channel.connect(60000);
133 // Wait until hello msg is sent from server
134 Thread.sleep(HELLO_WAIT);
135 final String helloMessage = inputStreamToString(inputStream);
136 LOG.info("NETCONF SSH sent hello: {}", helloMessage);
138 assertThat(helloMessage, containsString("hello"));
139 assertThat(helloMessage, containsString(NETCONF_NAMESPACE));
141 channel.disconnect();
142 session.disconnect();
145 private void assertRestconfHttp() throws Exception {
147 "http://127.0.0.1:" + HTTP_PORT + "/restconf/operational/ietf-netconf-monitoring:netconf-state";
148 LOG.info("RESTCONF HTTP GET to {}", url);
149 final HttpResponse<String> jsonNodeHttpResponse = Unirest.get(url)
150 .basicAuth(UNAME, PASSWORD)
152 LOG.info("RESTCONF HTTP GET to {}, status: {}, data: {}",
153 url, jsonNodeHttpResponse.getStatus(), jsonNodeHttpResponse.getBody());
155 assertSuccessStatus(jsonNodeHttpResponse);
156 assertSuccessResponseForNetconfMonitoring(jsonNodeHttpResponse);
159 private void assertRestconfHttps() throws Exception {
161 "https://127.0.0.1:" + HTTPS_PORT + "/restconf/operational/ietf-netconf-monitoring:netconf-state";
162 LOG.info("RESTCONF HTTPS GET to {}", url);
163 final HttpResponse<String> jsonNodeHttpResponse = Unirest.get(url)
164 .basicAuth(UNAME, PASSWORD)
166 LOG.info("RESTCONF HTTPS GET to {}, status: {}, data: {}",
167 url, jsonNodeHttpResponse.getStatus(), jsonNodeHttpResponse.getBody());
169 assertSuccessStatus(jsonNodeHttpResponse);
170 assertSuccessResponseForNetconfMonitoring(jsonNodeHttpResponse);
173 private void assertSuccessResponseForNetconfMonitoring(final HttpResponse<String> jsonNodeHttpResponse) {
174 assertThat(jsonNodeHttpResponse.getBody(), containsString("schemas"));
175 assertThat(jsonNodeHttpResponse.getBody(), containsString(NETCONF_NAMESPACE));
178 private void assertSuccessStatus(final HttpResponse<String> jsonNodeHttpResponse) {
179 assertTrue(jsonNodeHttpResponse.getStatus() >= 200);
180 assertTrue(jsonNodeHttpResponse.getStatus() < 400);