Skip to content
Permalink
Browse files

feat(scheduler): add api version parsing on scheduler init with tests

  • Loading branch information
Cryptophobia committed Jul 30, 2019
1 parent ef3f6b4 commit 8ce0e1a2c5fdf546edf4d526ef329aad1e9ba0e0
Showing with 30 additions and 3 deletions.
  1. +5 −2 rootfs/scheduler/__init__.py
  2. +25 −1 rootfs/scheduler/tests/test_deployments.py
@@ -1,11 +1,12 @@
from collections import OrderedDict
from datetime import datetime
import logging
from packaging.version import Version
from packaging.version import Version, parse
import requests
import requests.exceptions
from requests_toolbelt import user_agent
import time
import re
from urllib.parse import urljoin

from api import __version__ as deis_version
@@ -84,7 +85,9 @@ def version(self):
raise KubeHTTPException(response, 'fetching Kubernetes version')

data = response.json()
return Version('{}.{}'.format(data['major'], data['minor']))
parsed_version = parse(
re.sub("[^0-9\.]", '', str('{}.{}'.format(data['major'], data['minor']))))
return Version('{}'.format(parsed_version))

@staticmethod
def parse_date(date):
@@ -5,7 +5,7 @@
"""
from unittest import mock
import copy
from packaging.version import parse
from packaging.version import parse, Version, InvalidVersion
from scheduler import KubeHTTPException, KubeException
from scheduler.tests import TestCase
from scheduler.utils import generate_random_name
@@ -76,10 +76,34 @@ def scale(self, namespace=None, name=generate_random_name(), **kwargs):
self.scheduler.scale(namespace, name, **kwargs)
return name

def test_good_init_api_version(self):
try:
data = "1.13"
Version('{}'.format(data))
except InvalidVersion:
self.fail("Version {} raised InvalidVersion exception!".format(data))

def test_bad_init_api_version(self):
data = "1.13+"
with self.assertRaises(
InvalidVersion,
msg='packaging.version.InvalidVersion: Invalid version: {}'.format(data) # noqa
):
Version('{}'.format(data))

def test_deployment_api_version_1_9_and_up(self):
expected = 'apps/v1'
deployment = copy.copy(self.scheduler.deployment)

deployment.version = mock.MagicMock(return_value=parse("1.12+"))
self.assertEqual(expected, deployment.api_version, '1.12+ breaks')

deployment.version = mock.MagicMock(return_value=parse("1.11+"))
self.assertEqual(expected, deployment.api_version, '1.11+ breaks')

deployment.version = mock.MagicMock(return_value=parse("1.10+"))
self.assertEqual(expected, deployment.api_version, '1.10+ breaks')

deployment.version = mock.MagicMock(return_value=parse("1.9+"))
self.assertEqual(expected, deployment.api_version, '1.9+ breaks')

0 comments on commit 8ce0e1a

Please sign in to comment.
You can’t perform that action at this time.