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

Removes mock package dependency #1306

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 9 additions & 11 deletions tests/cross_agent/test_docker_container_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,27 @@
# limitations under the License.

import json
import mock
import os
import pytest

import newrelic.common.utilization as u

CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
DOCKER_FIXTURE = os.path.join(CURRENT_DIR, 'fixtures', 'docker_container_id')
DOCKER_FIXTURE = os.path.join(CURRENT_DIR, "fixtures", "docker_container_id")


def _load_docker_test_attributes():
"""Returns a list of docker test attributes in the form:
[(<filename>, <containerId>), ...]
[(<filename>, <containerId>), ...]

"""
docker_test_attributes = []
test_cases = os.path.join(DOCKER_FIXTURE, 'cases.json')
with open(test_cases, 'r') as fh:
test_cases = os.path.join(DOCKER_FIXTURE, "cases.json")
with open(test_cases, "r") as fh:
js = fh.read()
json_list = json.loads(js)
for json_record in json_list:
docker_test_attributes.append(
(json_record['filename'], json_record['containerId']))
docker_test_attributes.append((json_record["filename"], json_record["containerId"]))
return docker_test_attributes


Expand All @@ -46,16 +44,16 @@ def _mock_open(filename, mode):
elif filename == "/proc/self/cgroup":
return mock_file
raise RuntimeError()

return _mock_open


@pytest.mark.parametrize('filename, containerId',
_load_docker_test_attributes())
@pytest.mark.parametrize("filename, containerId", _load_docker_test_attributes())
def test_docker_container_id_v1(monkeypatch, filename, containerId):
path = os.path.join(DOCKER_FIXTURE, filename)
with open(path, 'rb') as f:
with open(path, "rb") as f:
monkeypatch.setattr(u, "open", mock_open(f), raising=False)
if containerId is not None:
assert u.DockerUtilization.detect() == {'id': containerId}
assert u.DockerUtilization.detect() == {"id": containerId}
else:
assert u.DockerUtilization.detect() is None
20 changes: 9 additions & 11 deletions tests/cross_agent/test_docker_container_id_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,27 @@
# limitations under the License.

import json
import mock
import os
import pytest

import newrelic.common.utilization as u

CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
DOCKER_FIXTURE = os.path.join(CURRENT_DIR, 'fixtures', 'docker_container_id_v2')
DOCKER_FIXTURE = os.path.join(CURRENT_DIR, "fixtures", "docker_container_id_v2")


def _load_docker_test_attributes():
"""Returns a list of docker test attributes in the form:
[(<filename>, <containerId>), ...]
[(<filename>, <containerId>), ...]

"""
docker_test_attributes = []
test_cases = os.path.join(DOCKER_FIXTURE, 'cases.json')
with open(test_cases, 'r') as fh:
test_cases = os.path.join(DOCKER_FIXTURE, "cases.json")
with open(test_cases, "r") as fh:
js = fh.read()
json_list = json.loads(js)
for json_record in json_list:
docker_test_attributes.append(
(json_record['filename'], json_record['containerId']))
docker_test_attributes.append((json_record["filename"], json_record["containerId"]))
return docker_test_attributes


Expand All @@ -46,16 +44,16 @@ def _mock_open(filename, mode):
elif filename == "/proc/self/mountinfo":
return mock_file
raise RuntimeError()

return _mock_open


@pytest.mark.parametrize('filename, containerId',
_load_docker_test_attributes())
@pytest.mark.parametrize("filename, containerId", _load_docker_test_attributes())
def test_docker_container_id_v2(monkeypatch, filename, containerId):
path = os.path.join(DOCKER_FIXTURE, filename)
with open(path, 'rb') as f:
with open(path, "rb") as f:
monkeypatch.setattr(u, "open", mock_open(f), raising=False)
if containerId is not None:
assert u.DockerUtilization.detect() == {'id': containerId}
assert u.DockerUtilization.detect() == {"id": containerId}
else:
assert u.DockerUtilization.detect() is None
Loading