Skip to content

Commit

Permalink
Update test suite ready for python 3.12
Browse files Browse the repository at this point in the history
Drop support for python 3.8 - it's almost end of life and doesn't
support importlib.resources.files() - we could use the backport
importlib_resources module but easier to drop support.

This change will only get merged into 1.29 and beyond - the
assumption being that anyone who's ready for kubernetes 1.29
is ready for python 3.9+
  • Loading branch information
willthames committed Dec 11, 2023
1 parent 526bb8c commit 85e4559
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
matrix:
platform: [ubuntu-latest, windows-latest]
python-version: ["3.8", "3.9", "3.10"]
python-version: ["3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v1
Expand Down
48 changes: 25 additions & 23 deletions src/kubernetes_validate/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import platform
import re
import sys
from distutils.version import LooseVersion
from typing import Any, Dict, Generator, List, Union
from typing_extensions import Protocol
from packaging.version import Version

import jsonschema
import pkg_resources
import importlib.resources
import yaml

from kubernetes_validate.version import __version__
Expand Down Expand Up @@ -48,10 +48,12 @@ def __init__(self, message: str):


def all_versions() -> List[str]:
schemas = pkg_resources.resource_listdir('kubernetes_validate', '/kubernetes-json-schema')
schemas = importlib.resources.files('kubernetes_validate').joinpath('kubernetes-json-schema')
version_regex = re.compile(r'^v([^-]*).*')
return sorted([version_regex.sub(r"\1", schema) for schema in schemas if version_regex.match(schema)],
key=LooseVersion)
return sorted([version_regex.sub(r"\1", schema.name)
for schema in schemas.iterdir()
if version_regex.match(schema.name)],
key=Version)


def major_minor(version: str) -> str:
Expand Down Expand Up @@ -83,24 +85,24 @@ def validate(data: Union[Dict[str, Any], SupportsToDict], desired_version: str,
schema_dir = 'v%s-local' % version
if strict:
schema_dir += '-strict'
schema_file = pkg_resources.resource_filename('kubernetes_validate',
'/kubernetes-json-schema/%s/%s-%s.json' %
(schema_dir, data['kind'].lower(), api_version))

try:
f = open(schema_file)
except IOError:
if not os.path.exists(os.path.dirname(schema_file)):
raise VersionNotSupportedError(version=desired_version)
raise SchemaNotFoundError(version=major_minor(desired_version), kind=data['kind'],
api_version=data['apiVersion'])
try:
schema = json.load(f)
except json.decoder.JSONDecodeError:
raise InvalidSchemaError("Couldn't parse schema %s" % schema_file)
finally:
f.close()
schema_dir = os.path.dirname(os.path.abspath(schema_file))
ref = importlib.resources.files('kubernetes_validate').joinpath('kubernetes-json-schema/%s/%s-%s.json' %
(schema_dir, data['kind'].lower(),
api_version))
with ref as schema_file:
try:
f = open(schema_file)
except IOError:
if not os.path.exists(os.path.dirname(schema_file)):
raise VersionNotSupportedError(version=desired_version)
raise SchemaNotFoundError(version=major_minor(desired_version), kind=data['kind'],
api_version=data['apiVersion'])
try:
schema = json.load(f)
except json.decoder.JSONDecodeError:
raise InvalidSchemaError("Couldn't parse schema %s" % schema_file)
finally:
f.close()
schema_dir = os.path.dirname(os.path.abspath(schema_file))

uri_prefix = "file://"
if platform.system() == 'Windows':
Expand Down
12 changes: 6 additions & 6 deletions tests/test_validate_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,34 @@
def test_validate_resource_file():
rc = kubernetes_validate.validate_file(os.path.join(parent, 'resource.yaml'), '1.22.0',
strict=False, quiet=True, no_warn=True)
assert(rc == 0)
assert (rc == 0)


def test_validate_multi_resource_file():
rc = kubernetes_validate.validate_file(os.path.join(parent, 'kuard-all.yaml'), '1.22.0',
strict=False, quiet=True, no_warn=True)
assert(rc == 1)
assert (rc == 1)


def test_validate_strict_file():
rc = kubernetes_validate.validate_file(os.path.join(parent, 'kuard-extra-property.yaml'), '1.22.0',
strict=True, quiet=True, no_warn=True)
assert(rc == 1)
assert (rc == 1)


def test_validate_invalid_file():
rc = kubernetes_validate.validate_file(os.path.join(parent, 'kuard-invalid-type.yaml'), '1.22.0',
strict=False, quiet=True, no_warn=True)
assert(rc == 1)
assert (rc == 1)


def test_validate_madeup_file():
rc = kubernetes_validate.validate_file(os.path.join(parent, 'kuard-made-up-kind.yaml'), '1.22.0',
strict=False, quiet=True, no_warn=True)
assert(rc == 0)
assert (rc == 0)


def test_validate_version_too_new():
rc = kubernetes_validate.validate_file(os.path.join(parent, 'resource.yaml'), '1.99.0',
strict=False, quiet=True, no_warn=True)
assert(rc == 2)
assert (rc == 2)
1 change: 1 addition & 0 deletions tests/test_validate_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def test_validate_version_too_new():
except utils.VersionNotSupportedError:
assert True


def test_validate_object_resource():
resources = utils.resources_from_file(os.path.join(parent, 'resource.yaml'))

Expand Down
11 changes: 6 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
[tox]
minversion = 1.6
envlist = py{38,39,310}-{pytest,flake8},mypy
envlist = py{39,310,311,312}-{pytest,flake8},mypy

[gh-actions]
python =
3.8: py38
3.9: py39
3.9: py39, mypy
3.10: py310, mypy
3.11: py311, mypy
3.12: py312, mypy

[testenv]

[testenv:py{38,39,310}-pytest]
[testenv:py{39,310,311,312}-pytest]
deps =
-rtest-deps.txt
commands = pytest
passenv = HOME
recreate = False

[testenv:py{38,39,310}-flake8]
[testenv:py{39,310,311,312}-flake8]
platform = linux|darwin
deps = flake8
commands = python -m flake8 src
Expand Down

0 comments on commit 85e4559

Please sign in to comment.