Skip to content

Commit

Permalink
Merge pull request #43 from themaxbelov/version
Browse files Browse the repository at this point in the history
Fix version in binary releases and check for updates in version command
  • Loading branch information
ToxicWar authored Jan 11, 2018
2 parents 39e92f3 + ac9032c commit d0c48b1
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 12 deletions.
6 changes: 3 additions & 3 deletions .travis/after_success.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
bash <(curl -s https://codecov.io/bash)

if [[ $BUILD == 'OSX' ]]; then
pyinstaller --onefile apsconnectcli/apsconnect.py
pyinstaller --add-data "VERSION:." --onefile apsconnectcli/apsconnect.py
mv dist/apsconnect dist/apsconnect-mac
fi

if [[ $BUILD == 'LINUX' ]]; then
pyinstaller --onefile apsconnectcli/apsconnect.py
pyinstaller --add-data "VERSION:." --onefile apsconnectcli/apsconnect.py
mv dist/apsconnect dist/apsconnect-lin
sudo add-apt-repository ppa:ubuntu-wine/ppa -y
sudo apt-get update -qq
Expand All @@ -18,5 +18,5 @@ if [[ $BUILD == 'LINUX' ]]; then
wine c:\\Python\\python.exe c:\\Python\\scripts\\pip.exe install pip --upgrade
wine c:\\Python\\python.exe c:\\Python\\scripts\\pip.exe install pyinstaller --upgrade
wine c:\\Python\\python.exe c:\\Python\\scripts\\pip.exe install -r requirements.txt
wine c:\\Python\\scripts\\pyinstaller.exe --onefile apsconnectcli/apsconnect.py
wine c:\\Python\\scripts\\pyinstaller.exe --add-data "VERSION:." --onefile apsconnectcli/apsconnect.py
fi
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.7.22
51 changes: 43 additions & 8 deletions apsconnectcli/apsconnect.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import tempfile
from backports.tempfile import TemporaryDirectory


LOG_DIR = os.path.expanduser('~/.apsconnect')

if not os.path.exists(LOG_DIR):
Expand Down Expand Up @@ -95,6 +94,10 @@
NULL_CFG_INFO = (None, None)
IS_PYTHON3 = sys.version_info >= (3,)

LATEST_RELEASE_URL = 'https://api.github.com/repos/ingrammicro/apsconnect-cli/releases/latest'
REQUEST_TIMEOUT = 5
GITHUB_RELEASES_PAGE = 'https://github.com/ingrammicro/apsconnect-cli/releases/'

PackageInfo = namedtuple('PackageInfo', 'meta_path tenant_schema_path app_schema_path user_service')


Expand Down Expand Up @@ -196,8 +199,17 @@ def check_backend(self):
sys.exit(0)

def version(self):
print("apsconnect-cli v.{} built with love."
.format(pkg_resources.get_distribution('apsconnectcli').version))
package_version = get_version()
latest_version = get_latest_version()

if package_version:
print("apsconnect-cli v{} built with love.".format(package_version))
if latest_version and latest_version != package_version:
print('apsconnect-cli v{} is available, check it here: {}'
.format(latest_version, GITHUB_RELEASES_PAGE))
else:
print("Could not determine apsconnect-cli version. Check {} for latest release."
.format(GITHUB_RELEASES_PAGE))

def install_backend(self, name, image, config_file, hostname, healthcheck_path=None,
root_path='/', namespace='default', replicas=2,
Expand Down Expand Up @@ -702,7 +714,6 @@ def _cluster_probe_connection(api, api_client):


def _create_secret(name, data_format, data, api, namespace='default', force=False):

if data_format == 'json':
config = json.dumps(data, ensure_ascii=False)
elif data_format == 'yaml':
Expand Down Expand Up @@ -1154,12 +1165,36 @@ def file_exists(filename):
return PackageInfo(meta_path, tenant_schema_path, app_schema_path, user_service)


def main():
def bin_version():
"""
This method will return version in binaries built with pyinstaller.
In all other cases it will return NONE.
"""
try:
with open(os.path.join(sys._MEIPASS, 'VERSION')) as f:
return f.read()
except:
return None


def get_version():
try:
print("APSConnect-cli v.{}".format(
pkg_resources.get_distribution('apsconnectcli').version))
return pkg_resources.get_distribution('apsconnectcli').version
except pkg_resources.DistributionNotFound:
pass
return bin_version()


def get_latest_version():
try:
return get(LATEST_RELEASE_URL, timeout=REQUEST_TIMEOUT).json()['tag_name'][1:]
except:
return None


def main():
version = get_version()
if version:
print("APSConnect-cli v{}".format(get_version()))

try:
log_entry = ("=============================\n{}\n".format(" ".join(sys.argv)))
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ PyYAML==3.12
kubernetes==1.0.0
six==1.11.0
backports.tempfile==1.0
requests==2.13.0
8 changes: 7 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
#!/usr/bin/env python

import os
from os.path import abspath, dirname, join

from pip.req import parse_requirements
from setuptools import setup

install_reqs = parse_requirements(os.path.join(os.path.dirname(os.path.abspath(__file__)),
'requirements.txt'), session='None')

here = abspath(dirname(__file__))

with open(join(here, 'VERSION')) as f:
VERSION = f.read()

setup(
name='apsconnectcli',
author='Ingram Micro',
version='1.7.21',
version=VERSION,
keywords='aps apsconnect connector automation',
extras_require={
':python_version<="2.7"': ['backports.tempfile==1.0']},
Expand Down
116 changes: 116 additions & 0 deletions tests/test_apsconnect_internals.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import base64
import os
import sys
from pkg_resources import DistributionNotFound
from unittest import TestCase

from apsconnectcli.apsconnect import (
GITHUB_RELEASES_PAGE,
KUBE_FILE_PATH,
_assert_hub_version,
_osaapi_raise_for_status,
Expand All @@ -16,6 +19,11 @@
_create_service,
_extract_files,
_to_bytes,
bin_version,
get_version,
get_latest_version,
main,
APSConnectUtil,
)

from apsconnectcli.cluster import AvailabilityCheckResult
Expand All @@ -25,10 +33,12 @@

if sys.version_info >= (3,):
from unittest.mock import patch, mock_open, call, MagicMock

_BUILTINS_OPEN = 'builtins.open'
_BUILTINS_PRINT = 'builtins.print'
else:
from mock import patch, mock_open, call, MagicMock

_BUILTINS_OPEN = 'apsconnectcli.apsconnect.open'
_BUILTINS_PRINT = 'apsconnectcli.apsconnect.print'

Expand Down Expand Up @@ -745,3 +755,109 @@ def test_unsupported_version(self):
_assert_hub_version('oa-7.0-1216')

sys_mock.exit.assert_called_with(1)


class TestVersion(TestCase):
def test_latest_version(self):
with patch('apsconnectcli.apsconnect.get_version') as version_mock, \
patch('apsconnectcli.apsconnect.get_latest_version') as latest_version_mock, \
patch(_BUILTINS_PRINT) as print_mock:

version_mock.return_value = '1.2.3'
latest_version_mock.return_value = '1.2.3'
APSConnectUtil().version()

self.assertEqual(print_mock.call_count, 1)
self.assertTrue('1.2.3' in print_mock.call_args[0][0])

def test_outdated_version(self):
with patch('apsconnectcli.apsconnect.get_version') as version_mock, \
patch('apsconnectcli.apsconnect.get_latest_version') as latest_version_mock, \
patch(_BUILTINS_PRINT) as print_mock:

version_mock.return_value = '1.2.3'
latest_version_mock.return_value = '1.2.4'
APSConnectUtil().version()

self.assertEqual(print_mock.call_count, 2)
self.assertTrue('1.2.4' in print_mock.call_args[0][0])

def test_unknown_version(self):
with patch('apsconnectcli.apsconnect.get_version') as version_mock, \
patch('apsconnectcli.apsconnect.get_latest_version'), \
patch(_BUILTINS_PRINT) as print_mock:

version_mock.return_value = None

APSConnectUtil().version()

self.assertEqual(print_mock.call_count, 1)
self.assertTrue(GITHUB_RELEASES_PAGE in print_mock.call_args[0][0])


class TestHelpers(TestCase):
def test_bin_version_ok(self):
with patch('apsconnectcli.apsconnect.sys') as sys_mock, \
patch(_BUILTINS_OPEN) as open_mock:
open_mock.return_value.__enter__.return_value.read.return_value = 'v100500'
sys_mock._MEIPASS = 'pyinstaller_data_dir'
result = bin_version()

open_mock.assert_called_once_with(os.path.join(sys_mock._MEIPASS, 'VERSION'))
self.assertEqual(result, 'v100500')

def test_bin_version_exception(self):
self.assertEqual(bin_version(), None)

def test_get_version_from_package_ok(self):
with patch('apsconnectcli.apsconnect.pkg_resources') as pkg_mock:
pkg_mock.get_distribution.return_value.version = 'v100500'
result = get_version()

self.assertEqual(result, 'v100500')

def test_get_version_from_package_error(self):
with patch('apsconnectcli.apsconnect.pkg_resources') as pkg_mock, \
patch('apsconnectcli.apsconnect.bin_version') as bin_mock:
bin_mock.return_value = 'v100500'
pkg_mock.DistributionNotFound = DistributionNotFound
pkg_mock.get_distribution.side_effect = DistributionNotFound()
result = get_version()

self.assertEqual(result, 'v100500')

def test_get_latest_version_ok(self):
with patch('apsconnectcli.apsconnect.get') as get_mock:
get_mock.return_value.json.return_value = {'tag_name': 'v123'}
result = get_latest_version()

self.assertEqual(result, '123')

def test_get_latest_version_error(self):
with patch('apsconnectcli.apsconnect.get') as get_mock:
get_mock.return_value = 'Definitely not JSON'
result = get_latest_version()

self.assertIsNone(result)

def test_main_prints_version(self):
with patch('apsconnectcli.apsconnect.fire'), \
patch('apsconnectcli.apsconnect.get_version') as get_version_mock, \
patch(_BUILTINS_PRINT) as print_mock:

get_version_mock.return_value = '100.500'
main()

self.assertTrue('100.500' in print_mock.call_args[0][0])

def test_main_prints_error_and_exists_if_there_are_problems(self):
with patch('apsconnectcli.apsconnect.fire') as fire_mock, \
patch('apsconnectcli.apsconnect.get_version'), \
patch(_BUILTINS_PRINT) as print_mock, \
patch('apsconnectcli.apsconnect.sys') as sys_mock:

fire_mock.Fire.side_effect = Exception('All is lost')
main()

self.assertTrue('All is lost' in print_mock.call_args[0][0])
sys_mock.exit.assert_called_once_with(1)

0 comments on commit d0c48b1

Please sign in to comment.