Skip to content

Commit

Permalink
Merge pull request #6 from sclorg/support_check_envs
Browse files Browse the repository at this point in the history
Add support for check_envs_set
  • Loading branch information
phracek authored Oct 26, 2021
2 parents 3501289 + d5261ca commit 83f0fd2
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repos:
- id: trailing-whitespace
- id: flake8
args:
- --max-line-length=100
- --max-line-length=120
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.711
hooks:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ build-test:
test:
cd tests && PYTHONPATH=$(CURDIR) pytest --color=yes -v --showlocals

test-in-container:
test-in-container: build-test
$(PODMAN) run --rm --net=host -e DEPLOYMENT=test ${TEST_IMAGE_NAME}

clean:
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class TestDummyImage(object):

* [x] ct_cleanup
* [ ] ct_enable_cleanup
* [ ] ct_check_envs_set
* [x] ct_check_envs_set
* [x] ct_get_cip
* [x] ct_get_cid
* [x] ct_wait_for_cid
Expand Down Expand Up @@ -69,7 +69,6 @@ class TestDummyImage(object):
* [ ] ct_clone_git_repository
* [ ] ct_show_resources
* [ ] ct_s2i_multistage_build
* [ ]


## OpenShift tests
Expand Down
32 changes: 32 additions & 0 deletions container_ci_suite/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import os
import logging
import re
import time
import subprocess
import shutil
Expand Down Expand Up @@ -416,3 +417,34 @@ def doc_content_old(self):

def test_response(self):
pass

def test_check_exec_env_vars(self, env_filter: str = "^X_SCLS=|/opt/rh|/opt/app-root"):
check_envs = DockerCLIWrapper.docker_run_command(f'--rm {self.image_name} /bin/bash -c env')
logger.debug(f"Run envs {check_envs}")
self.create_container(cid_file="exec_env_vars", container_args="bash -c 'sleep 1000'")
loop_envs = DockerCLIWrapper.run_docker_command(f"exec {self.get_cid_file(self.cid_file)} env")
self.test_check_envs_set(env_filter=env_filter, check_envs=check_envs, loop_envs=loop_envs)

def test_check_envs_set(self, env_filter: str, check_envs: str, loop_envs: str, env_format="VALUE"):
fields_to_check: List = [
x for x in loop_envs.split('\n') if re.findall(env_filter, x) and not x.startswith("PWD=")
]
for field in fields_to_check:
var_name, stripped = field.split('=', 2)
filtered_envs = [x for x in check_envs.split('\n') if x.startswith(f"{var_name}=")]
if not filtered_envs:
logger.error(f"{var_name} not found during 'docker exec'")
return False
filter_envs = ''.join(filtered_envs)
for value in stripped.split(':'):
# If the value checked does not go through env_filter we do not care about it
ret = re.findall(env_filter, value)
if not ret:
continue
new_env = env_format.replace('VALUE', value)
find_env = re.findall(rf"{new_env}", filter_envs)
if not find_env:
logger.error(f"Value {value} is missing from variable {var_name}")
logger.error(filtered_envs)
return False
return True
104 changes: 104 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,107 @@ def test_s2i_build_from_df(self, app_path, s2i_args, src_image, dest_image, df):
dst_image=dest_image,
)
assert generated_df == df

