def test_check_value_none_found(self) -> None: f = PropertiesFile() with self.assertRaises(PropertiesFile.UnexpectedKeyValueError) as err: f.check_value("invalid", "value") self.assertEqual( str(err.exception), "Expected to have invalid='value', but none was found.")
def check(self, path: str) -> None: ext = os.path.splitext(path)[1] if ext not in [ ".asc", ".jar", ".md5", ".module", ".pom", ".sha1", ".sha256", ".sha512", ".war", ".xml", ".zip", ]: raise BuildArtifactCheck.BuildArtifactInvalidError( path, f"{ext} is not a valid extension for a maven file") if os.path.splitext(path)[1] == ".jar": with ZipFile(path, "r") as zip: data = zip.read("META-INF/MANIFEST.MF").decode("UTF-8") properties = PropertiesFile(data) try: versions: List[Any] = [None] versions.extend(self.target.compatible_component_versions) versions.extend(self.target.compatible_min_versions) properties.check_value_in("Implementation-Version", versions) except PropertiesFile.CheckError as e: raise BuildArtifactCheck.BuildArtifactInvalidError( path, str(e)) logging.info( f'Checked {path} ({properties.get_value("Implementation-Version", "N/A")})' )
def test_check_value_in_none_found(self) -> None: f = PropertiesFile() with self.assertRaises(PropertiesFile.UnexpectedKeyValuesError) as err: f.check_value_in("invalid", ["one", "two"]) self.assertEqual( str(err.exception), "Expected to have invalid=any of ['one', 'two'], but none was found.", )
def test_check_value_in_invalid(self) -> None: f = PropertiesFile({"key": "value"}) with self.assertRaises(PropertiesFile.UnexpectedKeyValuesError) as err: f.check_value_in("key", ["one", "two"]) self.assertEqual( str(err.exception), "Expected to have key=any of ['one', 'two'], but was 'value'.", )
def __get_dependencies(self): cmd = " ".join([ f"./gradlew {self.gradle_project or ''}:dependencies", f"-Dopensearch.version={self.target.opensearch_version}", f"-Dbuild.snapshot={str(self.target.snapshot).lower()}", "--configuration compileOnly", '| grep -e "---"', ]) lines = self.git_repo.output(cmd) stack = ["root"] props = PropertiesFile("") for line in lines.split("\n"): # e.g. "| +--- org.opensearch:opensearch-core:1.1.0-SNAPSHOT" # see job_scheduler_dependencies.txt in tests for an example match = re.search(r"---\s(.*):([0-9,\w,.-]*)[\s]*", line) if match: levels = line.count(" ") + line.count("---") while levels < len(stack): del stack[-1] if levels == len(stack): stack[-1] = match.group(1).strip() elif levels > len(stack): stack.append(match.group(1).strip()) key = "/".join(stack) value = match.group(2).strip() logging.debug(f"{key}={value}") props[key] = value return props
def __get_properties(self): cmd = " ".join([ "./gradlew properties", f"-Dopensearch.version={self.target.opensearch_version}", f"-Dbuild.snapshot={str(self.target.snapshot).lower()}", ]) return PropertiesFile(self.git_repo.output(cmd))
def __mock_check(self, props: Any = None, component: Component = None, snapshot: bool = True) -> CiCheckGradlePropertiesVersion: with patch.object(CiCheckGradlePropertiesVersion, "_CiCheckGradleProperties__get_properties") as mock_properties: mock_properties.return_value = PropertiesFile(props) return CiCheckGradlePropertiesVersion( component=component or MagicMock(), git_repo=MagicMock(), target=CiTarget(version="2.0.0", name="opensearch", qualifier="alpha1", snapshot=snapshot), )
def properties(self): cmd = ComponentOpenSearch.gradle_cmd( "properties", { "opensearch.version": self.opensearch_version, "build.snapshot": str(self.snapshot).lower(), }, ) return PropertiesFile(self.git_repo.output(cmd))
def __mock_check(self, props=None, component=None, snapshot=True): with patch.object( CiCheckGradlePropertiesVersion, "_CiCheckGradleProperties__get_properties") as mock_properties: mock_properties.return_value = PropertiesFile(props) return CiCheckGradlePropertiesVersion( component=component or MagicMock(), git_repo=MagicMock(), target=CiTarget(version="1.1.0", name="opensearch", snapshot=snapshot), )
def __mock_check(self, props=None): with patch.object( CiCheckGradleDependenciesOpenSearchVersion, "_CiCheckGradleDependencies__get_dependencies", ) as mock_dependencies: mock_dependencies.return_value = PropertiesFile(props) return CiCheckGradleDependenciesOpenSearchVersion( component=MagicMock(), git_repo=MagicMock(), target=CiTarget(version="1.1.0", name="opensearch", snapshot=True), args=None, )
def __get_properties(self) -> PropertiesFile: cmd = " ".join( filter( None, [ "./gradlew properties", f"-Dopensearch.version={self.target.opensearch_version}", f"-Dbuild.snapshot={str(self.target.snapshot).lower()}", f"-Dbuild.version_qualifier={str(self.target.qualifier)}" if self.target.qualifier else None, ] ) ) return PropertiesFile(self.git_repo.output(cmd))
def __mock_check(self, props: Any = None, component: Component = None, snapshot: bool = True) -> CiCheckNpmPackageVersion: with patch.object( CiCheckNpmPackageVersion, "_CiCheckPackage__get_properties") as mock_properties: mock_properties.return_value = PropertiesFile(props) return CiCheckNpmPackageVersion( component=component or MagicMock(), git_repo=MagicMock(), target=CiTarget(version="1.1.0", name="dashboards-plugin", qualifier=None, snapshot=snapshot), )
def check(self, path: str) -> None: if os.path.splitext(path)[1] != ".zip": raise BuildArtifactCheck.BuildArtifactInvalidError(path, "Not a zip file.") if not self.valid_path(path): raise BuildArtifactCheck.BuildArtifactInvalidError(path, f"Expected filename to include one of {self.target.compatible_component_versions}.") with ZipFile(path, "r") as zip: data = zip.read("plugin-descriptor.properties").decode("UTF-8") properties = PropertiesFile(data) try: properties.check_value_in("version", self.target.compatible_component_versions) properties.check_value_in("opensearch.version", self.target.compatible_versions) except PropertiesFile.CheckError as e: raise BuildArtifactCheck.BuildArtifactInvalidError(path, e.__str__()) logging.info(f'Checked {path} ({properties.get_value("version", "N/A")})')
def properties(self) -> PropertiesFile: cmd = ComponentOpenSearch.gradle_cmd( "properties", {"build.snapshot": str(self.snapshot).lower()}) return PropertiesFile(self.git_repo.output(cmd))
def test_check_value_in(self) -> None: f = PropertiesFile({"key": "value"}) f.check_value_in("key", ["value"])
def __get_properties(self) -> PropertiesFile: with open(self.package_json_path, "r") as f: return PropertiesFile(CiCheckPackage.__flattenDict(json.load(f)))
def test_check_value_invalid(self) -> None: f = PropertiesFile({"key": "value"}) with self.assertRaises(PropertiesFile.UnexpectedKeyValueError) as err: f.check_value("key", "invalid") self.assertEqual(str(err.exception), "Expected to have key='invalid', but was 'value'.")
def test_check_value(self) -> None: f = PropertiesFile({"key": "value"}) f.check_value("key", "value")
def test_get_value(self) -> None: f = PropertiesFile({"key": "value"}) self.assertEqual(f.get_value("key"), "value") self.assertEqual(f.get_value("invalid"), None) self.assertEqual(f.get_value("invalid", "default"), "default")
def test_blank(self) -> None: f = PropertiesFile() self.assertEqual(f.properties, {})