示例#1
0
    def test_should_include_times(self,
                                  builder_context: PipelineBuilderContext,
                                  adviser_pipeline: bool) -> None:
        """Test including a pipeline unit based on `times`."""
        if adviser_pipeline:
            builder_context.recommendation_type = RecommendationType.LATEST
            builder_context.decision_type = None
            assert builder_context.is_adviser_pipeline()
            assert not builder_context.is_dependency_monkey_pipeline()
        else:
            builder_context.recommendation_type = None
            builder_context.decision_type = DecisionType.ALL
            assert builder_context.is_dependency_monkey_pipeline()
            assert not builder_context.is_adviser_pipeline()

        should_include_dict = {
            "times": 0,
            "adviser_pipeline": True,
            "dependency_monkey_pipeline": True
        }
        PRESCRIPTION_UNIT_SHOULD_INCLUDE_SCHEMA(should_include_dict)
        UnitPrescription._PRESCRIPTION = {
            "name": "Foo",
            "should_include": should_include_dict,
        }
        assert UnitPrescription._should_include_base(builder_context) is False

        builder_context.should_receive("is_included").with_args(
            UnitPrescription).and_return(False).and_return(True).twice()

        should_include_dict = {
            "times": 1,
            "adviser_pipeline": True,
            "dependency_monkey_pipeline": True
        }
        PRESCRIPTION_UNIT_SHOULD_INCLUDE_SCHEMA(should_include_dict)
        UnitPrescription._PRESCRIPTION = {
            "name": "Foo",
            "should_include": should_include_dict,
        }
        assert UnitPrescription._should_include_base(builder_context) is True

        should_include_dict = {
            "times": 1,
            "adviser_pipeline": True,
            "dependency_monkey_pipeline": True
        }
        PRESCRIPTION_UNIT_SHOULD_INCLUDE_SCHEMA(should_include_dict)
        UnitPrescription._PRESCRIPTION = {
            "name": "Foo",
            "should_include": should_include_dict,
        }
        assert UnitPrescription._should_include_base(builder_context) is False
示例#2
0
    def test_should_include_rpm_packages_no_analysis(self, builder_context: PipelineBuilderContext) -> None:
        """Test not including pipeline units if the base image was not analysed."""
        base_image_present_name, base_image_present_version = "s2i-thoth", "1.0.0"
        builder_context.project.runtime_environment.base_image = (
            f"{base_image_present_name}:v{base_image_present_version}"
        )
        should_include = {
            "adviser_pipeline": True,
            "runtime_environments": {
                "base_images": [builder_context.project.runtime_environment.base_image],
                "rpm_packages": [{"package_name": "glibc"}],
            },
        }

        PRESCRIPTION_UNIT_SHOULD_INCLUDE_SCHEMA(should_include)

        builder_context.graph.should_receive("get_last_analysis_document_id").with_args(
            base_image_present_name,
            base_image_present_version,
            is_external=False,
        ).and_return(None).once()
        builder_context.graph.should_receive("get_rpm_package_version_all").times(0)

        UnitPrescription._PRESCRIPTION = {
            "name": "RPMPackagesUnit",
            "should_include": should_include,
        }

        builder_context.recommendation_type = RecommendationType.LATEST
        builder_context.decision_type = None

        assert builder_context.is_adviser_pipeline()
        builder_context.should_receive("is_included").with_args(UnitPrescription).and_return(False).once()
        assert UnitPrescription._should_include_base(builder_context) is False
示例#3
0
    def test_prepare_justification_link(self) -> None:
        """Test preparing links to justifications."""
        justification = [
            {"type": "INFO", "message": "Some info message", "link": "s2i_thoth"},
            {"type": "WARNING", "message": "Some warning message", "link": "https://thoth-station.ninja"},
            {"type": "ERROR", "message": "Some error message", "link": "https://thoth-station.ninja"},
        ]

        flexmock(UnitPrescription, run_prescription=justification)
        UnitPrescription._prepare_justification_link(justification)

        assert justification == [
            {"type": "INFO", "message": "Some info message", "link": jl("s2i_thoth")},
            {"type": "WARNING", "message": "Some warning message", "link": "https://thoth-station.ninja"},
            {"type": "ERROR", "message": "Some error message", "link": "https://thoth-station.ninja"},
        ]
