|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.nodesinfo; |
| 21 | + |
| 22 | +import org.elasticsearch.Build; |
| 23 | +import org.elasticsearch.Version; |
| 24 | +import org.elasticsearch.action.admin.cluster.node.info.NodeInfo; |
| 25 | +import org.elasticsearch.action.admin.cluster.node.info.PluginsAndModules; |
| 26 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 27 | +import org.elasticsearch.common.io.stream.BytesStreamOutput; |
| 28 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 29 | +import org.elasticsearch.common.settings.Settings; |
| 30 | +import org.elasticsearch.common.transport.BoundTransportAddress; |
| 31 | +import org.elasticsearch.common.transport.DummyTransportAddress; |
| 32 | +import org.elasticsearch.common.transport.TransportAddress; |
| 33 | +import org.elasticsearch.common.xcontent.ToXContent; |
| 34 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 35 | +import org.elasticsearch.http.HttpInfo; |
| 36 | +import org.elasticsearch.monitor.jvm.JvmInfo; |
| 37 | +import org.elasticsearch.monitor.os.DummyOsInfo; |
| 38 | +import org.elasticsearch.monitor.os.OsInfo; |
| 39 | +import org.elasticsearch.monitor.process.ProcessInfo; |
| 40 | +import org.elasticsearch.plugins.DummyPluginInfo; |
| 41 | +import org.elasticsearch.test.ESTestCase; |
| 42 | +import org.elasticsearch.test.VersionUtils; |
| 43 | +import org.elasticsearch.threadpool.ThreadPool; |
| 44 | +import org.elasticsearch.threadpool.ThreadPoolInfo; |
| 45 | +import org.elasticsearch.transport.TransportInfo; |
| 46 | + |
| 47 | +import java.io.IOException; |
| 48 | +import java.util.ArrayList; |
| 49 | +import java.util.HashMap; |
| 50 | +import java.util.List; |
| 51 | +import java.util.Map; |
| 52 | + |
| 53 | +import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; |
| 54 | +import static org.hamcrest.core.IsEqual.equalTo; |
| 55 | + |
| 56 | +/** |
| 57 | + * |
| 58 | + */ |
| 59 | +public class NodeInfoStreamingTests extends ESTestCase { |
| 60 | + |
| 61 | + public void testNodeInfoStreaming() throws IOException { |
| 62 | + NodeInfo nodeInfo = createNodeInfo(); |
| 63 | + Version version = Version.CURRENT; |
| 64 | + BytesStreamOutput out = new BytesStreamOutput(); |
| 65 | + out.setVersion(version); |
| 66 | + nodeInfo.writeTo(out); |
| 67 | + out.close(); |
| 68 | + StreamInput in = StreamInput.wrap(out.bytes()); |
| 69 | + in.setVersion(version); |
| 70 | + NodeInfo readNodeInfo = NodeInfo.readNodeInfo(in); |
| 71 | + assertExpectedUnchanged(nodeInfo, readNodeInfo); |
| 72 | + |
| 73 | + } |
| 74 | + // checks all properties that are expected to be unchanged. Once we start changing them between versions this method has to be changed as well |
| 75 | + private void assertExpectedUnchanged(NodeInfo nodeInfo, NodeInfo readNodeInfo) throws IOException { |
| 76 | + assertThat(nodeInfo.getBuild().toString(), equalTo(readNodeInfo.getBuild().toString())); |
| 77 | + assertThat(nodeInfo.getHostname(), equalTo(readNodeInfo.getHostname())); |
| 78 | + assertThat(nodeInfo.getVersion(), equalTo(readNodeInfo.getVersion())); |
| 79 | + assertThat(nodeInfo.getServiceAttributes().size(), equalTo(readNodeInfo.getServiceAttributes().size())); |
| 80 | + for (Map.Entry<String, String> entry : nodeInfo.getServiceAttributes().entrySet()) { |
| 81 | + assertNotNull(readNodeInfo.getServiceAttributes().get(entry.getKey())); |
| 82 | + assertThat(readNodeInfo.getServiceAttributes().get(entry.getKey()), equalTo(entry.getValue())); |
| 83 | + } |
| 84 | + compareJsonOutput(nodeInfo.getHttp(), readNodeInfo.getHttp()); |
| 85 | + compareJsonOutput(nodeInfo.getJvm(), readNodeInfo.getJvm()); |
| 86 | + compareJsonOutput(nodeInfo.getProcess(), readNodeInfo.getProcess()); |
| 87 | + compareJsonOutput(nodeInfo.getSettings(), readNodeInfo.getSettings()); |
| 88 | + compareJsonOutput(nodeInfo.getThreadPool(), readNodeInfo.getThreadPool()); |
| 89 | + compareJsonOutput(nodeInfo.getTransport(), readNodeInfo.getTransport()); |
| 90 | + compareJsonOutput(nodeInfo.getNode(), readNodeInfo.getNode()); |
| 91 | + compareJsonOutput(nodeInfo.getOs(), readNodeInfo.getOs()); |
| 92 | + comparePluginsAndModules(nodeInfo, readNodeInfo); |
| 93 | + } |
| 94 | + |
| 95 | + private void comparePluginsAndModules(NodeInfo nodeInfo, NodeInfo readNodeInfo) throws IOException { |
| 96 | + ToXContent.Params params = ToXContent.EMPTY_PARAMS; |
| 97 | + XContentBuilder pluginsAndModules = jsonBuilder(); |
| 98 | + pluginsAndModules.startObject(); |
| 99 | + nodeInfo.getPlugins().toXContent(pluginsAndModules, params); |
| 100 | + pluginsAndModules.endObject(); |
| 101 | + XContentBuilder readPluginsAndModules = jsonBuilder(); |
| 102 | + readPluginsAndModules.startObject(); |
| 103 | + readNodeInfo.getPlugins().toXContent(readPluginsAndModules, params); |
| 104 | + readPluginsAndModules.endObject(); |
| 105 | + assertThat(pluginsAndModules.string(), equalTo(readPluginsAndModules.string())); |
| 106 | + } |
| 107 | + |
| 108 | + private void compareJsonOutput(ToXContent param1, ToXContent param2) throws IOException { |
| 109 | + ToXContent.Params params = ToXContent.EMPTY_PARAMS; |
| 110 | + XContentBuilder param1Builder = jsonBuilder(); |
| 111 | + XContentBuilder param2Builder = jsonBuilder(); |
| 112 | + param1.toXContent(param1Builder, params); |
| 113 | + param2.toXContent(param2Builder, params); |
| 114 | + assertThat(param1Builder.string(), equalTo(param2Builder.string())); |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + private NodeInfo createNodeInfo() { |
| 119 | + Build build = Build.CURRENT; |
| 120 | + DiscoveryNode node = new DiscoveryNode("test_node", DummyTransportAddress.INSTANCE, VersionUtils.randomVersion(random())); |
| 121 | + Map<String, String> serviceAttributes = new HashMap<>(); |
| 122 | + serviceAttributes.put("test", "attribute"); |
| 123 | + Settings settings = Settings.builder().put("test", "setting").build(); |
| 124 | + OsInfo osInfo = DummyOsInfo.INSTANCE; |
| 125 | + ProcessInfo process = new ProcessInfo(randomInt(), randomBoolean()); |
| 126 | + JvmInfo jvm = JvmInfo.jvmInfo(); |
| 127 | + List<ThreadPool.Info> threadPoolInfos = new ArrayList<>(); |
| 128 | + threadPoolInfos.add(new ThreadPool.Info("test_threadpool", ThreadPool.ThreadPoolType.FIXED, 5)); |
| 129 | + ThreadPoolInfo threadPoolInfo = new ThreadPoolInfo(threadPoolInfos); |
| 130 | + Map<String, BoundTransportAddress> profileAddresses = new HashMap<>(); |
| 131 | + BoundTransportAddress dummyBoundTransportAddress = new BoundTransportAddress(new TransportAddress[]{DummyTransportAddress.INSTANCE}, DummyTransportAddress.INSTANCE); |
| 132 | + profileAddresses.put("test_address", dummyBoundTransportAddress); |
| 133 | + TransportInfo transport = new TransportInfo(dummyBoundTransportAddress, profileAddresses); |
| 134 | + HttpInfo htttpInfo = new HttpInfo(dummyBoundTransportAddress, randomLong()); |
| 135 | + PluginsAndModules plugins = new PluginsAndModules(); |
| 136 | + plugins.addModule(DummyPluginInfo.INSTANCE); |
| 137 | + plugins.addPlugin(DummyPluginInfo.INSTANCE); |
| 138 | + return new NodeInfo(VersionUtils.randomVersion(random()), build, node, serviceAttributes, settings, osInfo, process, jvm, threadPoolInfo, transport, htttpInfo, plugins); |
| 139 | + } |
| 140 | +} |
0 commit comments