Skip to content

Commit 110d9c9

Browse files
committed
fix: Use the revert check isVersionAtLeast -> isVersionBelow
1 parent aec4371 commit 110d9c9

File tree

3 files changed

+94
-19
lines changed

3 files changed

+94
-19
lines changed
Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,73 @@
11
package datadog.communication.ddagent;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
36
public class AgentVersion {
47

8+
private static final Logger log = LoggerFactory.getLogger(AgentVersion.class);
9+
510
/**
6-
* Checks if the given version string represents a version that is at least the specified major,
11+
* Checks if the given version string represents a version that is below the specified major,
712
* minor, and patch version.
813
*
9-
* @param version the version string to check (e.g., "7.65.0")
10-
* @param minMajor minimum major version
11-
* @param minMinor minimum minor version
12-
* @param minPatch minimum patch version
13-
* @return true if version is at least the specified minimum, false otherwise (including when
14+
* @param version the version string to check (e.g., "7.64.0")
15+
* @param maxMajor maximum major version (exclusive)
16+
* @param maxMinor maximum minor version (exclusive)
17+
* @param maxPatch maximum patch version (exclusive)
18+
* @return true if version is below the specified maximum, false otherwise (including when
1419
* version is null or unparseable)
1520
*/
16-
public static boolean isVersionAtLeast(String version, int minMajor, int minMinor, int minPatch) {
21+
public static boolean isVersionBelow(String version, int maxMajor, int maxMinor, int maxPatch) {
1722
if (version == null || version.isEmpty()) {
18-
return false;
23+
return true;
1924
}
2025

2126
try {
2227
// Parse version string in format "major.minor.patch" (e.g., "7.65.0")
28+
// Assumes the 'version' is below if it can't be parsed.
2329
int majorDot = version.indexOf('.');
2430
if (majorDot == -1) {
25-
return false;
31+
return true;
2632
}
2733

2834
int major = Integer.parseInt(version.substring(0, majorDot));
2935

30-
if (major > minMajor) {
36+
if (major < maxMajor) {
3137
return true;
32-
} else if (major < minMajor) {
38+
} else if (major > maxMajor) {
3339
return false;
3440
}
3541

36-
// major == minMajor
42+
// major == maxMajor
3743
int minorDot = version.indexOf('.', majorDot + 1);
3844
if (minorDot == -1) {
39-
return false;
45+
return true;
4046
}
4147

4248
int minor = Integer.parseInt(version.substring(majorDot + 1, minorDot));
43-
if (minor > minMinor) {
49+
if (minor < maxMinor) {
4450
return true;
45-
} else if (minor < minMinor) {
51+
} else if (minor > maxMinor) {
4652
return false;
4753
}
4854

49-
// major == minMajor && minor == minMinor
55+
// major == maxMajor && minor == maxMinor
5056
// Find end of patch version (may have suffix like "-rc.1")
5157
int patchEnd = minorDot + 1;
5258
while (patchEnd < version.length() && Character.isDigit(version.charAt(patchEnd))) {
5359
patchEnd++;
5460
}
5561

5662
int patch = Integer.parseInt(version.substring(minorDot + 1, patchEnd));
57-
return patch >= minPatch;
63+
if (patch != maxPatch) {
64+
return patch < maxPatch;
65+
} else {
66+
// If there's a suffix (like "-rc.1"), consider it below the non-suffixed version
67+
return patchEnd < version.length();
68+
}
5869
} catch (NumberFormatException | IndexOutOfBoundsException e) {
59-
return false;
70+
return true;
6071
}
6172
}
6273
}

communication/src/main/java/datadog/communication/ddagent/DDAgentFeaturesDiscovery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ private boolean processInfoResponse(State newState, String response) {
308308

309309
if (metricsEnabled) {
310310
newState.supportsClientSideStats =
311-
AgentVersion.isVersionAtLeast(newState.version, 7, 65, 0);
311+
!AgentVersion.isVersionBelow(newState.version, 7, 65, 0);
312312
Object canDrop = map.get("client_drop_p0s");
313313
newState.supportsDropping =
314314
null != canDrop
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package datadog.communication.ddagent;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class AgentVersionTest {
8+
9+
@Test
10+
void testIsVersionBelow_VersionBelowThreshold() {
11+
assertTrue(AgentVersion.isVersionBelow("7.64.0", 7, 65, 0));
12+
assertTrue(AgentVersion.isVersionBelow("7.64.9", 7, 65, 0));
13+
assertTrue(AgentVersion.isVersionBelow("6.99.99", 7, 65, 0));
14+
assertTrue(AgentVersion.isVersionBelow("7.0.0", 7, 65, 0));
15+
}
16+
17+
@Test
18+
void testIsVersionBelow_VersionEqualToThreshold() {
19+
assertFalse(AgentVersion.isVersionBelow("7.65.0", 7, 65, 0));
20+
}
21+
22+
@Test
23+
void testIsVersionBelow_VersionAboveThreshold() {
24+
assertFalse(AgentVersion.isVersionBelow("7.65.1", 7, 65, 0));
25+
assertFalse(AgentVersion.isVersionBelow("7.66.0", 7, 65, 0));
26+
assertFalse(AgentVersion.isVersionBelow("8.0.0", 7, 65, 0));
27+
assertFalse(AgentVersion.isVersionBelow("7.65.10", 7, 65, 0));
28+
}
29+
30+
@Test
31+
void testIsVersionBelow_MajorVersionComparison() {
32+
assertTrue(AgentVersion.isVersionBelow("6.100.100", 7, 0, 0));
33+
assertFalse(AgentVersion.isVersionBelow("8.0.0", 7, 100, 100));
34+
}
35+
36+
@Test
37+
void testIsVersionBelow_MinorVersionComparison() {
38+
assertTrue(AgentVersion.isVersionBelow("7.64.100", 7, 65, 0));
39+
assertFalse(AgentVersion.isVersionBelow("7.66.0", 7, 65, 100));
40+
}
41+
42+
@Test
43+
void testIsVersionBelow_WithSuffix() {
44+
assertTrue(AgentVersion.isVersionBelow("7.64.0-rc.1", 7, 65, 0));
45+
assertTrue(AgentVersion.isVersionBelow("7.65.0-rc.1", 7, 65, 0));
46+
assertFalse(AgentVersion.isVersionBelow("7.65.1-snapshot", 7, 65, 0));
47+
}
48+
49+
@Test
50+
void testIsVersionBelow_NullOrEmpty() {
51+
assertTrue(AgentVersion.isVersionBelow(null, 7, 65, 0));
52+
assertTrue(AgentVersion.isVersionBelow("", 7, 65, 0));
53+
}
54+
55+
@Test
56+
void testIsVersionBelow_InvalidFormat() {
57+
assertTrue(AgentVersion.isVersionBelow("invalid", 7, 65, 0));
58+
assertTrue(AgentVersion.isVersionBelow("7", 7, 65, 0));
59+
assertTrue(AgentVersion.isVersionBelow("7.", 7, 65, 0));
60+
assertTrue(AgentVersion.isVersionBelow("7.65", 7, 65, 0));
61+
assertTrue(AgentVersion.isVersionBelow("7.65.", 7, 65, 0));
62+
assertTrue(AgentVersion.isVersionBelow("a.b.c", 7, 65, 0));
63+
}
64+
}

0 commit comments

Comments
 (0)