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

Collect DB dump when test failed #5197

Merged
merged 8 commits into from
Feb 24, 2022
Merged
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
36 changes: 36 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1357,3 +1357,39 @@ def get_reboot_cause(duthost):
uptime_end = duthost.get_up_time()
if not uptime_end == uptime_start:
duthost.show_and_parse("show reboot-cause history")

def collect_db_dump_on_duts(request, duthosts):
'''
When test failed, teardown of this fixture will dump all the DB and collect to the test servers
'''
if hasattr(request.node, 'rep_call') and request.node.rep_call.failed:
dut_file_path = "/tmp/db_dump"
docker_file_path = "./logs/db_dump"
db_dump_path = os.path.join(dut_file_path, request.module.__name__, request.node.name)
db_dump_tarfile = "{}.tar.gz".format(dut_file_path)

# Collect DB config
dbs = set()
result = duthosts[0].shell("cat /var/run/redis/sonic-db/database_config.json")
db_config = json.loads(result['stdout'])
for db in db_config['DATABASES']:
db_id = db_config['DATABASES'][db]['id']
dbs.add(db_id)

# Collect DB dump
duthosts.file(path = db_dump_path, state="directory")
for i in dbs:
duthosts.shell("redis-dump -d {} -y -o {}/{}".format(i, db_dump_path, i))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On duthost, it would be better to generate the dump files to "/tmp".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK,I will change the path

duthosts.shell("tar czf {} {}".format(db_dump_tarfile, dut_file_path))
duthosts.fetch(src = db_dump_tarfile, dest = docker_file_path)

#remove dump file from dut
duthosts.shell("rm -rf {} {}".format(dut_file_path, db_dump_tarfile))

@pytest.fixture(autouse=True)
def collect_db_dump(request, duthosts):
'''
When test failed, teardown of this fixture will dump all the DB and collect to the test servers
'''
yield
collect_db_dump_on_duts(request, duthosts)