Check fact arguments are valid in operation tests
This prevents the fake facts hiding bugs in the operation code that calls
those facts.
import json
import os
from datetime import datetime
+from inspect import getfullargspec
from io import open
from os import listdir, path
from unittest.mock import patch
@@ -187,12 +188,23 @@ class FakeHost(object):
def _get_fact_key(fact_cls):
return "{0}.{1}".format(fact_cls.__module__.split(".")[-1], fact_cls.__name__)
+ @staticmethod
+ def _check_fact_args(fact_cls, kwargs):
+ # Check that the arguments we're going to use to fake a fact are all actual arguments in
+ # the fact class, otherwise the test will hide a bug in the underlying operation.
+ real_args = getfullargspec(fact_cls.command)
+ for key in kwargs.keys():
+ assert (
+ key in real_args.args
+ ), f"Argument {key} is not a real argument in the `{fact_cls}.command` method"
+
def get_fact(self, fact_cls, **kwargs):
fact_key = self._get_fact_key(fact_cls)
fact = getattr(self.fact, fact_key, None)
if fact is None:
raise KeyError("Missing test fact data: {0}".format(fact_key))
if kwargs:
+ self._check_fact_args(fact_cls, kwargs)
fact_ordered_keys = {_sort_kwargs_str(key): value for key, value in fact.items()}
kwargs_str = _sort_kwargs_str(get_kwargs_str(kwargs))
if kwargs_str not in fact:
