Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More thorough testing with pytest #68

Merged
merged 25 commits into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d3740e1
Add gitpython as dev dependency
consideRatio Oct 22, 2019
fe68e51
Add git_repo fixture for repo interactions
consideRatio Oct 22, 2019
712c5da
Initial chartpress run test
consideRatio Oct 22, 2019
2d2573f
Allow for python passed arguments to main()
consideRatio Oct 22, 2019
8f251b3
Use pytest-flakes
consideRatio Oct 22, 2019
37cd973
Ability to work with a untagged branch
consideRatio Oct 22, 2019
7f80b23
Use pytest's monkeypatch and test_git_remote
consideRatio Oct 23, 2019
ca1c5f7
Add test_latest_tag_or_mod_commit
consideRatio Oct 23, 2019
1214e36
pytest everything previously in .travis.yml
consideRatio Oct 23, 2019
b77c849
Test --image-prefix and image_needs_pushing
consideRatio Oct 23, 2019
d21e6dc
Add back lru_cache
consideRatio Oct 24, 2019
f770169
Apply suggestions from code review
consideRatio Oct 24, 2019
cf7a028
Add gh-pages branch
consideRatio Oct 25, 2019
d14c6b8
Refactor for readability and DRY principle
consideRatio Oct 25, 2019
56e8dd0
Test successful gh-pages branch setup
consideRatio Oct 25, 2019
40cb6f2
Test --publish-chart and --extra-message
consideRatio Oct 25, 2019
284e53b
Install and initialize helm on TravisCI
consideRatio Oct 25, 2019
6ccf430
Cleanup print statements
consideRatio Oct 25, 2019
ad34f14
Add tests for paths configuration
consideRatio Oct 26, 2019
bcb4890
Reworked documentation about paths
consideRatio Oct 26, 2019
116e5bf
Test render_build_args
consideRatio Oct 28, 2019
655223e
Bugfixes relating to usage of chart/image paths
consideRatio Oct 28, 2019
ac85a6e
Clear test cache between CLI runs
consideRatio Oct 29, 2019
ccbbba7
Docstrings about helm chart registries
consideRatio Oct 30, 2019
68c7e70
Complement docstring
consideRatio Oct 30, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 3 additions & 30 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ cache: pip
install:
- set -e
- pip install --upgrade pip
- pip install pyflakes pytest .
- pip install .
- pip install -r dev-requirements.txt
script:
- chartpress --version
- chartpress --help
- pyflakes .
- pytest -v ./tests

# This is a workaround to an issue caused by the existence of a docker
# registrymirror in our CI environment. Without this fix that removes the
Expand All @@ -23,30 +22,4 @@ script:
sudo systemctl restart docker &&
echo "Travis docker registry mirroring disabled.\n\n"

- |-
git clone https://github.com/jupyterhub/zero-to-jupyterhub-k8s &&
cd zero-to-jupyterhub-k8s &&
chartpress &&
echo -e "\n\n### realistic run: expecting no rebuilds" &&
git --no-pager diff --unified=1

- |-
chartpress --skip-build --tag 1.2.3-test.tag &&
echo -e "\n\n### --tag --skip-build: expecting no rebuilds and version 1.2.3-test.tag" &&
git --no-pager diff --unified=1

- |-
git tag -a 1.2.3-test.tag -m 1.2.3-test.tag HEAD &&
chartpress --skip-build &&
echo -e "\n\n### git tag: expecting version 1.2.3-test.tag" &&
git --no-pager diff --unified=1

- |-
chartpress --skip-build --long &&
echo -e "\n\n### --long: expecting version 1.2.3-test.tag+000.asdf1234" &&
git --no-pager diff --unified=1

- |-
chartpress --reset &&
echo -e '\n\n### --reset: expecting version "0.0.1+set.by.chartpress"' &&
git --no-pager diff --unified=1
- pytest --verbose --flakes
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,17 @@ git:

## Development

