Skip to content

Fix JSON string comparision for tests #8183

Merged
yadvr merged 1 commit intoapache:mainfrom
rRajivramachandran:bug_flaky_server_test
Nov 3, 2023
Merged

Fix JSON string comparision for tests #8183
yadvr merged 1 commit intoapache:mainfrom
rRajivramachandran:bug_flaky_server_test

Conversation

@rRajivramachandran
Copy link
Copy Markdown
Contributor

Description

The PR fixes the following test which could produce non-deterministic/flaky pass/fail result
com.cloud.server.StatsCollectorTest.java#persistVirtualMachineStatsTestPersistsSuccessfully

Setup:

Java version: 11.0.20.1
Maven version: 3.8.8

Test failure reproduction:

The test com.cloud.server.StatsCollectorTest.java#persistVirtualMachineStatsTestPersistsSuccessfully compares two JSON Strings and asserts if they are equal. However, the order of key value pairs is not guaranteed in a JSON string. Hence the test can fail. This issue was verified using the NonDex plugin.

Steps:

git clone https://github.com/apache/cloudstack.git
cd cloudstack
mvn install -pl server -am -DskipTests
mvn -pl server edu.illinois:nondex-maven-plugin:2.1.1:nondex -Dtest=com.cloud.server.StatsCollectorTest.java#persistVirtualMachineStatsTestPersistsSuccessfully

Root cause and Fix

Test Failure in Nondex Mode

Results:
[INFO] 
[ERROR] Failures: 
[ERROR]   StatsCollectorTest.persistVirtualMachineStatsTestPersistsSuccessfully:335 expected:<{"vmId":2,"[cpuUtilization":6.0,"networkReadKBs":7.0,"networkWriteKBs":8.0,"diskReadIOs":12.0,"diskWriteIOs":13.0,"diskReadKBs":10.0,"diskWriteKBs":11.0,"memoryKBs":3.0,"intFreeMemoryKBs":4.0,"targetMemoryKBs":5.0,"numCPUs":9,"entityType":"vm"]}> but was:<{"vmId":2,"[networkWriteKBs":8.0,"memoryKBs":3.0,"entityType":"vm","cpuUtilization":6.0,"diskReadKBs":10.0,"networkReadKBs":7.0,"diskReadIOs":12.0,"targetMemoryKBs":5.0,"numCPUs":9,"intFreeMemoryKBs":4.0,"diskWriteIOs":13.0,"diskWriteKBs":11.0]}>
[INFO] 
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0

The following error occurs when comparing two json strings in this line. The expected string is a hardcoded json string. The actual string is obtained by converting the class attributes to JSON(using the Gson library) here. The comparision fails in some cases since the key orderings between the JSON data could be different.

Fix

The Fix converts both the JSON strings to TreeMaps(which maintain key order) and compares these for equality. This makes the test more stable without losing out on effectiveness

Types of changes

  • Breaking change (fix or feature that would cause existing functionality to change)
  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Enhancement (improves an existing feature and functionality)
  • Cleanup (Code refactoring and cleanup, that may add test cases)
  • build/CI

Feature/Enhancement Scale or Bug Severity

This code modifies only test files and should not affect anything in production

Feature/Enhancement Scale

  • Major
  • Minor

Bug Severity

  • BLOCKER
  • Critical
  • Major
  • Minor
  • Trivial

Screenshots (if appropriate):

How Has This Been Tested?

  • The test passes when run with the NonDex tool. (mvn -pl server edu.illinois:nondex-maven-plugin:2.1.1:nondex -Dtest=com.cloud.server.StatsCollectorTest.java#persistVirtualMachineStatsTestPersistsSuccessfully)
  • The entire test suite of the module passes. (mvn -pl server test)

How did you try to break this feature and the system with this change?

The change only creates a new test utility and calls it within a single test. Hence it should not affect any other code

@rRajivramachandran rRajivramachandran changed the title Fix JSON string comparision fin tests Fix JSON string comparision for tests Nov 3, 2023
Copy link
Copy Markdown
Contributor

@DaanHoogland DaanHoogland left a comment

Choose a reason for hiding this comment

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

clgtm

@codecov
Copy link
Copy Markdown

codecov Bot commented Nov 3, 2023

Codecov Report

Merging #8183 (2d06967) into main (78213da) will increase coverage by 7.15%.
The diff coverage is n/a.

@@             Coverage Diff              @@
##               main    #8183      +/-   ##
============================================
+ Coverage     22.00%   29.16%   +7.15%     
- Complexity    21855    30984    +9129     
============================================
  Files          5047     5181     +134     
  Lines        343240   365208   +21968     
  Branches      49449    53420    +3971     
============================================
+ Hits          75527   106509   +30982     
+ Misses       256710   244106   -12604     
- Partials      11003    14593    +3590     
Flag Coverage Δ
simulator-marvin-tests 25.10% <ø> (+1.55%) ⬆️
uitests 4.50% <ø> (ø)
unit-tests 14.80% <ø> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

see 1383 files with indirect coverage changes

📣 Codecov offers a browser extension for seamless coverage viewing on GitHub. Try it in Chrome or Firefox today!

@yadvr yadvr merged commit 6afffe1 into apache:main Nov 3, 2023
@boring-cyborg
Copy link
Copy Markdown

boring-cyborg Bot commented Nov 3, 2023

Awesome work, congrats on your first merged pull request!

dhslove pushed a commit to ablecloud-team/ablestack-cloud that referenced this pull request Nov 8, 2023
Dajeong-Park added a commit to Dajeong-Park/ablestack-cloud that referenced this pull request Nov 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants