You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by GitBox <gi...@apache.org> on 2022/12/27 13:09:02 UTC

[GitHub] [cloudstack] DaanHoogland commented on a diff in pull request #7026: utils: fix NetUtils method to retrieve all IPs for a CIDR

DaanHoogland commented on code in PR #7026:
URL: https://github.com/apache/cloudstack/pull/7026#discussion_r1057669485


##########
utils/src/test/java/com/cloud/utils/net/NetUtilsTest.java:
##########
@@ -741,4 +742,30 @@ public void testCidrNetmask() {
         assertEquals("255.255.0.0", NetUtils.cidr2Netmask("169.254.0.0/16"));
         assertEquals("255.255.240.0", NetUtils.cidr2Netmask("169.254.240.0/20"));
     }
+
+    @Test
+    public void testGetAllIpsFromCidr() {

Review Comment:
   how about asking for more IPs than a net contains?
   i.e. asking for 55 ips on a /24 net



##########
utils/src/test/java/com/cloud/utils/net/NetUtilsTest.java:
##########
@@ -741,4 +742,30 @@ public void testCidrNetmask() {
         assertEquals("255.255.0.0", NetUtils.cidr2Netmask("169.254.0.0/16"));
         assertEquals("255.255.240.0", NetUtils.cidr2Netmask("169.254.240.0/20"));
     }
+
+    @Test
+    public void testGetAllIpsFromCidr() {
+        String cidr = "10.20.0.0";
+        int size = 22;
+        Set<Long> result = NetUtils.getAllIpsFromCidr(cidr, size, new TreeSet<>(), -1);
+        assertNotNull(result);
+        assertEquals(1022, result.size());
+        result = NetUtils.getAllIpsFromCidr(cidr, size, new TreeSet<>(), 255);
+        assertNotNull(result);
+        assertEquals(255, result.size());
+        result = NetUtils.getAllIpsFromCidr(cidr, size, new TreeSet<>(), 10);
+        assertNotNull(result);
+        assertEquals(10, result.size());
+        Long usedIp = NetUtils.ip2Long("10.20.0.100");
+        Set<Long> usedIps = new TreeSet<>();
+        usedIps.add(usedIp);
+        result = NetUtils.getAllIpsFromCidr(cidr, size, usedIps, -1);
+        assertNotNull(result);
+        assertEquals(1022 - usedIps.size(), result.size());
+        assertFalse(result.contains(usedIp));
+        result = NetUtils.getAllIpsFromCidr(cidr, size, usedIps, 50);
+        assertNotNull(result);
+        assertEquals(50, result.size());
+        assertFalse(result.contains(usedIp));
+    }

Review Comment:
   these are sensible tests but should be in separate test methods imnsho



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org