示例#4
0
    def test_should_include_shared_objects_no_image_error(
        self, base_images: List[str], builder_context: PipelineBuilderContext
    ) -> None:
        """Test including pipeline units based on shared objects present in the base image.

        This test covers a case when base image is not present in the user's configuration file.
        """
        builder_context.project.runtime_environment.base_image = None
        should_include = {
            "adviser_pipeline": True,
            "runtime_environments": {
                "shared_objects": ["GNUTLS_3_6_14"],
            },
        }

        if base_images:
            should_include["runtime_environments"]["base_images"] = base_images

        PRESCRIPTION_UNIT_SHOULD_INCLUDE_SCHEMA(should_include)

        UnitPrescription._PRESCRIPTION = {
            "name": "SharedObjectsUnit",
            "should_include": should_include,
        }

        builder_context.recommendation_type = RecommendationType.LATEST
        builder_context.decision_type = None

        assert builder_context.is_adviser_pipeline()
        builder_context.should_receive("is_included").with_args(UnitPrescription).and_return(False).once()
        assert UnitPrescription._should_include_base(builder_context) is False
示例#5
0
    def test_should_include_labels(
        self,
        builder_context: PipelineBuilderContext,
        labels_expected: Optional[Dict[str, str]],
        labels_supplied: Dict[str, str],
        include: bool,
    ) -> None:
        """Test including pipeline units based on labels."""
        should_include = {"adviser_pipeline": True}
        if labels_expected is not None:
            should_include["labels"] = labels_expected

        PRESCRIPTION_UNIT_SHOULD_INCLUDE_SCHEMA(should_include)

        UnitPrescription._PRESCRIPTION = {
            "name": "LabelsUnit",
            "should_include": should_include,
        }

        builder_context.recommendation_type = RecommendationType.LATEST
        builder_context.decision_type = None
        builder_context.labels = labels_supplied
        assert builder_context.is_adviser_pipeline()
        builder_context.should_receive("is_included").with_args(UnitPrescription).and_return(False).once()
        assert UnitPrescription._should_include_base(builder_context) == include
示例#6
0
    def test_should_include_library_usage(
        self,
        builder_context: PipelineBuilderContext,
        library_usage_expected: Optional[Dict[str, Any]],
        library_usage_supplied: Optional[Dict[str, Any]],
        include: bool,
    ) -> None:
        """Test checking library usage."""
        should_include = {"adviser_pipeline": True}
        if library_usage_expected is not None:
            should_include["library_usage"] = library_usage_expected

        PRESCRIPTION_UNIT_SHOULD_INCLUDE_SCHEMA(should_include)

        UnitPrescription._PRESCRIPTION = {
            "name": "LibraryUsageUnit",
            "should_include": should_include,
        }

        builder_context.recommendation_type = RecommendationType.LATEST
        builder_context.decision_type = None
        assert builder_context.is_adviser_pipeline()
        builder_context.should_receive("is_included").with_args(
            UnitPrescription).and_return(False).once()
        assert UnitPrescription._should_include_base(
            builder_context) == include
示例#7
0
    def test_should_include_runtime_environment(
        self,
        builder_context: PipelineBuilderContext,
        prescription_runtime_environments: Dict[str, Any],
        used_runtime_environment_dict: Dict[str, Any],
        include: bool,
    ) -> None:
        """Test parsing and including the given should include entry."""
        PRESCRIPTION_UNIT_SHOULD_INCLUDE_RUNTIME_ENVIRONMENTS_SCHEMA(
            prescription_runtime_environments)
        builder_context.project.runtime_environment = RuntimeEnvironment.from_dict(
            used_runtime_environment_dict)
        UnitPrescription._PRESCRIPTION = {
            "name": "Bar",
            "should_include": {
                "adviser_pipeline": True,
                "runtime_environments": prescription_runtime_environments,
            },
        }
        builder_context.should_receive("is_included").with_args(
            UnitPrescription).and_return(False).once()

        assert builder_context.is_adviser_pipeline()
        assert UnitPrescription._should_include_base(
            builder_context) == include
