summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cloudinit/analyze/__main__.py13
-rwxr-xr-xcloudinit/cmd/cloud_id.py3
-rw-r--r--tests/unittests/analyze/test_boot.py26
-rw-r--r--tests/unittests/analyze/test_dump.py4
-rw-r--r--tests/unittests/test_util.py3
-rw-r--r--tox.ini9
6 files changed, 41 insertions, 17 deletions
diff --git a/cloudinit/analyze/__main__.py b/cloudinit/analyze/__main__.py
index 36a5be78..df08d46c 100644
--- a/cloudinit/analyze/__main__.py
+++ b/cloudinit/analyze/__main__.py
@@ -6,6 +6,7 @@ import argparse
import re
import sys
from datetime import datetime
+from typing import IO
from cloudinit.util import json_dumps
@@ -192,6 +193,7 @@ def analyze_boot(name, args):
}
outfh.write(status_map[status_code].format(**kwargs))
+ clean_io(infh, outfh)
return status_code
@@ -218,6 +220,7 @@ def analyze_blame(name, args):
outfh.write("\n".join(srecs) + "\n")
outfh.write("\n")
outfh.write("%d boot records analyzed\n" % (idx + 1))
+ clean_io(infh, outfh)
def analyze_show(name, args):
@@ -254,12 +257,14 @@ def analyze_show(name, args):
)
outfh.write("\n".join(record) + "\n")
outfh.write("%d boot records analyzed\n" % (idx + 1))
+ clean_io(infh, outfh)
def analyze_dump(name, args):
"""Dump cloud-init events in json format"""
(infh, outfh) = configure_io(args)
outfh.write(json_dumps(_get_events(infh)) + "\n")
+ clean_io(infh, outfh)
def _get_events(infile):
@@ -293,6 +298,14 @@ def configure_io(args):
return (infh, outfh)
+def clean_io(*file_handles: IO) -> None:
+ """close filehandles"""
+ for file_handle in file_handles:
+ if file_handle in (sys.stdin, sys.stdout):
+ continue
+ file_handle.close()
+
+
if __name__ == "__main__":
parser = get_parser()
args = parser.parse_args()
diff --git a/cloudinit/cmd/cloud_id.py b/cloudinit/cmd/cloud_id.py
index 34160f8c..567d341a 100755
--- a/cloudinit/cmd/cloud_id.py
+++ b/cloudinit/cmd/cloud_id.py
@@ -76,7 +76,8 @@ def handle_args(name, args):
return 3
try:
- instance_data = json.load(open(args.instance_data))
+ with open(args.instance_data) as file:
+ instance_data = json.load(file)
except IOError:
return error(
"File not found '%s'. Provide a path to instance data json file"
diff --git a/tests/unittests/analyze/test_boot.py b/tests/unittests/analyze/test_boot.py
index 68db69ec..261f4c4e 100644
--- a/tests/unittests/analyze/test_boot.py
+++ b/tests/unittests/analyze/test_boot.py
@@ -112,19 +112,19 @@ class TestAnalyzeBoot(CiTestCase):
analyze_boot(name_default, args)
# now args have been tested, go into outfile and make sure error
# message is in the outfile
- outfh = open(args.outfile, "r")
- data = outfh.read()
- err_string = (
- "Your Linux distro or container does not support this "
- "functionality.\nYou must be running a Kernel "
- "Telemetry supported distro.\nPlease check "
- "https://cloudinit.readthedocs.io/en/latest/topics"
- "/analyze.html for more information on supported "
- "distros.\n"
- )
-
- self.remove_dummy_file(path, log_path)
- self.assertEqual(err_string, data)
+ with open(args.outfile, "r") as outfh:
+ data = outfh.read()
+ err_string = (
+ "Your Linux distro or container does not support this "
+ "functionality.\nYou must be running a Kernel "
+ "Telemetry supported distro.\nPlease check "
+ "https://cloudinit.readthedocs.io/en/latest/topics"
+ "/analyze.html for more information on supported "
+ "distros.\n"
+ )
+
+ self.remove_dummy_file(path, log_path)
+ self.assertEqual(err_string, data)
@mock.patch("cloudinit.util.is_container", return_value=True)
@mock.patch("cloudinit.subp.subp", return_value=("U=1000000", None))
diff --git a/tests/unittests/analyze/test_dump.py b/tests/unittests/analyze/test_dump.py
index 56bbf97f..1b4ce820 100644
--- a/tests/unittests/analyze/test_dump.py
+++ b/tests/unittests/analyze/test_dump.py
@@ -216,8 +216,8 @@ class TestDumpEvents(CiTestCase):
tmpfile = self.tmp_path("logfile")
write_file(tmpfile, SAMPLE_LOGS)
m_parse_from_date.return_value = 1472594005.972
-
- events, data = dump_events(cisource=open(tmpfile))
+ with open(tmpfile) as file:
+ events, data = dump_events(cisource=file)
year = datetime.now().year
dt1 = datetime.strptime(
"Nov 03 06:51:06.074410 %d" % year, "%b %d %H:%M:%S.%f %Y"
diff --git a/tests/unittests/test_util.py b/tests/unittests/test_util.py
index 4d818a13..9722ddd5 100644
--- a/tests/unittests/test_util.py
+++ b/tests/unittests/test_util.py
@@ -2081,7 +2081,8 @@ class TestMultiLog(helpers.FilesystemMockingTestCase):
self._createConsole(self.root)
logged_string = "something very important"
util.multi_log(logged_string)
- self.assertEqual(logged_string, open("/dev/console").read())
+ with open("/dev/console") as f:
+ self.assertEqual(logged_string, f.read())
def test_logs_dont_go_to_stdout_if_console_exists(self):
self._createConsole(self.root)
diff --git a/tox.ini b/tox.ini
index f35194bf..05a3e929 100644
--- a/tox.ini
+++ b/tox.ini
@@ -150,6 +150,15 @@ commands = {envpython} -m pytest \
-m hypothesis_slow \
{posargs:--hypothesis-show-statistics tests/unittests}
+#commands = {envpython} -X tracemalloc=40 -Werror::ResourceWarning:cloudinit -m pytest \
+[testenv:py3-leak]
+deps = {[testenv:py3]deps}
+commands = {envpython} -X tracemalloc=40 -Wall -m pytest \
+ --durations 10 \
+ {posargs:--cov=cloudinit --cov-branch \
+ tests/unittests}
+
+
[lowest-supported-deps]
# Tox is going to install requirements from pip. This is fine for
# testing python version compatibility, but when we build cloud-init, we are