예제 #1
0
 def test_validate_id_negative(self):
     """ Assumptions:
             - given id is not Integer (String instead)
     """
     negative_id = "1"
     with self.assertRaisesRegex(IpIdNotValidError, "must be Integer"):
         IpOp.validate_id(negative_id)
예제 #2
0
    def case_get_by_ip(self, refresh_db_before):
        """ Get server row with ip keyword."""
        server_name = "TestServer"
        server_second_name = "TestServerTwo"
        server_status = "TestStatus"
        server_type = "TestType"
        ServerStatusOp.add(server_status)
        ServerTypeOp.add(server_type)

        ips = ["11.11.11.11", "22.22.22.22"]
        for ip in ips:
            IpOp.add(ip)

        srv_one = ServerOp.add(server_name,
                               server_status,
                               server_type,
                               ips=[ips[0]])
        srv_two = ServerOp.add(server_second_name,
                               server_status,
                               server_type,
                               ips=[ips[1]])

        get_first_ip = ServerOp.get(ip=ips[0])
        self.assertTrue(len(get_first_ip) is 1)
        self.assertEqual(get_first_ip[0], srv_one)
        self.assertNotEqual(get_first_ip[0], srv_two)
        self.assertTrue(len(get_first_ip[0].ips) is 1)
        self.assertEqual(get_first_ip[0].ips[0].address, ips[0])
예제 #3
0
 def test_validate_address_not_string(self):
     """ Assumptions:
             - given address is not a string
     """
     address = 1111
     with self.assertRaisesRegex(IpAddressNotValidError, "must be String"):
         IpOp.validate_address(address)
예제 #4
0
    def case_add_with_ips(self, refresh_db_before):
        """ Create new Server row with ips many-to-many relation."""
        server_name = "TestServer"
        server_status = "TestStatus"
        server_type = "TestType"
        ips = ["11.11.11.11", "22.22.22.22", "33.33.33.33"]

        ServerStatusOp.add(server_status)
        ServerTypeOp.add(server_type)
        for ip in ips:
            IpOp.add(ip)

        new_server = ServerOp.add(server_name,
                                  server_status,
                                  server_type,
                                  ips=ips)

        get_servers = ServerOp.get()
        self.assertTrue(len(get_servers) is 1)
        self.assertEqual(get_servers[0].description, None)
        self.assertEqual(get_servers[0].id, 1)
        self.assertEqual(get_servers[0].name, server_name)
        self.assertEqual(get_servers[0], new_server)

        for srv_ip, exp_ip in zip(get_servers[0].ips, ips):
            self.assertEqual(srv_ip.address, exp_ip)
예제 #5
0
    def case_update_all(self, refresh_db_before):
        """ Update all fields."""
        server_name = "TestServer"
        server_name_two = "TestServerTwo"
        ServerStatusOp.add("Status")
        ServerStatusOp.add("StatusTwo")
        ServerTypeOp.add("TypeOne")
        ServerTypeOp.add("TypeTwo")
        desc_one = "Desc one"
        desc_two = "Desc two"
        ips_one = IpOp.add("11.11.11.11")
        ips_two = IpOp.add("22.22.22.22")
        tag_one = TagOp.add("tag one")
        tag_two = TagOp.add("tag two")
        admin_one = AdminOp.add("Admin One")
        admin_two = AdminOp.add("Admin Two")

        srv = ServerOp.add(
            server_name,
            "Status",
            "TypeOne",
            description=desc_one,
            ips=["11.11.11.11"],
            tags=["tag one"],
            admins=["Admin One"],
        )

        get_before_update = ServerOp.get()
        self.assertTrue(len(get_before_update) is 1)
        self.assertEqual(get_before_update[0].id, 1)
        self.assertEqual(get_before_update[0].name, server_name)
        self.assertEqual(get_before_update[0].description, desc_one)
        self.assertEqual(get_before_update[0].status.name, "Status")
        self.assertEqual(get_before_update[0].type.name, "TypeOne")
        self.assertEqual(get_before_update[0].ips, [ips_one])
        self.assertEqual(get_before_update[0].tags, [tag_one])
        self.assertEqual(get_before_update[0].admins, [admin_one])

        ServerOp.update(
            srv,
            name=server_name_two,
            description=desc_two,
            srv_status="StatusTwo",
            srv_type="TypeTwo",
            ips=["22.22.22.22"],
            tags=["tag two"],
            admins=["Admin Two"],
        )

        get_srv = ServerOp.get()
        self.assertTrue(len(get_srv) is 1)
        self.assertEqual(get_srv[0].id, 1)
        self.assertEqual(get_srv[0].name, server_name_two)
        self.assertEqual(get_srv[0].description, desc_two)
        self.assertEqual(get_srv[0].status.name, "StatusTwo")
        self.assertEqual(get_srv[0].type.name, "TypeTwo")
        self.assertEqual(get_srv[0].ips, [ips_two])
        self.assertEqual(get_srv[0].tags, [tag_two])
        self.assertEqual(get_srv[0].admins, [admin_two])
