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])
Exemple #2
0
    def setUp(self) -> None:
        self.dynamic_mapper = EnforcementDynamicMapper()
        self.enforcement_repository = EnforcementRepository()
        self.enforcement = Enforcement(name='test', repo='somewhere')
        self.cluster_repository = ClusterRepository()
        self.project_repository = ProjectRepository()
        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.trigger_function = lambda enf, cluster: None

        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=self.trigger_function,
            after_install_trigger=self.trigger_function,
        )
 def setUp(self) -> None:
     self.enforcement_repository = EnforcementRepository()
     self.enforcement_dynamic_mapper = EnforcementDynamicMapper()
     self.enforcement = Enforcement(name='test',
                                    repo='somewhere',
                                    labels={'cluster_name': 'test'})
     self.cluster_repository = ClusterRepository()
     self.project_repository = ProjectRepository()
     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)
    def test_install_update_repository(self, mock_update_enforcement,
                                       mock_create_enforcement) -> None:
        enforcement = Enforcement(name='test-test',
                                  repo='somewhere',
                                  labels={'cluster_name': 'test'})

        repository = EnforcementRepository()
        repository.list_installed_enforcements = MagicMock(
            return_value=[enforcement])

        enforcement_installer = EnforcementInstaller(
            enforcements=[self.enforcement],
            cluster_group=self.cluster_group,
            enforcement_repository=repository,
            enforcement_dynamic_mapper=EnforcementDynamicMapper(),
            before_install_trigger=lambda cluster, enforcement: None,
            after_install_trigger=lambda cluster, enforcement: None)

        enforcements = enforcement_installer.install()

        self.assertListEqual([], enforcements)
    def test_install_create_repository(self, mock_list_installed_enforcements,
                                       mock_create_enforcement) -> None:
        enforcement_installer = EnforcementInstaller(
            enforcements=[self.enforcement],
            cluster_group=self.cluster_group,
            enforcement_repository=self.enforcement_repository,
            enforcement_dynamic_mapper=EnforcementDynamicMapper(),
            before_install_trigger=lambda cluster, enforcement: None,
            after_install_trigger=lambda cluster, enforcement: None)

        enforcement_installer.install()

        self.assertTrue(mock_list_installed_enforcements.called)
        self.assertTrue(mock_create_enforcement.called)
        self.assertEqual(mock_create_enforcement.call_args[0][0],
                         self.enforcement.name)
        self.assertEqual(
            mock_create_enforcement.call_args[0][1],
            enforcement_installer._make_enforcement_name(
                self.cluster, self.enforcement))
        self.assertEqual(mock_create_enforcement.call_args[0][2],
                         self.enforcement)
    def test_uninstall(self, mock_list_installed_enforcements,
                       mock_remove_enforcement) -> None:
        enforcement = Enforcement(name='test-test', repo='somewhere')
        mock_list_installed_enforcements.return_value = [enforcement]

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

        enforcement_installer.uninstall()

        self.assertTrue(mock_list_installed_enforcements.called)
        self.assertEqual(
            mock_list_installed_enforcements.call_args[1]['cluster_name'],
            self.cluster.name)
        self.assertTrue(mock_remove_enforcement.called)
        self.assertEqual(
            mock_remove_enforcement.call_args[0][0],
            enforcement_installer._make_enforcement_name(
                self.cluster, self.enforcement))
Exemple #7
0
 def provide_enforcement_dynamic_mapper(self) -> EnforcementDynamicMapper:
     return EnforcementDynamicMapper()