Skip to content

Commit 659daac

Browse files
author
Jan Musial
committed
Add tests for opencas.py
Signed-off-by: Jan Musial <[email protected]>
1 parent d773a81 commit 659daac

10 files changed

+1129
-9
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ Module.symvers
1313
Module.markers
1414
*.mod.c
1515
modules.order
16+
__pycache__/
17+
*.py[cod]
18+
*$py.class

ocf

Submodule ocf updated 93 files

test/utils_tests/opencas-py-tests/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#
2+
# Copyright(c) 2012-2019 Intel Corporation
3+
# SPDX-License-Identifier: BSD-3-Clause-Clear
4+
#
5+
6+
import sys
7+
import os
8+
9+
def pytest_configure(config):
10+
try:
11+
import helpers
12+
except:
13+
raise Exception("Couldn't import helpers")
14+
15+
sys.path.append(helpers.find_repo_root() + "/utils")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#
2+
# Copyright(c) 2012-2019 Intel Corporation
3+
# SPDX-License-Identifier: BSD-3-Clause-Clear
4+
#
5+
6+
import mock
7+
import re
8+
import os
9+
import sys
10+
import logging
11+
from io import StringIO
12+
from textwrap import dedent
13+
14+
def find_repo_root():
15+
path = os.getcwd()
16+
17+
while os.path.realpath(path) != "/":
18+
if ".git" in os.listdir(path):
19+
return path
20+
21+
path = os.path.dirname(path)
22+
23+
raise Exception("Couldn't find repository root - unable to locate opencas.py")
24+
25+
def get_process_mock(return_value, stdout, stderr):
26+
process_mock = mock.Mock()
27+
attrs = {
28+
"wait.return_value" : return_value,
29+
"communicate.return_value" : (stdout, stderr),
30+
}
31+
process_mock.configure_mock(**attrs)
32+
33+
return process_mock
34+
35+
def get_mock_os_exists(existing_files):
36+
return lambda x : x in existing_files
37+
38+
def get_hashed_config_list(conf):
39+
""" Convert list of config lines to list of config lines hashes, drop empty lines """
40+
hashed_conf = [get_conf_line_hash(x) for x in conf]
41+
42+
return [x for x in hashed_conf if x]
43+
44+
def get_conf_line_hash(line):
45+
"""
46+
Removes whitespace, lowercases, comments and sorts cache params if present.
47+
Returns empty line for comment-only lines
48+
49+
We don't care about order of params and kinds of whitespace in config lines
50+
so normalize it to compare. We do care about case in paths, but to simplify
51+
testing we pretend we don't.
52+
"""
53+
54+
def sort_cache_params(params):
55+
return ",".join(sorted(params.split(",")))
56+
57+
line = line.split("#")[0]
58+
59+
cache_params_pattern = re.compile(r"(.*?\s)(\S+=\S+)")
60+
match = cache_params_pattern.search(line)
61+
if match:
62+
sorted_params = sort_cache_params(match.group(2))
63+
line = match.group(1) + sorted_params
64+
65+
return "".join(line.lower().split())
66+
67+
68+
class MockConfigFile(object):
69+
def __init__(self, buffer=""):
70+
self.set_contents(buffer)
71+
72+
def __enter__(self):
73+
return self.buffer
74+
75+
def __exit__(self, *args, **kwargs):
76+
self.set_contents(self.buffer.getvalue())
77+
78+
def __call__(self, path, mode):
79+
if (mode == "w"):
80+
self.buffer = StringIO()
81+
82+
return self
83+
84+
def read(self):
85+
return self.buffer.read()
86+
87+
def write(self, str):
88+
return self.buffer.write(str)
89+
90+
def close(self):
91+
self.set_contents(self.buffer.getvalue())
92+
93+
def readline(self):
94+
return self.buffer.readline()
95+
96+
def __next__(self):
97+
return self.buffer.__next__()
98+
99+
def __iter__(self):
100+
return self
101+
102+
def set_contents(self, buffer):
103+
self.buffer = StringIO(dedent(buffer).strip())
104+
105+
class CopyableMock(mock.Mock):
106+
def __init__(self, *args, **kwargs):
107+
super(CopyableMock, self).__init__(*args, **kwargs)
108+
self.copies = []
109+
110+
def __deepcopy__(self, memo):
111+
copy = mock.Mock(spec=self)
112+
self.copies += [copy]
113+
return copy
114+

0 commit comments

Comments
 (0)