Skip to content

Commit 81d952e

Browse files
popovaanrkazants
andauthored
Docker check. (openvinotoolkit#65)
* Added docker check. * Fixed tests. * Docker test. * Minor fixes. * Corrected check. * Code corrections. * Minor fix. * Update src/backend/backend_ga4.py Co-authored-by: Roman Kazantsev <roman.kazantsev@intel.com> --------- Co-authored-by: Roman Kazantsev <roman.kazantsev@intel.com>
1 parent 4ed4949 commit 81d952e

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
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

+20
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from copy import copy
99
from urllib import request
10+
import os
1011

1112
from .backend import TelemetryBackend
1213
from ..utils.cid import get_or_generate_cid, remove_cid_file
@@ -21,6 +22,22 @@ def _send_func(request_data):
2122
pass # nosec
2223

2324

25+
def is_docker():
26+
def file_has_text(text, filename):
27+
try:
28+
with open(filename, encoding='utf-8') as lines:
29+
for line in lines:
30+
if text in line:
31+
return True
32+
return False
33+
except:
34+
return False
35+
36+
return os.path.exists('/.dockerenv') or \
37+
file_has_text('docker', '/proc/self/cgroup') or \
38+
file_has_text('docker', '/proc/self/mountinfo')
39+
40+
2441
class GA4Backend(TelemetryBackend):
2542
id = 'ga4'
2643
cid_filename = 'openvino_ga_cid'
@@ -74,10 +91,13 @@ def build_event_message(self, event_category: str, event_action: str, event_labe
7491
self.generate_new_session_id()
7592

7693
default_args = copy(self.default_message_attrs)
94+
default_args['docker'] = 'False'
7795
if app_name is not None:
7896
default_args['app_name'] = app_name
7997
if app_version is not None:
8098
default_args['app_version'] = app_version
99+
if is_docker():
100+
default_args['docker'] = 'True'
81101

82102
payload = {
83103
"client_id": client_id,

src/backend/backend_ga4_test.py

+6
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,9 @@ 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+
109+
self.assertTrue(run_from_docker == is_docker())

src/main_test.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ def make_message(self, client_id, app_name, app_version, category, action, label
7373
'session_id': session_id,
7474
'app_name': app_name,
7575
'app_version': app_version,
76-
'usage_count': 1}
76+
'usage_count': 1,
77+
'docker': 'False'}
7778
}
7879
]}
7980

0 commit comments

Comments
 (0)