Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix](test)Format ipv6 #43978

Open
wants to merge 2 commits into
base: branch-2.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions fe/fe-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,13 @@ under the License.
<groupId>dk.brics.automaton</groupId>
<artifactId>automaton</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.seancfoley/ipaddress -->
<dependency>
<groupId>com.github.seancfoley</groupId>
<artifactId>ipaddress</artifactId>
<version>5.5.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.github.mifmif/generex -->
<dependency>
<groupId>com.github.mifmif</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import inet.ipaddr.IPAddressString;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.ArrayList;
Expand Down Expand Up @@ -214,10 +215,18 @@ private static boolean isJoin(List<InetSocketAddress> allFeHosts, Frontend fe) {
LOG.warn("Failed to get InetAddress {}", addr);
continue;
}
if (fe.getHost().equals(address.getHostAddress())) {
// Format the IP address
String formattedIp = formatIp(address.getHostAddress());

if (formatIp(fe.getHost()).equals(formattedIp)) {
return true;
}
}
return false;
}

private static String formatIp(String str) {
// Use IPAddressString to format the IP address
return new IPAddressString(str).getAddress().toCanonicalString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.common.proc;

import org.junit.Assert;
import org.junit.Test;

import java.lang.reflect.Method;

public class FrontendsProcNodeTest {

@Test
public void testFormatIp() throws Exception {
// Test input and expected output for IPv6
String inputIPv6 = "fe80:0:0:0:20c:29ff:fef9:3a18";
String expectedIPv6 = "fe80::20c:29ff:fef9:3a18";

// Test input and expected output for IPv4
String inputIPv4 = "192.168.1.1";
String expectedIPv4 = "192.168.1.1"; // No change for IPv4

// Use reflection to get the formatIp method
Method formatIpMethod = FrontendsProcNode.class.getDeclaredMethod("formatIp", String.class);
formatIpMethod.setAccessible(true); // Make the method accessible

// Test IPv6
String actualIPv6 = (String) formatIpMethod.invoke(null, inputIPv6);
Assert.assertEquals(expectedIPv6, actualIPv6);

// Test IPv4
String actualIPv4 = (String) formatIpMethod.invoke(null, inputIPv4);
Assert.assertEquals(expectedIPv4, actualIPv4);
}
}
Loading