Skip to content

Commit 41ec2c2

Browse files
committed
Docker test.
1 parent cc5a866 commit 41ec2c2

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

.github/workflows/precommit.yml

+9
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,12 @@ jobs:
2626
with:
2727
name: wheel
2828
path: ${{ github.workspace }}/**/*.whl
29+
30+
Run_Docker_Tests:
31+
runs-on: ubuntu-20.04
32+
steps:
33+
- uses: actions/checkout@v4
34+
35+
- name: git ls-tree
36+
run: |
37+
docker build --tag 'image_tests' .

Dockerfile

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM python:3.12
2+
WORKDIR /usr/local/app
3+
ENV CI=true
4+
ENV DOCKER_RUN=true
5+
6+
RUN pip install pytest
7+
8+
COPY src ./src
9+
EXPOSE 5000
10+
11+
RUN useradd app
12+
USER app
13+
14+
RUN pytest -k test_is_docker ./

src/backend/backend_ga4.py

+3-11
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,10 @@ def _send_func(request_data):
2121
except Exception as err:
2222
pass # nosec
2323

24-
2524
def is_docker():
26-
def text_in_file(text, filename):
27-
try:
28-
with open(filename, encoding='utf-8') as lines:
29-
return any(text in line for line in lines)
30-
except OSError:
31-
return False
32-
33-
cgroup = os.path.join('proc', 'self', 'cgroup')
34-
return os.path.exists(os.sep + '.dockerenv') or text_in_file('docker', cgroup)
35-
25+
from pathlib import Path
26+
cgroup = Path('/proc/self/cgroup')
27+
return Path('/.dockerenv').is_file() or cgroup.is_file() and 'docker' in cgroup.read_text()
3628

3729
class GA4Backend(TelemetryBackend):
3830
id = 'ga4'

src/backend/backend_ga4_test.py

+8
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,11 @@ def test_new_cid_priority_over_old_file(self):
101101
self.backend.old_cid_filename)
102102

103103
self.assertTrue(cid_value == new_cid)
104+
105+
def test_is_docker(self):
106+
from .backend_ga4 import is_docker
107+
run_from_docker = "DOCKER_RUN" in os.environ and os.environ["DOCKER_RUN"].lower() == "true"
108+
print("os.environ {}".format(os.environ["DOCKER_RUN"]))
109+
print("is docker {}".format(is_docker()))
110+
111+
self.assertTrue(run_from_docker == is_docker())

0 commit comments

Comments
 (0)