test helper: improvements (#9672)
parent
250dc1139c
commit
7a6125b99a
|
@ -6,6 +6,7 @@
|
||||||
from __future__ import (absolute_import, division, print_function)
|
from __future__ import (absolute_import, division, print_function)
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
@ -13,49 +14,49 @@ import yaml
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
from ansible.module_utils.common._collections_compat import Sequence
|
|
||||||
|
|
||||||
|
|
||||||
class Helper(object):
|
class Helper(object):
|
||||||
TEST_SPEC_VALID_SECTIONS = ["anchors", "test_cases"]
|
TEST_SPEC_VALID_SECTIONS = ["anchors", "test_cases"]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_spec(test_module, ansible_module, test_spec, mocks=None):
|
def from_spec(ansible_module, test_module, test_spec, mocks=None):
|
||||||
helper = Helper(test_module, ansible_module, test_spec=test_spec, mocks=mocks)
|
helper = Helper(ansible_module, test_module, test_spec=test_spec, mocks=mocks)
|
||||||
return helper
|
return helper
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_file(test_module, ansible_module, filename, mocks=None):
|
def from_file(ansible_module, test_module, test_spec_filehandle, mocks=None):
|
||||||
with open(filename, "r") as test_cases:
|
test_spec = yaml.safe_load(test_spec_filehandle)
|
||||||
test_spec = yaml.safe_load(test_cases)
|
return Helper.from_spec(ansible_module, test_module, test_spec, mocks)
|
||||||
return Helper.from_spec(test_module, ansible_module, test_spec, mocks)
|
|
||||||
|
|
||||||
|
# @TODO: calculate the test_module_name automatically, remove one more parameter
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_module(ansible_module, test_module_name, test_spec=None, mocks=None):
|
def from_module(ansible_module, test_module_name, mocks=None):
|
||||||
test_module = sys.modules[test_module_name]
|
test_module = sys.modules[test_module_name]
|
||||||
if test_spec is None:
|
extensions = ['.yaml', '.yml']
|
||||||
test_spec = test_module.__file__.replace('.py', '.yaml')
|
for ext in extensions:
|
||||||
return Helper.from_file(test_module, ansible_module, test_spec, mocks=mocks)
|
test_spec_filename = test_module.__file__.replace('.py', ext)
|
||||||
|
if os.path.exists(test_spec_filename):
|
||||||
|
with open(test_spec_filename, "r") as test_spec_filehandle:
|
||||||
|
return Helper.from_file(ansible_module, test_module, test_spec_filehandle, mocks=mocks)
|
||||||
|
|
||||||
|
raise Exception("Cannot find test case file for {0} with one of the extensions: {1}".format(test_module.__file__, extensions))
|
||||||
|
|
||||||
def add_func_to_test_module(self, name, func):
|
def add_func_to_test_module(self, name, func):
|
||||||
setattr(self.test_module, name, func)
|
setattr(self.test_module, name, func)
|
||||||
|
|
||||||
def __init__(self, test_module, ansible_module, test_spec, mocks=None):
|
def __init__(self, ansible_module, test_module, test_spec, mocks=None):
|
||||||
self.test_module = test_module
|
|
||||||
self.ansible_module = ansible_module
|
self.ansible_module = ansible_module
|
||||||
|
self.test_module = test_module
|
||||||
self.test_cases = []
|
self.test_cases = []
|
||||||
self.fixtures = {}
|
self.fixtures = {}
|
||||||
if isinstance(test_spec, Sequence):
|
|
||||||
test_cases = test_spec
|
|
||||||
else: # it is a dict
|
|
||||||
test_cases = test_spec['test_cases']
|
|
||||||
spec_diff = set(test_spec.keys()) - set(self.TEST_SPEC_VALID_SECTIONS)
|
spec_diff = set(test_spec.keys()) - set(self.TEST_SPEC_VALID_SECTIONS)
|
||||||
if spec_diff:
|
if spec_diff:
|
||||||
raise ValueError("Test specification contain unknown keys: {0}".format(", ".join(spec_diff)))
|
raise ValueError("Test specification contain unknown keys: {0}".format(", ".join(spec_diff)))
|
||||||
|
|
||||||
self.mocks_map = {m.name: m for m in mocks} if mocks else {}
|
self.mocks_map = {m.name: m for m in mocks} if mocks else {}
|
||||||
|
|
||||||
for test_case in test_cases:
|
for spec_test_case in test_spec['test_cases']:
|
||||||
tc = ModuleTestCase.make_test_case(test_case, test_module, self.mocks_map)
|
tc = ModuleTestCase.make_test_case(spec_test_case, test_module, self.mocks_map)
|
||||||
self.test_cases.append(tc)
|
self.test_cases.append(tc)
|
||||||
self.fixtures.update(tc.fixtures)
|
self.fixtures.update(tc.fixtures)
|
||||||
self.set_test_func()
|
self.set_test_func()
|
||||||
|
|
|
@ -386,7 +386,8 @@ ubuntu 24.04
|
||||||
kernel 6.8.0-49-generic
|
kernel 6.8.0-49-generic
|
||||||
"""
|
"""
|
||||||
|
|
||||||
TEST_CASES = [
|
TEST_SPEC = dict(
|
||||||
|
test_cases=[
|
||||||
dict(
|
dict(
|
||||||
id="simple case",
|
id="simple case",
|
||||||
input={"name": ["hello-world"]},
|
input={"name": ["hello-world"]},
|
||||||
|
@ -498,5 +499,6 @@ TEST_CASES = [
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
)
|
||||||
|
|
||||||
Helper.from_spec(sys.modules[__name__], snap, TEST_CASES, mocks=[RunCommandMock])
|
Helper.from_spec(snap, sys.modules[__name__], TEST_SPEC, mocks=[RunCommandMock])
|
||||||
|
|
Loading…
Reference in New Issue