def test_detect_new_enforcements(self) -> None:
        new_enforcement = [Enforcement(name='git', repo='somewhere')]
        old_enforcement = [Enforcement(name='hub', repo='anywhere')]
        enforcement_change_detector = EnforcementChangeDetector(
            new_enforcements_list=new_enforcement,
            old_enforcements_list=old_enforcement)

        enforcements = enforcement_change_detector.detect_new_enforcements()
        self.assertEqual(new_enforcement, enforcements)
    def test_detect_removed_enforcements_with_no_changes(self) -> None:
        new_enforcement = [Enforcement(name='git', repo='anywhere')]
        old_enforcement = [Enforcement(name='git', repo='anywhere')]

        enforcement_change_detector = EnforcementChangeDetector(
            new_enforcements_list=new_enforcement,
            old_enforcements_list=old_enforcement)
        enforcements = enforcement_change_detector.detect_removed_enforcements(
        )
        self.assertEqual([], enforcements)
Ejemplo n.º 3
0
    def setUp(self) -> None:
        self.dynamic_mapper = EnforcementDynamicMapper()
        self.cluster_repository = ClusterRepository()
        self.project_repository = ProjectRepository()
        self.enforcement_repository = EnforcementRepository()
        self.enforcement = Enforcement(name='test', repo='somewhere')
        self.old_enforcement = Enforcement(name='test', repo='somewhere')
        self.new_enforcement = Enforcement(name='test1', repo='anywhere')
        self.cluster = Cluster(name='test',
                               url='test',
                               token='test',
                               id='test')
        self.cluster_group = ClusterGroup(
            clusters=[self.cluster],
            cluster_repository=self.cluster_repository,
            project_repository=self.project_repository)

        self.cluster_group_builder = ClusterGroupBuilder(
            cluster_repository=self.cluster_repository,
            project_repository=self.project_repository)

        self.cluster_rule = ClusterRule(enforcements=[self.enforcement],
                                        source=EnforcementSource())

        trigger_builder = MagicMock()
        trigger_builder.build_before_install = MagicMock(
            return_value=lambda enf, cluster: None)
        trigger_builder.build_after_install = MagicMock(
            return_value=lambda enf, cluster: None)

        self.enforcement_installer_builder = EnforcementInstallerBuilder(
            enforcement_repository=self.enforcement_repository,
            enforcement_dynamic_mapper=EnforcementDynamicMapper(),
            trigger_builder=trigger_builder,
        )

        self.enforcement_installer = EnforcementInstaller(
            enforcements=[self.enforcement],
            cluster_group=self.cluster_group,
            enforcement_repository=self.enforcement_repository,
            enforcement_dynamic_mapper=self.dynamic_mapper,
            before_install_trigger=lambda a1, a2: None,
            after_install_trigger=lambda a1, a2: None,
        )

        self.enforcement_change_detector_builder = EnforcementChangeDetectorBuilder(
        )
        self.enforcement_change_detector = EnforcementChangeDetector(
            old_enforcements_list=[self.old_enforcement],
            new_enforcements_list=[self.new_enforcement])
Ejemplo n.º 4
0
    def _install_added_enforcements(
            self, change_detector: EnforcementChangeDetector,
            clusters: List[Cluster]) -> (List[Enforcement], List[Enforcement]):
        added_enforcements = change_detector.detect_new_enforcements()

        if not added_enforcements:
            return [], []

        cluster_group_remove_enfocement = self._cluster_group_builder.build(
            clusters=clusters)

        enforcement_installer = self._enforcement_installer_builder.build(
            cluster_group=cluster_group_remove_enfocement,
            enforcements=added_enforcements)

        return enforcement_installer.install(), added_enforcements
Ejemplo n.º 5
0
    def _update_change_enforcements(
            self, change_detector: EnforcementChangeDetector,
            clusters: List[Cluster]) -> (List[Enforcement], List[Enforcement]):
        change_enforcements = change_detector.detect_change_enforcements()

        if not change_enforcements:
            return [], []

        cluster_group_change_enfocement = self._cluster_group_builder.build(
            clusters=clusters)

        enforcement_installer = self._enforcement_installer_builder.build(
            cluster_group=cluster_group_change_enfocement,
            enforcements=change_enforcements)

        return enforcement_installer.install(), change_enforcements
Ejemplo n.º 6
0
    def _uninstall_removed_enforcements(
            self, change_detector: EnforcementChangeDetector,
            clusters: List[Cluster]) -> List[Enforcement]:

        removed_enforcements = change_detector.detect_removed_enforcements()

        if not removed_enforcements:
            return []

        cluster_group_remove_enfocement = self._cluster_group_builder.build(
            clusters=clusters)

        enforcement_installer = self._enforcement_installer_builder.build(
            cluster_group=cluster_group_remove_enfocement,
            enforcements=removed_enforcements)

        enforcement_installer.uninstall()

        return removed_enforcements
 def build(self, new_enforcements_list: List[Enforcement],
           old_enforcements_list: List[Enforcement]) -> EnforcementChangeDetector:
     return EnforcementChangeDetector(new_enforcements_list=new_enforcements_list,
                                      old_enforcements_list=old_enforcements_list)
 def setUp(self) -> None:
     self.enforcement = Enforcement(name='test', repo='somewhere')
     self.enforcement_installer = EnforcementChangeDetector(
         new_enforcements_list=[self.enforcement],
         old_enforcements_list=[self.enforcement],
     )