Skip to content

Commit

Permalink
Add tests (close #10) (#15)
Browse files Browse the repository at this point in the history
* add .gitignore

* rename github action file

* add tests
  • Loading branch information
aifrak authored Jul 26, 2020
1 parent 736fcd1 commit c9b69ee
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 15 deletions.
File renamed without changes.
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Created by https://www.toptal.com/developers/gitignore/api/visualstudiocode
# Edit at https://www.toptal.com/developers/gitignore?templates=visualstudiocode

### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace

### VisualStudioCode Patch ###
# Ignore all local history of files
.history

# End of https://www.toptal.com/developers/gitignore/api/visualstudiocode
48 changes: 33 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM debian:buster-slim
FROM debian:buster-slim as base

RUN set -ex \
&& apt-get update \
Expand Down Expand Up @@ -52,20 +52,16 @@ RUN \
&& echo "$FIRA_CODE_BOLD_DOWNLOAD_SHA256 $FONT_DIR/Fura Code Bold Nerd Font Complete.ttf" | sha256sum -c - \
&& echo "$FIRA_CODE_RETINA_DOWNLOAD_SHA256 $FONT_DIR/Fura Code Retina Nerd Font Complete.ttf" | sha256sum -c -

ARG APP_USER=zsh-user
ARG APP_GROUP=zsh-group
ENV APP_USER=zsh-user
ENV APP_USER_GROUP=www-data
ARG APP_USER_HOME=/home/$APP_USER

# create non root user
RUN \
# create non root groups
addgroup $APP_USER \
&& addgroup $APP_GROUP \
# create non root user
&& adduser --quiet --disabled-password \
adduser --quiet --disabled-password \
--shell /bin/bash \
--gecos "ZSH user" $APP_USER \
--ingroup $APP_USER \
&& adduser $APP_USER $APP_GROUP
--ingroup $APP_USER_GROUP

USER $APP_USER
WORKDIR $APP_USER_HOME
Expand All @@ -74,16 +70,38 @@ WORKDIR $APP_USER_HOME
RUN wget -qO- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh | zsh || true

ARG ZSH_CUSTOM=$APP_USER_HOME/.oh-my-zsh/custom
ARG ZSH_PLUGINS=$ZSH_CUSTOM/plugins
ARG ZSH_THEMES=$ZSH_CUSTOM/themes

# install oh-my-zsh plugins and theme
RUN \
git clone --single-branch --branch '0.7.1' --depth 1 https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH_PLUGINS/zsh-syntax-highlighting \
ZSH_PLUGINS=$ZSH_CUSTOM/plugins \
&& ZSH_THEMES=$ZSH_CUSTOM/themes \
&& git clone --single-branch --branch '0.7.1' --depth 1 https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH_PLUGINS/zsh-syntax-highlighting \
&& git clone --single-branch --branch 'v0.6.4' --depth 1 https://github.com/zsh-users/zsh-autosuggestions.git $ZSH_PLUGINS/zsh-autosuggestions \
&& git clone --single-branch --depth 1 https://github.com/romkatv/powerlevel10k.git $ZSH_THEMES/powerlevel10k

COPY --chown=$APP_USER ./config/.zshrc ./config/.p10k.zsh $APP_USER_HOME/
COPY --chown=$APP_USER ./config/aliases.zsh $ZSH_CUSTOM
# install oh-my-zsh config files
COPY --chown=$APP_USER:$APP_USER_GROUP ./config/.zshrc ./config/.p10k.zsh $APP_USER_HOME/
COPY --chown=$APP_USER:$APP_USER_GROUP ./config/aliases.zsh $ZSH_CUSTOM

CMD ["zsh"]

# -------------------------- #
# TESTS #
# -------------------------- #

FROM aifrak/testinfra:5.2.2-python-3.8.5-slim-buster as test-testinfra
FROM base as test-build

# fix issue "/usr/local/bin/python: error while loading shared libraries: libpython3.8.so.1.0: cannot open shared object file: No such file or directory"
ENV LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib

ARG DOCKER_TEST_DIR=./docker/test

RUN mkdir -p $DOCKER_TEST_DIR

WORKDIR $DOCKER_TEST_DIR

COPY --from=test-testinfra /usr/local/ /usr/local/
COPY --chown=$APP_USER:$APP_USER_GROUP ./test .

ENTRYPOINT ["pytest"]
7 changes: 7 additions & 0 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: "3.8"

services:
sut:
build:
context: .
target: test-build
4 changes: 4 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

docker-compose --file docker-compose.test.yml up --build
docker-compose --file docker-compose.test.yml down --volumes --remove-orphans --rmi all
94 changes: 94 additions & 0 deletions test/build_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import os
import platform
import pytest
import testinfra

HOME_PATH = os.environ['HOME']
ROOT = 'root'
USER_NAME = 'zsh-user'
USER_GROUP = 'www-data'
FONTS_PATH = '/usr/local/share/fonts'
OH_MY_ZSH_PATH = HOME_PATH + '/.oh-my-zsh'


def test_python():
assert platform.python_version() == '3.8.5'


def test_pip_packages(host):
pip_packages = host.pip_package.get_packages

assert pip_packages()['pip']['version'] == '20.1.1'
assert pip_packages()['pytest']['version'] == '5.4.3'
assert pip_packages()['testinfra']['version'] == '5.2.2'


@pytest.mark.parametrize('name,version', [
('git', ''),
('wget', ''),
('zsh', ''),
('lsd', '0.17.0'),
])
def test_packages(host, name, version):
package = host.package(name)

assert package.is_installed
if version:
assert package.version.startswith(version)


def test_current_user(host):
user = host.user()

assert user.name == USER_NAME
assert user.group == USER_GROUP


def test_home_directory(host):
assert HOME_PATH == '/home/' + USER_NAME


def test_fzf_package(host):
# workaround to test if fzf is installed (not detected as installed by testinfra)
fzf_version_cmd = host.run('fzf --version')

assert fzf_version_cmd.succeeded
assert fzf_version_cmd.stdout.startswith('0.21.1')


@pytest.mark.parametrize('path,user,group', [
(OH_MY_ZSH_PATH + '/custom/plugins/zsh-syntax-highlighting', USER_NAME, USER_GROUP),
(OH_MY_ZSH_PATH + '/custom/plugins/zsh-autosuggestions', USER_NAME, USER_GROUP),
(OH_MY_ZSH_PATH + '/custom/themes/powerlevel10k', USER_NAME, USER_GROUP),
])
def test_copied_directories(host, path, user, group):
file = host.file(path)

assert file.is_directory
assert len(file.listdir()) > 0
assert file.user == user
assert file.group == group


@pytest.mark.parametrize('path,user,group', [
(HOME_PATH + '/.zshrc', USER_NAME, USER_GROUP),
(HOME_PATH + '/.p10k.zsh', USER_NAME, USER_GROUP),
(HOME_PATH + '/.oh-my-zsh/custom/aliases.zsh', USER_NAME, USER_GROUP),
(FONTS_PATH + '/Fura Code Light Nerd Font Complete.ttf', ROOT, ROOT),
(FONTS_PATH + '/Fura Code Regular Nerd Font Complete.ttf', ROOT, ROOT),
(FONTS_PATH + '/Fura Code Medium Nerd Font Complete.ttf', ROOT, ROOT),
(FONTS_PATH + '/Fura Code Bold Nerd Font Complete.ttf', ROOT, ROOT),
(FONTS_PATH + '/Fura Code Retina Nerd Font Complete.ttf', ROOT, ROOT),
])
def test_copied_files(host, path, user, group):
file = host.file(path)

assert file.is_file
assert file.user == user
assert file.group == group


def test_zsh(host):
cmd = host.run("zsh")

assert cmd.succeeded

0 comments on commit c9b69ee

Please sign in to comment.