Skip to content

Commit

Permalink
Merge pull request #60 from jmolina4/fix_java_test_for_gitpod_env
Browse files Browse the repository at this point in the history
Fixing java version test in Gitpod environment
  • Loading branch information
lauris-tw authored Jul 8, 2024
2 parents cc76033 + ae0d3fb commit baafd69
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions tests/integration/test_validate_spark_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,39 @@ def test_java_home_is_set():


def test_java_version_is_greater_or_equal_11():
version_regex = re.compile(r'(?P<major>\d+)\.(?P<minor>\d+)\.\w+')

java_version_output = subprocess.check_output(['java', '-version'], stderr=subprocess.STDOUT).decode("utf-8")
java_version_output = get_java_version_output()
print(f"\n`java -version` returned\n{java_version_output}")

version_number = java_version_output.splitlines()[0].split('"')[1].strip('"')
print(f"Assuming {version_number=} is the version to check.")

regex_match = version_regex.search(version_number)
if not regex_match:
pytest.fail(f"Couldn't parse Java version from {version_number=} using {version_regex=}.")
if regex_match["major"] == "1":
# we need to jump this hoop due to Java version naming conventions - it's fun:
# https://softwareengineering.stackexchange.com/questions/175075/why-is-java-version-1-x-referred-to-as-java-x
actual_major_version = int(regex_match["minor"])
else:
actual_major_version = int(regex_match["major"])
version_line = extract_version_line(java_version_output)
if not version_line:
pytest.fail("Couldn't find version information in `java -version` output.")

major_version = parse_major_version(version_line)
if major_version is None:
pytest.fail(f"Couldn't parse Java version from {version_line}.")

expected_major_version = 11
assert actual_major_version >= expected_major_version, (f"Major version {actual_major_version} is not recent "
f"enough, we need at least version {expected_major_version}.")
assert major_version >= expected_major_version, (f"Major version {major_version} is not recent enough, "
f"we need at least version {expected_major_version}.")


def get_java_version_output():
return subprocess.check_output(['java', '-version'], stderr=subprocess.STDOUT).decode("utf-8")


def extract_version_line(java_version_output):
for line in java_version_output.splitlines():
if "version" in line:
return line
return None


def parse_major_version(version_line):
version_regex = re.compile(r'version "(?P<major>\d+)\.(?P<minor>\d+)\.\d+"')
match = version_regex.search(version_line)
if not match:
return None
major_version = int(match.group("major"))
if major_version == 1:
major_version = int(match.group("minor"))
return major_version

0 comments on commit baafd69

Please sign in to comment.