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

Docker #922

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
68 changes: 41 additions & 27 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@ env:
PYTHON_DEFAULT_VERSION: "3.11"

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: codespell-project/actions-codespell@2391250ab05295bddd51e36a8c6295edb6343b0e
with:
ignore_words_list: datas
- name: Set up Python ${{ env.PYTHON_DEFAULT_VERSION }}
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_DEFAULT_VERSION }}
- name: Install dependencies
run: python -m pip install --upgrade nox pip setuptools
- name: Run linters
run: nox -vs lint
- name: Validate changelog
# Library was designed to be used with pull requests only.
if: ${{ github.event_name == 'pull_request' && github.actor != 'dependabot[bot]' }}
uses: zattoo/changelog@v1
with:
token: ${{ github.token }}
# lint:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v3
# with:
# fetch-depth: 0
# - uses: codespell-project/actions-codespell@2391250ab05295bddd51e36a8c6295edb6343b0e
# with:
# ignore_words_list: datas
# - name: Set up Python ${{ env.PYTHON_DEFAULT_VERSION }}
# uses: actions/setup-python@v4
# with:
# python-version: ${{ env.PYTHON_DEFAULT_VERSION }}
# - name: Install dependencies
# run: python -m pip install --upgrade nox pip setuptools
# - name: Run linters
# run: nox -vs lint
# - name: Validate changelog
# # Library was designed to be used with pull requests only.
# if: ${{ github.event_name == 'pull_request' && github.actor != 'dependabot[bot]' }}
# uses: zattoo/changelog@v1
# with:
# token: ${{ github.token }}
build:
needs: lint
# needs: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -49,7 +49,7 @@ jobs:
- name: Build the distribution
run: nox -vs build >> $GITHUB_OUTPUT
cleanup_buckets:
needs: lint
# needs: lint
env:
B2_TEST_APPLICATION_KEY: ${{ secrets.B2_TEST_APPLICATION_KEY }}
B2_TEST_APPLICATION_KEY_ID: ${{ secrets.B2_TEST_APPLICATION_KEY_ID }}
Expand Down Expand Up @@ -127,9 +127,23 @@ jobs:
python-version: ${{ env.PYTHON_DEFAULT_VERSION }}
- name: Install dependencies
run: python -m pip install --upgrade nox pip setuptools
- name: Run dockerized tests
- name: Generate Dockerfile
run: nox -vs generate_dockerfile
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Build Docker
if: ${{ env.DOCKERHUB_USERNAME != '' && env.DOCKERHUB_TOKEN != '' }} # TODO: skip whole job without marking it as an error
uses: docker/build-push-action@v5
with:
context: .
load: true
tags: backblazeit/b2:test
platforms: linux/amd64
- name: Generate Dockerfile
if: ${{ env.B2_TEST_APPLICATION_KEY != '' && env.B2_TEST_APPLICATION_KEY_ID != '' }}
run: nox -vs docker_test
run: nox -vs docker_test backblazeit/b2:test
test-linux-bundle:
needs: cleanup_buckets
env:
Expand Down
52 changes: 52 additions & 0 deletions .github/workflows/push_docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Deploy docker

on:
workflow_dispatch:

jobs:
deploy-docker:
runs-on: ubuntu-latest
env:
DEBIAN_FRONTEND: noninteractive
DOCKERHUB_USERNAME: secrets.DOCKERHUB_USERNAME
DOCKERHUB_TOKEN: secrets.DOCKERHUB_TOKEN
B2_TEST_APPLICATION_KEY: ${{ secrets.B2_TEST_APPLICATION_KEY }}
B2_TEST_APPLICATION_KEY_ID: ${{ secrets.B2_TEST_APPLICATION_KEY_ID }}
PYTHON_DEFAULT_VERSION: 3.11
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: show docker
run: which docker
- name: Set up Python ${{ env.PYTHON_DEFAULT_VERSION }}
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_DEFAULT_VERSION }}
- name: Install dependencies
run: python -m pip install --upgrade nox pip setuptools
- name: Build Dockerfile
id: build-dockerfile
run: nox -vs docker
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to Docker Hub
if: ${{ env.DOCKERHUB_USERNAME != '' && env.DOCKERHUB_TOKEN != '' }} # TODO: skip whole job without marking it as an error
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
if: ${{ env.DOCKERHUB_USERNAME != '' && env.DOCKERHUB_TOKEN != '' }} # TODO: skip whole job without marking it as an error
uses: docker/build-push-action@v3
with:
context: .
push: false
tags: mpnowacki/b2:test
platforms: linux/amd64
- name: Run dockerized tests
if: ${{ env.B2_TEST_APPLICATION_KEY != '' && env.B2_TEST_APPLICATION_KEY_ID != '' }}
run: nox -vs docker_test -- mpnowacki/b2:test

22 changes: 1 addition & 21 deletions Dockerfile.template
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,10 @@ LABEL vcs-url="${vcs_url}"
LABEL vcs-ref="${vcs_ref}"
LABEL build-date-iso8601="${build_date}"

WORKDIR ${homedir}
WORKDIR /root

COPY ${tar_path}/${tar_name} .
RUN ["pip", "install", "${tar_name}[full]"]
ENV PATH=${homedir}/.local/bin:$$PATH


FROM base as test

