From c1fa5c3e52fc54a9da0ce6f2b743765187b7186e Mon Sep 17 00:00:00 2001 From: gardlt Date: Wed, 14 Jun 2017 13:30:15 -0500 Subject: [PATCH] testing-utils-lib * added unittest for the utils module --- .travis.yml | 23 ++-- CODE_OF_CONDUCT.rst | 84 +++++++++++++++ armada/handlers/chartbuilder.py | 4 +- armada/tests/unit/api/__init__.py | 0 armada/tests/unit/cli/__init__.py | 0 .../tests/unit/handlers/test_chartbuilder.py | 100 ++++++++++++++++++ armada/tests/unit/utils/__init__.py | 0 armada/tests/unit/utils/test_git.py | 53 ++++++++++ armada/tests/unit/utils/test_lint.py | 75 +++++++++++++ armada/tests/unit/utils/test_release.py | 37 +++++++ armada/utils/git.py | 4 + requirements.txt | 3 +- setup.cfg | 4 + test-requirements.txt | 10 +- tox.ini | 2 +- 15 files changed, 377 insertions(+), 22 deletions(-) create mode 100644 CODE_OF_CONDUCT.rst create mode 100644 armada/tests/unit/api/__init__.py create mode 100644 armada/tests/unit/cli/__init__.py create mode 100644 armada/tests/unit/handlers/test_chartbuilder.py create mode 100644 armada/tests/unit/utils/__init__.py create mode 100644 armada/tests/unit/utils/test_git.py create mode 100644 armada/tests/unit/utils/test_lint.py create mode 100644 armada/tests/unit/utils/test_release.py diff --git a/.travis.yml b/.travis.yml index c6bfacfe..c0ddfee2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,30 +6,19 @@ python: - "2.7" before_install: - - export LIGGIT2_VERSION=0.24.0 - - export LIBGIT2=$VIRTUAL_ENV - - wget https://github.com/libgit2/libgit2/archive/v${LIGGIT2_VERSION}.tar.gz - - tar xzf v${LIGGIT2_VERSION}.tar.gz - - cd libgit2-${LIGGIT2_VERSION}/ - - cmake . -DCMAKE_INSTALL_PREFIX=$LIBGIT2 - - make - - make install - - export LDFLAGS="-Wl,-rpath='$LIBGIT2/lib',--enable-new-dtags $LDFLAGS" - - cd .. + - sudo sh tools/libgit2.sh - pip install -r test-requirements.txt - pip install -r requirements.txt - pip install tox - - pip install pygit2==0.24.0 + - pip install pygit2==0.25.0 install: - - pip install . + - pip install -e . script: - flake8 - armada -h -# - armada -c examples/armada.yaml -d + - nosetests -w ./armada --with-coverage --cover-package=armada --cover-tests -after_success: - - cd docs && make html - - touch .nojekyll - - git clean -fdx +# after_success: +# - codecov diff --git a/CODE_OF_CONDUCT.rst b/CODE_OF_CONDUCT.rst new file mode 100644 index 00000000..040dc33c --- /dev/null +++ b/CODE_OF_CONDUCT.rst @@ -0,0 +1,84 @@ +Contributor Covenant Code of Conduct +==================================== + +Our Pledge +---------- + +In the interest of fostering an open and welcoming environment, we as +contributors and maintainers pledge to making participation in our +project and our community a harassment-free experience for everyone, +regardless of age, body size, disability, ethnicity, gender identity and +expression, level of experience, nationality, personal appearance, race, +religion, or sexual identity and orientation. + +Our Standards +------------- + +Examples of behavior that contributes to creating a positive environment +include: + +- Using welcoming and inclusive language +- Being respectful of differing viewpoints and experiences +- Gracefully accepting constructive criticism +- Focusing on what is best for the community +- Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +- The use of sexualized language or imagery and unwelcome sexual + attention or advances +- Trolling, insulting/derogatory comments, and personal or political + attacks +- Public or private harassment +- Publishing others’ private information, such as a physical or + electronic address, without explicit permission +- Other conduct which could reasonably be considered inappropriate in a + professional setting + +Our Responsibilities +-------------------- + +Project maintainers are responsible for clarifying the standards of +acceptable behavior and are expected to take appropriate and fair +corrective action in response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, +or reject comments, commits, code, wiki edits, issues, and other +contributions that are not aligned to this Code of Conduct, or to ban +temporarily or permanently any contributor for other behaviors that they +deem inappropriate, threatening, offensive, or harmful. + +Scope +----- + +This Code of Conduct applies both within project spaces and in public +spaces when an individual is representing the project or its community. +Examples of representing a project or community include using an +official project e-mail address, posting via an official social media +account, or acting as an appointed representative at an online or +offline event. Representation of a project may be further defined and +clarified by project maintainers. + +Enforcement +----------- + +Instances of abusive, harassing, or otherwise unacceptable behavior may +be reported by contacting the project team at garivera89@gmail.com. The +project team will review and investigate all complaints, and will +respond in a way that it deems appropriate to the circumstances. The +project team is obligated to maintain confidentiality with regard to the +reporter of an incident. Further details of specific enforcement +policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in +good faith may face temporary or permanent repercussions as determined +by other members of the project’s leadership. + +Attribution +----------- + +This Code of Conduct is adapted from the `Contributor Covenant`_, +version 1.4, available at `http://contributor-covenant.org/version/1/4`_ + +.. _Contributor Covenant: http://contributor-covenant.org +.. _`http://contributor-covenant.org/version/1/4`: http://contributor-covenant.org/version/1/4/ diff --git a/armada/handlers/chartbuilder.py b/armada/handlers/chartbuilder.py index ecf05eaa..817afed5 100644 --- a/armada/handlers/chartbuilder.py +++ b/armada/handlers/chartbuilder.py @@ -110,8 +110,8 @@ class ChartBuilder(object): Process metadata ''' # extract Chart.yaml to construct metadata - chart_yaml = dotify(yaml.load(open( - os.path.join(self.source_directory, 'Chart.yaml')).read())) + chart_yaml = dotify(yaml.load(open(os.path.join(self.source_directory, + 'Chart.yaml')).read())) # construct Metadata object return Metadata( diff --git a/armada/tests/unit/api/__init__.py b/armada/tests/unit/api/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/armada/tests/unit/cli/__init__.py b/armada/tests/unit/cli/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/armada/tests/unit/handlers/test_chartbuilder.py b/armada/tests/unit/handlers/test_chartbuilder.py new file mode 100644 index 00000000..fdd6eae3 --- /dev/null +++ b/armada/tests/unit/handlers/test_chartbuilder.py @@ -0,0 +1,100 @@ +# Copyright 2017 The Armada Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import unittest +import mock + +from supermutes.dot import dotify + +from armada.handlers.chartbuilder import ChartBuilder + + +class ChartBuilderTestCase(unittest.TestCase): + chart_stream = """ + chart: + name: mariadb + release_name: mariadb + namespace: openstack + install: + no_hooks: false + upgrade: + no_hooks: false + values: + replicas: 1 + volume: + size: 1Gi + source: + type: git + location: git://github.com/openstack/openstack-helm + subpath: mariadb + reference: master + dependencies: [] + """ + + chart_yaml = """ + apiVersion: v1 + description: A Helm chart for Kubernetes + name: hello-world-chart + version: 0.1.0 + """ + + chart_value = """ + # Default values for hello-world-chart. + # This is a YAML-formatted file. + # Declare variables to be passed into your templates. + replicaCount: 1 + image: + repository: nginx + tag: stable + pullPolicy: IfNotPresent + service: + name: nginx + type: ClusterIP + externalPort: 38443 + internalPort: 80 + resources: + limits: + cpu: 100m + memory: 128Mi + requests: + cpu: 100m + memory: 128Mi + """ + + def test_chartbuilder_source_clone(self): + + chart = dotify(self.chart_stream) + ChartBuilder.source_clone = mock.Mock(return_value='path') + chartbuilder = ChartBuilder(chart) + resp = getattr(chartbuilder, 'source_directory', None) + + self.assertIsNotNone(resp) + self.assertIsInstance(resp, basestring) + + @unittest.skip("we are having wierd scenario") + @mock.patch('armada.handlers.chartbuilder.dotify') + @mock.patch('armada.handlers.chartbuilder.os') + def test_chart_source_clone(self, mock_os, mock_dot): + from supermutes.dot import dotify + import yaml + mock_dot.dotify.return_value = dotify(yaml.load(self.chart_stream)) + mock_os.path.join.return_value = self.chart_stream + + ChartBuilder.source_clone = mock.Mock(return_value='path') + chartbuilder = ChartBuilder(self.chart_stream) + resp = chartbuilder.get_metadata() + + self.assertIsNotNone(resp) + self.assertIsInstance(resp, basestring) diff --git a/armada/tests/unit/utils/__init__.py b/armada/tests/unit/utils/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/armada/tests/unit/utils/test_git.py b/armada/tests/unit/utils/test_git.py new file mode 100644 index 00000000..b55744cd --- /dev/null +++ b/armada/tests/unit/utils/test_git.py @@ -0,0 +1,53 @@ +# Copyright 2017 The Armada Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import mock +import unittest + +from armada.utils import git + +class GitTestCase(unittest.TestCase): + + @mock.patch('armada.utils.git.tempfile') + @mock.patch('armada.utils.git.pygit2') + def test_git_clone_repo_pass(self, mock_pygit, mock_temp): + mock_temp.mkdtemp.return_value = '/tmp/armada' + mock_pygit.clone_repository.return_value = "Repository" + url = 'http://github.com/att-comdev/armada' + dir = git.git_clone(url) + + self.assertIsNotNone(dir) + + def test_git_clone_empty_url(self): + url = '' + dir = git.git_clone(url) + + self.assertFalse(dir) + + def test_git_clone_bad_url(self): + url = 'http://github.com/dummy/armada' + dir = git.git_clone(url) + + self.assertFalse(dir) + + def test_check_available_repo_pass(self): + url = 'http://github.com/att-comdev/armada' + resp = git.check_available_repo(url) + + self.assertTrue(resp) + + def test_check_available_repo_dummy_url(self): + url = 'http://github.com/dummy/armada' + resp = git.check_available_repo(url) + self.assertFalse(resp) diff --git a/armada/tests/unit/utils/test_lint.py b/armada/tests/unit/utils/test_lint.py new file mode 100644 index 00000000..66836c49 --- /dev/null +++ b/armada/tests/unit/utils/test_lint.py @@ -0,0 +1,75 @@ +# Copyright 2017 The Armada Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest +import yaml + +from armada.utils import lint + +class LintTestCase(unittest.TestCase): + + def test_lint_armada_yaml_pass(self): + config = yaml.load(""" + armada: + release_prefix: armada-test + charts: + - chart: + name: chart + release_name: chart + namespace: chart + """) + resp = lint.valid_manifest(config) + self.assertTrue(resp) + + def test_lint_armada_keyword_removed(self): + config = yaml.load(""" + armasda: + release_prefix: armada-test + charts: + - chart: + name: chart + release_name: chart + namespace: chart + """) + + with self.assertRaises(Exception): + lint.valid_manifest(config) + + def test_lint_prefix_keyword_removed(self): + config = yaml.load(""" + armada: + release: armada-test + charts: + - chart: + name: chart + release_name: chart + namespace: chart + """) + + with self.assertRaises(Exception): + lint.valid_manifest(config) + + def test_lint_armada_removed(self): + config = yaml.load(""" + armasda: + release_prefix: armada-test + chart: + - chart: + name: chart + release_name: chart + namespace: chart + """) + + with self.assertRaises(Exception): + lint.valid_manifest(config) diff --git a/armada/tests/unit/utils/test_release.py b/armada/tests/unit/utils/test_release.py new file mode 100644 index 00000000..37b66256 --- /dev/null +++ b/armada/tests/unit/utils/test_release.py @@ -0,0 +1,37 @@ +# Copyright 2017 The Armada Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest + +from armada.utils import release as rel + +class ReleaseTestCase(unittest.TestCase): + + def test_release_prefix_pass(self): + expected = 'armada-test' + prefix, chart = ('armada', 'test') + + assert rel.release_prefix(prefix, chart) == expected + + def test_release_prefix_int_string(self): + expected = 'armada-4' + prefix, chart = ('armada', 4) + + assert rel.release_prefix(prefix, chart) == expected + + def test_release_prefix_int_int(self): + expected = '4-4' + prefix, chart = (4, 4) + + assert rel.release_prefix(prefix, chart) == expected diff --git a/armada/utils/git.py b/armada/utils/git.py index 655776e4..a7e66a6c 100644 --- a/armada/utils/git.py +++ b/armada/utils/git.py @@ -9,6 +9,10 @@ def git_clone(repo_url, branch='master'): ''' clones repo to a /tmp/ dir ''' + + if repo_url == '' or not check_available_repo(repo_url): + return False + _tmp_dir = tempfile.mkdtemp(prefix='armada', dir='/tmp') pygit2.clone_repository(repo_url, _tmp_dir, checkout_branch=branch) diff --git a/requirements.txt b/requirements.txt index e01a7e9e..acdd2736 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ grpc==0.3.post19 grpcio==1.1.3 grpcio-tools==1.1.3 -kubernetes==1.0.0 +kubernetes>=1.0.0 oslo.log==3.28.0 oslo.messaging==5.28.0 protobuf==3.2.0 @@ -18,5 +18,6 @@ gunicorn==19.7.1 cliff==2.7.0 # Oslo +oslo.log==3.28.0 oslo.config>=3.22.0 # Apache-2.0 oslo.middleware>=3.27.0 # Apache-2.0 diff --git a/setup.cfg b/setup.cfg index 66d9b2e2..b4b03917 100644 --- a/setup.cfg +++ b/setup.cfg @@ -47,3 +47,7 @@ warnerrors = True [wheel] universal = 1 + +[nosetests] +verbosity=3 +with-doctest=1 diff --git a/test-requirements.txt b/test-requirements.txt index 35e90679..ef2749bf 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,3 +1,11 @@ -flake8==3.3.0 +# General tox + +# Docs Sphinx + +# Testing +flake8==3.3.0 +nose==1.3.7 +testtools==2.3.0 +codecov diff --git a/tox.ini b/tox.ini index 3003512a..f21fe111 100644 --- a/tox.ini +++ b/tox.ini @@ -26,5 +26,5 @@ commands = nosetest -w armada [flake8] ignore=E302,H306 -exclude= libgit2-0.24.0, .git, .idea, .tox, *.egg-info, *.eggs, bin, dist, hapi +exclude= libgit2-0.25.0, .git, .idea, .tox, *.egg-info, *.eggs, bin, dist, hapi