def test_check_envs_set(self):
run_envs = """MANPATH=/opt/rh/rh-ruby26/root/usr/local/share/man:/opt/rh/rh-ruby26/root/usr/share/man:/opt/rh/rh-nodejs14/root/usr/share/man:
APP_ROOT=/opt/app-root
NODEJS_SCL=rh-nodejs14
X_SCLS=rh-nodejs14 rh-ruby26
LD_LIBRARY_PATH=/opt/rh/rh-ruby26/root/usr/local/lib64:/opt/rh/rh-ruby26/root/usr/lib64:/opt/rh/rh-nodejs14/root/usr/lib64
PATH=/opt/rh/rh-ruby26/root/usr/local/bin:/opt/rh/rh-ruby26/root/usr/bin:/opt/rh/rh-nodejs14/root/usr/bin:/opt/app-root/src/bin:/opt/app-root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
STI_SCRIPTS_URL=image:///usr/libexec/s2i
PWD=/opt/app-root/src
STI_SCRIPTS_PATH=/usr/libexec/s2i
IMAGE_NAME=rhscl/ruby-26-rhel7
HOME=/opt/app-root/src
RUBY_SCL=rh-ruby26
XDG_DATA_DIRS=/opt/rh/rh-ruby26/root/usr/local/share:/opt/rh/rh-ruby26/root/usr/share:/usr/local/share:/usr/share
PKG_CONFIG_PATH=/opt/rh/rh-ruby26/root/usr/local/lib64/pkgconfig:/opt/rh/rh-ruby26/root/usr/lib64/pkgconfig
RUBY_VERSION=2.6"""
exec_envs = """PATH=/opt/rh/rh-ruby26/root/usr/local/bin:/opt/rh/rh-ruby26/root/usr/bin:/opt/rh/rh-nodejs14/root/usr/bin:/opt/app-root/src/bin:/opt/app-root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
SUMMARY=Platform for building and running Ruby 2.6 applications
STI_SCRIPTS_URL=image:///usr/libexec/s2i
STI_SCRIPTS_PATH=/usr/libexec/s2i
APP_ROOT=/opt/app-root
HOME=/opt/app-root/src
BASH_ENV=/opt/app-root/etc/scl_enable
ENV=/opt/app-root/etc/scl_enable
PROMPT_COMMAND=. /opt/app-root/etc/scl_enable
NODEJS_SCL=rh-nodejs14
RUBY_SCL=rh-ruby26
IMAGE_NAME=rhscl/ruby-26-rhel7
LD_LIBRARY_PATH=/opt/rh/rh-ruby26/root/usr/local/lib64:/opt/rh/rh-ruby26/root/usr/lib64:/opt/rh/rh-nodejs14/root/usr/lib64
X_SCLS=rh-nodejs14 rh-ruby26
MANPATH=/opt/rh/rh-ruby26/root/usr/local/share/man:/opt/rh/rh-ruby26/root/usr/share/man:/opt/rh/rh-nodejs14/root/usr/share/man:
XDG_DATA_DIRS=/opt/rh/rh-ruby26/root/usr/local/share:/opt/rh/rh-ruby26/root/usr/share:/usr/local/share:/usr/share
PKG_CONFIG_PATH=/opt/rh/rh-ruby26/root/usr/local/lib64/pkgconfig:/opt/rh/rh-ruby26/root/usr/lib64/pkgconfig
"""
ccs = ContainerCISuite(image_name="f32/nodejs:12")
ccs.test_check_envs_set(env_filter="^X_SCLS=|/opt/rh|/opt/app-root", check_envs=exec_envs, loop_envs=run_envs)

def test_check_envs_set_home_not_in_docker_exec(self):
run_envs = """MANPATH=/opt/rh/rh-ruby26/root/usr/local/share/man:/opt/rh/rh-ruby26/root/usr/share/man:/opt/rh/rh-nodejs14/root/usr/share/man:
APP_ROOT=/opt/app-root
X_SCLS=rh-nodejs14 rh-ruby26
LD_LIBRARY_PATH=/opt/rh/rh-ruby26/root/usr/local/lib64:/opt/rh/rh-ruby26/root/usr/lib64:/opt/rh/rh-nodejs14/root/usr/lib64
PATH=/opt/rh/rh-ruby26/root/usr/local/bin:/opt/rh/rh-ruby26/root/usr/bin:/opt/rh/rh-nodejs14/root/usr/bin:/opt/app-root/src/bin:/opt/app-root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
STI_SCRIPTS_URL=image:///usr/libexec/s2i
STI_SCRIPTS_PATH=/usr/libexec/s2i
HOME=/opt/app-root/src
XDG_DATA_DIRS=/opt/rh/rh-ruby26/root/usr/local/share:/opt/rh/rh-ruby26/root/usr/share:/usr/local/share:/usr/share
PKG_CONFIG_PATH=/opt/rh/rh-ruby26/root/usr/local/lib64/pkgconfig:/opt/rh/rh-ruby26/root/usr/lib64/pkgconfig
RUBY_VERSION=2.6"""
exec_envs = """PATH=/opt/rh/rh-ruby26/root/usr/local/bin:/opt/rh/rh-ruby26/root/usr/bin:/opt/rh/rh-nodejs14/root/usr/bin:/opt/app-root/src/bin:/opt/app-root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
STI_SCRIPTS_URL=image:///usr/libexec/s2i
STI_SCRIPTS_PATH=/usr/libexec/s2i
APP_ROOT=/opt/app-root
BASH_ENV=/opt/app-root/etc/scl_enable
ENV=/opt/app-root/etc/scl_enable
PROMPT_COMMAND=. /opt/app-root/etc/scl_enable
LD_LIBRARY_PATH=/opt/rh/rh-ruby26/root/usr/local/lib64:/opt/rh/rh-ruby26/root/usr/lib64:/opt/rh/rh-nodejs14/root/usr/lib64
X_SCLS=rh-nodejs14 rh-ruby26
MANPATH=/opt/rh/rh-ruby26/root/usr/local/share/man:/opt/rh/rh-ruby26/root/usr/share/man:/opt/rh/rh-nodejs14/root/usr/share/man:
XDG_DATA_DIRS=/opt/rh/rh-ruby26/root/usr/local/share:/opt/rh/rh-ruby26/root/usr/share:/usr/local/share:/usr/share
PKG_CONFIG_PATH=/opt/rh/rh-ruby26/root/usr/local/lib64/pkgconfig:/opt/rh/rh-ruby26/root/usr/lib64/pkgconfig
"""
ccs = ContainerCISuite(image_name="f32/nodejs:12")
ret = ccs.test_check_envs_set(
env_filter="^X_SCLS=|/opt/rh|/opt/app-root",
check_envs=exec_envs,
loop_envs=run_envs
)
assert not ret

