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

Add dnf4 updateinfo --json tests #1627

Open
wants to merge 3 commits into
base: dnf-4-stack
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
17 changes: 17 additions & 0 deletions dnf-behave-tests/dnf/steps/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import sys
import time
from datetime import datetime
import json

from common.lib.cmd import assert_exitcode, run_in_context
from common.lib.file import prepend_installroot
from fixtures import start_server_based_on_type
from lib.rpmdb import get_rpmdb_rpms
from lib.json import diff_json_pattern_values


def get_boot_time():
Expand Down Expand Up @@ -483,3 +485,18 @@ def execute_transaction(goal,description):
repo_sack.update_and_load_enabled_repos(True)
"""
execute_python_script(context, libdnf5_setup_script + context.text)


@behave.then("stdout json matches")
def then_json_matches(context):
"""
Compare json output from stdout with specified json.
Specified json can contain fnmatch patterns in string values.
"""
table_json = json.loads(context.text)
out_json = json.loads(context.cmd_stdout)
diffs = diff_json_pattern_values(".", table_json, out_json)
if diffs:
for diff in diffs:
print(diff)
raise AssertionError("Expected JSON doesn't match")
69 changes: 69 additions & 0 deletions dnf-behave-tests/dnf/steps/lib/json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import print_function

from behave.formatter.ansi_escapes import escapes
import fnmatch

def diff_recursive(parent, obj1, obj2):
problems = []
if isinstance(obj1, list) and isinstance(obj2, list):
# The list was originally a dictionary
if (len(obj1) != 0 and isinstance(obj1[0], tuple)) or len(obj2) != 0 and isinstance(obj2[0], tuple):
keys1 = next(zip(*obj1)) if len(obj1) != 0 else []
keys2 = next(zip(*obj2)) if len(obj2) != 0 else []
if keys1 != keys2:
return ["Different keys in %s object: expected: %s%s%s vs actual: %s%s%s"
% (parent, escapes['passed_arg'], keys1, escapes['reset'], escapes['failed_arg'], keys2,
escapes['reset'])]
for i in range(0, len(keys1)):
problems += diff_recursive(parent + "[" + keys1[i] + "]", obj1[i][1], obj2[i][1])
return problems

if len(obj1) != len(obj2):
return ["Different count of elements in %s array: Expected: %s%s%s vs Actual: %s%s%s"
% (parent, escapes['passed_arg'], len(obj1), escapes['reset'], escapes['failed_arg'], len(obj2),
escapes['reset'])]
for i in range(0, len(obj1)):
problems += diff_recursive(parent + "[" + str(i) + "]", obj1[i], obj2[i])
return problems

else:
# The pattern is the second argument of fnmatch
if not fnmatch.fnmatch(str(obj2), str(obj1)):
return ["Different values for %s: Expected: '%s%s%s' vs Actual: '%s%s%s'"
% (parent, escapes['passed_arg'], obj1, escapes['reset'], escapes['failed_arg'],
obj2, escapes['reset'])]
return problems


def diff_json_pattern_values(parent, obj1, obj2):
"""
Recursively compare json objects obj1 and obj2
String values of obj1 can contain fnmatch patterns

Returns a list with problems
"""

# Recursively sort lists
# (and convert dictionaries to lists of (key, value) pairs so that they're orderable)
def ordered(obj):
if isinstance(obj, list):
return sorted(ordered(x) for x in obj)
if isinstance(obj, dict):
return sorted((k, ordered(v)) for k, v in obj.items())
else:
return obj

try:
obj1 = ordered(obj1)
except TypeError:
raise AssertionError("Cannot sort expected json, this could be caused by different types in an array.")

try:
obj2 = ordered(obj2)
except TypeError:
raise AssertionError("Cannot sort input json, this could be caused by different types in an array.")

return diff_recursive(parent, obj1, obj2)
Loading
Loading