def test_task_templates_resource_too_long(self) -> None:
     self.test_data.saas_files = [self.saas1]
     self.test_data.providers = [self.provider1]
     msg = r"name o-openshift-saas-deploy-saas1 is longer than 1 characters"
     with pytest.raises(otr.OpenshiftTektonResourcesNameTooLongError,
                        match=msg):
         otr.fetch_desired_resources(otr.fetch_tkn_providers(None))
 def test_task_templates_unknown_deploy_resources_step(self) -> None:
     self.test_data.saas_files = [self.saas1]
     self.test_data.providers = [self.provider1]
     msg = (r"Cannot find a step named unknown-step to set resources in "
            r"task template openshift-saas-deploy")
     with pytest.raises(otr.OpenshiftTektonResourcesBadConfigError,
                        match=msg):
         otr.fetch_desired_resources(otr.fetch_tkn_providers(None))
    def test_task_templates_unknown_task(self) -> None:
        self.provider5_wut = self.fxt.get_json(
            "provider5-with-unknown-task.json")
        self.saas5 = self.fxt.get_json("saas5.json")
        self.test_data.saas_files = [self.saas5]
        self.test_data.providers = [self.provider5_wut]

        msg = (r"Unknown task this-is-an-unknown-task in pipeline template "
               r"openshift-saas-deploy")
        with pytest.raises(otr.OpenshiftTektonResourcesBadConfigError,
                           match=msg):
            otr.fetch_desired_resources(otr.fetch_tkn_providers(None))
    def test_task_templates_name_duplicates(self) -> None:
        self.provider4_wtd = self.fxt.get_json(
            "provider4-with-task-duplicates.json")
        self.saas4 = self.fxt.get_json("saas4.json")
        self.test_data.saas_files = [self.saas4]
        self.test_data.providers = [self.provider4_wtd]

        msg = (r"There are duplicates in task templates names in tekton "
               r"provider provider4-with-task-duplicates")
        with pytest.raises(otr.OpenshiftTektonResourcesBadConfigError,
                           match=msg):
            otr.fetch_desired_resources(otr.fetch_tkn_providers(None))
    def test_managed_resources_from_desired_resources(self) -> None:
        self.test_data.saas_files = [self.saas1, self.saas2, self.saas2_wr]
        self.test_data.providers = [self.provider1, self.provider2_wr]

        tkn_providers = otr.fetch_tkn_providers(None)
        _ = otr.fetch_desired_resources(tkn_providers)
        p1_managed = tkn_providers[
            self.provider1["name"]]["namespace"]["managedResourceNames"]
        p2_managed = tkn_providers[
            self.provider2_wr["name"]]["namespace"]["managedResourceNames"]

        assert len(p1_managed) == 2
        assert len(p2_managed) == 2

        # 1 namespace task, 1 saas file task, 1 saas file pipeline
        for managed in p1_managed:
            if managed["resource"] == "Task":
                assert len(managed["resourceNames"]) == 2
            else:
                assert len(managed["resourceNames"]) == 1

        # 1 namespace task, 2 saas file tasks, 2 saas file pipelines
        for managed in p2_managed:
            if managed["resource"] == "Task":
                assert len(managed["resourceNames"]) == 3
            else:
                assert len(managed["resourceNames"]) == 2
    def test_fetch_desired_resources(self) -> None:
        self.test_data.saas_files = [self.saas1, self.saas2, self.saas2_wr]
        self.test_data.providers = [self.provider1, self.provider2_wr]

        desired_resources = otr.fetch_desired_resources(
            otr.fetch_tkn_providers(None))

        # we have one task per namespace and a pipeline + task per saas file
        assert len(desired_resources) == 8
    def test_set_deploy_resources_from_saas_file(self) -> None:
        self.test_data.saas_files = [self.saas2_wr]
        self.test_data.providers = [self.provider2_wr]
        desired_resources = otr.fetch_desired_resources(
            otr.fetch_tkn_providers(None))

        task_name = otr.build_one_per_saas_file_tkn_object_name(
            template_name=self.provider2_wr['taskTemplates'][0]['name'],
            saas_file_name=self.saas2['name'])
        self._test_deploy_resources_in_task(desired_resources, task_name,
                                            self.saas2_wr['deployResources'])
    def test_set_deploy_resources_default(self) -> None:
        self.test_data.saas_files = [self.saas1]
        self.test_data.providers = [self.provider1]
        desired_resources = otr.fetch_desired_resources(
            otr.fetch_tkn_providers(None))

        # we need to locate the onePerSaasFile task in the desired resources
        # we could be very strict and find the onePerSaasFile task in
        # self.provider1 or just use the actual structure of the fixtures
        task_name = otr.build_one_per_saas_file_tkn_object_name(
            template_name=self.provider1['taskTemplates'][0]['name'],
            saas_file_name=self.saas1['name'])
        self._test_deploy_resources_in_task(desired_resources, task_name,
                                            otr.DEFAULT_DEPLOY_RESOURCES)
    def test_fetch_desired_resources_names(self) -> None:
        self.test_data.saas_files = [self.saas1]
        self.test_data.providers = [self.provider1]
        desired_resources = otr.fetch_desired_resources(
            otr.fetch_tkn_providers(None))

        expected_task_names = set([
            "o-push-gateway-openshift-saas-deploy-task-status-metric",
            "o-openshift-saas-deploy-saas1",
        ])
        expected_pipeline_name = "o-openshift-saas-deploy-saas1"

        task_names = set()
        for dr in desired_resources:
            body = dr["value"].body
            if body["kind"] == "Task":
                task_names.add(body["metadata"]["name"])
            else:
                pipeline_name = body["metadata"]["name"]

        assert task_names == expected_task_names
        assert pipeline_name == expected_pipeline_name
    def test_fetch_desired_resources_names(self) -> None:
        self.test_data.saas_files = [self.saas1]
        self.test_data.providers = [self.provider1]
        desired_resources = otr.fetch_desired_resources(
            otr.fetch_tkn_providers(None))

        expected_task_names = set([
            'o-push-gateway-openshift-saas-deploy-task-status-metric',
            'o-openshift-saas-deploy-saas1'
        ])
        expected_pipeline_name = 'o-openshift-saas-deploy-saas1'

        task_names = set()
        for dr in desired_resources:
            body = dr['value'].body
            if body['kind'] == 'Task':
                task_names.add(body['metadata']['name'])
            else:
                pipeline_name = body['metadata']['name']

        assert task_names == expected_task_names
        assert pipeline_name == expected_pipeline_name