示例#8
0
    def test_should_include_decision_type(
        self,
        builder_context: PipelineBuilderContext,
        decision_type: DecisionType,
        allowed_decision_types: List[str],
        include: bool,
    ) -> None:
        """Test including a pipeline unit based on decision type."""
        should_include_dict = {"dependency_monkey_pipeline": True}
        if allowed_decision_types is not None:
            should_include_dict["decision_types"] = allowed_decision_types

        builder_context.recommendation_type = None
        builder_context.decision_type = decision_type

        assert builder_context.is_dependency_monkey_pipeline()
        PRESCRIPTION_UNIT_SHOULD_INCLUDE_SCHEMA(should_include_dict)
        UnitPrescription._PRESCRIPTION = {
            "name": "SomePrescriptionUnitName",
            "should_include": should_include_dict,
        }
        builder_context.should_receive("is_included").with_args(
            UnitPrescription).and_return(False).once()
        assert UnitPrescription._should_include_base(
            builder_context) == include
示例#9
0
    def test_should_include_pipeline(
            self, builder_context: PipelineBuilderContext) -> None:
        """Test including based on pipeline type."""
        builder_context.recommendation_type = None
        builder_context.decision_type = DecisionType.ALL
        assert builder_context.is_dependency_monkey_pipeline()

        should_include_dict = {"dependency_monkey_pipeline": True}
        PRESCRIPTION_UNIT_SHOULD_INCLUDE_SCHEMA(should_include_dict)
        UnitPrescription._PRESCRIPTION = {
            "name": "SomePrescriptionUnitName",
            "should_include": should_include_dict,
        }
        builder_context.should_receive("is_included").with_args(
            UnitPrescription).and_return(False)
        # builder context has decision type ALL and prescription has dependency monkey pipeline configured
        assert UnitPrescription._should_include_base(builder_context) is True

        builder_context.recommendation_type = RecommendationType.LATEST
        builder_context.decision_type = None
        assert builder_context.is_adviser_pipeline()

        # builder context has recommendation type LATEST and prescription has dependency monkey pipeline configured
        assert UnitPrescription._should_include_base(builder_context) is False

        should_include_dict = {"adviser_pipeline": True}
        PRESCRIPTION_UNIT_SHOULD_INCLUDE_SCHEMA(should_include_dict)
        UnitPrescription._PRESCRIPTION = {
            "name": "SomePrescriptionUnitName",
            "should_include": should_include_dict,
        }
        builder_context.should_receive("is_included").with_args(
            UnitPrescription).and_return(False)
        # builder context has recommendation type LATEST and prescription has adviser pipeline configured
        assert UnitPrescription._should_include_base(builder_context) is True

        builder_context.recommendation_type = None
        builder_context.decision_type = DecisionType.ALL
        assert builder_context.is_dependency_monkey_pipeline()

        # builder context has decision type ALL and prescription has adviser pipeline configured
        assert UnitPrescription._should_include_base(builder_context) is False
示例#10
0
    def test_should_include_dependencies(self, caplog, builder_context: PipelineBuilderContext) -> None:
        """Test including a pipeline unit based on dependencies."""
        assert builder_context.is_adviser_pipeline()

        should_include_dict = {"adviser_pipeline": True, "dependencies": {"boots": ["Foo"]}}
        PRESCRIPTION_UNIT_SHOULD_INCLUDE_SCHEMA(should_include_dict)
        UnitPrescription._PRESCRIPTION = {"name": "SomeUnitName", "should_include": should_include_dict}
        builder_context.should_receive("get_included_boot_names").and_yield("Foo")
        builder_context.should_receive("is_included").with_args(UnitPrescription).and_return(False)
        assert UnitPrescription._should_include_base(builder_context) is True

        should_include_dict = {"adviser_pipeline": True, "dependencies": {"boots": ["Foo"]}}
        PRESCRIPTION_UNIT_SHOULD_INCLUDE_SCHEMA(should_include_dict)
        UnitPrescription._PRESCRIPTION = {"name": "SomeUnitName", "should_include": should_include_dict}
        builder_context.should_receive("get_included_boot_names").and_yield()
        builder_context.should_receive("is_included").with_args(UnitPrescription).and_return(False)
        assert UnitPrescription._should_include_base(builder_context) is False

        should_include_dict = {
            "adviser_pipeline": True,
            "dependencies": {
                "boots": ["BootUnit1", "BootUnit2"],
                "pseudonyms": ["PseudonymUnit1"],
                "sieves": ["SieveUnit1"],
                "steps": ["StepUnit1"],
                "strides": ["StrideUnit1"],
                "wraps": ["WrapUnit1"],
            },
        }
        PRESCRIPTION_UNIT_SHOULD_INCLUDE_SCHEMA(should_include_dict)
        UnitPrescription._PRESCRIPTION = {"name": "SomeUnitName", "should_include": should_include_dict}
        builder_context.should_receive("get_included_boot_names").and_yield("BootUnit1", "BootUnit2")
        builder_context.should_receive("get_included_pseudonym_names").and_yield("PseudonymUnit1", "PseudonymUnit2")
        builder_context.should_receive("get_included_sieve_names").and_yield("SieveUnit1", "SieveUnit2")
        builder_context.should_receive("get_included_step_names").and_yield("StepUnit0", "StepUnit1")
        builder_context.should_receive("get_included_stride_names").and_yield("StrideUnit1")
        builder_context.should_receive("get_included_wrap_names").and_yield("WrapUnit1")
        builder_context.should_receive("is_included").with_args(UnitPrescription).and_return(False)
        assert UnitPrescription._should_include_base(builder_context) is True