WORKDIR ${tests_image_dir}
COPY ${tests_path} ./${tests_path}
COPY noxfile.py .

# Files used by tests.
${files_used_by_tests}

RUN ["pip", "install", "nox"]
ENTRYPOINT ["nox", "--no-venv", "--no-install", "-s"]


FROM base

RUN ["useradd", "--home", "${homedir}", "--shell", "/usr/sbin/nologin", "--badnames", "${username}"]
USER ${username}

ENTRYPOINT ["b2"]
CMD ["--help"]
75 changes: 41 additions & 34 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
'3.10',
'3.11',
] if NOX_PYTHONS is None else NOX_PYTHONS.split(',')
PYTHON_DEFAULT_VERSION = PYTHON_VERSIONS[-1]
PYTHON_DEFAULT_VERSION = PYTHON_VERSIONS[-2]
PYTHON_VERSIONS += ['3.12'] # move this into PYTHON_VERSION above after official 3.12 release

PY_PATHS = ['b2', 'test', 'noxfile.py', 'setup.py']
Expand Down Expand Up @@ -182,25 +182,30 @@ def unit(session):
session.notify('cover')


@nox.session(python=PYTHON_VERSIONS)
def integration(session):
def run_integration_test(session, pytest_posargs):
"""Run integration tests."""
install_myself(session, ['license'])
session.run('pip', 'install', *REQUIREMENTS_TEST)
session.run(
'pytest',
'test/integration',
'-s',
'-n',
'auto',
'--log-level',
'INFO',
'-W',
'ignore::DeprecationWarning:rst2ansi.visitor:',
*session.posargs,
'test/integration',
*pytest_posargs,
)


@nox.session(python=PYTHON_VERSIONS)
def integration(session):
"""Run integration tests."""
run_integration_test(session, session.posargs)


@nox.session(python=PYTHON_VERSIONS)
def test(session):
"""Run all tests."""
Expand Down Expand Up @@ -482,27 +487,22 @@ def _read_readme_name_and_description() -> Tuple[str, str]:


@nox.session(python=PYTHON_DEFAULT_VERSION)
def docker(session):
"""Build the docker image."""
def generate_dockerfile(session):
"""Generate Dockerfile from Dockerfile.template"""
build(session)

install_myself(session)
# This string is like `b2 command line tool, version <sem-ver-string>`
version = session.run('b2', 'version', silent=True).split(' ')[-1].strip()

dist_path = 'dist'
tests_image_dir = '/test'
tests_path = 'test/'

full_name, description = _read_readme_name_and_description()
vcs_ref = session.run("git", "rev-parse", "HEAD", external=True, silent=True).strip()
built_distribution = list(pathlib.Path('.').glob(f'{dist_path}/*'))[0]
username = 'b2'

template_mapping = dict(
username='b2',
homedir=f'/{username}',
python_version=session.python,
python_version=PYTHON_DEFAULT_VERSION,
vendor='Backblaze',
name=full_name,
description=description,
Expand All @@ -512,11 +512,8 @@ def docker(session):
vcs_url='https://github.com/Backblaze/B2_Command_Line_Tool',
vcs_ref=vcs_ref,
build_date=datetime.datetime.utcnow().isoformat(),
tests_image_dir=tests_image_dir,
tests_path=tests_path,
tar_path=dist_path,
tar_name=built_distribution.name,
files_used_by_tests='\n'.join([f'COPY {filename} .' for filename in FILES_USED_IN_TESTS])
)

template_file = DOCKER_TEMPLATE.read_text()
Expand All @@ -525,23 +522,33 @@ def docker(session):
pathlib.Path('./Dockerfile').write_text(dockerfile)



def run_docker_tests(session, image_tag):
"""Run unittests against a docker image."""
run_integration_test(session, [
"--sut",
f"docker run -v b2:/b2 -v /tmp:/tmp:rw -v {os.getcwd()}/b2_cli.log:/root/b2_cli.log:rw "
f"--env-file ENVFILE {image_tag}",
"--env-file-cmd-placeholder",
"ENVFILE",
])

@nox.session(python=PYTHON_DEFAULT_VERSION)
def docker_test(session):
"""Run unittests against the docker image."""
docker(session)

image_tag = 'b2:test'

session.run('docker', 'build', '-t', image_tag, '--target', 'test', '.', external=True)
docker_test_run = [
'docker',
'run',
'--rm',
'-e',
'B2_TEST_APPLICATION_KEY',
'-e',
'B2_TEST_APPLICATION_KEY_ID',
image_tag,
]
session.run(*docker_test_run, 'unit', external=True)
session.run(*docker_test_run, 'integration', '--', '--cleanup', external=True)
"""Run unittests against a docker image."""
if session.posargs:
image_tag = session.posargs[0]
else:
raise ValueError('Provide -- {docker_image_tag}')
run_docker_tests(session, image_tag)


@nox.session(python=PYTHON_DEFAULT_VERSION)
def build_and_test_docker(session):
"""
For running locally, CI uses a different set of sessions
"""
test_image_tag = 'b2:test'
generate_dockerfile(session)
session.run('docker', 'build', '-t', test_image_tag, '.', external=True)
run_docker_tests(session, test_image_tag)
19 changes: 0 additions & 19 deletions pyproject.toml

This file was deleted.

Loading