def test_convert_sonic_yang_to_config_db__table_name_with_unexpected_colons__returns_config_db_as_json(self):
        # Arrange
        config_wrapper = gu_common.ConfigWrapper()
        expected = Files.CROPPED_CONFIG_DB_AS_JSON

        # Act and assert
        self.assertRaises(ValueError,
                          config_wrapper.convert_sonic_yang_to_config_db,
                          Files.SONIC_YANG_AS_JSON_WITH_UNEXPECTED_COLONS)
    def test_convert_sonic_yang_to_config_db__table_name_without_colons__returns_config_db_as_json(self):
        # Arrange
        config_wrapper = gu_common.ConfigWrapper()
        expected = Files.CROPPED_CONFIG_DB_AS_JSON

        # Act
        actual = config_wrapper.convert_sonic_yang_to_config_db(Files.SONIC_YANG_AS_JSON_WITHOUT_COLONS)

        # Assert
        self.assertDictEqual(expected, actual)
    def test_crop_tables_without_yang__returns_cropped_config_db_as_json(self):
        # Arrange
        config_wrapper = gu_common.ConfigWrapper()
        expected = Files.CROPPED_CONFIG_DB_AS_JSON

        # Act
        actual = config_wrapper.crop_tables_without_yang(Files.CONFIG_DB_AS_JSON)

        # Assert
        self.assertDictEqual(expected, actual)
    def test_convert_sonic_yang_to_config_db__empty_sonic_yang__returns_empty_config_db(self):
        # Arrange
        config_wrapper = gu_common.ConfigWrapper()
        expected = {}

        # Act
        actual = config_wrapper.convert_sonic_yang_to_config_db({})

        # Assert
        self.assertDictEqual(expected, actual)
    def test_validate_config_db_config__invalid_config__returns_false(self):
        # Arrange
        config_wrapper = gu_common.ConfigWrapper()
        expected = False

        # Act
        actual = config_wrapper.validate_config_db_config(Files.CONFIG_DB_AS_JSON_INVALID)

        # Assert
        self.assertEqual(expected, actual)
    def test_remove_empty_tables__no_empty_tables__returns_whole_config(self):
        # Arrange
        config_wrapper = gu_common.ConfigWrapper()
        config = {"any_table": {"key": "value"}}

        # Act
        actual = config_wrapper.remove_empty_tables(config)

        # Assert
        self.assertDictEqual({"any_table": {"key": "value"}}, actual)
    def test_convert_config_db_to_sonic_yang__non_empty_config_db__returns_sonic_yang_as_json(self):
        # Arrange
        config_wrapper = gu_common.ConfigWrapper()
        expected = Files.SONIC_YANG_AS_JSON

        # Act
        actual = config_wrapper.convert_config_db_to_sonic_yang(Files.CONFIG_DB_AS_JSON)

        # Assert
        self.assertDictEqual(expected, actual)
    def test_remove_empty_tables__multiple_empty_tables__returns_config_without_empty_tables(self):
        # Arrange
        config_wrapper = gu_common.ConfigWrapper()
        config = {"any_table": {"key": "value"}, "another_table":{}, "yet_another_table":{}}

        # Act
        actual = config_wrapper.remove_empty_tables(config)

        # Assert
        self.assertDictEqual({"any_table": {"key": "value"}}, actual)
    def test_get_empty_tables__no_empty_tables__returns_no_tables(self):
        # Arrange
        config_wrapper = gu_common.ConfigWrapper()
        config = {"any_table": {"key": "value"}}

        # Act
        empty_tables = config_wrapper.get_empty_tables(config)

        # Assert
        self.assertCountEqual([], empty_tables)
    def test_get_empty_tables__multiple_empty_tables__returns_multiple_tables(self):
        # Arrange
        config_wrapper = gu_common.ConfigWrapper()
        config = {"any_table": {"key": "value"}, "another_table":{}, "yet_another_table":{}}

        # Act
        empty_tables = config_wrapper.get_empty_tables(config)

        # Assert
        self.assertCountEqual(["another_table", "yet_another_table"], empty_tables)
    def test_validate_sonic_yang_config__valid_config__returns_true(self):
        # Arrange
        config_wrapper = gu_common.ConfigWrapper()
        expected = True

        # Act
        actual = config_wrapper.validate_sonic_yang_config(Files.SONIC_YANG_AS_JSON)

        # Assert
        self.assertEqual(expected, actual)
    def test_validate_config_db_config__valid_config__returns_true(self):
        # Arrange
        config_wrapper = gu_common.ConfigWrapper()
        expected = True

        # Act
        actual, error = config_wrapper.validate_config_db_config(Files.CONFIG_DB_AS_JSON)

        # Assert
        self.assertEqual(expected, actual)
        self.assertIsNone(error)
    def test_validate_sonic_yang_config__invvalid_config__returns_false(self):
        # Arrange
        config_wrapper = gu_common.ConfigWrapper()
        expected = False

        # Act
        actual, error = config_wrapper.validate_sonic_yang_config(Files.SONIC_YANG_AS_JSON_INVALID)

        # Assert
        self.assertEqual(expected, actual)
        self.assertIsNotNone(error)
    def validate_lanes(self, config_db, expected_error=None):
        # Arrange
        config_wrapper = gu_common.ConfigWrapper()
        expected = expected_error is None # if expected_error is None, then the input is valid

        # Act
        actual, error = config_wrapper.validate_lanes(config_db)

        # Assert
        self.assertEqual(expected, actual)
        if expected_error:
            self.assertTrue(expected_error in error)
    def test_create_sonic_yang_with_loaded_models__creates_new_sonic_yang_every_call(
            self):
        # check yang models fields are the same or None, non-yang model fields are different
        def check(sy1, sy2):
            # instances are different
            self.assertNotEqual(sy1, sy2)

            # yang models fields are same or None
            self.assertTrue(sy1.confDbYangMap is sy2.confDbYangMap)
            self.assertTrue(sy1.ctx is sy2.ctx)
            self.assertTrue(sy1.DEBUG is sy2.DEBUG)
            self.assertTrue(sy1.preProcessedYang is sy2.preProcessedYang)
            self.assertTrue(sy1.SYSLOG_IDENTIFIER is sy2.SYSLOG_IDENTIFIER)
            self.assertTrue(sy1.yang_dir is sy2.yang_dir)
            self.assertTrue(sy1.yangFiles is sy2.yangFiles)
            self.assertTrue(sy1.yJson is sy2.yJson)
            self.assertTrue(
                not (hasattr(sy1, 'module'))
                or sy1.module is None)  # module is unused, might get deleted
            self.assertTrue(not (hasattr(sy2, 'module')) or sy2.module is None)

            # non yang models fields are different
            self.assertFalse(sy1.root is sy2.root)
            self.assertFalse(sy1.jIn is sy2.jIn)
            self.assertFalse(sy1.tablesWithOutYang is sy2.tablesWithOutYang)
            self.assertFalse(sy1.xlateJson is sy2.xlateJson)
            self.assertFalse(sy1.revXlateJson is sy2.revXlateJson)

        config_wrapper = gu_common.ConfigWrapper()
        self.assertTrue(config_wrapper.sonic_yang_with_loaded_models is None)

        sy1 = config_wrapper.create_sonic_yang_with_loaded_models()
        sy2 = config_wrapper.create_sonic_yang_with_loaded_models()

        # Simulating loading non-yang model fields
        sy1.loadData(Files.ANY_CONFIG_DB)
        sy1.getData()

        # Simulating loading non-yang model fields
        sy2.loadData(Files.ANY_CONFIG_DB)
        sy2.getData()

        check(sy1, sy2)
        check(sy1, config_wrapper.sonic_yang_with_loaded_models)
        check(sy2, config_wrapper.sonic_yang_with_loaded_models)
    def check_validate_bgp_peer_group(self, ip_range, other_ip_range=[], duplicated_ip=None):
        # Arrange
        config_wrapper = gu_common.ConfigWrapper()
        config = {
            "BGP_PEER_RANGE":
            {
                "BGPSLBPassive": {
                    "ip_range": ip_range
                },
                "BgpVac": {
                    "ip_range": other_ip_range
                },
            }
        }

        # Act
        actual, error = config_wrapper.validate_bgp_peer_group(config)

        # Assert
        self.assertFalse(actual)
        self.assertTrue(duplicated_ip in error)
    def test_validate_bgp_peer_group__valid_non_intersecting_ip_ranges__returns_true(self):
        # Arrange
        config_wrapper = gu_common.ConfigWrapper()
        config = {
            "BGP_PEER_RANGE":
            {
                "BGPSLBPassive": {
                    "ip_range": ["1.1.1.1/31", "10.10.10.10/16", "100.100.100.100/24"]
                },
                "BgpVac": {
                    "ip_range": ["2.2.2.2/31", "20.20.20.20/16", "200.200.200.200/24"]
                }
            }
        }

        # Act
        actual, error = config_wrapper.validate_bgp_peer_group(config)

        # Assert
        self.assertTrue(actual)
        self.assertIsNone(error)
 def setUp(self):
     self.path_addressing = gu_common.PathAddressing(gu_common.ConfigWrapper())
     self.sy_only_models = sonic_yang.SonicYang(gu_common.YANG_DIR)
     self.sy_only_models.loadYangModel()
    def test_ctor__default_values_set(self):
        config_wrapper = gu_common.ConfigWrapper()

        self.assertEqual("/usr/local/yang-models", gu_common.YANG_DIR)
 def setUp(self):
     self.config_wrapper_mock = gu_common.ConfigWrapper()
     self.config_wrapper_mock.get_config_db_as_json=MagicMock(return_value=Files.CONFIG_DB_AS_JSON)