示例#11
0
    def test_should_include_rpm_packages(
        self,
        builder_context: PipelineBuilderContext,
        rpms_present: List[Dict[str, str]],
        rpms_configured: List[Dict[str, str]],
        include: bool,
    ) -> None:
        """Test including pipeline units based on RPM packages present in the base image."""
        base_image_present_name, base_image_present_version = "s2i-thoth", "1.0.0"
        builder_context.project.runtime_environment.base_image = (
            f"{base_image_present_name}:v{base_image_present_version}"
        )
        should_include = {
            "adviser_pipeline": True,
            "runtime_environments": {"base_images": [builder_context.project.runtime_environment.base_image]},
        }

        if rpms_configured:
            should_include["runtime_environments"]["rpm_packages"] = rpms_configured

        PRESCRIPTION_UNIT_SHOULD_INCLUDE_SCHEMA(should_include)

        if rpms_configured:
            builder_context.graph.should_receive("get_last_analysis_document_id").with_args(
                base_image_present_name,
                base_image_present_version,
                is_external=False,
            ).and_return("package-extract-foo-bar").once()
            builder_context.graph.should_receive("get_rpm_package_version_all").with_args(
                "package-extract-foo-bar"
            ).and_return(rpms_present).once()
        else:
            builder_context.graph.should_receive("get_last_analysis_document_id").times(0)
            builder_context.graph.should_receive("get_rpm_package_version_all").times(0)

        UnitPrescription._PRESCRIPTION = {
            "name": "RPMPackagesUnit",
            "should_include": should_include,
        }

        builder_context.recommendation_type = RecommendationType.LATEST
        builder_context.decision_type = None

        assert builder_context.is_adviser_pipeline()
        builder_context.should_receive("is_included").with_args(UnitPrescription).and_return(False).once()
        assert UnitPrescription._should_include_base(builder_context) == include
示例#12
0
    def test_should_include_shared_objects(
        self,
        builder_context: PipelineBuilderContext,
        shared_objects_present: Optional[List[str]],
        shared_objects_configured: Union[List[str], Dict[str, List[str]]],
        include: bool,
    ) -> None:
        """Test including pipeline units based on shared objects present in the base image."""
        base_image_present_name, base_image_present_version = "s2i-thoth", "1.0.0"
        builder_context.project.runtime_environment.base_image = (
            f"{base_image_present_name}:v{base_image_present_version}"
        )
        should_include = {
            "adviser_pipeline": True,
            "runtime_environments": {"base_images": [builder_context.project.runtime_environment.base_image]},
        }

        if shared_objects_configured:
            should_include["runtime_environments"]["shared_objects"] = shared_objects_configured

        PRESCRIPTION_UNIT_SHOULD_INCLUDE_SCHEMA(should_include)

        if shared_objects_configured:
            builder_context.graph.should_receive("get_thoth_s2i_analyzed_image_symbols_all").with_args(
                base_image_present_name,
                base_image_present_version,
                is_external=False,
            ).and_return(shared_objects_present).once()
        else:
            builder_context.graph.should_receive("get_thoth_s2i_analyzed_image_symbols_all").times(0)

        UnitPrescription._PRESCRIPTION = {
            "name": "SharedObjectsUnit",
            "should_include": should_include,
        }

        builder_context.recommendation_type = RecommendationType.LATEST
        builder_context.decision_type = None

        assert builder_context.is_adviser_pipeline()
        builder_context.should_receive("is_included").with_args(UnitPrescription).and_return(False).once()
        assert UnitPrescription._should_include_base(builder_context) == include