#!/usr/bin/env python3

import ipaddress
import unittest

import groupips.groupips as groupips


class TestGroupIPs(unittest.TestCase):
    def setUp(self):
        pass

    def test_group(self):
        IPs = ["127.0.0.1",
               "127.0.1.1",
               "127.1.1.1",
               "127.1.0.1",
               "127.2.0.1",
               "127.2.1.1",
               ]

        expected_results = {"127.0.0.0/16": 2,
                            "127.1.0.0/16": 2,
                            "127.2.0.0/16": 2,
                            }

        group = groupips.group_IPs(IPs, 16)

        self.assertEqual(expected_results, group)

    def test_group2(self):
        IPs = ["127.0.0.1",
               "127.0.1.1",
               "127.1.1.1",
               "127.1.0.1",
               "127.2.0.1",
               "127.2.1.1",
               ]

        expected_results = {"127.0.0.0/24": 1,
                            "127.0.1.0/24": 1,
                            "127.1.0.0/24": 1,
                            "127.1.1.0/24": 1,
                            "127.2.0.0/24": 1,
                            "127.2.1.0/24": 1,
                            }

        group = groupips.group_IPs(IPs, 24)

        self.assertEqual(expected_results, group)

if __name__ == "__main__":
    unittest.main()

@@ -34,16 +34,30 @@
 address%0A
+import random%0A
 import u
@@ -1280,16 +1280,647 @@
 group)%0A%0A
+    def test_group3(self):%0A        %22%22%22 'Random' test %22%22%22%0A%0A        # Small Netblock so we don't do over 2**22 hosts to test with%0A        random_cidr = random.randint(22, 30)%0A%0A        network = ipaddress.IPv4Network((%22129.21.0.0/%25s%22 %25 random_cidr))%0A%0A        # So out sample size is never bigger than the population of hosts%0A        random_int = random.randint(1, 2**(32 - random_cidr - 1))%0A%0A        IPs = random.sample(set(network.hosts()), random_int)%0A%0A        expected_results = %7B(%22129.21.0.0/%25s%22 %25 random_cidr): random_int%7D%0A%0A        group = groupips.group_IPs(IPs, random_cidr)%0A%0A        self.assertEqual(expected_results, group)%0A%0A
 if __nam

Add unit test, samples hosts from a random network