Skip to content

Commit 1507209

Browse files
add test
1 parent 0f88603 commit 1507209

File tree

2 files changed

+141
-2
lines changed

2 files changed

+141
-2
lines changed

hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/OzoneS3Util.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,26 @@ public static String buildServiceNameForToken(
5454
@Nonnull OzoneConfiguration configuration, @Nonnull String serviceId,
5555
@Nonnull Collection<String> omNodeIds) {
5656
StringBuilder rpcAddress = new StringBuilder();
57+
58+
int nodesLength = omNodeIds.size();
59+
int counter = 0;
5760
for (String nodeId : omNodeIds) {
61+
counter++;
5862
String rpcAddrKey = OmUtils.addKeySuffixes(OZONE_OM_ADDRESS_KEY,
5963
serviceId, nodeId);
6064
String rpcAddrStr = OmUtils.getOmRpcAddress(configuration, rpcAddrKey);
6165
if (rpcAddrStr == null) {
6266
throw new IllegalArgumentException("Could not find rpcAddress for " +
6367
OZONE_OM_ADDRESS_KEY + "." + serviceId + "." + nodeId);
6468
}
65-
rpcAddress.append(SecurityUtil.buildTokenService(
66-
NetUtils.createSocketAddr(rpcAddrStr)));
69+
70+
if (counter != nodesLength) {
71+
rpcAddress.append(SecurityUtil.buildTokenService(
72+
NetUtils.createSocketAddr(rpcAddrStr)) + ",");
73+
} else {
74+
rpcAddress.append(SecurityUtil.buildTokenService(
75+
NetUtils.createSocketAddr(rpcAddrStr)));
76+
}
6777
}
6878
return rpcAddress.toString();
6979
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
* <p>
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* <p>
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.ozone.s3.util;
20+
21+
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
22+
import org.apache.hadoop.ozone.OmUtils;
23+
import org.apache.hadoop.ozone.om.OMConfigKeys;
24+
import org.apache.hadoop.security.SecurityUtil;
25+
import org.apache.hadoop.test.GenericTestUtils;
26+
import org.junit.Assert;
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
30+
import java.util.Collection;
31+
32+
import static org.apache.hadoop.fs.CommonConfigurationKeys.HADOOP_SECURITY_TOKEN_SERVICE_USE_IP;
33+
import static org.junit.Assert.fail;
34+
35+
/**
36+
* Class used to test OzoneS3Util.
37+
*/
38+
public class TestOzoneS3Util {
39+
40+
41+
private OzoneConfiguration configuration;
42+
43+
@Before
44+
public void setConf() {
45+
configuration = new OzoneConfiguration();
46+
String serviceID = "omService";
47+
String nodeIDs = "om1,om2,om3";
48+
configuration.set(OMConfigKeys.OZONE_OM_SERVICE_IDS_KEY, serviceID);
49+
configuration.set(OMConfigKeys.OZONE_OM_NODE_ID_KEY, nodeIDs);
50+
configuration.setBoolean(HADOOP_SECURITY_TOKEN_SERVICE_USE_IP, false);
51+
}
52+
53+
@Test
54+
public void testBuildServiceNameForToken() {
55+
String serviceID = "omService";
56+
String nodeIDs = "om1,om2,om3";
57+
configuration.set(OMConfigKeys.OZONE_OM_SERVICE_IDS_KEY, serviceID);
58+
configuration.set(OMConfigKeys.OZONE_OM_NODE_ID_KEY, nodeIDs);
59+
configuration.setBoolean(HADOOP_SECURITY_TOKEN_SERVICE_USE_IP, false);
60+
61+
Collection<String> nodeIDList = configuration.getStringCollection(
62+
OMConfigKeys.OZONE_OM_NODE_ID_KEY);
63+
64+
String expectedOmServiceAddress = buildOMNodeAddresses(nodeIDList,
65+
serviceID);
66+
67+
SecurityUtil.setConfiguration(configuration);
68+
String omserviceAddr = OzoneS3Util.buildServiceNameForToken(configuration,
69+
serviceID, nodeIDList);
70+
71+
Assert.assertEquals(expectedOmServiceAddress, omserviceAddr);
72+
}
73+
74+
@Test
75+
public void testBuildServiceNameForTokenIncorrectConfig() {
76+
String serviceID = "omService";
77+
String nodeIDs = "om1,om2,om3";
78+
configuration.set(OMConfigKeys.OZONE_OM_SERVICE_IDS_KEY, serviceID);
79+
configuration.set(OMConfigKeys.OZONE_OM_NODE_ID_KEY, nodeIDs);
80+
configuration.setBoolean(HADOOP_SECURITY_TOKEN_SERVICE_USE_IP, false);
81+
82+
Collection<String> nodeIDList = configuration.getStringCollection(
83+
OMConfigKeys.OZONE_OM_NODE_ID_KEY);
84+
85+
// Don't set om3 node rpc address.
86+
configuration.set(OmUtils.addKeySuffixes(OMConfigKeys.OZONE_OM_ADDRESS_KEY,
87+
serviceID, "om1"), "om1:9862");
88+
configuration.set(OmUtils.addKeySuffixes(OMConfigKeys.OZONE_OM_ADDRESS_KEY,
89+
serviceID, "om2"), "om2:9862");
90+
91+
92+
SecurityUtil.setConfiguration(configuration);
93+
94+
try {
95+
OzoneS3Util.buildServiceNameForToken(configuration,
96+
serviceID, nodeIDList);
97+
fail("testBuildServiceNameForTokenIncorrectConfig failed");
98+
} catch (IllegalArgumentException ex) {
99+
GenericTestUtils.assertExceptionContains("Could not find rpcAddress " +
100+
"for", ex);
101+
}
102+
103+
104+
}
105+
106+
107+
private String buildOMNodeAddresses(Collection<String> nodeIDList,
108+
String serviceID) {
109+
StringBuilder omServiceAddrBuilder = new StringBuilder();
110+
int port = 9862;
111+
int nodesLength = nodeIDList.size();
112+
int counter = 0;
113+
for (String nodeID : nodeIDList) {
114+
counter++;
115+
String addr = nodeID + ":" + port++;
116+
configuration.set(OmUtils.addKeySuffixes(OMConfigKeys.OZONE_OM_ADDRESS_KEY,
117+
serviceID, nodeID), addr);
118+
119+
if (counter != nodesLength) {
120+
omServiceAddrBuilder.append(addr + ",");
121+
} else {
122+
omServiceAddrBuilder.append(addr);
123+
}
124+
}
125+
126+
return omServiceAddrBuilder.toString();
127+
}
128+
129+
}

0 commit comments

Comments
 (0)