def test_check_envs_set_not_in_run_envs_not_path(self):
run_envs = """MANPATH=/opt/rh/rh-ruby26/root/usr/local/share/man:/opt/rh/rh-ruby26/root/usr/share/man:/opt/rh/rh-nodejs14/root/usr/share/man:
APP_ROOT=/opt/app-root
X_SCLS=rh-nodejs14 rh-ruby26
LD_LIBRARY_PATH=/opt/rh/rh-ruby26/root/usr/local/lib64:/opt/rh/rh-ruby26/root/usr/lib64:/opt/rh/rh-nodejs14/root/usr/lib64
PATH=/opt/rh/rh-ruby26/root/usr/local/bin:/opt/rh/rh-ruby26/root/usr/bin:/opt/rh/rh-nodejs14/root/usr/bin:/opt/app-root/src/bin:/opt/app-root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
STI_SCRIPTS_URL=image:///usr/libexec/s2i
STI_SCRIPTS_PATH=/usr/libexec/s2i
HOME=/opt/app-root/src
XDG_DATA_DIRS=/opt/rh/rh-ruby26/root/usr/local/share:/opt/rh/rh-ruby26/root/usr/share:/usr/local/share:/usr/share
PKG_CONFIG_PATH=/opt/rh/rh-ruby26/root/usr/local/lib64/pkgconfig:/opt/rh/rh-ruby26/root/usr/lib64/pkgconfig
RUBY_VERSION=2.6"""
exec_envs = """PATH=/opt/rh/rh-ruby26/root/usr/local/bin:/opt/rh/rh-nodejs14/root/usr/bin:/opt/app-root/src/bin:/opt/app-root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
STI_SCRIPTS_URL=image:///usr/libexec/s2i
STI_SCRIPTS_PATH=/usr/libexec/s2i
APP_ROOT=/opt/app-root
BASH_ENV=/opt/app-root/etc/scl_enable
ENV=/opt/app-root/etc/scl_enable
PROMPT_COMMAND=. /opt/app-root/etc/scl_enable
LD_LIBRARY_PATH=/opt/rh/rh-ruby26/root/usr/local/lib64:/opt/rh/rh-ruby26/root/usr/lib64:/opt/rh/rh-nodejs14/root/usr/lib64
X_SCLS=rh-nodejs14 rh-ruby26
HOME=/opt/app-root/src
MANPATH=/opt/rh/rh-ruby26/root/usr/local/share/man:/opt/rh/rh-ruby26/root/usr/share/man:/opt/rh/rh-nodejs14/root/usr/share/man:
XDG_DATA_DIRS=/opt/rh/rh-ruby26/root/usr/local/share:/opt/rh/rh-ruby26/root/usr/share:/usr/local/share:/usr/share
PKG_CONFIG_PATH=/opt/rh/rh-ruby26/root/usr/local/lib64/pkgconfig:/opt/rh/rh-ruby26/root/usr/lib64/pkgconfig
"""
ccs = ContainerCISuite(image_name="f32/nodejs:12")
ret = ccs.test_check_envs_set(
env_filter="^X_SCLS=|/opt/rh|/opt/app-root",
check_envs=exec_envs,
loop_envs=run_envs
)
assert not ret

0 comments on commit 83f0fd2

Please sign in to comment.