예제 #6
0
 def test_validate_id_positive(self):
     """ Assumptions:
             - given id is Integer
     """
     positive_id = 1
     try:
         IpOp.validate_id(positive_id)
     except IpError:
         self.fail("Database operations error raised.")
예제 #7
0
 def test_validate_address_regex_match(self):
     """ Assumptions:
             - given correct IP address match regex.
     """
     good_addresses = ["1.1.1.1", "11.11.11.11", "111.111.111.111"]
     for good_address in good_addresses:
         try:
             IpOp.validate_address(good_address)
         except IpAddressNotValidError:
             self.fail("IpAddressNotValidError raised.")
예제 #8
0
    def case_resolve_ip_positive(self, refresh_db_before):
        """ Try to resolve Ip row."""
        ips = ["11.11.11.11", "22.22.22.22", "33.33.33.33"]
        for ip in ips:
            IpOp.add(address=ip)

        for id, ip in enumerate(ips):
            ip_obj = ServerOp.resolve_ip(ip)
            self.assertEqual(ip_obj.id, id + 1)
            self.assertEqual(ip_obj.address, ip)
            self.assertTrue(isinstance(ip_obj, Ip))
예제 #9
0
    def case_add_records(self, refresh_db_before):
        """ Add new records to Ip table."""
        new_addresses = ["0.0.0.0", "11.11.11.11", "222.222.222.222"]

        for address in new_addresses:
            IpOp.add(address)

        check_records = IpOp.get()

        self.assertEqual(len(check_records), len(new_addresses))

        for record, exp_name in zip(check_records, new_addresses):
            self.assertTrue(isinstance(record, Ip))
            self.assertEqual(record.address, exp_name)
예제 #10
0
    def test_delete(self, mock_ip, mock_db):
        """ Assumptions:
                - delete method run
        """
        ip_obj = mock_ip()

        IpOp.delete(ip_obj)

        db_exp_calls = [
            mock.call.session.delete(ip_obj),
            mock.call.session.commit()
        ]

        mock_db.assert_has_calls(db_exp_calls)
예제 #11
0
    def test_get_address(self, mock_ip, mock_val_id, mock_val_address):
        """ Assumptions:
                - address argument given
        """
        test_address = "test_address"
        IpOp.get(address=test_address)

        self.assertFalse(mock_val_id.called)
        self.assertTrue(mock_val_address.called)

        self.assertTrue(mock_ip.query.filter_by.called)

        exp_calls = [mock.call(address=test_address), mock.call().all()]
        mock_ip.query.filter_by.assert_has_calls(exp_calls)
예제 #12
0
    def case_update_record(self, refresh_db_before):
        """ Create test record in Ip table and then update it."""
        original_address = "0.0.0.0"
        update_address = "11.11.11.11"

        original_ip = IpOp.add(original_address)

        updated_ip_local = IpOp.update(original_ip, update_address)

        self.assertEqual(updated_ip_local.address, update_address)

        updated_ip_get = IpOp.get(address=update_address)

        self.assertTrue(len(updated_ip_get) is 1)
        self.assertEqual(updated_ip_get[0].address, update_address)
        self.assertEqual(updated_ip_get[0].id, original_ip.id)
예제 #13
0
    def case_get_by_address_one_result(self, refresh_db_before):
        """ Create test records in Ip table and get them by name."""
        addresses_with_id = {
            "0.0.0.0": 1,
            "11.11.11.11": 2,
            "222.222.222.222": 3
        }

        for address in addresses_with_id.keys():
            IpOp.add(address)

        for exp_address, exp_id in addresses_with_id.items():
            ip_obj = IpOp.get(address=exp_address)
            self.assertTrue(len(ip_obj) is 1)
            self.assertEqual(exp_address, ip_obj[0].address)
            self.assertEqual(exp_id, ip_obj[0].id)
예제 #14
0
 def test_validate_address_regex_not_match(self):
     """ Assumptions:
             - given address does not match respective regex for ip
     """
     wrong_addresses = [
         "0000.0.0.0",
         "0.0000.0.0",
         "0.0.0000.0",
         "0.0.0.0000",
         ".0.0.0",
         "0..0.0",
         "0.0..0",
         "0.0.0..",
     ]
     for wrong_address in wrong_addresses:
         with self.assertRaisesRegex(IpAddressNotValidError,
                                     "does not match regex"):
             IpOp.validate_address(wrong_address)