Testing of this python package can be done using [`pyflakes`](https://github.com/PyCQA/pyflakes) and [`pytest`](https://github.com/pytest-dev/pytest). There is also some additional testing that is only run as part of TravisCI, as declared in [`.travis.yml`](.travis.yml).
Testing of this python package can be done using
[`pytest`](https://github.com/pytest-dev/pytest). For more details on the
testing, see [tests/README.md](tests/README.md).

```
```bash
# install chartpress locally
pip install -e .

# install dev dependencies
pip install pyflakes pytest
pip install -r dev-requirements.txt

# run tests
pyflakes .
pytest -v
pytest --verbose --flakes --exitfirst
```
29 changes: 19 additions & 10 deletions chartpress.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,17 @@ def latest_tag_or_mod_commit(*paths, **kwargs):
**kwargs,
).decode('utf-8').strip()

git_describe_head = check_output(
[
'git', 'describe', '--tags', '--long',
],
**kwargs,
).decode('utf-8').strip().rsplit("-", maxsplit=2)
try:
git_describe_head = check_output(
[
'git', 'describe', '--tags', '--long',
],
**kwargs,
).decode('utf-8').strip().rsplit("-", maxsplit=2)
except subprocess.CalledProcessError:
# no tags on branch
return latest_modification_commit

latest_tag = git_describe_head[0]
latest_tagged_commit = check_output(
[
Expand Down Expand Up @@ -167,7 +172,6 @@ def image_needs_pushing(image):
return False


@lru_cache()
minrk marked this conversation as resolved.
Show resolved Hide resolved
def image_needs_building(image):
"""Return whether an image needs building

Expand All @@ -187,9 +191,11 @@ def image_needs_building(image):

# first, check for locally built image
try:
print(image)
d.images.get(image)
except docker.errors.ImageNotFound:
# image not found, check registry
print("not found")
pass
else:
# it exists locally, no need to check remote
Expand Down Expand Up @@ -446,7 +452,9 @@ def build_chart(name, version=None, paths=None, long=False):

version = _get_identifier(latest_tag_in_branch, n_commits, chart_commit, long)

chart['version'] = version
if chart['version'] != version:
print(f"Updating {chart_file}: version: {version}")
chart['version'] = version

with open(chart_file, 'w') as f:
yaml.dump(chart, f)
Expand All @@ -465,6 +473,7 @@ def publish_pages(chart_name, chart_version, chart_repo_github_path, chart_repo_
git_remote(chart_repo_github_path),
checkout_dir,
],
# warning: if echoed, this call could reveal the github token
echo=False,
)
check_call(['git', 'checkout', 'gh-pages'], cwd=checkout_dir)
Expand Down Expand Up @@ -519,7 +528,7 @@ def __call__(self, parser, namespace, values, option_string=None):



def main():
def main(args=None):
"""Run chartpress"""
argparser = argparse.ArgumentParser(description=__doc__)

Expand Down Expand Up @@ -576,7 +585,7 @@ def main():
help='Deprecated: this flag will be ignored. The new logic to determine if an image needs to be rebuilt does not require this. It will find the time in git history where the image was last in need of a rebuild due to changes, and check if that build exists locally or remotely already.',
)

args = argparser.parse_args()
args = argparser.parse_args(args)

if args.version:
print(f"chartpress version {__version__}")
Expand Down
3 changes: 3 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
gitpython
pytest
pytest-flakes
17 changes: 17 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Notes about the tests

These tests are [pytest](https://docs.pytest.org) tests.

## Not yet tested
- `--push`
- `--publish-chart`
- `--extra-message`

## References
- [pytest](https://docs.pytest.org)
- Fixture: [capfd](https://docs.pytest.org/en/latest/reference.html#_pytest.capture.capfd)
- Fixture: [monkeypatching](https://docs.pytest.org/en/latest/capfd.html)
- [pytest-flakes](https://github.com/fschulze/pytest-flakes) and
[pyflakes](https://github.com/PyCQA/pyflakes) for passive code check tests
added to the other pytest tests when using the `--flakes` flag with `pytest`.
- [GitPython](https://gitpython.readthedocs.io/en/stable/)
23 changes: 23 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os
import tempfile
from distutils.dir_util import copy_tree

import git
import pytest

@pytest.fixture(scope="function")
def git_repo(monkeypatch):
"""A temporary git repo with """
with tempfile.TemporaryDirectory() as temp_dir:
# copy content of tests/data folder to the temp dir
copy_tree("tests/data", os.path.join(temp_dir))

# enter the directory
monkeypatch.chdir(temp_dir)

# initialize the repo and make one initial commit
r = git.Repo.init(temp_dir)
r.git.add(all=True)
r.index.commit("initial commit")

yield r
24 changes: 24 additions & 0 deletions tests/data/chartpress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
charts:
- name: testchart
imagePrefix: testchart/
resetTag: test-reset-tag
resetVersion: 0.0.1-test.reset.version
repo:
git: jupyterhub/we-are-not-ready-for-this-yet
published: https://test.local
paths:
- testchart/
- extra-path.txt
images:
testimage:
buildArgs:
TEST_STATIC_BUILD_ARG: "test"
TEST_DYNAMIC_BUILD_ARG: "{TAG}-{LAST_COMMIT}"
contextPath: .
dockerfilePath: image/Dockerfile
valuesPath:
- image
- list.0
- list.1.image
paths:
- extra-path.txt
7 changes: 7 additions & 0 deletions tests/data/image/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM scratch

ARG TEST_STATIC_BUILD_ARG
ARG TEST_DYNAMIC_BUILD_ARG

LABEL test_static_build_arg="$TEST_STATIC_BUILD_ARG"
LABEL test_dynamic_build_arg="$TEST_DYNAMIC_BUILD_ARG"
3 changes: 3 additions & 0 deletions tests/data/testchart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
apiVersion: v1
name: testchart
version: 0.0.1-test.reset.version
4 changes: 4 additions & 0 deletions tests/data/testchart/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
image: testchart/testimage:test-reset-tag
list:
- "testchart/testimage:test-reset-tag"
- image: "testchart/testimage:test-reset-tag"
44 changes: 44 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from chartpress import GITHUB_TOKEN_KEY

from chartpress import git_remote
from chartpress import image_needs_pushing
from chartpress import latest_tag_or_mod_commit
from chartpress import _strip_identifiers_build_suffix
from chartpress import _get_identifier

def test__strip_identifiers_build_suffix():
assert _strip_identifiers_build_suffix(identifier="0.1.2-005.asdf1234") == "0.1.2"
assert _strip_identifiers_build_suffix(identifier="0.1.2-alpha.1.005.asdf1234") == "0.1.2-alpha.1"

def test__get_identifier():
assert _get_identifier(tag="0.1.2", n_commits="0", commit="asdf123", long=True) == "0.1.2-000.asdf123"
assert _get_identifier(tag="0.1.2", n_commits="0", commit="asdf123", long=False) == "0.1.2"
assert _get_identifier(tag="0.1.2", n_commits="5", commit="asdf123", long=False) == "0.1.2-005.asdf123"
assert _get_identifier(tag="0.1.2-alpha.1", n_commits="0", commit="asdf1234", long=True) == "0.1.2-alpha.1.000.asdf1234"
assert _get_identifier(tag="0.1.2-alpha.1", n_commits="0", commit="asdf1234", long=False) == "0.1.2-alpha.1"
assert _get_identifier(tag="0.1.2-alpha.1", n_commits="5", commit="asdf1234", long=False) == "0.1.2-alpha.1.005.asdf1234"

def test_git_remote(monkeypatch):
monkeypatch.setenv(GITHUB_TOKEN_KEY, "test-github-token")
assert git_remote("jupyterhub/helm-chart") == "https://test-github-token@github.com/jupyterhub/helm-chart"

monkeypatch.delenv(GITHUB_TOKEN_KEY)
assert git_remote("jupyterhub/helm-chart") == "git@github.com:jupyterhub/helm-chart"

def test_image_needs_pushing():
assert image_needs_pushing("jupyterhub/image-not-to-be-found:latest")
assert not image_needs_pushing("jupyterhub/k8s-hub:0.8.2")

def test_latest_tag_or_mod_commit(git_repo):
open('tag-mod.txt', "w").close()
git_repo.index.add("tag-mod.txt")
tag_commit = git_repo.index.commit("tag commit")
git_repo.create_tag("1.0.0", message="1.0.0")

open('post-tag-mod.txt', "w").close()
git_repo.index.add("post-tag-mod.txt")
post_tag_commit = git_repo.index.commit("post tag commit")

assert latest_tag_or_mod_commit("chartpress.yaml") == tag_commit.hexsha[:7]
assert latest_tag_or_mod_commit("tag-mod.txt") == tag_commit.hexsha[:7]
assert latest_tag_or_mod_commit("post-tag-mod.txt") == post_tag_commit.hexsha[:7]
14 changes: 0 additions & 14 deletions tests/test_regexp.py

This file was deleted.

Loading