예제 #15
0
    def case_update_ips(self, refresh_db_before):
        """ Update ip addresses. """
        server_name = "TestServer"
        ServerStatusOp.add("Status")
        ServerTypeOp.add("Type")
        ips_one = IpOp.add("11.11.11.11")
        ips_two = IpOp.add("22.22.22.22")

        srv = ServerOp.add(server_name, "Status", "Type", ips=["11.11.11.11"])

        get_before_update = ServerOp.get()
        self.assertTrue(len(get_before_update) is 1)
        self.assertEqual(get_before_update[0].id, 1)
        self.assertEqual(get_before_update[0].ips, [ips_one])

        ServerOp.update(srv, ips=["22.22.22.22"])

        get_srv = ServerOp.get()
        self.assertTrue(len(get_srv) is 1)
        self.assertEqual(get_srv[0].id, 1)
        self.assertEqual(get_srv[0].ips, [ips_two])
예제 #16
0
    def test_update(self, mock_ip, mock_val_address, mock_db):
        """ Assumptions:
                - update method run
        """
        new_name = "Test Name"
        ip_obj = mock_ip()

        updated_ip = IpOp.update(ip_obj, new_name)

        self.assertTrue(mock_val_address.called)

        db_exp_calls = [
            mock.call.session.add(updated_ip),
            mock.call.session.commit()
        ]

        mock_db.assert_has_calls(db_exp_calls)
예제 #17
0
    def resolve_ip(cls, ip_address):
        """ Find Ip record according to given address.
            Return Ip model object.

            Args:
                ip_address(str): address to find

            Returns:
                ip_obj(app.db.models.ip.Ip): Ip object
        """
        ip_results = IpOp.get(address=ip_address)

        if len(ip_results) is not 1:
            raise ServerIpNotFoundError(f"Not found IP address: {ip_address}.")

        ip_obj = ip_results[0]
        return ip_obj
예제 #18
0
    def test_add(self, mock_ip, mock_val_address, mock_db):
        """ Assumptions:
                - add method run
        """
        new_name = "Test Name"
        new_ip = IpOp.add(new_name)

        exp_calls = [mock.call(new_name)]
        mock_ip.assert_has_calls(exp_calls)

        self.assertEqual(new_ip, mock_ip())
        self.assertTrue(mock_val_address.called)

        db_exp_calls = [
            mock.call.session.add(new_ip),
            mock.call.session.commit()
        ]

        mock_db.assert_has_calls(db_exp_calls)
예제 #19
0
    def case_delete_records(self, refresh_db_before):
        """ Create new record in Ip table and then delete it."""
        new_address = "0.0.0.0"

        IpOp.add(new_address)

        ip_obj = IpOp.get(address=new_address)
        self.assertTrue(len(ip_obj) is 1)
        self.assertEqual(ip_obj[0].address, new_address)

        IpOp.delete(ip_obj[0])

        ip_obj = IpOp.get(address=new_address)
        self.assertFalse(ip_obj)
예제 #20
0
    def case_get_by_all(self, refresh_db_before):
        """ Get server row with both srv_status
            srv_type keywords.
        """
        server_name = "TestServer"
        server_second_name = "TestServerTwo"
        server_third_name = "TestServerThree"
        server_fourth_name = "TestServerFour"
        ServerStatusOp.add("StatusOne")
        ServerStatusOp.add("StatusTwo")
        ServerTypeOp.add("TypeOne")
        ServerTypeOp.add("TypeTwo")

        IpOp.add("11.11.11.11")
        IpOp.add("22.22.22.22")

        TagOp.add("Tag one")
        TagOp.add("Tag two")

        AdminOp.add("Admin One")
        AdminOp.add("Admin Two")

        srv_one = ServerOp.add(
            server_name,
            "StatusOne",
            "TypeOne",
            ips=["11.11.11.11"],
            tags=["Tag one"],
            admins=["Admin One"],
        )

        ServerOp.add(
            server_second_name,
            "StatusTwo",
            "TypeTwo",
            ips=["22.22.22.22"],
            tags=["Tag two"],
            admins=["Admin Two"],
        )

        ServerOp.add(
            server_third_name,
            "StatusOne",
            "TypeTwo",
            ips=["22.22.22.22"],
            tags=["Tag two"],
            admins=["Admin Two"],
        )

        srv_four = ServerOp.add(
            server_fourth_name,
            "StatusOne",
            "TypeOne",
            ips=["11.11.11.11"],
            tags=["Tag one"],
            admins=["Admin One"],
        )

        get_by_all = ServerOp.get(
            srv_status="StatusOne",
            srv_type="TypeOne",
            ip="11.11.11.11",
            tags=["Tag one"],
            admins=["Admin One"],
        )

        self.assertTrue(len(get_by_all) is 2)
        self.assertEqual(get_by_all[0].id, 1)
        self.assertEqual(get_by_all[1].id, 4)
        self.assertEqual(get_by_all[0], srv_one)
        self.assertEqual(get_by_all[1], srv_four)